-
Notifications
You must be signed in to change notification settings - Fork 403
Add azure arc managed identity source #3862
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
99760e4
Add azure arc managed identity source
neha-bhargava 61c99be
Add more tests
neha-bhargava fc6b318
Update tests
neha-bhargava 347dcce
Address comments
neha-bhargava 7d1e606
Update src/client/Microsoft.Identity.Client/MsalErrorMessage.cs
neha-bhargava 35f519a
Update src/client/Microsoft.Identity.Client/ManagedIdentity/AzureArcM…
neha-bhargava 4c321d0
Address comments
neha-bhargava eb2388a
Merge branch 'main' into nebharg/MsiAzureArc
neha-bhargava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/client/Microsoft.Identity.Client/ManagedIdentity/AzureArcManagedIdentitySource.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Globalization; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Identity.Client.Core; | ||
| using Microsoft.Identity.Client.Extensibility; | ||
| using Microsoft.Identity.Client.Http; | ||
| using Microsoft.Identity.Client.Internal; | ||
| using Microsoft.Identity.Client.Utils; | ||
|
|
||
| namespace Microsoft.Identity.Client.ManagedIdentity | ||
| { | ||
| /// <summary> | ||
| /// Original source of code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/src/AzureArcManagedIdentitySource.cs | ||
| /// </summary> | ||
| internal class AzureArcManagedIdentitySource : ManagedIdentitySource | ||
| { | ||
| private const string ArcApiVersion = "2019-11-01"; | ||
|
|
||
| private readonly string _clientId; | ||
| private readonly string _resourceId; | ||
| private readonly Uri _endpoint; | ||
|
|
||
| public static ManagedIdentitySource TryCreate(RequestContext requestContext) | ||
| { | ||
| string identityEndpoint = EnvironmentVariables.IdentityEndpoint; | ||
| string imdsEndpoint = EnvironmentVariables.ImdsEndpoint; | ||
|
|
||
| // if BOTH the env vars IDENTITY_ENDPOINT and IMDS_ENDPOINT are set the MsiType is Azure Arc | ||
| if (string.IsNullOrEmpty(identityEndpoint) || string.IsNullOrEmpty(imdsEndpoint)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (!Uri.TryCreate(identityEndpoint, UriKind.Absolute, out Uri endpointUri)) | ||
| { | ||
| throw new MsalClientException(MsalError.InvalidManagedIdentityEndpoint, string.Format(CultureInfo.InvariantCulture, MsalErrorMessage.ManagedIdentityEndpointInvalidUriError, identityEndpoint, "Azure arc")); | ||
| } | ||
|
|
||
| return new AzureArcManagedIdentitySource(endpointUri, requestContext); | ||
| } | ||
|
|
||
| private AzureArcManagedIdentitySource(Uri endpoint, RequestContext requestContext) : base(requestContext) | ||
| { | ||
| _endpoint = endpoint; | ||
| _clientId = requestContext.ServiceBundle.Config.ManagedIdentityUserAssignedClientId; | ||
| _resourceId = requestContext.ServiceBundle.Config.ManagedIdentityUserAssignedResourceId; | ||
|
|
||
| if (!string.IsNullOrEmpty(_clientId) || !string.IsNullOrEmpty(_resourceId)) | ||
| { | ||
| throw new MsalClientException(MsalError.UserAssignedManagedIdentityNotSupported, MsalErrorMessage.ManagedIdentityUserAssignedNotSupported); | ||
|
neha-bhargava marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| protected override ManagedIdentityRequest CreateRequest(string resource) | ||
| { | ||
| ManagedIdentityRequest request = new ManagedIdentityRequest(System.Net.Http.HttpMethod.Get, _endpoint); | ||
|
|
||
| request.Headers.Add("Metadata", "true"); | ||
| request.QueryParameters["api-version"] = ArcApiVersion; | ||
| request.QueryParameters["resource"] = resource; | ||
|
|
||
| return request; | ||
| } | ||
|
|
||
| protected override async Task<ManagedIdentityResponse> HandleResponseAsync( | ||
| AppTokenProviderParameters parameters, | ||
| HttpResponse response, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| _requestContext.Logger.Verbose($"[Managed Identity] Response received. Status code: {response.StatusCode}"); | ||
|
|
||
| if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) | ||
|
neha-bhargava marked this conversation as resolved.
|
||
| { | ||
| if (!response.HeadersAsDictionary.TryGetValue("WWW-Authenticate", out string challenge)) | ||
| { | ||
| _requestContext.Logger.Error("[Managed Identity] WWW-Authenticate header is expected but not found."); | ||
| throw new MsalServiceException(MsalError.ManagedIdentityRequestFailed, MsalErrorMessage.ManagedIdentityNoChallengeError); | ||
| } | ||
|
|
||
| var splitChallenge = challenge.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); | ||
|
|
||
| if (splitChallenge.Length != 2) | ||
| { | ||
| _requestContext.Logger.Error("[Managed Identity] The WWW-Authenticate header for Azure arc managed identity is not an expected format."); | ||
| throw new MsalServiceException(MsalError.ManagedIdentityRequestFailed, MsalErrorMessage.ManagedIdentityInvalidChallange); | ||
| } | ||
|
|
||
| var authHeaderValue = "Basic " + File.ReadAllText(splitChallenge[1]); | ||
|
neha-bhargava marked this conversation as resolved.
|
||
|
|
||
| ManagedIdentityRequest request = CreateRequest(ScopeHelper.ScopesToResource(parameters.Scopes.ToArray())); | ||
|
neha-bhargava marked this conversation as resolved.
|
||
|
|
||
| _requestContext.Logger.Verbose("[Managed Identity] Adding authorization header to the request."); | ||
| request.Headers.Add("Authorization", authHeaderValue); | ||
|
|
||
| response = await _requestContext.ServiceBundle.HttpManager.SendGetAsync(request.ComputeUri(), request.Headers, _requestContext.Logger, cancellationToken: cancellationToken).ConfigureAwait(false); | ||
|
|
||
| return await base.HandleResponseAsync(parameters, response, cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
|
|
||
| return await base.HandleResponseAsync(parameters, response, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.