|
22 | 22 | import java.io.IOException; |
23 | 23 | import java.nio.charset.StandardCharsets; |
24 | 24 | import java.time.Duration; |
25 | | -import java.util.Arrays; |
26 | 25 | import java.util.Collections; |
27 | 26 | import java.util.List; |
28 | | -import java.util.Optional; |
29 | 27 | import java.util.stream.Stream; |
30 | 28 | import org.junit.jupiter.api.BeforeAll; |
31 | 29 | import org.junit.jupiter.api.Test; |
32 | 30 | import org.junit.jupiter.params.ParameterizedTest; |
33 | 31 | import org.junit.jupiter.params.provider.Arguments; |
34 | 32 | import org.junit.jupiter.params.provider.MethodSource; |
35 | 33 | import software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute; |
36 | | -import software.amazon.awssdk.auth.signer.S3SignerExecutionAttribute; |
37 | 34 | import software.amazon.awssdk.auth.signer.internal.SignerConstant; |
38 | 35 | import software.amazon.awssdk.awscore.presigner.PresignedRequest; |
39 | | -import software.amazon.awssdk.core.SdkRequest; |
| 36 | +import software.amazon.awssdk.core.SdkPlugin; |
40 | 37 | import software.amazon.awssdk.core.interceptor.Context; |
41 | 38 | import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
42 | 39 | import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; |
|
46 | 43 | import software.amazon.awssdk.core.waiters.WaiterAcceptor; |
47 | 44 | import software.amazon.awssdk.http.HttpExecuteRequest; |
48 | 45 | import software.amazon.awssdk.http.HttpExecuteResponse; |
49 | | -import software.amazon.awssdk.http.SdkHttpMethod; |
50 | 46 | import software.amazon.awssdk.http.SdkHttpRequest; |
51 | 47 | import software.amazon.awssdk.http.apache.ApacheHttpClient; |
52 | 48 | import software.amazon.awssdk.regions.Region; |
53 | 49 | import software.amazon.awssdk.services.s3.S3Client; |
54 | 50 | import software.amazon.awssdk.services.s3.S3Configuration; |
| 51 | +import software.amazon.awssdk.services.s3.internal.plugins.S3OverrideAuthSchemePropertiesPlugin; |
55 | 52 | import software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException; |
56 | 53 | import software.amazon.awssdk.services.s3.model.NoSuchKeyException; |
57 | | -import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
58 | | -import software.amazon.awssdk.services.s3.model.UploadPartRequest; |
59 | 54 | import software.amazon.awssdk.services.s3.presigner.S3Presigner; |
60 | 55 | import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest; |
61 | 56 | import software.amazon.awssdk.services.s3control.model.BucketAlreadyExistsException; |
@@ -99,7 +94,7 @@ public static void setupFixture() { |
99 | 94 | .build(); |
100 | 95 |
|
101 | 96 | s3Client = mrapEnabledS3Client(Collections.singletonList(captureInterceptor)); |
102 | | - s3ClientWithPayloadSigning = mrapEnabledS3Client(Arrays.asList(captureInterceptor, new PayloadSigningInterceptor())); |
| 97 | + s3ClientWithPayloadSigning = mrapEnabledS3ClientWithPayloadSigning(captureInterceptor); |
103 | 98 |
|
104 | 99 | stsClient = StsClient.builder() |
105 | 100 | .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN) |
@@ -309,6 +304,25 @@ private static S3Client mrapEnabledS3Client(List<ExecutionInterceptor> execution |
309 | 304 | .build(); |
310 | 305 | } |
311 | 306 |
|
| 307 | + private static S3Client mrapEnabledS3ClientWithPayloadSigning(ExecutionInterceptor executionInterceptor) { |
| 308 | + // We can't use here `S3OverrideAuthSchemePropertiesPlugin.enablePayloadSigningPlugin()` since |
| 309 | + // it enables payload signing for *all* operations. |
| 310 | + SdkPlugin plugin = S3OverrideAuthSchemePropertiesPlugin.builder() |
| 311 | + .payloadSigningEnabled(true) |
| 312 | + .addOperationConstraint("UploadPart") |
| 313 | + .addOperationConstraint("PutObject") |
| 314 | + .build(); |
| 315 | + return S3Client.builder() |
| 316 | + .region(REGION) |
| 317 | + .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN) |
| 318 | + .serviceConfiguration(S3Configuration.builder() |
| 319 | + .useArnRegionEnabled(true) |
| 320 | + .build()) |
| 321 | + .overrideConfiguration(o -> o.addExecutionInterceptor(executionInterceptor)) |
| 322 | + .addPlugin(plugin) |
| 323 | + .build(); |
| 324 | + } |
| 325 | + |
312 | 326 | private void deleteObjectIfExists(S3Client s31, String bucket1, String key) { |
313 | 327 | System.out.println(bucket1); |
314 | 328 | try { |
@@ -341,21 +355,4 @@ public void beforeTransmission(Context.BeforeTransmission context, ExecutionAttr |
341 | 355 | this.normalizePath = executionAttributes.getAttribute(AwsSignerExecutionAttribute.SIGNER_NORMALIZE_PATH); |
342 | 356 | } |
343 | 357 | } |
344 | | - |
345 | | - private static class PayloadSigningInterceptor implements ExecutionInterceptor { |
346 | | - |
347 | | - public Optional<RequestBody> modifyHttpContent(Context.ModifyHttpRequest context, |
348 | | - ExecutionAttributes executionAttributes) { |
349 | | - SdkRequest sdkRequest = context.request(); |
350 | | - |
351 | | - if (sdkRequest instanceof PutObjectRequest || sdkRequest instanceof UploadPartRequest) { |
352 | | - executionAttributes.putAttribute(S3SignerExecutionAttribute.ENABLE_PAYLOAD_SIGNING, true); |
353 | | - } |
354 | | - if (!context.requestBody().isPresent() && context.httpRequest().method().equals(SdkHttpMethod.POST)) { |
355 | | - return Optional.of(RequestBody.fromBytes(new byte[0])); |
356 | | - } |
357 | | - |
358 | | - return context.requestBody(); |
359 | | - } |
360 | | - } |
361 | 358 | } |
0 commit comments