-
Notifications
You must be signed in to change notification settings - Fork 877
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
Add tests to assert signer and endpoint auth-scheme behaviors #4651
Open
haydenbaker
wants to merge
6
commits into
master
Choose a base branch
from
haydenbaker/signer-override-auth-scheme-testing
base: master
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7768fe5
Add tests to assert signer and endpoint override behaviors
haydenbaker 509c65f
Handle TODOs that were causing test to fail
haydenbaker 2829700
Address nits
haydenbaker 5cf2ed1
Use capturing-interceptor
haydenbaker 343ab78
Make parameterized for different signer type
haydenbaker f1c81e9
Add crt dep for test
haydenbaker 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
66 changes: 0 additions & 66 deletions
66
...d-classes-test/src/test/java/software/amazon/awssdk/services/AsyncSignerOverrideTest.java
This file was deleted.
Oops, something went wrong.
237 changes: 237 additions & 0 deletions
237
...es-test/src/test/java/software/amazon/awssdk/services/SignerAndEndpointOverridesTest.java
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 |
---|---|---|
@@ -0,0 +1,237 @@ | ||
/* | ||
* 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.services; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static software.amazon.awssdk.core.client.config.SdkAdvancedClientOption.SIGNER; | ||
|
||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.auth.signer.Aws4Signer; | ||
import software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute; | ||
import software.amazon.awssdk.authcrt.signer.AwsCrtV4aSigner; | ||
import software.amazon.awssdk.awscore.endpoints.AwsEndpointAttribute; | ||
import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4AuthScheme; | ||
import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4aAuthScheme; | ||
import software.amazon.awssdk.core.interceptor.Context; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; | ||
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute; | ||
import software.amazon.awssdk.core.signer.Signer; | ||
import software.amazon.awssdk.endpoints.Endpoint; | ||
import software.amazon.awssdk.http.auth.aws.scheme.AwsV4aAuthScheme; | ||
import software.amazon.awssdk.http.auth.aws.signer.AwsV4FamilyHttpSigner; | ||
import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner; | ||
import software.amazon.awssdk.http.auth.aws.signer.AwsV4aHttpSigner; | ||
import software.amazon.awssdk.http.auth.aws.signer.RegionSet; | ||
import software.amazon.awssdk.http.auth.spi.scheme.AuthSchemeOption; | ||
import software.amazon.awssdk.services.protocolquery.ProtocolQueryAsyncClient; | ||
import software.amazon.awssdk.services.protocolquery.ProtocolQueryClient; | ||
import software.amazon.awssdk.services.protocolquery.auth.scheme.ProtocolQueryAuthSchemeProvider; | ||
import software.amazon.awssdk.services.protocolquery.endpoints.ProtocolQueryEndpointProvider; | ||
|
||
/** | ||
* Tests to ensure that parameters set when endpoint and auth-scheme resolution occurs get propagated to the overriden | ||
* signer (i.e. pre-SRA signers). These tests also test that a different type of signer from the auth-scheme still has params | ||
* propagated to it. | ||
*/ | ||
public class SignerAndEndpointOverridesTest { | ||
|
||
private CapturingInterceptor interceptor; | ||
|
||
@BeforeEach | ||
void setup() { | ||
this.interceptor = new CapturingInterceptor(); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideSigners") | ||
void test_whenV4EndpointAuthSchemeWithSignerOverride_thenEndpointParamsShouldPropagateToSigner(Signer signer) { | ||
ProtocolQueryClient client = ProtocolQueryClient | ||
.builder() | ||
.authSchemeProvider(v4AuthSchemeProviderOverride()) | ||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) | ||
.endpointProvider(v4EndpointProviderOverride()) | ||
.overrideConfiguration(o -> o.putAdvancedOption(SIGNER, signer).addExecutionInterceptor(interceptor)) | ||
.build(); | ||
|
||
assertThatThrownBy(() -> client.allTypes(r -> {})).hasMessageContaining("boom!"); | ||
assertEquals("us-west-1", interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SIGNING_REGION).id()); | ||
assertEquals("query-test-v4", interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SERVICE_SIGNING_NAME)); | ||
assertEquals(Boolean.FALSE, interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SIGNER_DOUBLE_URL_ENCODE)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideSigners") | ||
void testAsync_whenV4EndpointAuthSchemeWithSignerOverride_thenEndpointParamsShouldPropagateToSigner(Signer signer) { | ||
ProtocolQueryAsyncClient client = ProtocolQueryAsyncClient | ||
.builder() | ||
.authSchemeProvider(v4AuthSchemeProviderOverride()) | ||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) | ||
.endpointProvider(v4EndpointProviderOverride()) | ||
.overrideConfiguration(o -> o.putAdvancedOption(SIGNER, signer).addExecutionInterceptor(interceptor)) | ||
.build(); | ||
|
||
assertThatThrownBy(() -> client.allTypes(r -> {}).join()).hasMessageContaining("boom!"); | ||
assertEquals("us-west-1", interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SIGNING_REGION).id()); | ||
assertEquals("query-test-v4", interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SERVICE_SIGNING_NAME)); | ||
assertEquals(Boolean.FALSE, interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SIGNER_DOUBLE_URL_ENCODE)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideSigners") | ||
void test_whenV4aEndpointAuthSchemeWithSignerOverride_thenEndpointParamsShouldPropagateToSigner(Signer signer) { | ||
ProtocolQueryClient client = ProtocolQueryClient | ||
.builder() | ||
.authSchemeProvider(v4aAuthSchemeProviderOverride()) | ||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) | ||
.endpointProvider(v4aEndpointProviderOverride()) | ||
.overrideConfiguration( | ||
o -> o.putAdvancedOption(SIGNER, signer) | ||
.addExecutionInterceptor(interceptor) | ||
.putExecutionAttribute(SdkInternalExecutionAttribute.AUTH_SCHEMES, Collections.singletonMap( | ||
"aws.auth#sigv4a", AwsV4aAuthScheme.create() | ||
))) | ||
.build(); | ||
|
||
assertThatThrownBy(() -> client.allTypes(r -> {})).hasMessageContaining("boom!"); | ||
assertEquals("us-east-1", interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SIGNING_REGION_SCOPE).id()); | ||
assertEquals("query-test-v4a", | ||
interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SERVICE_SIGNING_NAME)); | ||
assertEquals(Boolean.FALSE, interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SIGNER_DOUBLE_URL_ENCODE)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideSigners") | ||
void testAsync_whenV4aEndpointAuthSchemeWithSignerOverride_thenEndpointParamsShouldPropagateToSigner(Signer signer) { | ||
ProtocolQueryAsyncClient client = ProtocolQueryAsyncClient | ||
.builder() | ||
.authSchemeProvider(v4aAuthSchemeProviderOverride()) | ||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) | ||
.endpointProvider(v4aEndpointProviderOverride()) | ||
.overrideConfiguration( | ||
o -> o.putAdvancedOption(SIGNER, signer) | ||
.addExecutionInterceptor(interceptor) | ||
.putExecutionAttribute(SdkInternalExecutionAttribute.AUTH_SCHEMES, Collections.singletonMap( | ||
"aws.auth#sigv4a", AwsV4aAuthScheme.create() | ||
))) | ||
.build(); | ||
|
||
assertThatThrownBy(() -> client.allTypes(r -> {}).join()).hasMessageContaining("boom!"); | ||
assertEquals("us-east-1", interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SIGNING_REGION_SCOPE).id()); | ||
assertEquals("query-test-v4a", | ||
interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SERVICE_SIGNING_NAME)); | ||
assertEquals(Boolean.FALSE, interceptor.executionAttributes.getAttribute(AwsSignerExecutionAttribute.SIGNER_DOUBLE_URL_ENCODE)); | ||
} | ||
|
||
private static List<Signer> provideSigners() { | ||
return Arrays.asList(Aws4Signer.create(), AwsCrtV4aSigner.create()); | ||
} | ||
|
||
private static ProtocolQueryAuthSchemeProvider v4AuthSchemeProviderOverride() { | ||
return x -> { | ||
List<AuthSchemeOption> options = new ArrayList<>(); | ||
options.add( | ||
AuthSchemeOption.builder().schemeId("aws.auth#sigv4") | ||
.putSignerProperty(AwsV4FamilyHttpSigner.SERVICE_SIGNING_NAME, "query-will-be-overriden") | ||
.putSignerProperty(AwsV4HttpSigner.REGION_NAME, "region-will-be-overriden") | ||
.build() | ||
); | ||
return Collections.unmodifiableList(options); | ||
}; | ||
} | ||
|
||
private static ProtocolQueryAuthSchemeProvider v4aAuthSchemeProviderOverride() { | ||
return x -> { | ||
List<AuthSchemeOption> options = new ArrayList<>(); | ||
options.add( | ||
AuthSchemeOption.builder().schemeId("aws.auth#sigv4a") | ||
.putSignerProperty(AwsV4FamilyHttpSigner.SERVICE_SIGNING_NAME, "query-will-be-overriden") | ||
.putSignerProperty(AwsV4aHttpSigner.REGION_SET, RegionSet.create("region-will-be-overriden")) | ||
.build() | ||
); | ||
return Collections.unmodifiableList(options); | ||
}; | ||
} | ||
|
||
private static ProtocolQueryEndpointProvider v4EndpointProviderOverride() { | ||
return x -> { | ||
Endpoint endpoint = | ||
Endpoint.builder() | ||
.url(URI.create("https://testv4.query.us-west-1")) | ||
.putAttribute( | ||
AwsEndpointAttribute.AUTH_SCHEMES, | ||
Collections.singletonList(SigV4AuthScheme.builder() | ||
.signingRegion("us-west-1") | ||
.signingName("query-test-v4") | ||
.disableDoubleEncoding(true) | ||
.build())) | ||
.build(); | ||
|
||
return CompletableFuture.completedFuture(endpoint); | ||
}; | ||
} | ||
|
||
private static ProtocolQueryEndpointProvider v4aEndpointProviderOverride() { | ||
return x -> { | ||
Endpoint endpoint = | ||
Endpoint.builder() | ||
.url(URI.create("https://testv4a.query.us-east-1")) | ||
.putAttribute( | ||
AwsEndpointAttribute.AUTH_SCHEMES, | ||
Collections.singletonList(SigV4aAuthScheme.builder() | ||
.addSigningRegion("us-east-1") | ||
.signingName("query-test-v4a") | ||
.disableDoubleEncoding(true) | ||
.build())) | ||
.build(); | ||
|
||
return CompletableFuture.completedFuture(endpoint); | ||
}; | ||
} | ||
|
||
private static class CapturingInterceptor implements ExecutionInterceptor { | ||
private Context.BeforeTransmission context; | ||
private ExecutionAttributes executionAttributes; | ||
|
||
@Override | ||
public void beforeTransmission(Context.BeforeTransmission context, ExecutionAttributes executionAttributes) { | ||
this.context = context; | ||
this.executionAttributes = executionAttributes; | ||
throw new RuntimeException("boom!"); | ||
} | ||
|
||
public ExecutionAttributes executionAttributes() { | ||
return executionAttributes; | ||
} | ||
|
||
public class CaptureCompletedException extends RuntimeException { | ||
CaptureCompletedException(String message) { | ||
super(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.
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.
Directly setting AUTH_SCHEME_RESOLVER into ExecutionAttributes is not how users would specify request level override for it. These attributes are SdkInternalExecutionAttributes (and SdkProtectedApi) which customers are not supposed to use directly. Even if they can set ExecutionAttributes in RequestOverrideConfiguration. To resolve this TODO we need to support a first class setter somewhere in RequestOverrideConfiguration(or sub-class) for each of these properties.
We have other internal tasks tracking these TODOs, let's not try to address those in the context of this PR.
I think you made this change to have the tests below with sigv4a. The supported way to do that is
putAuthScheme
on the clientBuilder. (also note that's also at client level not request level which is what this TODO is about).