Skip to content

Commit f01c6be

Browse files
committed
Fix the bug where AwsS34Singer tries to sign the payload when the request doesn't have payload. see #612
1 parent cffc36b commit f01c6be

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"category": "Aamazon S3",
3+
"type": "bugfix",
4+
"description": "Fix NPE for S3 GET request using http protocol. see [#612](https://github.com/aws/aws-sdk-java-v2/issues/612)"
5+
}

core/auth/src/main/java/software/amazon/awssdk/auth/signer/AwsS3V4Signer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ private boolean isChunkedEncodingEnabled(AwsS3V4SignerParams signerParams) {
220220
*/
221221
private boolean isPayloadSigningEnabled(SdkHttpFullRequest.Builder request, AwsS3V4SignerParams signerParams) {
222222
/**
223-
* If we aren't using https we should always sign the payload.
223+
* If we aren't using https we should always sign the payload unless there is no payload
224224
*/
225-
if (!request.protocol().equals("https")) {
225+
if (!request.protocol().equals("https") && request.content() != null) {
226226
return true;
227227
}
228228

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2010-2018 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 com.github.tomakehurst.wiremock.client.WireMock.aResponse;
19+
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
21+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
25+
import java.io.IOException;
26+
import java.net.URI;
27+
import org.junit.Before;
28+
import org.junit.Rule;
29+
import org.junit.Test;
30+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
31+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
32+
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
33+
import software.amazon.awssdk.regions.Region;
34+
35+
public class EndpointOverrideTest {
36+
37+
@Rule
38+
public WireMockRule mockServer = new WireMockRule(0);
39+
40+
private S3Client s3Client;
41+
42+
private S3AsyncClient s3AsyncClient;
43+
44+
@Before
45+
public void setup() {
46+
s3Client = S3Client.builder()
47+
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")))
48+
.region(Region.US_WEST_2).endpointOverride(URI.create(getEndpoint()))
49+
.build();
50+
51+
s3AsyncClient = S3AsyncClient.builder()
52+
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")))
53+
.region(Region.US_WEST_2)
54+
.endpointOverride(URI.create(getEndpoint()))
55+
.build();
56+
}
57+
58+
private String getEndpoint() {
59+
return "http://localhost:" + mockServer.port();
60+
}
61+
62+
//https://github.com/aws/aws-sdk-java-v2/issues/437
63+
@Test
64+
public void getObjectAsync_shouldNotThrowNPE() throws IOException {
65+
stubFor(get(anyUrl())
66+
.willReturn(aResponse()
67+
.withStatus(200)
68+
.withBody("<?xml version=\"1.0\"?><GetObjectResult xmlns=\"http://s3"
69+
+ ".amazonaws.com/doc/2006-03-01\"></GetObjectResult>")));
70+
assertThat(s3AsyncClient.getObject(b -> b.bucket("test").key("test").build(),
71+
AsyncResponseTransformer.toBytes()).join()).isNotNull();
72+
}
73+
74+
@Test
75+
public void getObject_shouldNotThrowNPE() {
76+
stubFor(get(anyUrl())
77+
.willReturn(aResponse()
78+
.withStatus(200).withBody("<?xml version=\"1.0\"?><GetObjectResult xmlns=\"http://s3"
79+
+ ".amazonaws.com/doc/2006-03-01\"></GetObjectResult>")));
80+
assertThat(s3Client.listObjectVersions(b -> b.bucket("test"))).isNotNull();
81+
}
82+
}

0 commit comments

Comments
 (0)