Skip to content

Commit

Permalink
Merge pull request #4732 from vincepri/bucket-region-andpolicy
Browse files Browse the repository at this point in the history
🐛 S3 Bucket should be created in the same region, always add transport encryption policy
  • Loading branch information
k8s-ci-robot committed Jan 18, 2024
2 parents 3618d1c + c0e941b commit 370a1f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
51 changes: 27 additions & 24 deletions pkg/cloud/services/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ 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{
LocationConstraint: aws.String(s.scope.Region()),
},
}

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

func (s *Service) ensureBucketPolicy(bucketName string) error {
if s.scope.Bucket().PresignedURLDuration != nil {
// If presigned URL is enabled, we don't need to set bucket policy.
return nil
}

bucketPolicy, err := s.bucketPolicy(bucketName)
if err != nil {
return errors.Wrap(err, "generating Bucket policy")
Expand Down Expand Up @@ -322,15 +320,6 @@ func (s *Service) bucketPolicy(bucketName string) (string, error) {
partition := system.GetPartitionFromRegion(s.scope.Region())

statements := []iam.StatementEntry{
{
Sid: "control-plane",
Effect: iam.EffectAllow,
Principal: map[iam.PrincipalType]iam.PrincipalID{
iam.PrincipalAWS: []string{fmt.Sprintf("arn:%s:iam::%s:role/%s", partition, *accountID.Account, bucket.ControlPlaneIAMInstanceProfile)},
},
Action: []string{"s3:GetObject"},
Resource: []string{fmt.Sprintf("arn:%s:s3:::%s/control-plane/*", partition, bucketName)},
},
{
Sid: "ForceSSLOnlyAccess",
Effect: iam.EffectDeny,
Expand All @@ -347,16 +336,30 @@ func (s *Service) bucketPolicy(bucketName string) (string, error) {
},
}

for _, iamInstanceProfile := range bucket.NodesIAMInstanceProfiles {
statements = append(statements, iam.StatementEntry{
Sid: iamInstanceProfile,
Effect: iam.EffectAllow,
Principal: map[iam.PrincipalType]iam.PrincipalID{
iam.PrincipalAWS: []string{fmt.Sprintf("arn:%s:iam::%s:role/%s", partition, *accountID.Account, iamInstanceProfile)},
},
Action: []string{"s3:GetObject"},
Resource: []string{fmt.Sprintf("arn:%s:s3:::%s/node/*", partition, bucketName)},
})
if bucket.PresignedURLDuration == nil {
if bucket.ControlPlaneIAMInstanceProfile != "" {
statements = append(statements, iam.StatementEntry{
Sid: "control-plane",
Effect: iam.EffectAllow,
Principal: map[iam.PrincipalType]iam.PrincipalID{
iam.PrincipalAWS: []string{fmt.Sprintf("arn:%s:iam::%s:role/%s", partition, *accountID.Account, bucket.ControlPlaneIAMInstanceProfile)},
},
Action: []string{"s3:GetObject"},
Resource: []string{fmt.Sprintf("arn:%s:s3:::%s/control-plane/*", partition, bucketName)},
})
}

for _, iamInstanceProfile := range bucket.NodesIAMInstanceProfiles {
statements = append(statements, iam.StatementEntry{
Sid: iamInstanceProfile,
Effect: iam.EffectAllow,
Principal: map[iam.PrincipalType]iam.PrincipalID{
iam.PrincipalAWS: []string{fmt.Sprintf("arn:%s:iam::%s:role/%s", partition, *accountID.Account, iamInstanceProfile)},
},
Action: []string{"s3:GetObject"},
Resource: []string{fmt.Sprintf("arn:%s:s3:::%s/node/*", partition, bucketName)},
})
}
}

policy := iam.PolicyDocument{
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloud/services/s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func TestReconcileBucket(t *testing.T) {

input := &s3svc.CreateBucketInput{
Bucket: aws.String(expectedBucketName),
CreateBucketConfiguration: &s3svc.CreateBucketConfiguration{
LocationConstraint: aws.String("us-west-2"),
},
}

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

0 comments on commit 370a1f0

Please sign in to comment.