Skip to content

Commit

Permalink
🐛 fix: s3 bucket in us-east-1 should not set locations
Browse files Browse the repository at this point in the history
`us-east-1` is invalid as a Location Contraint when creating a bucket.

"Buckets in Region us-east-1 have a LocationConstraint of null."[1]

[1] https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLocation.html#API_GetBucketLocation_ResponseSyntax
  • Loading branch information
mtulio committed Mar 1, 2024
1 parent 25a0086 commit 3442f78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/cloud/services/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import (
"sigs.k8s.io/cluster-api-provider-aws/v2/util/system"
)

// AWSDefaultRegion is the default AWS region.
const AWSDefaultRegion string = "us-east-1"

// Service holds a collection of interfaces.
// The interfaces are broken down like this to group functions together.
// One alternative is to have a large list of functions from the ec2 client.
Expand Down Expand Up @@ -223,11 +226,13 @@ func (s *Service) Delete(m *scope.MachineScope) error {
}

func (s *Service) createBucketIfNotExist(bucketName string) error {
input := &s3.CreateBucketInput{
Bucket: aws.String(bucketName),
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
input := &s3.CreateBucketInput{Bucket: aws.String(bucketName)}

// See https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html#AmazonS3-CreateBucket-request-LocationConstraint.
if s.scope.Region() != AWSDefaultRegion {
input.CreateBucketConfiguration = &s3.CreateBucketConfiguration{
LocationConstraint: aws.String(s.scope.Region()),
},
}
}

_, err := s.S3Client.CreateBucket(input)
Expand Down
17 changes: 17 additions & 0 deletions pkg/cloud/services/s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@ func TestReconcileBucket(t *testing.T) {
t.Fatalf("Expected error")
}
})

t.Run("creates_bucket_without_location", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
input := &s3svc.CreateBucketInput{
CreateBucketConfiguration: &s3svc.CreateBucketConfiguration{
LocationConstraint: nil,
},
}

s3Mock.EXPECT().CreateBucket(gomock.Eq(input)).Return(nil, nil).Times(1)

if err := svc.ReconcileBucket(); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
})
})
}

Expand Down

0 comments on commit 3442f78

Please sign in to comment.