Skip to content

Commit 2307338

Browse files
authored
Fix a bug where tags were being lost when creating buckets in us-east-1 (#6492)
* Fix a bug where tags were being lost when creating buckets in us-east-1 * changelog
1 parent 85f098a commit 2307338

File tree

3 files changed

+94
-9
lines changed

3 files changed

+94
-9
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "Amazon S3",
4+
"contributor": "",
5+
"description": "Fix a bug where tags were being lost when creating buckets in us-east-1"
6+
}

services/s3/src/main/java/software/amazon/awssdk/services/s3/internal/handlers/CreateBucketInterceptor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
2424
import software.amazon.awssdk.regions.Region;
2525
import software.amazon.awssdk.services.s3.internal.BucketUtils;
26+
import software.amazon.awssdk.services.s3.model.BucketLocationConstraint;
2627
import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration;
2728
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
2829

@@ -63,7 +64,12 @@ private SdkRequest addLocationConstraintToRequest(CreateBucketRequest request,
6364
private CreateBucketConfiguration setLocationConstraint(Region region, CreateBucketConfiguration configuration) {
6465
if (region.equals(Region.US_EAST_1)) {
6566
// us-east-1 requires no location restraint. See http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUT.html
66-
return null;
67+
// But if tags exists, must keep them
68+
if (configuration.hasTags()) {
69+
return configuration.copy(c -> c.locationConstraint((BucketLocationConstraint) null));
70+
} else {
71+
return null;
72+
}
6773
}
6874
return configuration.copy(c -> c.locationConstraint(region.id()).build());
6975
}

services/s3/src/test/java/software/amazon/awssdk/services/s3/internal/handlers/CreateBucketInterceptorTest.java

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616

1717
import static org.assertj.core.api.Assertions.assertThat;
1818

19+
import java.util.Arrays;
20+
import java.util.List;
21+
import java.util.stream.Stream;
1922
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.Arguments;
25+
import org.junit.jupiter.params.provider.MethodSource;
2026
import software.amazon.awssdk.awscore.AwsExecutionAttribute;
2127
import software.amazon.awssdk.core.SdkRequest;
2228
import software.amazon.awssdk.core.interceptor.Context;
@@ -26,11 +32,12 @@
2632
import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration;
2733
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
2834
import software.amazon.awssdk.services.s3.model.LocationInfo;
35+
import software.amazon.awssdk.services.s3.model.Tag;
2936

3037
public class CreateBucketInterceptorTest {
3138

3239
@Test
33-
public void modifyRequest_DoesNotOverrideExistingLocationConstraint() {
40+
void modifyRequest_DoesNotOverrideExistingLocationConstraint() {
3441
CreateBucketRequest request = CreateBucketRequest.builder()
3542
.bucket("test-bucket")
3643
.createBucketConfiguration(CreateBucketConfiguration.builder()
@@ -51,7 +58,7 @@ public void modifyRequest_DoesNotOverrideExistingLocationConstraint() {
5158
}
5259

5360
@Test
54-
public void modifyRequest_UpdatesLocationConstraint_When_NullCreateBucketConfiguration() {
61+
void modifyRequest_UpdatesLocationConstraint_When_NullCreateBucketConfiguration() {
5562
CreateBucketRequest request = CreateBucketRequest.builder()
5663
.bucket("test-bucket")
5764
.build();
@@ -68,7 +75,7 @@ public void modifyRequest_UpdatesLocationConstraint_When_NullCreateBucketConfigu
6875
}
6976

7077
@Test
71-
public void modifyRequest_UpdatesLocationConstraint_When_NullLocationConstraint() {
78+
void modifyRequest_UpdatesLocationConstraint_When_NullLocationConstraint() {
7279
CreateBucketRequest request = CreateBucketRequest.builder()
7380
.bucket("test-bucket")
7481
.createBucketConfiguration(CreateBucketConfiguration.builder()
@@ -87,7 +94,7 @@ public void modifyRequest_UpdatesLocationConstraint_When_NullLocationConstraint(
8794
}
8895

8996
@Test
90-
public void modifyRequest_DoesNotSetLocationConstraint_When_LocationPresent() {
97+
void modifyRequest_DoesNotSetLocationConstraint_When_LocationPresent() {
9198
LocationInfo location = LocationInfo.builder().name("name").type("type").build();
9299
CreateBucketConfiguration configuration = CreateBucketConfiguration.builder().location(location).build();
93100

@@ -112,7 +119,7 @@ public void modifyRequest_DoesNotSetLocationConstraint_When_LocationPresent() {
112119
* For us-east-1 there must not be a location constraint (or containing CreateBucketConfiguration) sent.
113120
*/
114121
@Test
115-
public void modifyRequest_UsEast1_UsesNullBucketConfiguration() {
122+
void modifyRequest_UsEast1_UsesNullBucketConfiguration() {
116123
CreateBucketRequest request = CreateBucketRequest.builder()
117124
.bucket("test-bucket")
118125
.createBucketConfiguration(CreateBucketConfiguration.builder()
@@ -122,14 +129,14 @@ public void modifyRequest_UsEast1_UsesNullBucketConfiguration() {
122129
Context.ModifyRequest context = () -> request;
123130

124131
ExecutionAttributes attributes = new ExecutionAttributes()
125-
.putAttribute(AwsExecutionAttribute.AWS_REGION, Region.US_EAST_1);
132+
.putAttribute(AwsExecutionAttribute.AWS_REGION, Region.US_EAST_1);
126133

127134
SdkRequest modifiedRequest = new CreateBucketInterceptor().modifyRequest(context, attributes);
128135
assertThat(((CreateBucketRequest) modifiedRequest).createBucketConfiguration()).isNull();
129136
}
130137

131138
@Test
132-
public void modifyRequest__When_NullLocationConstraint() {
139+
void modifyRequest__When_NullLocationConstraint() {
133140
CreateBucketRequest request = CreateBucketRequest.builder()
134141
.bucket("test-bucket")
135142
.createBucketConfiguration(CreateBucketConfiguration.builder()
@@ -142,8 +149,74 @@ public void modifyRequest__When_NullLocationConstraint() {
142149
.putAttribute(AwsExecutionAttribute.AWS_REGION, Region.US_WEST_2);
143150

144151
SdkRequest modifiedRequest = new CreateBucketInterceptor().modifyRequest(context, attributes);
145-
String locationConstraint = ((CreateBucketRequest) modifiedRequest).createBucketConfiguration().locationConstraintAsString();
152+
String locationConstraint =
153+
((CreateBucketRequest) modifiedRequest).createBucketConfiguration().locationConstraintAsString();
146154

147155
assertThat(locationConstraint).isEqualToIgnoringCase("us-west-2");
148156
}
157+
158+
/*
159+
Tags on create test cases:
160+
Region has tags locationConstraint Expected CreateBucketConfiguration body
161+
----------------------------------------------------------------
162+
us-east-1 - tags - no locationConstraint: Body with tags only
163+
us-east-1 - tags - locationConstraint: Body with both (locationConstraint=EU)
164+
us-east-1 - no tags - no locationConstraint: Empty body (null)
165+
us-east-1 - no tags - locationConstraint: Body with locationConstraint=EU
166+
167+
us-west-2 - tags - no locationConstraint: Body with tags and locationConstraint=us-west-2
168+
us-west-2 - tags - locationConstraint: Body with both (locationConstraint=EU)
169+
us-west-2 - no tags - no locationConstraint: Body with locationConstraint=us-west-2
170+
us-west-2 - no tags - locationConstraint: Body with locationConstraint=EU
171+
*/
172+
public static Stream<Arguments> createWithTagsTestParam() {
173+
List<Tag> tags = Arrays.asList(
174+
Tag.builder().key("foo-key").value("foo-value").build(),
175+
Tag.builder().key("bar-key").value("bar-value").build()
176+
);
177+
return Stream.of(
178+
Arguments.of(Region.US_EAST_1, tags, null,
179+
CreateBucketConfiguration.builder().tags(tags).build()),
180+
Arguments.of(Region.US_EAST_1, tags, BucketLocationConstraint.EU,
181+
CreateBucketConfiguration.builder().tags(tags).locationConstraint(BucketLocationConstraint.EU).build()),
182+
Arguments.of(Region.US_EAST_1, null, null, null),
183+
Arguments.of(Region.US_EAST_1, null, BucketLocationConstraint.EU,
184+
CreateBucketConfiguration.builder().locationConstraint(BucketLocationConstraint.EU).build()),
185+
186+
Arguments.of(Region.US_WEST_2, tags, null,
187+
CreateBucketConfiguration.builder().tags(tags).locationConstraint(BucketLocationConstraint.US_WEST_2).build()),
188+
Arguments.of(Region.US_WEST_2, tags, BucketLocationConstraint.EU,
189+
CreateBucketConfiguration.builder().tags(tags).locationConstraint(BucketLocationConstraint.EU).build()),
190+
Arguments.of(Region.US_WEST_2, null, null,
191+
CreateBucketConfiguration.builder().locationConstraint(BucketLocationConstraint.US_WEST_2).build()),
192+
Arguments.of(Region.US_WEST_2, null, BucketLocationConstraint.EU,
193+
CreateBucketConfiguration.builder().locationConstraint(BucketLocationConstraint.EU).build())
194+
);
195+
}
196+
197+
@ParameterizedTest
198+
@MethodSource("createWithTagsTestParam")
199+
void modifyRequest__CreateWithTags(Region region,
200+
List<Tag> tags,
201+
BucketLocationConstraint locationConstraint,
202+
CreateBucketConfiguration expected) {
203+
CreateBucketRequest request = CreateBucketRequest.builder()
204+
.bucket("test-bucket")
205+
.createBucketConfiguration(
206+
CreateBucketConfiguration.builder()
207+
.tags(tags)
208+
.locationConstraint(locationConstraint)
209+
.build())
210+
.build();
211+
212+
Context.ModifyRequest context = () -> request;
213+
ExecutionAttributes attributes = new ExecutionAttributes().putAttribute(AwsExecutionAttribute.AWS_REGION, region);
214+
215+
CreateBucketRequest modifiedRequest =
216+
(CreateBucketRequest) new CreateBucketInterceptor().modifyRequest(context, attributes);
217+
218+
assertThat(modifiedRequest.createBucketConfiguration()).isEqualTo(expected);
219+
220+
}
221+
149222
}

0 commit comments

Comments
 (0)