Skip to content

Commit 1f989a7

Browse files
committed
change integration test to wiremock test
1 parent 06a9eeb commit 1f989a7

4 files changed

Lines changed: 144 additions & 245 deletions

File tree

services/s3/src/it/java/software/amazon/awssdk/services/s3/crt/S3CrtRequestLevelCredentialsIntegrationTest.java

Lines changed: 0 additions & 138 deletions
This file was deleted.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
}

services/s3/src/test/java/software/amazon/awssdk/services/s3/internal/crt/DefaultS3CrtAsyncClientTest.java

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import org.junit.jupiter.api.Test;
2323
import org.junit.jupiter.params.ParameterizedTest;
2424
import org.junit.jupiter.params.provider.ValueSource;
25-
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
2625
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
27-
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
2826
import software.amazon.awssdk.auth.signer.AwsS3V4Signer;
2927
import software.amazon.awssdk.core.async.AsyncRequestBody;
3028
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
@@ -33,7 +31,6 @@
3331
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
3432
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
3533
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
36-
import software.amazon.awssdk.http.SdkHttpExecutionAttributes;
3734
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
3835
import software.amazon.awssdk.identity.spi.IdentityProvider;
3936
import software.amazon.awssdk.services.s3.DelegatingS3AsyncClient;
@@ -165,75 +162,4 @@ void build_withAdvancedOptions() {
165162
}
166163
}
167164

168-
@Test
169-
void afterMarshalling_withRequestLevelCredentials_shouldAttachCrtCredentialsAdapterToHttpAttributes() {
170-
AtomicReference<SdkHttpExecutionAttributes> capturedAttributes = new AtomicReference<>();
171-
172-
ExecutionInterceptor attributeCaptor = new ExecutionInterceptor() {
173-
@Override
174-
public void afterMarshalling(Context.AfterMarshalling context, ExecutionAttributes executionAttributes) {
175-
capturedAttributes.set(
176-
executionAttributes.getAttribute(SdkInternalExecutionAttribute.SDK_HTTP_EXECUTION_ATTRIBUTES));
177-
throw new RuntimeException("STOP");
178-
}
179-
};
180-
181-
DefaultS3CrtAsyncClient.DefaultS3CrtClientBuilder builder =
182-
(DefaultS3CrtAsyncClient.DefaultS3CrtClientBuilder) S3CrtAsyncClient.builder();
183-
builder.addExecutionInterceptor(attributeCaptor);
184-
185-
try (S3AsyncClient s3AsyncClient = builder.build()) {
186-
IdentityProvider<? extends AwsCredentialsIdentity> requestCredentials =
187-
StaticCredentialsProvider.create(AwsBasicCredentials.create("requestAkid", "requestSkid"));
188-
189-
assertThatThrownBy(() -> s3AsyncClient.getObject(
190-
b -> b.bucket("bucket").key("key")
191-
.overrideConfiguration(o -> o.credentialsProvider(requestCredentials)),
192-
AsyncResponseTransformer.toBytes()).join()).hasMessageContaining("STOP");
193-
194-
SdkHttpExecutionAttributes httpAttributes = capturedAttributes.get();
195-
assertThat(httpAttributes).isNotNull();
196-
CrtCredentialsProviderAdapter adapter =
197-
httpAttributes.getAttribute(S3InternalSdkHttpExecutionAttribute.CRT_CREDENTIALS_PROVIDER_ADAPTER);
198-
assertThat(adapter).isNotNull();
199-
200-
software.amazon.awssdk.crt.auth.credentials.Credentials crtCreds =
201-
adapter.crtCredentials().getCredentials().join();
202-
assertThat(crtCreds.getAccessKeyId())
203-
.isEqualTo("requestAkid".getBytes(java.nio.charset.StandardCharsets.UTF_8));
204-
assertThat(crtCreds.getSecretAccessKey())
205-
.isEqualTo("requestSkid".getBytes(java.nio.charset.StandardCharsets.UTF_8));
206-
}
207-
}
208-
209-
@Test
210-
void afterMarshalling_withoutRequestLevelCredentials_shouldNotAttachCrtCredentialsAdapter() {
211-
AtomicReference<SdkHttpExecutionAttributes> capturedAttributes = new AtomicReference<>();
212-
213-
ExecutionInterceptor attributeCaptor = new ExecutionInterceptor() {
214-
@Override
215-
public void afterMarshalling(Context.AfterMarshalling context, ExecutionAttributes executionAttributes) {
216-
capturedAttributes.set(
217-
executionAttributes.getAttribute(SdkInternalExecutionAttribute.SDK_HTTP_EXECUTION_ATTRIBUTES));
218-
throw new RuntimeException("STOP");
219-
}
220-
};
221-
222-
DefaultS3CrtAsyncClient.DefaultS3CrtClientBuilder builder =
223-
(DefaultS3CrtAsyncClient.DefaultS3CrtClientBuilder) S3CrtAsyncClient.builder();
224-
builder.addExecutionInterceptor(attributeCaptor);
225-
226-
try (S3AsyncClient s3AsyncClient = builder.build()) {
227-
assertThatThrownBy(() -> s3AsyncClient.getObject(
228-
b -> b.bucket("bucket").key("key"),
229-
AsyncResponseTransformer.toBytes()).join()).hasMessageContaining("STOP");
230-
231-
SdkHttpExecutionAttributes httpAttributes = capturedAttributes.get();
232-
assertThat(httpAttributes).isNotNull();
233-
CrtCredentialsProviderAdapter adapter =
234-
httpAttributes.getAttribute(S3InternalSdkHttpExecutionAttribute.CRT_CREDENTIALS_PROVIDER_ADAPTER);
235-
assertThat(adapter).isNull();
236-
}
237-
}
238-
239165
}

0 commit comments

Comments
 (0)