-
Notifications
You must be signed in to change notification settings - Fork 1k
Cache auth scheme resolution #7282
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
6 commits
Select commit
Hold shift + click to select a range
6dfd73c
Cache auth scheme resolution results per operation
davidh44 421e679
Update codegenerated test files
davidh44 b81733a
Refactor and address comments
davidh44 778b5fb
Update codegenearted test files
davidh44 52f1bde
Guard against future new auth params
davidh44 534f2d9
Guard against future new auth params
davidh44 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "feature", | ||
| "category": "AWS SDK for Java v2", | ||
| "contributor": "", | ||
| "description": "Cache auth scheme resolution results per operation" | ||
| } |
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
95 changes: 95 additions & 0 deletions
95
codegen/src/test/java/software/amazon/awssdk/codegen/poet/client/AuthSchemeCacheKeyTest.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,95 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.codegen.poet.client; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static software.amazon.awssdk.codegen.poet.ClientTestModels.customPackageModels; | ||
| import static software.amazon.awssdk.codegen.poet.ClientTestModels.opsWithSigv4a; | ||
| import static software.amazon.awssdk.codegen.poet.ClientTestModels.restJsonServiceModels; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import org.junit.Test; | ||
| import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; | ||
| import software.amazon.awssdk.codegen.poet.auth.scheme.AuthSchemeSpecUtils; | ||
| import software.amazon.awssdk.codegen.poet.rules.EndpointRulesSpecUtils; | ||
|
|
||
| /** | ||
| * Verifies that every AwsExecutionAttribute used in auth scheme params building is also present in the cache key. | ||
| * If a new attribute is added to the params without updating the cache key, this test will fail. | ||
| */ | ||
| public class AuthSchemeCacheKeyTest { | ||
|
|
||
| private static final Pattern AWS_EXEC_ATTR_PATTERN = | ||
| Pattern.compile("AwsExecutionAttribute\\.(\\w+)"); | ||
|
|
||
| @Test | ||
| public void restJson_cacheKeyCoversAllParamAttributes() { | ||
| verifyConsistency(restJsonServiceModels()); | ||
| } | ||
|
|
||
| @Test | ||
| public void sigv4a_cacheKeyCoversAllParamAttributes() { | ||
| verifyConsistency(opsWithSigv4a()); | ||
| } | ||
|
|
||
| @Test | ||
| public void uniformAuth_cacheKeyCoversAllParamAttributes() { | ||
| verifyConsistency(customPackageModels()); | ||
| } | ||
|
|
||
| private void verifyConsistency(IntermediateModel model) { | ||
| AuthSchemeSpecUtils authSchemeSpecUtils = new AuthSchemeSpecUtils(model); | ||
| EndpointRulesSpecUtils endpointRulesSpecUtils = new EndpointRulesSpecUtils(model); | ||
|
|
||
| String source = ClientClassUtils.resolveAuthSchemeOptionsMethod(authSchemeSpecUtils, endpointRulesSpecUtils) | ||
| .toString(); | ||
|
|
||
| int resolveCallIndex = source.indexOf(".resolveAuthScheme("); | ||
| assertThat(resolveCallIndex).as("resolveAuthScheme call should exist in generated method").isGreaterThan(0); | ||
|
|
||
| int cacheKeyStart = source.indexOf("cacheKey ="); | ||
| if (cacheKeyStart < 0) { | ||
| // No cache — endpoint-based service, nothing to verify | ||
| return; | ||
| } | ||
| int cacheKeyEnd = source.indexOf(";", cacheKeyStart); | ||
|
|
||
| int paramsStart = source.indexOf("paramsBuilder"); | ||
| String paramsSection = source.substring(paramsStart, resolveCallIndex); | ||
|
|
||
| Set<String> paramsAttributes = extractAttributes(paramsSection); | ||
| assertThat(paramsAttributes).as("Expected auth params to reference AwsExecutionAttributes").isNotEmpty(); | ||
| String cacheKeySection = source.substring(cacheKeyStart, cacheKeyEnd); | ||
| Set<String> cacheKeyAttributes = extractAttributes(cacheKeySection); | ||
|
|
||
| assertThat(cacheKeyAttributes) | ||
| .as("Cache key must include all AwsExecutionAttributes used in params building. " | ||
| + "If you added a new attribute to params, add it to addAuthSchemeCacheLookup() too.") | ||
| .containsAll(paramsAttributes); | ||
| } | ||
|
|
||
| private Set<String> extractAttributes(String section) { | ||
| Set<String> attributes = new HashSet<>(); | ||
| Matcher matcher = AWS_EXEC_ATTR_PATTERN.matcher(section); | ||
| while (matcher.find()) { | ||
| attributes.add(matcher.group(1)); | ||
| } | ||
| return attributes; | ||
| } | ||
| } |
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.
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.