Skip to content

Commit d301273

Browse files
committed
Updates
1 parent 488b723 commit d301273

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/DefaultAwsV4HttpSigner.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public CompletableFuture<AsyncSignedRequest> signAsync(AsyncSignRequest<? extend
7272
Checksummer checksummer = asyncChecksummer(request, checksumStore(request));
7373
V4Properties v4Properties = v4Properties(request);
7474
V4RequestSigner v4RequestSigner = v4RequestSigner(request, v4Properties);
75-
V4PayloadSigner payloadSigner = v4PayloadSigner(request, v4Properties);
75+
V4PayloadSigner payloadSigner = v4PayloadAsyncSigner(request, v4Properties);
7676

7777
return doSignAsync(request, checksummer, v4RequestSigner, payloadSigner);
7878
}
@@ -129,12 +129,10 @@ private static V4RequestSigner v4RequestSigner(
129129
return requestSigner.apply(v4Properties);
130130
}
131131

132+
// TODO: remove this once we consolidate the behavior for plaintext HTTP signing for sync and async
132133
private static Checksummer asyncChecksummer(BaseSignRequest<?, ? extends AwsCredentialsIdentity> request,
133134
PayloadChecksumStore checksumStore) {
134-
boolean isHttp = !"https".equals(request.request().protocol());
135-
boolean isPayloadSigning = isPayloadSigning(request);
136-
boolean isChunkEncoding = request.requireProperty(CHUNK_ENCODING_ENABLED, false);
137-
boolean shouldTreatAsUnsigned = isHttp && isPayloadSigning && isChunkEncoding;
135+
boolean shouldTreatAsUnsigned = asyncShouldTreatAsUnsigned(request);
138136

139137
// set the override to false if it should be treated as unsigned, otherwise, null should be passed so that the normal
140138
// check for payload signing is done.
@@ -143,6 +141,15 @@ private static Checksummer asyncChecksummer(BaseSignRequest<?, ? extends AwsCred
143141
return checksummer(request, overridePayloadSigning, checksumStore);
144142
}
145143

144+
// TODO: remove this once we consolidate the behavior for plaintext HTTP signing for sync and async
145+
private static boolean asyncShouldTreatAsUnsigned(BaseSignRequest<?, ? extends AwsCredentialsIdentity> request) {
146+
boolean isHttp = !"https".equals(request.request().protocol());
147+
boolean isPayloadSigning = isPayloadSigning(request);
148+
boolean isChunkEncoding = request.requireProperty(CHUNK_ENCODING_ENABLED, false);
149+
150+
return isHttp && isPayloadSigning && isChunkEncoding;
151+
}
152+
146153
private static V4PayloadSigner v4PayloadSigner(
147154
BaseSignRequest<?, ? extends AwsCredentialsIdentity> request, V4Properties properties) {
148155

@@ -175,6 +182,40 @@ private static V4PayloadSigner v4PayloadSigner(
175182
return V4PayloadSigner.create();
176183
}
177184

185+
// TODO: remove this once we consolidate the behavior for plaintext HTTP signing for sync and async
186+
private static V4PayloadSigner v4PayloadAsyncSigner(
187+
AsyncSignRequest<? extends AwsCredentialsIdentity> request,
188+
V4Properties properties) {
189+
190+
boolean isPayloadSigning = !asyncShouldTreatAsUnsigned(request);
191+
boolean isEventStreaming = isEventStreaming(request.request());
192+
boolean isChunkEncoding = request.requireProperty(CHUNK_ENCODING_ENABLED, false);
193+
boolean isTrailing = request.request().firstMatchingHeader(X_AMZ_TRAILER).isPresent();
194+
boolean isFlexible = request.hasProperty(CHECKSUM_ALGORITHM) && !hasChecksumHeader(request);
195+
196+
if (isEventStreaming) {
197+
if (isPayloadSigning) {
198+
return getEventStreamV4PayloadSigner(
199+
properties.getCredentials(),
200+
properties.getCredentialScope(),
201+
properties.getSigningClock()
202+
);
203+
}
204+
throw new UnsupportedOperationException("Unsigned payload is not supported with event-streaming.");
205+
}
206+
207+
if (useChunkEncoding(isPayloadSigning, isChunkEncoding, isTrailing || isFlexible)) {
208+
return AwsChunkedV4PayloadSigner.builder()
209+
.credentialScope(properties.getCredentialScope())
210+
.chunkSize(DEFAULT_CHUNK_SIZE_IN_BYTES)
211+
.checksumStore(checksumStore(request))
212+
.checksumAlgorithm(request.property(CHECKSUM_ALGORITHM))
213+
.build();
214+
}
215+
216+
return V4PayloadSigner.create();
217+
}
218+
178219
private static SignedRequest doSign(SignRequest<? extends AwsCredentialsIdentity> request,
179220
Checksummer checksummer,
180221
V4RequestSigner requestSigner,

services/s3/src/test/java/software/amazon/awssdk/services/s3/functionaltests/S3ExpressTest.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void putObject(ClientType clientType, Protocol protocol,
115115
createClientAndCallPutObject(clientType, protocol, s3ExpressSessionAuth, checksumAlgorithm, wm);
116116

117117
verifyPutObject(s3ExpressSessionAuth);
118-
verifyPutObjectHeaders(protocol, checksumAlgorithm);
118+
verifyPutObjectHeaders(clientType, protocol, checksumAlgorithm);
119119
}
120120

121121
@ParameterizedTest
@@ -126,7 +126,7 @@ public void uploadPart(ClientType clientType, Protocol protocol,
126126
createClientAndCallUploadPart(clientType, protocol, s3ExpressSessionAuth, checksumAlgorithm, wm);
127127

128128
verifyUploadPart(s3ExpressSessionAuth);
129-
verifyUploadPartHeaders(protocol);
129+
verifyUploadPartHeaders(clientType, protocol);
130130
}
131131

132132
@ParameterizedTest
@@ -268,12 +268,10 @@ private static void verifySessionHeaders() {
268268
.withHeader("x-amz-content-sha256", equalTo("UNSIGNED-PAYLOAD")));
269269
}
270270

271-
void verifyPutObjectHeaders(Protocol protocol, ChecksumAlgorithm checksumAlgorithm) {
272-
String streamingSha256;
273-
if (protocol == Protocol.HTTP) {
271+
void verifyPutObjectHeaders(ClientType clientType, Protocol protocol, ChecksumAlgorithm checksumAlgorithm) {
272+
String streamingSha256 = "STREAMING-UNSIGNED-PAYLOAD-TRAILER";
273+
if (protocol == Protocol.HTTP && clientType == ClientType.SYNC) {
274274
streamingSha256 = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER";
275-
} else {
276-
streamingSha256 = "STREAMING-UNSIGNED-PAYLOAD-TRAILER";
277275
}
278276
ChecksumAlgorithm expectedChecksumAlgorithm = checksumAlgorithm == ChecksumAlgorithm.UNKNOWN_TO_SDK_VERSION ?
279277
ChecksumAlgorithm.CRC32 : checksumAlgorithm;
@@ -292,17 +290,15 @@ void verifyPutObjectHeaders(Protocol protocol, ChecksumAlgorithm checksumAlgorit
292290
assertThat(headers.get("x-amz-content-sha256").get(0)).isEqualToIgnoringCase(streamingSha256);
293291
}
294292

295-
void verifyUploadPartHeaders(Protocol protocol) {
293+
void verifyUploadPartHeaders(ClientType clientType, Protocol protocol) {
296294
Map<String, List<String>> headers = CAPTURING_INTERCEPTOR.headers;
297295
assertThat(headers.get("Content-Length")).isNotNull();
298296
assertThat(headers.get("x-amz-content-sha256")).isNotNull();
299297

300298
assertThat(headers.get("x-amz-decoded-content-length")).isNotNull();
301-
String streamingSha256;
302-
if (protocol == Protocol.HTTP) {
299+
String streamingSha256 = "STREAMING-UNSIGNED-PAYLOAD-TRAILER";
300+
if (protocol == Protocol.HTTP && clientType == ClientType.SYNC) {
303301
streamingSha256 = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER";
304-
} else {
305-
streamingSha256 = "STREAMING-UNSIGNED-PAYLOAD-TRAILER";
306302
}
307303
assertThat(headers.get("x-amz-content-sha256").get(0)).isEqualToIgnoringCase(streamingSha256);
308304
}

0 commit comments

Comments
 (0)