Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix: s3 bucket in us-east-1 should not set location constraint #4839

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
113 changes: 79 additions & 34 deletions pkg/cloud/services/s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ func TestReconcileBucket(t *testing.T) {

expectedBucketName := "baz"

svc, s3Mock := testService(t, &infrav1.S3Bucket{
Name: expectedBucketName,
svc, s3Mock := testService(t, &testServiceInput{
Bucket: &infrav1.S3Bucket{
Name: expectedBucketName,
},
})

input := &s3svc.CreateBucketInput{
Expand Down Expand Up @@ -168,11 +170,13 @@ func TestReconcileBucket(t *testing.T) {

bucketName := "bar"

svc, s3Mock := testService(t, &infrav1.S3Bucket{
Name: bucketName,
ControlPlaneIAMInstanceProfile: fmt.Sprintf("control-plane%s", iamv1.DefaultNameSuffix),
NodesIAMInstanceProfiles: []string{
fmt.Sprintf("nodes%s", iamv1.DefaultNameSuffix),
svc, s3Mock := testService(t, &testServiceInput{
Bucket: &infrav1.S3Bucket{
Name: bucketName,
ControlPlaneIAMInstanceProfile: fmt.Sprintf("control-plane%s", iamv1.DefaultNameSuffix),
NodesIAMInstanceProfiles: []string{
fmt.Sprintf("nodes%s", iamv1.DefaultNameSuffix),
},
},
})

Expand Down Expand Up @@ -218,7 +222,7 @@ func TestReconcileBucket(t *testing.T) {
t.Run("is_idempotent", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

s3Mock.EXPECT().CreateBucket(gomock.Any()).Return(nil, nil).Times(2)
s3Mock.EXPECT().PutBucketTagging(gomock.Any()).Return(nil, nil).Times(2)
Expand All @@ -236,7 +240,7 @@ func TestReconcileBucket(t *testing.T) {
t.Run("ignores_when_bucket_already_exists_but_its_owned_by_the_same_account", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

err := awserr.New(s3svc.ErrCodeBucketAlreadyOwnedByYou, "err", errors.New("err"))

Expand All @@ -255,7 +259,7 @@ func TestReconcileBucket(t *testing.T) {
t.Run("bucket_creation_fails", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

s3Mock.EXPECT().CreateBucket(gomock.Any()).Return(nil, errors.New("error")).Times(1)

Expand All @@ -267,7 +271,7 @@ func TestReconcileBucket(t *testing.T) {
t.Run("bucket_creation_returns_unexpected_AWS_error", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

s3Mock.EXPECT().CreateBucket(gomock.Any()).Return(nil, awserr.New("foo", "", nil)).Times(1)

Expand All @@ -279,7 +283,7 @@ func TestReconcileBucket(t *testing.T) {
t.Run("generating_bucket_policy_fails", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

s3Mock.EXPECT().CreateBucket(gomock.Any()).Return(nil, nil).Times(1)
s3Mock.EXPECT().PutBucketTagging(gomock.Any()).Return(nil, nil).Times(1)
Expand All @@ -297,7 +301,7 @@ func TestReconcileBucket(t *testing.T) {
t.Run("creating_bucket_policy_fails", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

s3Mock.EXPECT().CreateBucket(gomock.Any()).Return(nil, nil).Times(1)
s3Mock.EXPECT().PutBucketTagging(gomock.Any()).Return(nil, nil).Times(1)
Expand All @@ -307,6 +311,27 @@ func TestReconcileBucket(t *testing.T) {
t.Fatalf("Expected error")
}
})

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

bucketName := "test"
svc, s3Mock := testService(t, &testServiceInput{
Region: "us-east-1",
Bucket: &infrav1.S3Bucket{Name: bucketName},
})
input := &s3svc.CreateBucketInput{
Bucket: aws.String(bucketName),
}

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

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

Expand All @@ -328,8 +353,10 @@ func TestDeleteBucket(t *testing.T) {
t.Run("deletes_bucket_with_configured_name", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{
Name: bucketName,
svc, s3Mock := testService(t, &testServiceInput{
Bucket: &infrav1.S3Bucket{
Name: bucketName,
},
})

input := &s3svc.DeleteBucketInput{
Expand All @@ -348,7 +375,7 @@ func TestDeleteBucket(t *testing.T) {
t.Run("unexpected_error", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

s3Mock.EXPECT().DeleteBucket(gomock.Any()).Return(nil, errors.New("err")).Times(1)

Expand All @@ -360,7 +387,7 @@ func TestDeleteBucket(t *testing.T) {
t.Run("unexpected_AWS_error", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

s3Mock.EXPECT().DeleteBucket(gomock.Any()).Return(nil, awserr.New("foo", "", nil)).Times(1)

Expand All @@ -373,7 +400,7 @@ func TestDeleteBucket(t *testing.T) {
t.Run("ignores_when_bucket_has_already_been_removed", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

s3Mock.EXPECT().DeleteBucket(gomock.Any()).Return(nil, awserr.New(s3svc.ErrCodeNoSuchBucket, "", nil)).Times(1)

Expand All @@ -385,7 +412,7 @@ func TestDeleteBucket(t *testing.T) {
t.Run("skips_bucket_removal_when_bucket_is_not_empty", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

s3Mock.EXPECT().DeleteBucket(gomock.Any()).Return(nil, awserr.New("BucketNotEmpty", "", nil)).Times(1)

Expand All @@ -406,8 +433,10 @@ func TestCreateObject(t *testing.T) {
t.Run("for_machine", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{
Name: bucketName,
svc, s3Mock := testService(t, &testServiceInput{
Bucket: &infrav1.S3Bucket{
Name: bucketName,
},
})

machineScope := &scope.MachineScope{
Expand Down Expand Up @@ -487,7 +516,7 @@ func TestCreateObject(t *testing.T) {
t.Run("is_idempotent", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

machineScope := &scope.MachineScope{
Machine: &clusterv1.Machine{},
Expand Down Expand Up @@ -516,7 +545,7 @@ func TestCreateObject(t *testing.T) {
t.Run("object_creation_fails", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

machineScope := &scope.MachineScope{
Machine: &clusterv1.Machine{},
Expand All @@ -542,7 +571,7 @@ func TestCreateObject(t *testing.T) {
t.Run("given_empty_machine_scope", func(t *testing.T) {
t.Parallel()

svc, _ := testService(t, &infrav1.S3Bucket{})
svc, _ := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

bootstrapDataURL, err := svc.Create(nil, []byte("foo"))
if err == nil {
Expand All @@ -558,7 +587,7 @@ func TestCreateObject(t *testing.T) {
t.Run("given_empty_bootstrap_data", func(t *testing.T) {
t.Parallel()

svc, _ := testService(t, &infrav1.S3Bucket{})
svc, _ := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

machineScope := &scope.MachineScope{
Machine: &clusterv1.Machine{},
Expand Down Expand Up @@ -615,8 +644,10 @@ func TestDeleteObject(t *testing.T) {

expectedBucketName := "foo"

svc, s3Mock := testService(t, &infrav1.S3Bucket{
Name: expectedBucketName,
svc, s3Mock := testService(t, &testServiceInput{
Bucket: &infrav1.S3Bucket{
Name: expectedBucketName,
},
})

machineScope := &scope.MachineScope{
Expand Down Expand Up @@ -666,7 +697,7 @@ func TestDeleteObject(t *testing.T) {
t.Run("succeeds_when_bucket_has_already_been_removed", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

machineScope := &scope.MachineScope{
Machine: &clusterv1.Machine{},
Expand All @@ -690,7 +721,7 @@ func TestDeleteObject(t *testing.T) {
t.Run("object_deletion_fails", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

machineScope := &scope.MachineScope{
Machine: &clusterv1.Machine{},
Expand All @@ -712,7 +743,7 @@ func TestDeleteObject(t *testing.T) {
t.Run("given_empty_machine_scope", func(t *testing.T) {
t.Parallel()

svc, _ := testService(t, &infrav1.S3Bucket{})
svc, _ := testService(t, nil)

if err := svc.Delete(nil); err == nil {
t.Fatalf("Expected error")
Expand Down Expand Up @@ -742,7 +773,7 @@ func TestDeleteObject(t *testing.T) {
t.Run("is_idempotent", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &infrav1.S3Bucket{})
svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})

machineScope := &scope.MachineScope{
Machine: &clusterv1.Machine{},
Expand All @@ -766,7 +797,14 @@ func TestDeleteObject(t *testing.T) {
})
}

func testService(t *testing.T, bucket *infrav1.S3Bucket) (*s3.Service, *mock_s3iface.MockS3API) {
type testServiceInput struct {
Bucket *infrav1.S3Bucket
Region string
}

const testAWSRegion string = "us-west-2"

func testService(t *testing.T, si *testServiceInput) (*s3.Service, *mock_s3iface.MockS3API) {
t.Helper()

mockCtrl := gomock.NewController(t)
Expand All @@ -780,6 +818,13 @@ func testService(t *testing.T, bucket *infrav1.S3Bucket) (*s3.Service, *mock_s3i
_ = infrav1.AddToScheme(scheme)
client := fake.NewClientBuilder().WithScheme(scheme).Build()

if si == nil {
si = &testServiceInput{}
}
if si.Region == "" {
si.Region = testAWSRegion
}

scope, err := scope.NewClusterScope(scope.ClusterScopeParams{
Client: client,
Cluster: &clusterv1.Cluster{
Expand All @@ -790,8 +835,8 @@ func testService(t *testing.T, bucket *infrav1.S3Bucket) (*s3.Service, *mock_s3i
},
AWSCluster: &infrav1.AWSCluster{
Spec: infrav1.AWSClusterSpec{
S3Bucket: bucket,
Region: "us-west-2",
S3Bucket: si.Bucket,
Region: si.Region,
AdditionalTags: infrav1.Tags{
"additional": "from-aws-cluster",
},
Expand Down