|
| 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.crt; |
| 17 | + |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.containing; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; |
| 22 | +import static com.github.tomakehurst.wiremock.client.WireMock.head; |
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.headRequestedFor; |
| 24 | +import static com.github.tomakehurst.wiremock.client.WireMock.put; |
| 25 | +import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor; |
| 26 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 27 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 28 | +import static com.github.tomakehurst.wiremock.client.WireMock.verify; |
| 29 | + |
| 30 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 31 | +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; |
| 32 | +import com.github.tomakehurst.wiremock.junit5.WireMockTest; |
| 33 | +import java.net.URI; |
| 34 | +import java.nio.charset.StandardCharsets; |
| 35 | +import org.junit.jupiter.api.AfterEach; |
| 36 | +import org.junit.jupiter.api.BeforeAll; |
| 37 | +import org.junit.jupiter.api.BeforeEach; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.junit.jupiter.api.Timeout; |
| 40 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 41 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 42 | +import software.amazon.awssdk.core.async.AsyncRequestBody; |
| 43 | +import software.amazon.awssdk.core.async.AsyncResponseTransformer; |
| 44 | +import software.amazon.awssdk.crt.Log; |
| 45 | +import software.amazon.awssdk.regions.Region; |
| 46 | +import software.amazon.awssdk.services.s3.S3AsyncClient; |
| 47 | + |
| 48 | +/** |
| 49 | + * WireMock tests verifying that request-level credential overrides are used for signing |
| 50 | + * with the S3 CRT client. Verifies the Authorization header contains the expected access key. |
| 51 | + */ |
| 52 | +@WireMockTest |
| 53 | +@Timeout(10) |
| 54 | +public class S3CrtRequestLevelCredentialsWireMockTest { |
| 55 | + |
| 56 | + private static final String BUCKET = "my-bucket"; |
| 57 | + private static final String KEY = "my-key"; |
| 58 | + private static final String PATH = String.format("/%s/%s", BUCKET, KEY); |
| 59 | + private static final byte[] CONTENT = "hello".getBytes(StandardCharsets.UTF_8); |
| 60 | + |
| 61 | + private static final StaticCredentialsProvider CLIENT_CREDENTIALS = |
| 62 | + StaticCredentialsProvider.create(AwsBasicCredentials.create("clientAccessKey", "clientSecretKey")); |
| 63 | + |
| 64 | + private static final StaticCredentialsProvider REQUEST_CREDENTIALS = |
| 65 | + StaticCredentialsProvider.create(AwsBasicCredentials.create("requestAccessKey", "requestSecretKey")); |
| 66 | + |
| 67 | + private S3AsyncClient s3; |
| 68 | + |
| 69 | + @BeforeAll |
| 70 | + public static void setUpBeforeAll() { |
| 71 | + System.setProperty("aws.crt.debugnative", "true"); |
| 72 | + Log.initLoggingToStdout(Log.LogLevel.Warn); |
| 73 | + } |
| 74 | + |
| 75 | + @BeforeEach |
| 76 | + public void setup(WireMockRuntimeInfo wiremock) { |
| 77 | + stubFor(head(urlPathEqualTo(PATH)) |
| 78 | + .willReturn(WireMock.aResponse().withStatus(200) |
| 79 | + .withHeader("ETag", "etag") |
| 80 | + .withHeader("Content-Length", |
| 81 | + Integer.toString(CONTENT.length)))); |
| 82 | + stubFor(get(urlPathEqualTo(PATH)) |
| 83 | + .willReturn(WireMock.aResponse().withStatus(200) |
| 84 | + .withHeader("Content-Type", "text/plain") |
| 85 | + .withBody(CONTENT))); |
| 86 | + stubFor(put(urlPathEqualTo(PATH)) |
| 87 | + .willReturn(WireMock.aResponse().withStatus(200) |
| 88 | + .withHeader("ETag", "etag"))); |
| 89 | + |
| 90 | + s3 = S3AsyncClient.crtBuilder() |
| 91 | + .endpointOverride(URI.create("http://localhost:" + wiremock.getHttpPort())) |
| 92 | + .credentialsProvider(CLIENT_CREDENTIALS) |
| 93 | + .forcePathStyle(true) |
| 94 | + .region(Region.US_EAST_1) |
| 95 | + .build(); |
| 96 | + } |
| 97 | + |
| 98 | + @AfterEach |
| 99 | + public void tearDown() { |
| 100 | + s3.close(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void getObject_withRequestLevelCredentials_shouldSignWithOverrideCredentials() { |
| 105 | + s3.getObject( |
| 106 | + b -> b.bucket(BUCKET).key(KEY) |
| 107 | + .overrideConfiguration(o -> o.credentialsProvider(REQUEST_CREDENTIALS)), |
| 108 | + AsyncResponseTransformer.toBytes()).join(); |
| 109 | + |
| 110 | + verify(getRequestedFor(urlPathEqualTo(PATH)) |
| 111 | + .withHeader("Authorization", containing("Credential=requestAccessKey/"))); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void getObject_withoutRequestLevelCredentials_shouldSignWithClientCredentials() { |
| 116 | + s3.getObject( |
| 117 | + b -> b.bucket(BUCKET).key(KEY), |
| 118 | + AsyncResponseTransformer.toBytes()).join(); |
| 119 | + |
| 120 | + verify(getRequestedFor(urlPathEqualTo(PATH)) |
| 121 | + .withHeader("Authorization", containing("Credential=clientAccessKey/"))); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void putObject_withRequestLevelCredentials_shouldSignWithOverrideCredentials() { |
| 126 | + s3.putObject( |
| 127 | + b -> b.bucket(BUCKET).key(KEY) |
| 128 | + .overrideConfiguration(o -> o.credentialsProvider(REQUEST_CREDENTIALS)), |
| 129 | + AsyncRequestBody.fromString("hello")).join(); |
| 130 | + |
| 131 | + verify(putRequestedFor(urlPathEqualTo(PATH)) |
| 132 | + .withHeader("Authorization", containing("Credential=requestAccessKey/"))); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void putObject_withoutRequestLevelCredentials_shouldSignWithClientCredentials() { |
| 137 | + s3.putObject( |
| 138 | + b -> b.bucket(BUCKET).key(KEY), |
| 139 | + AsyncRequestBody.fromString("hello")).join(); |
| 140 | + |
| 141 | + verify(putRequestedFor(urlPathEqualTo(PATH)) |
| 142 | + .withHeader("Authorization", containing("Credential=clientAccessKey/"))); |
| 143 | + } |
| 144 | +} |
0 commit comments