-
Notifications
You must be signed in to change notification settings - Fork 5.1k
[App Configuration] - Add audience error handling policy #53834
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
zhiyuanliang-ms
merged 11 commits into
Azure:main
from
zhiyuanliang-ms:zhiyuanliang/audience-error
Nov 24, 2025
+181
−1
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
530f325
add Audience error handling policy
zhiyuanliang-ms b40d5c6
update
zhiyuanliang-ms e608aaa
not introduce Azure.Identity
zhiyuanliang-ms 8d5d15f
update
zhiyuanliang-ms 4fbea4e
update
zhiyuanliang-ms 56503a5
update
zhiyuanliang-ms 414f6c4
add test
zhiyuanliang-ms 89f2706
Revert "add test"
zhiyuanliang-ms 16d06ee
add unit test
zhiyuanliang-ms b8ee403
update
zhiyuanliang-ms 1743786
update
zhiyuanliang-ms 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
52 changes: 52 additions & 0 deletions
52
sdk/appconfiguration/Azure.Data.AppConfiguration/src/AudienceErrorHandlingPolicy.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,52 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Azure.Core; | ||
| using Azure.Core.Pipeline; | ||
|
|
||
| namespace Azure.Data.AppConfiguration | ||
| { | ||
| /// <summary> | ||
| /// Pipeline policy that provides more helpful errors when Entra ID audience misconfiguration is detected. | ||
| /// </summary> | ||
| internal class AudienceErrorHandlingPolicy : HttpPipelinePolicy | ||
| { | ||
| private readonly bool _isAudienceConfigured; | ||
| private const string AadAudienceErrorCode = "AADSTS500011"; | ||
| private const string NoAudienceErrorMessage = $"Unable to authenticate to Azure App Configuration. No authentication token audience was provided. Please set {nameof(ConfigurationClientOptions)}.{nameof(ConfigurationClientOptions.Audience)} to the appropriate audience for the target cloud. For details on how to configure the authentication token audience visit https://aka.ms/appconfig/client-token-audience."; | ||
| private const string WrongAudienceErrorMessage = $"Unable to authenticate to Azure App Configuration. An incorrect token audience was provided. Please set {nameof(ConfigurationClientOptions)}.{nameof(ConfigurationClientOptions.Audience)} to the appropriate audience for the target cloud. For details on how to configure the authentication token audience visit https://aka.ms/appconfig/client-token-audience."; | ||
|
|
||
| public AudienceErrorHandlingPolicy(bool isAudienceConfigured) | ||
| { | ||
| _isAudienceConfigured = isAudienceConfigured; | ||
| } | ||
|
|
||
| public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) | ||
| { | ||
| try | ||
| { | ||
| ProcessNext(message, pipeline); | ||
| } | ||
| catch (Exception ex) when (ex.Message.Contains(AadAudienceErrorCode)) | ||
| { | ||
| string errorMessage = _isAudienceConfigured ? WrongAudienceErrorMessage : NoAudienceErrorMessage; | ||
| throw new RequestFailedException(errorMessage, ex); | ||
| } | ||
| } | ||
|
|
||
| public override async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) | ||
| { | ||
| try | ||
| { | ||
| await ProcessNextAsync(message, pipeline).ConfigureAwait(false); | ||
| } | ||
| catch (Exception ex) when (ex.Message.Contains(AadAudienceErrorCode)) | ||
| { | ||
| string errorMessage = _isAudienceConfigured ? WrongAudienceErrorMessage : NoAudienceErrorMessage; | ||
| throw new RequestFailedException(errorMessage, ex); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
128 changes: 128 additions & 0 deletions
128
sdk/appconfiguration/Azure.Data.AppConfiguration/tests/AudienceErrorHandlingPolicyTests.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,128 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Azure.Core; | ||
| using Azure.Core.Pipeline; | ||
| using Azure.Core.TestFramework; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Azure.Data.AppConfiguration.Tests | ||
| { | ||
| [TestFixture(true)] | ||
| [TestFixture(false)] | ||
| public class AudienceErrorHandlingPolicyTests : SyncAsyncPolicyTestBase | ||
| { | ||
| private const string AadAudienceErrorCode = "AADSTS500011"; // Must match code in AudienceErrorHandlingPolicy | ||
|
|
||
| public AudienceErrorHandlingPolicyTests(bool isAsync) : base(isAsync) | ||
| { | ||
| } | ||
|
|
||
| private static string ExpectedErrorMessage(bool isAudienceConfigured) | ||
| { | ||
| string leading = "Unable to authenticate to Azure App Configuration."; | ||
| string detail = isAudienceConfigured | ||
| ? " An incorrect token audience was provided." | ||
| : " No authentication token audience was provided."; | ||
| string guidance = $" Please set {nameof(ConfigurationClientOptions)}.{nameof(ConfigurationClientOptions.Audience)} to the appropriate audience for the target cloud. For details on how to configure the authentication token audience visit https://aka.ms/appconfig/client-token-audience."; | ||
| return leading + detail + guidance; | ||
| } | ||
|
|
||
| private class ThrowingPolicy : HttpPipelinePolicy | ||
| { | ||
| private readonly string _message; | ||
| public ThrowingPolicy(string message) => _message = message; | ||
| public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) | ||
| { | ||
| throw new Exception(_message); | ||
| } | ||
| public override ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) | ||
| { | ||
| throw new Exception(_message); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void WrapsError_NoAudienceConfigured() => AssertWrapsError(isAudienceConfigured: false); | ||
|
|
||
| [Test] | ||
| public void WrapsError_WrongAudienceConfigured() => AssertWrapsError(isAudienceConfigured: true); | ||
|
|
||
| [Test] | ||
| public void NonAudienceError_PassesThrough() | ||
| { | ||
| var transport = new MockTransport(new MockResponse(200)); | ||
| var pipeline = new HttpPipeline( | ||
| transport, | ||
| new HttpPipelinePolicy[] | ||
| { | ||
| new AudienceErrorHandlingPolicy(isAudienceConfigured: true), // value irrelevant since code won't match | ||
| new ThrowingPolicy("Simulated failure WITHOUT code") | ||
| }, | ||
| responseClassifier: null); | ||
|
|
||
| var requestUri = new Uri("http://example.com"); | ||
|
|
||
| Exception ex = Assert.ThrowsAsync<Exception>(async () => | ||
| { | ||
| if (IsAsync) | ||
| { | ||
| var message = pipeline.CreateMessage(); | ||
| message.Request.Method = RequestMethod.Get; | ||
| message.Request.Uri.Reset(requestUri); | ||
| await pipeline.SendAsync(message, CancellationToken.None); | ||
| } | ||
| else | ||
| { | ||
| var message = pipeline.CreateMessage(); | ||
| message.Request.Method = RequestMethod.Get; | ||
| message.Request.Uri.Reset(requestUri); | ||
| pipeline.Send(message, CancellationToken.None); | ||
| } | ||
| }); | ||
|
|
||
| Assert.IsNotInstanceOf<RequestFailedException>(ex); // Should not be wrapped | ||
| Assert.AreEqual("Simulated failure WITHOUT code", ex.Message); | ||
| } | ||
|
|
||
| private void AssertWrapsError(bool isAudienceConfigured) | ||
| { | ||
| var transport = new MockTransport(new MockResponse(200)); // Transport won't be reached because throwing policy throws first. | ||
| var pipeline = new HttpPipeline( | ||
| transport, | ||
| new HttpPipelinePolicy[] | ||
| { | ||
| new AudienceErrorHandlingPolicy(isAudienceConfigured), | ||
| new ThrowingPolicy($"Simulated authentication failure {AadAudienceErrorCode}: Resource principal not found") | ||
| }, | ||
| responseClassifier: null); | ||
|
|
||
| var requestUri = new Uri("http://example.com"); | ||
| RequestFailedException ex = Assert.ThrowsAsync<RequestFailedException>(async () => | ||
| { | ||
| if (IsAsync) | ||
| { | ||
| var message = pipeline.CreateMessage(); | ||
| message.Request.Method = RequestMethod.Get; | ||
| message.Request.Uri.Reset(requestUri); | ||
| await pipeline.SendAsync(message, CancellationToken.None); | ||
| } | ||
| else | ||
| { | ||
| var message = pipeline.CreateMessage(); | ||
| message.Request.Method = RequestMethod.Get; | ||
| message.Request.Uri.Reset(requestUri); | ||
| pipeline.Send(message, CancellationToken.None); | ||
| } | ||
| }); | ||
|
|
||
| Assert.NotNull(ex); | ||
| StringAssert.Contains(ExpectedErrorMessage(isAudienceConfigured), ex.Message); | ||
| Assert.NotNull(ex.InnerException); | ||
| StringAssert.Contains(AadAudienceErrorCode, ex.InnerException.Message); | ||
| } | ||
| } | ||
| } |
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.