-
Notifications
You must be signed in to change notification settings - Fork 2.1k
App Config - Audience policy #47224
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
Open
mrm9084
wants to merge
8
commits into
Azure:main
Choose a base branch
from
mrm9084:AudiencePolicy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
App Config - Audience policy #47224
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a854181
AudiencePolicy
mrm9084 9a82d80
Updated usage + tests
mrm9084 b7048c2
fixing code owners
mrm9084 458d5af
Update sdk/appconfiguration/azure-data-appconfiguration/src/main/java…
mrm9084 c7f2d12
linting changes
mrm9084 dc19956
Revert "linting changes"
mrm9084 22c4e96
text update + suppression
mrm9084 4fba9a5
Trying to fix tests
mrm9084 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
Some comments aren't visible on the classic Files Changed page.
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
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
74 changes: 74 additions & 0 deletions
74
...guration/src/main/java/com/azure/data/appconfiguration/implementation/AudiencePolicy.java
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,74 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
| package com.azure.data.appconfiguration.implementation; | ||
|
|
||
| import com.azure.core.exception.HttpResponseException; | ||
| import com.azure.core.http.HttpPipelineCallContext; | ||
| import com.azure.core.http.HttpPipelineNextPolicy; | ||
| import com.azure.core.http.HttpPipelineNextSyncPolicy; | ||
| import com.azure.core.http.HttpResponse; | ||
| import com.azure.core.http.policy.HttpPipelinePolicy; | ||
| import com.azure.data.appconfiguration.models.ConfigurationAudience; | ||
|
|
||
| import reactor.core.publisher.Mono; | ||
|
|
||
| /** | ||
| * HTTP pipeline policy that handles Azure Active Directory audience-related authentication errors. | ||
| * This policy intercepts HTTP responses and provides more meaningful error messages when | ||
| * audience configuration issues occur during authentication. | ||
| */ | ||
| public class AudiencePolicy implements HttpPipelinePolicy { | ||
|
|
||
| private static final String NO_AUDIENCE_ERROR_MESSAGE | ||
| = "Unable to authenticate to Azure App Configuration. No authentication token audience was provided. " | ||
| + "Please set an Audience in your ConfigurationClientBuilder for the target cloud. " | ||
| + "For details on how to configure the authentication token audience visit " | ||
| + "https://aka.ms/appconfig/client-token-audience."; | ||
|
|
||
| private static final String INCORRECT_AUDIENCE_ERROR_MESSAGE | ||
| = "Unable to authenticate to Azure App Configuration. An incorrect token audience was provided. " | ||
| + "Please set the Audience in your ConfigurationClientBuilder to the appropriate audience for this cloud. " | ||
| + "For details on how to configure the authentication token audience visit " | ||
| + "https://aka.ms/appconfig/client-token-audience."; | ||
|
|
||
| private static final String AAD_AUDIENCE_ERROR_CODE = "AADSTS500011"; | ||
|
|
||
| private final ConfigurationAudience audience; | ||
|
|
||
| /** | ||
| * Creates a new instance of AudiencePolicy. | ||
| * | ||
| * @param audience The configuration audience to use for validation. May be null. | ||
| */ | ||
| public AudiencePolicy(ConfigurationAudience audience) { | ||
| this.audience = audience; | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { | ||
| return next.process().onErrorMap(HttpResponseException.class, this::handleAudienceException); | ||
| } | ||
|
|
||
| @Override | ||
| public HttpResponse processSync(HttpPipelineCallContext context, HttpPipelineNextSyncPolicy next) { | ||
| try { | ||
| return next.processSync(); | ||
| } catch (HttpResponseException ex) { | ||
| throw handleAudienceException(ex); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handles audience-related authentication exceptions by providing more meaningful error messages. | ||
| * | ||
| * @param ex The original HttpResponseException | ||
| * @return A new HttpResponseException with improved error message if audience-related, otherwise the original exception | ||
| */ | ||
| private HttpResponseException handleAudienceException(HttpResponseException ex) { | ||
| if (ex.getMessage() != null && ex.getMessage().contains(AAD_AUDIENCE_ERROR_CODE)) { | ||
| String message = audience == null ? NO_AUDIENCE_ERROR_MESSAGE : INCORRECT_AUDIENCE_ERROR_MESSAGE; | ||
| return new HttpResponseException(message, ex.getResponse(), ex); | ||
| } | ||
| return ex; | ||
| } | ||
| } | ||
167 changes: 167 additions & 0 deletions
167
...tion/src/test/java/com/azure/data/appconfiguration/implementation/AudiencePolicyTest.java
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,167 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.data.appconfiguration.implementation; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.azure.core.exception.HttpResponseException; | ||
| import com.azure.core.http.HttpMethod; | ||
| import com.azure.core.http.HttpPipeline; | ||
| import com.azure.core.http.HttpPipelineBuilder; | ||
| import com.azure.core.http.HttpRequest; | ||
| import com.azure.core.http.HttpResponse; | ||
| import com.azure.core.http.policy.HttpPipelinePolicy; | ||
| import com.azure.core.test.SyncAsyncExtension; | ||
| import com.azure.core.test.annotation.SyncAsyncTest; | ||
| import com.azure.core.test.http.MockHttpResponse; | ||
| import com.azure.core.test.http.NoOpHttpClient; | ||
| import com.azure.core.util.Context; | ||
| import com.azure.data.appconfiguration.models.ConfigurationAudience; | ||
|
|
||
| import reactor.core.publisher.Mono; | ||
| import reactor.test.StepVerifier; | ||
|
|
||
| /** | ||
| * Unit tests for AudiencePolicy | ||
| */ | ||
| public class AudiencePolicyTest { | ||
| private static final String LOCAL_HOST = "http://localhost"; | ||
| private static final String AAD_AUDIENCE_ERROR_CODE = "AADSTS500011"; | ||
| private static final String NO_AUDIENCE_ERROR_MESSAGE | ||
| = "Unable to authenticate to Azure App Configuration. No authentication token audience was provided. " | ||
| + "Please set an Audience in your ConfigurationClientBuilder for the target cloud. " | ||
| + "For details on how to configure the authentication token audience visit " | ||
| + "https://aka.ms/appconfig/client-token-audience."; | ||
|
|
||
| private static final String INCORRECT_AUDIENCE_ERROR_MESSAGE | ||
| = "Unable to authenticate to Azure App Configuration. An incorrect token audience was provided. " | ||
| + "Please set the Audience in your ConfigurationClientBuilder to the appropriate audience for this cloud. " | ||
| + "For details on how to configure the authentication token audience visit " | ||
| + "https://aka.ms/appconfig/client-token-audience."; | ||
|
|
||
| @SyncAsyncTest | ||
| public void processWithoutException() { | ||
| AudiencePolicy audiencePolicy = new AudiencePolicy(ConfigurationAudience.AZURE_PUBLIC_CLOUD); | ||
|
|
||
| HttpPipelinePolicy testPolicy = (context, next) -> { | ||
| return next.process(); | ||
| }; | ||
|
|
||
| final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() { | ||
| @Override | ||
| public Mono<HttpResponse> send(HttpRequest request) { | ||
| return Mono.just(new MockHttpResponse(request, 200)); | ||
| } | ||
| }).policies(audiencePolicy, testPolicy).build(); | ||
|
|
||
| SyncAsyncExtension.execute(() -> sendRequestSync(pipeline), () -> sendRequest(pipeline)); | ||
| } | ||
|
|
||
| @Test | ||
| public void processWithNonAudienceException() { | ||
| AudiencePolicy audiencePolicy = new AudiencePolicy(ConfigurationAudience.AZURE_PUBLIC_CLOUD); | ||
|
|
||
| HttpPipelinePolicy exceptionPolicy = (context, next) -> { | ||
| HttpResponseException ex | ||
| = new HttpResponseException("Some other error", new MockHttpResponse(context.getHttpRequest(), 401)); | ||
| return Mono.error(ex); | ||
| }; | ||
|
|
||
| final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient()) | ||
| .policies(audiencePolicy, exceptionPolicy) | ||
| .build(); | ||
|
|
||
| StepVerifier.create(sendRequest(pipeline)) | ||
| .expectErrorMatches(throwable -> throwable instanceof HttpResponseException | ||
| && throwable.getMessage().equals("Some other error")) | ||
| .verify(); | ||
|
|
||
| // Test sync version | ||
| HttpResponseException thrown = assertThrows(HttpResponseException.class, () -> sendRequestSync(pipeline)); | ||
| assertEquals("Some other error", thrown.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void processWithAudienceExceptionAndNullAudience() { | ||
| AudiencePolicy audiencePolicy = new AudiencePolicy(null); | ||
|
|
||
| HttpPipelinePolicy exceptionPolicy = (context, next) -> { | ||
| HttpResponseException ex = new HttpResponseException("Error " + AAD_AUDIENCE_ERROR_CODE + " occurred", | ||
| new MockHttpResponse(context.getHttpRequest(), 401)); | ||
| return Mono.error(ex); | ||
| }; | ||
|
|
||
| final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient()) | ||
| .policies(audiencePolicy, exceptionPolicy) | ||
| .build(); | ||
|
|
||
| StepVerifier.create(sendRequest(pipeline)) | ||
| .expectErrorMatches(throwable -> throwable instanceof HttpResponseException | ||
| && throwable.getMessage().equals(NO_AUDIENCE_ERROR_MESSAGE)) | ||
| .verify(); | ||
|
|
||
| // Test sync version | ||
| HttpResponseException thrown = assertThrows(HttpResponseException.class, () -> sendRequestSync(pipeline)); | ||
| assertEquals(NO_AUDIENCE_ERROR_MESSAGE, thrown.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void processWithAudienceExceptionAndConfiguredAudience() { | ||
| AudiencePolicy audiencePolicy = new AudiencePolicy(ConfigurationAudience.AZURE_PUBLIC_CLOUD); | ||
|
|
||
| HttpPipelinePolicy exceptionPolicy = (context, next) -> { | ||
| HttpResponseException ex = new HttpResponseException("Error " + AAD_AUDIENCE_ERROR_CODE + " occurred", | ||
| new MockHttpResponse(context.getHttpRequest(), 401)); | ||
| return Mono.error(ex); | ||
| }; | ||
|
|
||
| final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient()) | ||
| .policies(audiencePolicy, exceptionPolicy) | ||
| .build(); | ||
|
|
||
| StepVerifier.create(sendRequest(pipeline)) | ||
| .expectErrorMatches(throwable -> throwable instanceof HttpResponseException | ||
| && throwable.getMessage().equals(INCORRECT_AUDIENCE_ERROR_MESSAGE)) | ||
| .verify(); | ||
|
|
||
| // Test sync version | ||
| HttpResponseException thrown = assertThrows(HttpResponseException.class, () -> sendRequestSync(pipeline)); | ||
| assertEquals(INCORRECT_AUDIENCE_ERROR_MESSAGE, thrown.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void processWithNullMessageException() { | ||
| AudiencePolicy audiencePolicy = new AudiencePolicy(ConfigurationAudience.AZURE_PUBLIC_CLOUD); | ||
|
|
||
| HttpPipelinePolicy exceptionPolicy = (context, next) -> { | ||
| HttpResponseException ex | ||
| = new HttpResponseException(null, new MockHttpResponse(context.getHttpRequest(), 401)); | ||
| return Mono.error(ex); | ||
| }; | ||
|
|
||
| final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient()) | ||
| .policies(audiencePolicy, exceptionPolicy) | ||
| .build(); | ||
|
|
||
| StepVerifier.create(sendRequest(pipeline)) | ||
| .expectErrorMatches( | ||
| throwable -> throwable instanceof HttpResponseException && throwable.getMessage() == null) | ||
| .verify(); | ||
|
|
||
| // Test sync version | ||
| HttpResponseException thrown = assertThrows(HttpResponseException.class, () -> sendRequestSync(pipeline)); | ||
| assertEquals(null, thrown.getMessage(), "Should return original exception when message is null"); | ||
| } | ||
|
|
||
| private Mono<HttpResponse> sendRequest(HttpPipeline pipeline) { | ||
| return pipeline.send(new HttpRequest(HttpMethod.GET, LOCAL_HOST)); | ||
| } | ||
|
|
||
| private HttpResponse sendRequestSync(HttpPipeline pipeline) { | ||
| return pipeline.sendSync(new HttpRequest(HttpMethod.GET, LOCAL_HOST), Context.NONE); | ||
| } | ||
| } |
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.