Skip to content

Commit 538f998

Browse files
authored
Fixed an issue that resulted in a NullPointerException when an invalid or global region was used on an S3 client. (#2889)
Fixes #2885
1 parent 57aa10b commit 538f998

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"category": "Amazon S3",
3+
"contributor": "",
4+
"type": "bugfix",
5+
"description": "Fixed an issue that resulted in a NullPointerException when an invalid or global region was used on an S3 client. Fixes [#2885](https://github.com/aws/aws-sdk-java-v2/issues/2885)."
6+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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;
17+
18+
import org.junit.Test;
19+
import software.amazon.awssdk.regions.Region;
20+
21+
public class ListBucketsIntegrationTest extends S3IntegrationTestBase {
22+
@Test
23+
public void listBuckets_InGlobal_DoesNotThrowException() {
24+
try (S3Client s3 = s3ClientBuilder().region(Region.AWS_GLOBAL).build()) {
25+
s3.listBuckets();
26+
}
27+
}
28+
}

services/s3/src/main/java/software/amazon/awssdk/services/s3/internal/endpoints/S3BucketEndpointResolver.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.net.URI;
2727
import software.amazon.awssdk.annotations.SdkInternalApi;
2828
import software.amazon.awssdk.http.SdkHttpRequest;
29+
import software.amazon.awssdk.regions.PartitionMetadata;
2930
import software.amazon.awssdk.regions.RegionMetadata;
3031
import software.amazon.awssdk.services.s3.S3Configuration;
3132
import software.amazon.awssdk.services.s3.internal.BucketUtils;
@@ -69,7 +70,10 @@ public ConfiguredS3SdkHttpRequest applyEndpointConfiguration(S3EndpointResolverC
6970
private static URI resolveEndpoint(S3EndpointResolverContext context) {
7071
SdkHttpRequest request = context.request();
7172
String protocol = request.protocol();
72-
String dnsSuffixWithoutTagConsideration = RegionMetadata.of(context.region()).domain();
73+
RegionMetadata regionMetadata = RegionMetadata.of(context.region());
74+
String dnsSuffixWithoutTagConsideration = regionMetadata != null ?
75+
regionMetadata.domain() :
76+
PartitionMetadata.of(context.region()).dnsSuffix();
7377
S3Configuration serviceConfiguration = context.serviceConfiguration();
7478

7579
boolean useAccelerate = isAccelerateEnabled(serviceConfiguration) && isAccelerateSupported(context.originalRequest());

services/s3/src/test/java/software/amazon/awssdk/services/s3/InvalidRegionTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
2323
import software.amazon.awssdk.core.exception.SdkClientException;
2424
import software.amazon.awssdk.regions.Region;
25+
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
2526
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
2627

2728
public class InvalidRegionTest {
@@ -47,6 +48,17 @@ public void invalidS3UtilitiesRegionAtRequestGivesHelpfulMessage() {
4748
.hasMessageContaining("us-west-2");
4849
}
4950

51+
@Test
52+
public void nonExistentRegionGivesHelpfulMessage() {
53+
S3Client s3Client = S3Client.builder()
54+
.region(Region.of("does-not-exist"))
55+
.credentialsProvider(AnonymousCredentialsProvider.create())
56+
.build();
57+
assertThatThrownBy(() -> s3Client.headBucket(HeadBucketRequest.builder().bucket("myBucket").build()))
58+
.isInstanceOf(SdkClientException.class)
59+
.hasMessageContaining("UnknownHostException");
60+
}
61+
5062
@Test
5163
public void invalidS3ArnRegionAtRequestGivesHelpfulMessage() {
5264
S3Client client = S3Client.builder()

0 commit comments

Comments
 (0)