Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat support for managed identity authentication in azure locking #1896

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

oker1
Copy link

@oker1 oker1 commented Mar 7, 2025

Summary by CodeRabbit

  • New Features
    • Introduced a new "Managed Identity" authentication option, enhancing Azure connection flexibility.
  • Documentation
    • Updated configuration guides to include the new authentication method and its required environment variable.
  • Tests
    • Added test cases to verify functionality and error handling for the new authentication mode.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here: app.greptile.com/review/github.

💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!

3 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

Copy link
Contributor

coderabbitai bot commented Mar 7, 2025

Walkthrough

This change adds a new authentication method "MANAGED_IDENTITY" across the documentation, implementation, and testing for Azure DevOps locking connection methods. The documentation now includes "MANAGED_IDENTITY" with details on setting the DIGGER_AZURE_SA_NAME environment variable. In the code, a new function getManagedIdentitySvcCLient is introduced to handle service client creation with managed identity, and error handling is updated accordingly. Corresponding test cases ensure proper behavior for both valid and missing managed identity configurations.

Changes

File(s) Change Summary
docs/ce/azure-specific/azure-devops-locking-connection-methods.mdx Added "MANAGED_IDENTITY" to the list of authentication methods and documented the required DIGGER_AZURE_SA_NAME environment variable.
libs/locking/azure/storage_account.go Added support for "MANAGED_IDENTITY" in storage account locking by introducing getManagedIdentitySvcCLient() and updating error messages.
libs/locking/azure/storage_account_test.go Introduced new test cases and helper functions (loadManagedIdentityEnv and TestNewStorageAccountLock_WithManagedIdentity_MissingEnv) to validate managed identity authentication.

Sequence Diagram(s)

sequenceDiagram
    participant U as User/Application
    participant SA as Storage Account Lock Creator
    participant ENV as Environment Variables
    participant MIC as Managed Identity Client Creator

    U->>SA: Request to create Storage Account Lock
    SA->>ENV: Read DIGGER_AZURE_AUTH_METHOD
    alt Method is MANAGED_IDENTITY
        SA->>ENV: Verify DIGGER_AZURE_SA_NAME exists
        SA->>MIC: Call getManagedIdentitySvcCLient()
        MIC-->>SA: Return service client or error if DIGGER_AZURE_SA_NAME missing
    else Other Methods
        SA->>SA: Process with SHARED_KEY/CLIENT_SECRET flow
    end
    SA->>U: Return Storage Account Lock instance (or error)
Loading

Poem

I'm a sprightly rabbit, hopping with delight,
New auth paths in our code shine bright.
Managed Identity leads the way,
In docs and tests, it's here to stay.
With whiskers twitching, I cheer each new line—
Updates complete, simply divine!

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
libs/locking/azure/storage_account.go (1)

194-213: Function implementation looks good, minor typo in function name

The getManagedIdentitySvcCLient function is well-structured and follows the same pattern as the other authentication methods. However, there's a minor typo in the function name: "CLient" should be "Client".

Fix the typo in the function name:

-func getManagedIdentitySvcCLient() (*aztables.ServiceClient, error) {
+func getManagedIdentitySvcClient() (*aztables.ServiceClient, error) {

Don't forget to update the function call on line 132 as well:

-		return getManagedIdentitySvcCLient()
+		return getManagedIdentitySvcClient()
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 84f1c0e and 97eb953.

📒 Files selected for processing (3)
  • docs/ce/azure-specific/azure-devops-locking-connection-methods.mdx (1 hunks)
  • libs/locking/azure/storage_account.go (3 hunks)
  • libs/locking/azure/storage_account_test.go (3 hunks)
🔇 Additional comments (10)
docs/ce/azure-specific/azure-devops-locking-connection-methods.mdx (4)

15-15: Looks good - MANAGED_IDENTITY auth method added

The addition of the managed identity authentication method to the documentation is clear and consistent with the existing format.


19-21: Formatting update looks good

The formatting change for the SHARED_KEY section maintains consistency with the other methods, improving readability.


23-30: Formatting update looks good

The reformatting of CONNECTION_STRING and CLIENT_SECRET sections ensures a consistent documentation style.


32-33: MANAGED_IDENTITY documentation is well-structured

The documentation for the new MANAGED_IDENTITY authentication method clearly specifies the required environment variable.

libs/locking/azure/storage_account.go (3)

30-30: Error message updated correctly for new auth method

The error message has been properly updated to include the new MANAGED_IDENTITY authentication method.


131-133: Managed Identity support added correctly

The implementation correctly handles the MANAGED_IDENTITY case by calling the new getManagedIdentitySvcCLient function.


203-206:

✅ Verification successful

Verify managed identity behavior in Azure environments

The code uses NewManagedIdentityCredential with nil options, which will use the system-assigned managed identity. It's worth confirming that this works in your target Azure environments (VMs, App Services, Azure Functions, etc.)


🌐 Web query:

How does Azure SDK for Go's NewManagedIdentityCredential function behave in different Azure environments?

💡 Result:

The NewManagedIdentityCredential function in Azure SDK for Go adapts its behavior based on the Azure environment and configuration. Here's how it operates across different scenarios:

System-Assigned Managed Identity

  • Default behavior: When created without parameters (azidentity.NewManagedIdentityCredential(nil)), it uses the system-assigned identity of the Azure resource (e.g., VM, App Service, AKS)[1][4].
  • Environment detection: Automatically works in supported Azure services like:
    • Azure Virtual Machines
    • Azure App Service
    • Azure Kubernetes Service (AKS)
    • Azure Functions[2][6]

User-Assigned Managed Identity

  • Client ID specification: Pass ManagedIdentityCredentialOptions with ID: ClientID("your-client-id")[7][16]:
    opts := &azidentity.ManagedIdentityCredentialOptions{ID: azidentity.ClientID("abcd1234-...")}
    cred, err := azidentity.NewManagedIdentityCredential(opts)
  • Environment variable: Alternatively, set AZURE_CLIENT_ID to use a user-assigned identity[5][16].

Platform-Specific Constraints

The function throws errors when specifying user-assigned identities in environments with unsupported APIs:

  • Azure Arc
  • Cloud Shell
  • Service Fabric
  • Azure ML (when using resource ID)[10][16]

Example error trigger:

// Fails in Azure Arc/Cloud Shell:
cred, err := azidentity.NewManagedIdentityCredential(&azidentity.ManagedIdentityCredentialOptions{
    ID: azidentity.ClientID("invalid-for-this-platform"),
})

Environment Variable Interactions

The credential checks these variables for configuration:

Variable Purpose Example Platforms
IDENTITY_ENDPOINT Managed identity endpoint App Service 2019+, Azure Arc
IDENTITY_HEADER Authentication header App Service 2019+, Service Fabric
MSI_ENDPOINT Legacy MSI endpoint App Service 2017, Cloud Shell[5][9]

Error Handling Changes

Since v1.8.0-beta.2:

  • Strict validation: Returns errors instead of warnings for unsupported user-assigned identity configurations[10]
  • Improved fallback: DefaultAzureCredential continues credential chain if IMDS probe fails[10]

For production reliability, Microsoft recommends:

// Explicit credential selection
if isProduction {
    cred = azidentity.NewManagedIdentityCredential(nil) // System-assigned
} else {
    cred = azidentity.NewAzureCLICredential(nil) // Local development
}

This behavior ensures consistent authentication while accommodating Azure's diverse hosting environments[3][8][13].

Citations:


Verify Managed Identity Configuration in Target Environments

The current implementation correctly uses azidentity.NewManagedIdentityCredential(nil), which defaults to using the system-assigned managed identity as expected. Documentation confirms that this behavior applies to common Azure services (e.g., Virtual Machines, App Services, AKS, and Functions). Please ensure that your deployment targets support system-assigned identities. If you need to use a user-assigned identity, consider configuring ManagedIdentityCredentialOptions with the appropriate Client ID.

libs/locking/azure/storage_account_test.go (3)

67-77: Test case for managed identity looks good

The test case for managed identity authentication is properly structured and includes a check to skip the test when not using a real storage account, which makes sense since managed identity only works in Azure environments.


151-158: Negative test case implementation is correct

The negative test for managed identity with missing environment variables follows the same pattern as the other authentication methods and provides good test coverage.


396-400: Environment setup function implemented correctly

The loadManagedIdentityEnv function correctly sets up the required environment variables for managed identity testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant