-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Cherry-Pick Managed Identity Metadata fix for patch release 1.8.1 #33478
Merged
schaabs
merged 3 commits into
Azure:hotfix/identity/1.8.1
from
schaabs:hotfix/identity/1.8.1
Jan 13, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
using System.Web; | ||
using Azure.Core; | ||
using Azure.Core.Diagnostics; | ||
using Azure.Core.Pipeline; | ||
using Azure.Core.TestFramework; | ||
using Azure.Identity.Tests.Mock; | ||
using Microsoft.AspNetCore.Http; | ||
|
@@ -100,6 +101,73 @@ public async Task VerifyImdsRequestWithClientIdMockAsync() | |
Assert.AreEqual("true", metadataValue); | ||
} | ||
|
||
[NonParallelizable] | ||
[Test] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip: when using |
||
[TestCase(null)] | ||
[TestCase("Auto-Detect")] | ||
[TestCase("eastus")] | ||
[TestCase("westus")] | ||
public async Task VerifyImdsRequestWithClientIdAndRegionalAuthorityNameMockAsync(string regionName) | ||
{ | ||
using var environment = new TestEnvVar(new() { {"AZURE_REGIONAL_AUTHORITY_NAME", regionName}, {"MSI_ENDPOINT", null }, { "MSI_SECRET", null }, { "IDENTITY_ENDPOINT", null }, { "IDENTITY_HEADER", null }, { "AZURE_POD_IDENTITY_AUTHORITY_HOST", null } }); | ||
|
||
var response = CreateMockResponse(200, ExpectedToken); | ||
var mockTransport = new MockTransport(response); | ||
var options = new TokenCredentialOptions() { Transport = mockTransport }; | ||
var pipeline = CredentialPipeline.GetInstance(options); | ||
|
||
ManagedIdentityCredential credential = InstrumentClient(new ManagedIdentityCredential("mock-client-id", pipeline)); | ||
|
||
AccessToken actualToken = await credential.GetTokenAsync(new TokenRequestContext(MockScopes.Default)); | ||
|
||
Assert.AreEqual(ExpectedToken, actualToken.Token); | ||
|
||
MockRequest request = mockTransport.Requests[0]; | ||
|
||
string query = request.Uri.Query; | ||
|
||
Assert.AreEqual(request.Uri.Host, "169.254.169.254"); | ||
Assert.AreEqual(request.Uri.Path, "/metadata/identity/oauth2/token"); | ||
Assert.IsTrue(query.Contains("api-version=2018-02-01")); | ||
Assert.IsTrue(query.Contains($"resource={Uri.EscapeDataString(ScopeUtilities.ScopesToResource(MockScopes.Default))}")); | ||
Assert.IsTrue(request.Headers.TryGetValue("Metadata", out string metadataValue)); | ||
Assert.IsTrue(query.Contains($"{Constants.ManagedIdentityClientId}=mock-client-id")); | ||
Assert.AreEqual("true", metadataValue); | ||
} | ||
|
||
[NonParallelizable] | ||
[Test] | ||
[TestCaseSource(nameof(AuthorityHostValues))] | ||
public async Task VerifyImdsRequestWithClientIdAndNonPubCloudMockAsync(Uri authority) | ||
{ | ||
using var environment = new TestEnvVar(new() { { "MSI_ENDPOINT", null }, { "MSI_SECRET", null }, { "IDENTITY_ENDPOINT", null }, { "IDENTITY_HEADER", null }, { "AZURE_POD_IDENTITY_AUTHORITY_HOST", null } }); | ||
|
||
var response = CreateMockResponse(200, ExpectedToken); | ||
var mockTransport = new MockTransport(response); | ||
var options = new TokenCredentialOptions() { Transport = mockTransport, AuthorityHost = authority }; | ||
//var pipeline = CredentialPipeline.GetInstance(options); | ||
var _pipeline = new HttpPipeline(mockTransport); | ||
var pipeline = new CredentialPipeline(authority, _pipeline, new ClientDiagnostics(options)); | ||
|
||
ManagedIdentityCredential credential = InstrumentClient(new ManagedIdentityCredential(new ManagedIdentityClient( pipeline, "mock-client-id"))); | ||
|
||
AccessToken actualToken = await credential.GetTokenAsync(new TokenRequestContext(MockScopes.Default)); | ||
|
||
Assert.AreEqual(ExpectedToken, actualToken.Token); | ||
|
||
MockRequest request = mockTransport.Requests[0]; | ||
|
||
string query = request.Uri.Query; | ||
|
||
Assert.AreEqual(request.Uri.Host, "169.254.169.254"); | ||
Assert.AreEqual(request.Uri.Path, "/metadata/identity/oauth2/token"); | ||
Assert.IsTrue(query.Contains("api-version=2018-02-01")); | ||
Assert.IsTrue(query.Contains($"resource={Uri.EscapeDataString(ScopeUtilities.ScopesToResource(MockScopes.Default))}")); | ||
Assert.IsTrue(request.Headers.TryGetValue("Metadata", out string metadataValue)); | ||
Assert.IsTrue(query.Contains($"{Constants.ManagedIdentityClientId}=mock-client-id")); | ||
Assert.AreEqual("true", metadataValue); | ||
} | ||
|
||
[NonParallelizable] | ||
[Test] | ||
public async Task VerifyImdsRequestWithResourceIdMockAsync() | ||
|
@@ -781,6 +849,17 @@ private static IEnumerable<TestCaseData> ExceptionalEnvironmentConfigs() | |
yield return new TestCaseData(new Dictionary<string, string>() { { "MSI_ENDPOINT", null }, { "MSI_SECRET", null }, { "IDENTITY_ENDPOINT", null }, { "IDENTITY_HEADER", null }, { "IDENTITY_SERVER_THUMBPRINT", "null" }, { "AZURE_POD_IDENTITY_AUTHORITY_HOST", "http::@/bogusuri" } }); | ||
} | ||
|
||
public static IEnumerable<object[]> AuthorityHostValues() | ||
{ | ||
// params | ||
// az thrown Exception message, expected message, expected exception | ||
yield return new object[] { AzureAuthorityHosts.AzureChina }; | ||
yield return new object[] { AzureAuthorityHosts.AzureGermany }; | ||
yield return new object[] { AzureAuthorityHosts.AzureGovernment }; | ||
yield return new object[] { AzureAuthorityHosts.AzurePublicCloud }; | ||
yield return new object[] { new Uri("https://foo.bar") }; | ||
} | ||
|
||
private MockResponse CreateMockResponse(int responseCode, string token) | ||
{ | ||
var response = new MockResponse(responseCode); | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't catch this (lack of) change. It's concerning the bot didn't update this before. Was this branched from the 1.7.0 commit, can I safely assume?