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

🐛 S3 Bucket should be created in the same region, always add transport encryption policy #4732

Merged
merged 1 commit into from
Jan 18, 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
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