Skip to content

Commit

Permalink
feat: Add s3 bucket policy
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhops committed Sep 6, 2024
1 parent 1b560f0 commit 5b543f1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
39 changes: 39 additions & 0 deletions s3_bucket.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,42 @@ resource "aws_s3_bucket_lifecycle_configuration" "terraform_state" {
}
}
}

data "aws_caller_identity" "current" {}

resource "aws_s3_bucket_policy" "terraform_state_policy" {
bucket = aws_s3_bucket.terraform_state.id

policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Sid" : "EnforcedTLS",
"Effect" : "Deny",
"Principal" : "*",
"Action" : "s3:*",
"Resource" : [
aws_s3_bucket.terraform_state.arn,
"${aws_s3_bucket.terraform_state.arn}/*"
],
"Condition" : {
"Bool" : {
"aws:SecureTransport" : "false"
}
}
},
{
"Sid" : "RootAccess",
"Effect" : "Allow",
"Principal" : {
"AWS" : "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
},
"Action" : "s3:*",
"Resource" : [
aws_s3_bucket.terraform_state.arn,
"${aws_s3_bucket.terraform_state.arn}/*"
]
}
]
})
}
7 changes: 7 additions & 0 deletions tests/s3_bucket.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ run "encryption_config_attachment" {
}
}

run "bucket_policy_attachment" {
assert {
condition = aws_s3_bucket_policy.terraform_state_policy.bucket == aws_s3_bucket.terraform_state.id
error_message = "Bucket policy is not attached to bucket"
}
}

run "bucket_public" {
command = plan
assert {
Expand Down

0 comments on commit 5b543f1

Please sign in to comment.