|
| 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.checksum; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static software.amazon.awssdk.services.s3.utils.S3TestUtils.KB; |
| 20 | +import static software.amazon.awssdk.services.s3.utils.S3TestUtils.createDataOfSizeInKb; |
| 21 | +import static software.amazon.awssdk.services.s3.utils.S3TestUtils.fixedLengthFileWithRandomCharacters; |
| 22 | +import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.io.IOException; |
| 26 | +import java.net.URI; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.util.concurrent.CompletableFuture; |
| 29 | +import org.junit.jupiter.api.BeforeAll; |
| 30 | +import org.junit.jupiter.api.Disabled; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.params.ParameterizedTest; |
| 33 | +import org.junit.jupiter.params.provider.ValueSource; |
| 34 | +import software.amazon.awssdk.auth.signer.S3SignerExecutionAttribute; |
| 35 | +import software.amazon.awssdk.authcrt.signer.internal.DefaultAwsCrtS3V4aSigner; |
| 36 | +import software.amazon.awssdk.core.async.AsyncRequestBody; |
| 37 | +import software.amazon.awssdk.core.async.AsyncResponseTransformer; |
| 38 | +import software.amazon.awssdk.core.checksums.Algorithm; |
| 39 | +import software.amazon.awssdk.core.checksums.ChecksumValidation; |
| 40 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 41 | +import software.amazon.awssdk.core.internal.async.FileAsyncRequestBody; |
| 42 | +import software.amazon.awssdk.services.s3.S3AsyncClient; |
| 43 | +import software.amazon.awssdk.services.s3.S3IntegrationTestBase; |
| 44 | +import software.amazon.awssdk.services.s3.model.ChecksumAlgorithm; |
| 45 | +import software.amazon.awssdk.services.s3.model.ChecksumMode; |
| 46 | +import software.amazon.awssdk.services.s3.model.GetObjectRequest; |
| 47 | +import software.amazon.awssdk.services.s3.model.NoSuchBucketException; |
| 48 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 49 | +import software.amazon.awssdk.services.s3.model.PutObjectResponse; |
| 50 | +import software.amazon.awssdk.services.s3.utils.CaptureChecksumValidationInterceptor; |
| 51 | +import software.amazon.awssdk.testutils.Waiter; |
| 52 | + |
| 53 | +public class AsyncHttpChecksumIntegrationTest extends S3IntegrationTestBase { |
| 54 | + |
| 55 | + protected static final String KEY = "some-key"; |
| 56 | + private static final String BUCKET = temporaryBucketName(AsyncHttpChecksumIntegrationTest.class); |
| 57 | + public static CaptureChecksumValidationInterceptor interceptor = new CaptureChecksumValidationInterceptor(); |
| 58 | + protected static S3AsyncClient s3HttpAsync; |
| 59 | + |
| 60 | + |
| 61 | + @BeforeAll |
| 62 | + public static void setUp() throws Exception { |
| 63 | + |
| 64 | + s3 = s3ClientBuilder().build(); |
| 65 | + |
| 66 | + s3Async = s3AsyncClientBuilder().overrideConfiguration(o -> o.addExecutionInterceptor(interceptor)).build(); |
| 67 | + |
| 68 | + // Http Client to generate Signed request |
| 69 | + s3HttpAsync = s3AsyncClientBuilder().overrideConfiguration(o -> o.addExecutionInterceptor(interceptor)) |
| 70 | + .endpointOverride(URI.create("http://s3." + DEFAULT_REGION + ".amazonaws.com")).build(); |
| 71 | + |
| 72 | + |
| 73 | + createBucket(BUCKET); |
| 74 | + |
| 75 | + Waiter.run(() -> s3.headBucket(r -> r.bucket(BUCKET))) |
| 76 | + .ignoringException(NoSuchBucketException.class) |
| 77 | + .orFail(); |
| 78 | + interceptor.reset(); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + @Test |
| 83 | + void asyncValidUnsignedTrailerChecksumCalculatedBySdkClient() { |
| 84 | + s3Async.putObject(PutObjectRequest.builder() |
| 85 | + .bucket(BUCKET) |
| 86 | + .key(KEY) |
| 87 | + .overrideConfiguration(o -> o.signer(DefaultAwsCrtS3V4aSigner.create())) |
| 88 | + |
| 89 | + .checksumAlgorithm(ChecksumAlgorithm.CRC32) |
| 90 | + .build(), AsyncRequestBody.fromString("Hello world")).join(); |
| 91 | + assertThat(interceptor.requestChecksumInTrailer()).isEqualTo("x-amz-checksum-crc32"); |
| 92 | + assertThat(interceptor.requestChecksumInHeader()).isNull(); |
| 93 | + String response = s3Async.getObject(GetObjectRequest.builder().bucket(BUCKET) |
| 94 | + .key(KEY).checksumMode(ChecksumMode.ENABLED) |
| 95 | + .build(), AsyncResponseTransformer.toBytes()).join().asUtf8String(); |
| 96 | + assertThat(interceptor.validationAlgorithm()).isEqualTo(Algorithm.CRC32); |
| 97 | + assertThat(interceptor.responseValidation()).isEqualTo(ChecksumValidation.VALIDATED); |
| 98 | + assertThat(response).isEqualTo("Hello world"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void asyncHttpsValidUnsignedTrailerChecksumCalculatedBySdkClient_withSmallRequestBody() throws InterruptedException { |
| 103 | + s3Async.putObject(PutObjectRequest.builder() |
| 104 | + .bucket(BUCKET) |
| 105 | + .key(KEY) |
| 106 | + .checksumAlgorithm(ChecksumAlgorithm.CRC32) |
| 107 | + .build(), AsyncRequestBody.fromString("Hello world")).join(); |
| 108 | + assertThat(interceptor.requestChecksumInTrailer()).isEqualTo("x-amz-checksum-crc32"); |
| 109 | + assertThat(interceptor.requestChecksumInHeader()).isNull(); |
| 110 | + |
| 111 | + String response = s3Async.getObject(GetObjectRequest.builder().bucket(BUCKET) |
| 112 | + .key(KEY).checksumMode(ChecksumMode.ENABLED) |
| 113 | + .build(), AsyncResponseTransformer.toBytes()).join().asUtf8String(); |
| 114 | + assertThat(interceptor.validationAlgorithm()).isEqualTo(Algorithm.CRC32); |
| 115 | + assertThat(interceptor.responseValidation()).isEqualTo(ChecksumValidation.VALIDATED); |
| 116 | + assertThat(response).isEqualTo("Hello world"); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + @ParameterizedTest |
| 121 | + @ValueSource(ints = {1, 12, 16, 17, 32, 33}) |
| 122 | + void asyncHttpsValidUnsignedTrailerChecksumCalculatedBySdkClient_withHugeRequestBody(int dataSize) throws InterruptedException { |
| 123 | + s3Async.putObject(PutObjectRequest.builder() |
| 124 | + .bucket(BUCKET) |
| 125 | + .key(KEY) |
| 126 | + .checksumAlgorithm(ChecksumAlgorithm.CRC32) |
| 127 | + .build(), AsyncRequestBody.fromString(createDataOfSizeInKb(dataSize, 'a'))).join(); |
| 128 | + assertThat(interceptor.requestChecksumInTrailer()).isEqualTo("x-amz-checksum-crc32"); |
| 129 | + assertThat(interceptor.requestChecksumInHeader()).isNull(); |
| 130 | + |
| 131 | + String response = s3Async.getObject(GetObjectRequest.builder().bucket(BUCKET) |
| 132 | + .key(KEY).checksumMode(ChecksumMode.ENABLED) |
| 133 | + .build(), AsyncResponseTransformer.toBytes()).join().asUtf8String(); |
| 134 | + assertThat(interceptor.validationAlgorithm()).isEqualTo(Algorithm.CRC32); |
| 135 | + assertThat(interceptor.responseValidation()).isEqualTo(ChecksumValidation.VALIDATED); |
| 136 | + assertThat(response).isEqualTo(createDataOfSizeInKb(dataSize, 'a')); |
| 137 | + } |
| 138 | + |
| 139 | + @ParameterizedTest |
| 140 | + @ValueSource(ints = {1*KB, 12*KB, 16*KB, 17*KB, 32*KB, 33*KB}) |
| 141 | + void asyncHttpsValidUnsignedTrailerChecksumCalculatedBySdkClient_withDifferentChunkSize_OfFileAsyncFileRequestBody |
| 142 | + (int chunkSize) throws IOException { |
| 143 | + File randomFileOfFixedLength = fixedLengthFileWithRandomCharacters(64); |
| 144 | + s3Async.putObject(PutObjectRequest.builder() |
| 145 | + .bucket(BUCKET) |
| 146 | + .key(KEY) |
| 147 | + .checksumAlgorithm(ChecksumAlgorithm.CRC32) |
| 148 | + .build(), FileAsyncRequestBody.builder().path(randomFileOfFixedLength.toPath()) |
| 149 | + .chunkSizeInBytes(chunkSize) |
| 150 | + .build()).join(); |
| 151 | + assertThat(interceptor.requestChecksumInTrailer()).isEqualTo("x-amz-checksum-crc32"); |
| 152 | + assertThat(interceptor.requestChecksumInHeader()).isNull(); |
| 153 | + |
| 154 | + String response = s3Async.getObject(GetObjectRequest.builder().bucket(BUCKET) |
| 155 | + .key(KEY).checksumMode(ChecksumMode.ENABLED) |
| 156 | + .build(), AsyncResponseTransformer.toBytes()).join().asUtf8String(); |
| 157 | + assertThat(interceptor.validationAlgorithm()).isEqualTo(Algorithm.CRC32); |
| 158 | + assertThat(interceptor.responseValidation()).isEqualTo(ChecksumValidation.VALIDATED); |
| 159 | + |
| 160 | + byte[] bytes = Files.readAllBytes(randomFileOfFixedLength.toPath()); |
| 161 | + assertThat(response).isEqualTo(new String(bytes)); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Test two async call made back to back with different sizes parameterized to test for different chunk sizes |
| 166 | + */ |
| 167 | + @ParameterizedTest |
| 168 | + @ValueSource(ints = {1 * KB, 12 * KB, 16 * KB, 17 * KB, 32 * KB, 33 * KB}) |
| 169 | + void asyncHttpsValidUnsignedTrailer_TwoRequests_withDifferentChunkSize_OfFileAsyncFileRequestBody(int chunkSize) |
| 170 | + throws IOException { |
| 171 | + |
| 172 | + File randomFileOfFixedLengthOne = fixedLengthFileWithRandomCharacters(64); |
| 173 | + File randomFileOfFixedLengthTwo = fixedLengthFileWithRandomCharacters(17); |
| 174 | + CompletableFuture<PutObjectResponse> putObjectFutureOne = |
| 175 | + s3Async.putObject(PutObjectRequest.builder() |
| 176 | + .bucket(BUCKET) |
| 177 | + .key(KEY) |
| 178 | + .checksumAlgorithm(ChecksumAlgorithm.CRC32) |
| 179 | + .build(), |
| 180 | + FileAsyncRequestBody.builder().path(randomFileOfFixedLengthOne.toPath()).chunkSizeInBytes(chunkSize).build()); |
| 181 | + |
| 182 | + String keyTwo = KEY + "_two"; |
| 183 | + CompletableFuture<PutObjectResponse> putObjectFutureTwo = |
| 184 | + s3Async.putObject(PutObjectRequest.builder() |
| 185 | + .bucket(BUCKET) |
| 186 | + .key(keyTwo) |
| 187 | + .checksumAlgorithm(ChecksumAlgorithm.CRC32) |
| 188 | + .build(), |
| 189 | + FileAsyncRequestBody.builder().path(randomFileOfFixedLengthTwo.toPath()).chunkSizeInBytes(chunkSize).build()); |
| 190 | + |
| 191 | + putObjectFutureOne.join(); |
| 192 | + putObjectFutureTwo.join(); |
| 193 | + assertThat(interceptor.requestChecksumInTrailer()).isEqualTo("x-amz-checksum-crc32"); |
| 194 | + assertThat(interceptor.requestChecksumInHeader()).isNull(); |
| 195 | + |
| 196 | + String response = s3Async.getObject(GetObjectRequest.builder().bucket(BUCKET) |
| 197 | + .key(KEY).checksumMode(ChecksumMode.ENABLED) |
| 198 | + .build(), AsyncResponseTransformer.toBytes()).join().asUtf8String(); |
| 199 | + |
| 200 | + String responseTwo = s3Async.getObject(GetObjectRequest.builder().bucket(BUCKET) |
| 201 | + .key(keyTwo).checksumMode(ChecksumMode.ENABLED) |
| 202 | + .build(), AsyncResponseTransformer.toBytes()).join().asUtf8String(); |
| 203 | + |
| 204 | + assertThat(interceptor.validationAlgorithm()).isEqualTo(Algorithm.CRC32); |
| 205 | + assertThat(interceptor.responseValidation()).isEqualTo(ChecksumValidation.VALIDATED); |
| 206 | + |
| 207 | + assertThat(response).isEqualTo(new String(Files.readAllBytes(randomFileOfFixedLengthOne.toPath()))); |
| 208 | + assertThat(responseTwo).isEqualTo(new String(Files.readAllBytes(randomFileOfFixedLengthTwo.toPath()))); |
| 209 | + |
| 210 | + } |
| 211 | + |
| 212 | + |
| 213 | + @Disabled("Http Async Signing is not supported for S3") |
| 214 | + void asyncValidSignedTrailerChecksumCalculatedBySdkClient() { |
| 215 | + ExecutionAttributes executionAttributes = ExecutionAttributes.builder() |
| 216 | + .put(S3SignerExecutionAttribute.ENABLE_PAYLOAD_SIGNING, |
| 217 | + true).build(); |
| 218 | + s3HttpAsync.putObject(PutObjectRequest.builder() |
| 219 | + .bucket(BUCKET) |
| 220 | + .overrideConfiguration(o -> o.executionAttributes(executionAttributes)) |
| 221 | + .key(KEY) |
| 222 | + .build(), AsyncRequestBody.fromString("Hello world")).join(); |
| 223 | + String response = s3HttpAsync.getObject(GetObjectRequest.builder().bucket(BUCKET) |
| 224 | + .key(KEY) |
| 225 | + .build(), AsyncResponseTransformer.toBytes()).join() |
| 226 | + .asUtf8String(); |
| 227 | + assertThat(response).isEqualTo("Hello world"); |
| 228 | + } |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | +} |
0 commit comments