Skip to content

Commit 370a1f0

Browse files
authored
Merge pull request #4732 from vincepri/bucket-region-andpolicy
🐛 S3 Bucket should be created in the same region, always add transport encryption policy
2 parents 3618d1c + c0e941b commit 370a1f0

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

pkg/cloud/services/s3/s3.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ func (s *Service) Delete(m *scope.MachineScope) error {
225225
func (s *Service) createBucketIfNotExist(bucketName string) error {
226226
input := &s3.CreateBucketInput{
227227
Bucket: aws.String(bucketName),
228+
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
229+
LocationConstraint: aws.String(s.scope.Region()),
230+
},
228231
}
229232

230233
_, err := s.S3Client.CreateBucket(input)
@@ -251,11 +254,6 @@ func (s *Service) createBucketIfNotExist(bucketName string) error {
251254
}
252255

253256
func (s *Service) ensureBucketPolicy(bucketName string) error {
254-
if s.scope.Bucket().PresignedURLDuration != nil {
255-
// If presigned URL is enabled, we don't need to set bucket policy.
256-
return nil
257-
}
258-
259257
bucketPolicy, err := s.bucketPolicy(bucketName)
260258
if err != nil {
261259
return errors.Wrap(err, "generating Bucket policy")
@@ -322,15 +320,6 @@ func (s *Service) bucketPolicy(bucketName string) (string, error) {
322320
partition := system.GetPartitionFromRegion(s.scope.Region())
323321

324322
statements := []iam.StatementEntry{
325-
{
326-
Sid: "control-plane",
327-
Effect: iam.EffectAllow,
328-
Principal: map[iam.PrincipalType]iam.PrincipalID{
329-
iam.PrincipalAWS: []string{fmt.Sprintf("arn:%s:iam::%s:role/%s", partition, *accountID.Account, bucket.ControlPlaneIAMInstanceProfile)},
330-
},
331-
Action: []string{"s3:GetObject"},
332-
Resource: []string{fmt.Sprintf("arn:%s:s3:::%s/control-plane/*", partition, bucketName)},
333-
},
334323
{
335324
Sid: "ForceSSLOnlyAccess",
336325
Effect: iam.EffectDeny,
@@ -347,16 +336,30 @@ func (s *Service) bucketPolicy(bucketName string) (string, error) {
347336
},
348337
}
349338

350-
for _, iamInstanceProfile := range bucket.NodesIAMInstanceProfiles {
351-
statements = append(statements, iam.StatementEntry{
352-
Sid: iamInstanceProfile,
353-
Effect: iam.EffectAllow,
354-
Principal: map[iam.PrincipalType]iam.PrincipalID{
355-
iam.PrincipalAWS: []string{fmt.Sprintf("arn:%s:iam::%s:role/%s", partition, *accountID.Account, iamInstanceProfile)},
356-
},
357-
Action: []string{"s3:GetObject"},
358-
Resource: []string{fmt.Sprintf("arn:%s:s3:::%s/node/*", partition, bucketName)},
359-
})
339+
if bucket.PresignedURLDuration == nil {
340+
if bucket.ControlPlaneIAMInstanceProfile != "" {
341+
statements = append(statements, iam.StatementEntry{
342+
Sid: "control-plane",
343+
Effect: iam.EffectAllow,
344+
Principal: map[iam.PrincipalType]iam.PrincipalID{
345+
iam.PrincipalAWS: []string{fmt.Sprintf("arn:%s:iam::%s:role/%s", partition, *accountID.Account, bucket.ControlPlaneIAMInstanceProfile)},
346+
},
347+
Action: []string{"s3:GetObject"},
348+
Resource: []string{fmt.Sprintf("arn:%s:s3:::%s/control-plane/*", partition, bucketName)},
349+
})
350+
}
351+
352+
for _, iamInstanceProfile := range bucket.NodesIAMInstanceProfiles {
353+
statements = append(statements, iam.StatementEntry{
354+
Sid: iamInstanceProfile,
355+
Effect: iam.EffectAllow,
356+
Principal: map[iam.PrincipalType]iam.PrincipalID{
357+
iam.PrincipalAWS: []string{fmt.Sprintf("arn:%s:iam::%s:role/%s", partition, *accountID.Account, iamInstanceProfile)},
358+
},
359+
Action: []string{"s3:GetObject"},
360+
Resource: []string{fmt.Sprintf("arn:%s:s3:::%s/node/*", partition, bucketName)},
361+
})
362+
}
360363
}
361364

362365
policy := iam.PolicyDocument{

pkg/cloud/services/s3/s3_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ func TestReconcileBucket(t *testing.T) {
7272

7373
input := &s3svc.CreateBucketInput{
7474
Bucket: aws.String(expectedBucketName),
75+
CreateBucketConfiguration: &s3svc.CreateBucketConfiguration{
76+
LocationConstraint: aws.String("us-west-2"),
77+
},
7578
}
7679

7780
s3Mock.EXPECT().CreateBucket(gomock.Eq(input)).Return(nil, nil).Times(1)
@@ -788,6 +791,7 @@ func testService(t *testing.T, bucket *infrav1.S3Bucket) (*s3.Service, *mock_s3i
788791
AWSCluster: &infrav1.AWSCluster{
789792
Spec: infrav1.AWSClusterSpec{
790793
S3Bucket: bucket,
794+
Region: "us-west-2",
791795
AdditionalTags: infrav1.Tags{
792796
"additional": "from-aws-cluster",
793797
},

0 commit comments

Comments
 (0)