|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.services;
|
17 | 17 |
|
| 18 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
18 | 19 | import static org.mockito.ArgumentMatchers.any;
|
19 | 20 | import static org.mockito.Mockito.verify;
|
| 21 | +import static org.mockito.Mockito.when; |
20 | 22 | import static software.amazon.awssdk.core.client.config.SdkAdvancedClientOption.SIGNER;
|
21 | 23 |
|
| 24 | +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; |
| 25 | +import com.github.tomakehurst.wiremock.junit5.WireMockTest; |
22 | 26 | import java.net.URI;
|
| 27 | +import java.nio.ByteBuffer; |
| 28 | +import java.util.concurrent.CompletableFuture; |
23 | 29 | import org.junit.Test;
|
| 30 | +import org.junit.jupiter.api.BeforeEach; |
24 | 31 | import org.junit.runner.RunWith;
|
25 | 32 | import org.mockito.Mock;
|
26 | 33 | import org.mockito.junit.MockitoJUnitRunner;
|
| 34 | +import org.reactivestreams.Publisher; |
27 | 35 | import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
28 | 36 | import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
29 | 37 | import software.amazon.awssdk.awscore.AwsRequest;
|
|
33 | 41 | import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
|
34 | 42 | import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
|
35 | 43 | import software.amazon.awssdk.core.signer.Signer;
|
| 44 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 45 | +import software.amazon.awssdk.http.SdkHttpClient; |
36 | 46 | import software.amazon.awssdk.http.SdkHttpFullRequest;
|
| 47 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 48 | +import software.amazon.awssdk.http.async.SdkAsyncHttpClient; |
| 49 | +import software.amazon.awssdk.http.auth.aws.scheme.AwsV4AuthScheme; |
| 50 | +import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner; |
| 51 | +import software.amazon.awssdk.http.auth.spi.signer.AsyncSignRequest; |
| 52 | +import software.amazon.awssdk.http.auth.spi.signer.AsyncSignedRequest; |
| 53 | +import software.amazon.awssdk.http.auth.spi.signer.SignRequest; |
| 54 | +import software.amazon.awssdk.http.auth.spi.signer.SignedRequest; |
| 55 | +import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; |
| 56 | +import software.amazon.awssdk.identity.spi.IdentityProvider; |
| 57 | +import software.amazon.awssdk.identity.spi.IdentityProviders; |
37 | 58 | import software.amazon.awssdk.regions.Region;
|
38 | 59 | import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient;
|
39 | 60 | import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient;
|
40 | 61 | import software.amazon.awssdk.services.protocolrestjson.model.AllTypesRequest;
|
41 | 62 | import software.amazon.awssdk.services.protocolrestjson.model.StreamingInputOperationRequest;
|
42 |
| - |
| 63 | +import software.amazon.awssdk.services.testutil.ValidSdkObjects; |
| 64 | +import software.amazon.awssdk.testutils.service.http.MockAsyncHttpClient; |
| 65 | +import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; |
43 | 66 |
|
44 | 67 | @RunWith(MockitoJUnitRunner.class)
|
45 | 68 | public class SignerOverrideTest {
|
46 | 69 | @Mock
|
47 | 70 | public Signer mockSigner;
|
48 | 71 |
|
| 72 | + @Mock |
| 73 | + public static AwsV4HttpSigner mockHttpSigner; |
| 74 | + |
| 75 | + @Mock |
| 76 | + public SignedRequest signedRequest; |
| 77 | + @Mock |
| 78 | + public AsyncSignedRequest asyncSignedRequest; |
| 79 | + |
| 80 | + @BeforeEach |
| 81 | + public void setup() { |
| 82 | + SdkHttpRequest sdkHttpRequest = ValidSdkObjects.sdkHttpFullRequest().build(); |
| 83 | + Publisher<ByteBuffer> signedPayload = AsyncRequestBody.fromString("signed async request body"); |
| 84 | + |
| 85 | + when(mockHttpSigner.sign(any(SignRequest.class))).thenReturn(SignedRequest.builder().build()); |
| 86 | + |
| 87 | + CompletableFuture<AsyncSignedRequest> requestFuture = new CompletableFuture<>(); |
| 88 | + requestFuture.complete(asyncSignedRequest); |
| 89 | + when(mockHttpSigner.signAsync(any(AsyncSignRequest.class))) |
| 90 | + .thenReturn( |
| 91 | + CompletableFuture.completedFuture(AsyncSignedRequest.builder() |
| 92 | + .request(sdkHttpRequest) |
| 93 | + .payload(signedPayload) |
| 94 | + .build())); |
| 95 | + } |
| 96 | + |
49 | 97 | /**
|
50 | 98 | * Test to ensure that operations that use the {@link software.amazon.awssdk.auth.signer.AsyncAws4Signer} don't apply
|
51 | 99 | * the override when the signer is overridden by the customer.
|
@@ -99,21 +147,61 @@ public void syncClient_oldSignerOverriddenInExecutionInterceptor_takesPrecedence
|
99 | 147 | verify(mockSigner).sign(any(SdkHttpFullRequest.class), any(ExecutionAttributes.class));
|
100 | 148 | }
|
101 | 149 |
|
| 150 | + @Test |
| 151 | + public void sync_httpSignerOverride_takesPrecedence() { |
| 152 | + try (ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() |
| 153 | + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) |
| 154 | + .region(Region.US_WEST_2) |
| 155 | + .putAuthScheme(new MockAuthScheme()) |
| 156 | + .build()) { |
| 157 | + |
| 158 | + assertThatThrownBy(() -> client.streamingInputOperation(StreamingInputOperationRequest.builder().build(), |
| 159 | + RequestBody.fromString("test"))).isInstanceOf(NullPointerException.class); |
| 160 | + verify(mockHttpSigner).sign(any(SignRequest.class)); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void async_httpSignerOverride_takesPrecedence() { |
| 166 | + try(ProtocolRestJsonAsyncClient asyncClient = ProtocolRestJsonAsyncClient.builder() |
| 167 | + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) |
| 168 | + .region(Region.US_WEST_2) |
| 169 | + .putAuthScheme(new MockAuthScheme()) |
| 170 | + .build()) { |
| 171 | + assertThatThrownBy(() -> asyncClient.streamingInputOperation(StreamingInputOperationRequest.builder().build(), |
| 172 | + AsyncRequestBody.fromString("test")).join()).hasRootCauseInstanceOf(NullPointerException.class); |
| 173 | + } |
| 174 | + verify(mockHttpSigner).signAsync(any(AsyncSignRequest.class)); |
| 175 | + } |
| 176 | + |
| 177 | + |
102 | 178 | private ExecutionInterceptor signerOverrideExecutionInterceptor(Signer signer) {
|
103 | 179 | return new ExecutionInterceptor() {
|
104 | 180 | @Override
|
105 | 181 | public SdkRequest modifyRequest(Context.ModifyRequest context, ExecutionAttributes executionAttributes) {
|
106 | 182 | AwsRequest.Builder builder = (AwsRequest.Builder) context.request().toBuilder();
|
107 | 183 | builder.overrideConfiguration(c -> c.signer(signer)
|
108 |
| - .build()); |
| 184 | + .build()); |
109 | 185 |
|
110 | 186 | return builder.build();
|
111 | 187 | }
|
112 | 188 | };
|
113 | 189 | }
|
114 | 190 |
|
115 |
| - // TODO(sra-identity-and-auth): Add test for SRA way of overriding signer to assert that overridden signer is used. |
116 |
| - // At that point, rename this class to SignerOverrideTest, not specific to AsyncSignerOverride (which was for operation |
117 |
| - // level codegen changes). |
| 191 | + private static class MockAuthScheme implements AwsV4AuthScheme { |
| 192 | + @Override |
| 193 | + public IdentityProvider<AwsCredentialsIdentity> identityProvider(IdentityProviders providers) { |
| 194 | + return providers.identityProvider(AwsCredentialsIdentity.class); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public AwsV4HttpSigner signer() { |
| 199 | + return mockHttpSigner; |
| 200 | + } |
118 | 201 |
|
| 202 | + @Override |
| 203 | + public String schemeId() { |
| 204 | + return SCHEME_ID; |
| 205 | + } |
| 206 | + } |
119 | 207 | }
|
0 commit comments