Skip to content

Commit

Permalink
Merge pull request #1272 from gruntwork-io/s3-tests-fix
Browse files Browse the repository at this point in the history
Update S3 bucket creation to pass ObjectOwnership
  • Loading branch information
denis256 authored Apr 25, 2023
2 parents e0c790c + 926e08b commit 6c49de6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
26 changes: 22 additions & 4 deletions examples/terraform-aws-s3-example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ resource "aws_s3_bucket_versioning" "test_bucket" {
}
}

resource "aws_s3_bucket_acl" "test_bucket" {
resource "aws_s3_bucket_ownership_controls" "test_bucket" {
bucket = aws_s3_bucket.test_bucket.id
acl = "private"
rule {
object_ownership = "ObjectWriter"
}
depends_on = [aws_s3_bucket.test_bucket]
}

resource "aws_s3_bucket_acl" "test_bucket" {
bucket = aws_s3_bucket.test_bucket.id
acl = "private"
depends_on = [aws_s3_bucket_ownership_controls.test_bucket]
}


Expand All @@ -59,9 +68,18 @@ resource "aws_s3_bucket" "test_bucket_logs" {
force_destroy = true
}

resource "aws_s3_bucket_acl" "test_bucket_logs" {
resource "aws_s3_bucket_ownership_controls" "test_bucket_logs" {
bucket = aws_s3_bucket.test_bucket_logs.id
acl = "log-delivery-write"
rule {
object_ownership = "ObjectWriter"
}
depends_on = [aws_s3_bucket.test_bucket_logs]
}

resource "aws_s3_bucket_acl" "test_bucket_logs" {
bucket = aws_s3_bucket.test_bucket_logs.id
acl = "log-delivery-write"
depends_on = [aws_s3_bucket_ownership_controls.test_bucket_logs]
}

# Configure bucket access policies
Expand Down
2 changes: 2 additions & 0 deletions modules/aws/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ func CreateS3BucketE(t testing.TestingT, region string, name string) error {

params := &s3.CreateBucketInput{
Bucket: aws.String(name),
// https://github.com/aws/aws-sdk-go/blob/v1.44.122/service/s3/api.go#L41646
ObjectOwnership: aws.String(s3.ObjectOwnershipObjectWriter),
}
_, err = s3Client.CreateBucket(params)
return err
Expand Down
2 changes: 1 addition & 1 deletion modules/aws/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestAssertS3BucketPolicyExists(t *testing.T) {
logger.Logf(t, "Random values selected. Region = %s, Id = %s\n", region, id)

s3BucketName := "gruntwork-terratest-" + strings.ToLower(id)
exampleBucketPolicy := fmt.Sprintf(`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":"s3:Get*","Resource":"arn:aws:s3:::%s/*","Condition":{"Bool":{"aws:SecureTransport":"false"}}}]}`, s3BucketName)
exampleBucketPolicy := fmt.Sprintf(`{"Version":"2012-10-17","Statement":[{"Effect":"Deny","Principal":{"AWS":["*"]},"Action":"s3:Get*","Resource":"arn:aws:s3:::%s/*","Condition":{"Bool":{"aws:SecureTransport":"false"}}}]}`, s3BucketName)

CreateS3Bucket(t, region, s3BucketName)
defer DeleteS3Bucket(t, region, s3BucketName)
Expand Down

0 comments on commit 6c49de6

Please sign in to comment.