|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services.s3; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 22 | +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
| 23 | +import software.amazon.awssdk.auth.signer.S3SignerExecutionAttribute; |
| 24 | +import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; |
| 25 | +import software.amazon.awssdk.http.HttpExecuteResponse; |
| 26 | +import software.amazon.awssdk.http.SdkHttpResponse; |
| 27 | +import software.amazon.awssdk.regions.Region; |
| 28 | +import software.amazon.awssdk.testutils.service.http.MockAsyncHttpClient; |
| 29 | +import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; |
| 30 | + |
| 31 | +/** |
| 32 | + * Ensure that payload signing is disabled for S3 operations. |
| 33 | + */ |
| 34 | +public class PayloadSigningDisabledTest { |
| 35 | + private static final AwsCredentialsProvider CREDENTIALS = () -> AwsBasicCredentials.create("akid", "skid"); |
| 36 | + private static final ClientOverrideConfiguration ENABLE_PAYLOAD_SIGNING_CONFIG = |
| 37 | + ClientOverrideConfiguration.builder() |
| 38 | + .putExecutionAttribute(S3SignerExecutionAttribute.ENABLE_PAYLOAD_SIGNING, true) |
| 39 | + .build(); |
| 40 | + |
| 41 | + @Test |
| 42 | + public void syncPayloadSigningIsDisabled() { |
| 43 | + try (MockSyncHttpClient httpClient = new MockSyncHttpClient(); |
| 44 | + S3Client s3 = S3Client.builder() |
| 45 | + .region(Region.US_WEST_2) |
| 46 | + .credentialsProvider(CREDENTIALS) |
| 47 | + .httpClient(httpClient) |
| 48 | + .build()) { |
| 49 | + httpClient.stubNextResponse(HttpExecuteResponse.builder() |
| 50 | + .response(SdkHttpResponse.builder().statusCode(200).build()) |
| 51 | + .build()); |
| 52 | + |
| 53 | + s3.createBucket(r -> r.bucket("foo")); |
| 54 | + |
| 55 | + assertThat(httpClient.getLastRequest().firstMatchingHeader("x-amz-content-sha256")) |
| 56 | + .hasValue("UNSIGNED-PAYLOAD"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void asyncPayloadSigningIsDisabled() { |
| 62 | + try (MockAsyncHttpClient httpClient = new MockAsyncHttpClient(); |
| 63 | + S3AsyncClient s3 = S3AsyncClient.builder() |
| 64 | + .region(Region.US_WEST_2) |
| 65 | + .credentialsProvider(CREDENTIALS) |
| 66 | + .httpClient(httpClient) |
| 67 | + .build()) { |
| 68 | + httpClient.stubNextResponse(HttpExecuteResponse.builder() |
| 69 | + .response(SdkHttpResponse.builder().statusCode(200).build()) |
| 70 | + .build()); |
| 71 | + |
| 72 | + s3.createBucket(r -> r.bucket("foo")).join(); |
| 73 | + |
| 74 | + assertThat(httpClient.getLastRequest().firstMatchingHeader("x-amz-content-sha256")) |
| 75 | + .hasValue("UNSIGNED-PAYLOAD"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void syncPayloadSigningCanBeEnabled() { |
| 81 | + try (MockSyncHttpClient httpClient = new MockSyncHttpClient(); |
| 82 | + S3Client s3 = S3Client.builder() |
| 83 | + .region(Region.US_WEST_2) |
| 84 | + .credentialsProvider(CREDENTIALS) |
| 85 | + .httpClient(httpClient) |
| 86 | + .overrideConfiguration(ENABLE_PAYLOAD_SIGNING_CONFIG) |
| 87 | + .build()) { |
| 88 | + httpClient.stubNextResponse(HttpExecuteResponse.builder() |
| 89 | + .response(SdkHttpResponse.builder().statusCode(200).build()) |
| 90 | + .build()); |
| 91 | + |
| 92 | + s3.createBucket(r -> r.bucket("foo")); |
| 93 | + |
| 94 | + assertThat(httpClient.getLastRequest().firstMatchingHeader("x-amz-content-sha256")) |
| 95 | + .hasValue("a40ef303139635de59992f34c1c7da763f89200f2d55b71016f7c156527d63a0"); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void asyncPayloadSigningCanBeEnabled() { |
| 101 | + try (MockAsyncHttpClient httpClient = new MockAsyncHttpClient(); |
| 102 | + S3AsyncClient s3 = S3AsyncClient.builder() |
| 103 | + .region(Region.US_WEST_2) |
| 104 | + .credentialsProvider(CREDENTIALS) |
| 105 | + .httpClient(httpClient) |
| 106 | + .overrideConfiguration(ENABLE_PAYLOAD_SIGNING_CONFIG) |
| 107 | + .build()) { |
| 108 | + httpClient.stubNextResponse(HttpExecuteResponse.builder() |
| 109 | + .response(SdkHttpResponse.builder().statusCode(200).build()) |
| 110 | + .build()); |
| 111 | + |
| 112 | + s3.createBucket(r -> r.bucket("foo")).join(); |
| 113 | + |
| 114 | + assertThat(httpClient.getLastRequest().firstMatchingHeader("x-amz-content-sha256")) |
| 115 | + .hasValue("a40ef303139635de59992f34c1c7da763f89200f2d55b71016f7c156527d63a0"); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments