-
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 3 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
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
68 changes: 68 additions & 0 deletions
68
...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,68 @@ | ||
| // 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 | ||
| = "No audience was provided. An audience must be configured to connect to this cloud."; | ||
|
|
||
| private static final String INCORRECT_AUDIENCE_ERROR_MESSAGE | ||
| = "An incorrect audience was provided. Please update the audience to connect to this cloud."; | ||
|
|
||
| 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)) { | ||
mrm9084 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| String message = audience == null ? NO_AUDIENCE_ERROR_MESSAGE : INCORRECT_AUDIENCE_ERROR_MESSAGE; | ||
| return new HttpResponseException(message, ex.getResponse(), ex); | ||
| } | ||
| return ex; | ||
| } | ||
| } | ||
222 changes: 222 additions & 0 deletions
222
...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,222 @@ | ||
| // 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.assertSame; | ||
| 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 | ||
| = "No audience was provided. An audience must be configured to connect to this cloud."; | ||
| private static final String INCORRECT_AUDIENCE_ERROR_MESSAGE | ||
| = "An incorrect audience was provided. Please update the audience to connect to this cloud."; | ||
|
|
||
| @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 handleAudienceExceptionWithNullMessage() { | ||
| AudiencePolicy audiencePolicy = new AudiencePolicy(ConfigurationAudience.AZURE_PUBLIC_CLOUD); | ||
|
|
||
| HttpResponseException originalException | ||
| = new HttpResponseException(null, new MockHttpResponse(new HttpRequest(HttpMethod.GET, LOCAL_HOST), 401)); | ||
|
|
||
| // Use reflection to access the private method for testing | ||
| try { | ||
| java.lang.reflect.Method method | ||
| = AudiencePolicy.class.getDeclaredMethod("handleAudienceException", HttpResponseException.class); | ||
| method.setAccessible(true); | ||
|
|
||
| HttpResponseException result = (HttpResponseException) method.invoke(audiencePolicy, originalException); | ||
| assertSame(originalException, result, "Should return original exception when message is null"); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to test handleAudienceException with null message", e); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void handleAudienceExceptionWithoutErrorCode() { | ||
| AudiencePolicy audiencePolicy = new AudiencePolicy(ConfigurationAudience.AZURE_PUBLIC_CLOUD); | ||
|
|
||
| HttpResponseException originalException = new HttpResponseException("Some other error", | ||
| new MockHttpResponse(new HttpRequest(HttpMethod.GET, LOCAL_HOST), 401)); | ||
|
|
||
| // Use reflection to access the private method for testing | ||
| try { | ||
| java.lang.reflect.Method method | ||
| = AudiencePolicy.class.getDeclaredMethod("handleAudienceException", HttpResponseException.class); | ||
| method.setAccessible(true); | ||
|
|
||
| HttpResponseException result = (HttpResponseException) method.invoke(audiencePolicy, originalException); | ||
| assertSame(originalException, result, "Should return original exception when error code is not found"); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to test handleAudienceException without error code", e); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void handleAudienceExceptionWithErrorCodeNullAudience() { | ||
| AudiencePolicy audiencePolicy = new AudiencePolicy(null); | ||
|
|
||
| HttpResponseException originalException | ||
| = new HttpResponseException("Error " + AAD_AUDIENCE_ERROR_CODE + " occurred", | ||
| new MockHttpResponse(new HttpRequest(HttpMethod.GET, LOCAL_HOST), 401)); | ||
|
|
||
| // Use reflection to access the private method for testing | ||
| try { | ||
| java.lang.reflect.Method method | ||
| = AudiencePolicy.class.getDeclaredMethod("handleAudienceException", HttpResponseException.class); | ||
| method.setAccessible(true); | ||
|
|
||
| HttpResponseException result = (HttpResponseException) method.invoke(audiencePolicy, originalException); | ||
| assertEquals(NO_AUDIENCE_ERROR_MESSAGE, result.getMessage()); | ||
| assertSame(originalException.getResponse(), result.getResponse()); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to test handleAudienceException with error code and null audience", e); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void handleAudienceExceptionWithErrorCodeConfiguredAudience() { | ||
| AudiencePolicy audiencePolicy = new AudiencePolicy(ConfigurationAudience.AZURE_PUBLIC_CLOUD); | ||
|
|
||
| HttpResponseException originalException | ||
| = new HttpResponseException("Error " + AAD_AUDIENCE_ERROR_CODE + " occurred", | ||
| new MockHttpResponse(new HttpRequest(HttpMethod.GET, LOCAL_HOST), 401)); | ||
|
|
||
| // Use reflection to access the private method for testing | ||
| try { | ||
| java.lang.reflect.Method method | ||
| = AudiencePolicy.class.getDeclaredMethod("handleAudienceException", HttpResponseException.class); | ||
| method.setAccessible(true); | ||
|
|
||
| HttpResponseException result = (HttpResponseException) method.invoke(audiencePolicy, originalException); | ||
| assertEquals(INCORRECT_AUDIENCE_ERROR_MESSAGE, result.getMessage()); | ||
| assertSame(originalException.getResponse(), result.getResponse()); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to test handleAudienceException with error code and configured audience", | ||
| e); | ||
| } | ||
| } | ||
mrm9084 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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.