Skip to content

Commit

Permalink
Fix bucket settings (#130)
Browse files Browse the repository at this point in the history
* Fix bucket ACL

* Fix bucket ACL

* Fix bucket ACL

* Fix bucket ACL

* Fix bucket ACL

* Fix bucket ACL

* Fix bucket ACL

* Fix bucket ACL

* Update main.tf

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* readme

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

* Fix bucket settings

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
max-lobur and github-actions[bot] authored Nov 11, 2023
1 parent 0586277 commit 41b7d76
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 30 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ Available targets:
| [aws_iam_role_policy_attachment.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.default_cache_bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_s3_bucket.cache_bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
| [aws_s3_bucket_acl.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_acl) | resource |
| [aws_s3_bucket_lifecycle_configuration.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration) | resource |
| [aws_s3_bucket_logging.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_logging) | resource |
| [aws_s3_bucket_ownership_controls.s3_bucket_acl_ownership](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_ownership_controls) | resource |
| [aws_s3_bucket_public_access_block.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
| [aws_s3_bucket_server_side_encryption_configuration.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
| [aws_s3_bucket_versioning.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_versioning) | resource |
| [random_string.bucket_prefix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
| [aws_caller_identity.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.combined_permissions](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
Expand Down
7 changes: 7 additions & 0 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
| [aws_iam_role_policy_attachment.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.default_cache_bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_s3_bucket.cache_bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
| [aws_s3_bucket_acl.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_acl) | resource |
| [aws_s3_bucket_lifecycle_configuration.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration) | resource |
| [aws_s3_bucket_logging.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_logging) | resource |
| [aws_s3_bucket_ownership_controls.s3_bucket_acl_ownership](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_ownership_controls) | resource |
| [aws_s3_bucket_public_access_block.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
| [aws_s3_bucket_server_side_encryption_configuration.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
| [aws_s3_bucket_versioning.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_versioning) | resource |
| [random_string.bucket_prefix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
| [aws_caller_identity.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.combined_permissions](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
Expand Down
93 changes: 63 additions & 30 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,86 @@ data "aws_caller_identity" "default" {}

data "aws_region" "default" {}

resource "aws_s3_bucket" "cache_bucket" {
#bridgecrew:skip=BC_AWS_S3_13:Skipping `Enable S3 Bucket Logging` check until bridgecrew will support dynamic blocks (https://github.com/bridgecrewio/checkov/issues/776).
#bridgecrew:skip=BC_AWS_S3_14:Skipping `Ensure all data stored in the S3 bucket is securely encrypted at rest` check until bridgecrew will support dynamic blocks (https://github.com/bridgecrewio/checkov/issues/776).
#bridgecrew:skip=CKV_AWS_52:Skipping `Ensure S3 bucket has MFA delete enabled` due to issue in terraform (https://github.com/hashicorp/terraform-provider-aws/issues/629).
count = module.this.enabled && local.create_s3_cache_bucket ? 1 : 0
bucket = local.cache_bucket_name_normalised
acl = "private"
force_destroy = true
tags = module.this.tags
resource "aws_s3_bucket_acl" "default" {
count = module.this.enabled && local.create_s3_cache_bucket ? 1 : 0
bucket = join("", resource.aws_s3_bucket.cache_bucket[*].id)
acl = "private"
depends_on = [aws_s3_bucket_ownership_controls.s3_bucket_acl_ownership]
}

versioning {
enabled = var.versioning_enabled
resource "aws_s3_bucket_ownership_controls" "s3_bucket_acl_ownership" {
count = module.this.enabled && local.create_s3_cache_bucket ? 1 : 0
bucket = join("", resource.aws_s3_bucket.cache_bucket[*].id)
rule {
object_ownership = "BucketOwnerPreferred"
}
}

dynamic "logging" {
for_each = var.access_log_bucket_name != "" ? [1] : []
content {
target_bucket = var.access_log_bucket_name
target_prefix = "logs/${module.this.id}/"
}
resource "aws_s3_bucket_versioning" "default" {
count = module.this.enabled && local.create_s3_cache_bucket ? 1 : 0
bucket = join("", resource.aws_s3_bucket.cache_bucket[*].id)
versioning_configuration {
status = "Enabled"
}
}

lifecycle_rule {
id = "codebuildcache"
enabled = true
resource "aws_s3_bucket_lifecycle_configuration" "default" {
count = module.this.enabled && local.create_s3_cache_bucket ? 1 : 0
bucket = join("", resource.aws_s3_bucket.cache_bucket[*].id)

prefix = "/"
tags = module.this.tags
rule {
id = "codebuildcache"
status = "Enabled"

filter {
prefix = "/"
}

expiration {
days = var.cache_expiration_days
}
}
}

dynamic "server_side_encryption_configuration" {
for_each = var.encryption_enabled ? ["true"] : []
resource "aws_s3_bucket_server_side_encryption_configuration" "default" {
count = module.this.enabled && local.create_s3_cache_bucket ? 1 : 0
bucket = join("", resource.aws_s3_bucket.cache_bucket[*].id)

content {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}

resource "aws_s3_bucket_logging" "default" {
count = module.this.enabled && local.create_s3_cache_bucket && var.access_log_bucket_name != "" ? 1 : 0
bucket = join("", resource.aws_s3_bucket.cache_bucket[*].id)

target_bucket = var.access_log_bucket_name
target_prefix = "logs/${module.this.id}/"
}

resource "aws_s3_bucket_public_access_block" "default" {
count = module.this.enabled && local.create_s3_cache_bucket ? 1 : 0
bucket = join("", resource.aws_s3_bucket.cache_bucket[*].id)

block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}

resource "aws_s3_bucket" "cache_bucket" {
#bridgecrew:skip=BC_AWS_S3_13:Skipping `Enable S3 Bucket Logging` check until bridgecrew will support dynamic blocks (https://github.com/bridgecrewio/checkov/issues/776).
#bridgecrew:skip=BC_AWS_S3_14:Skipping `Ensure all data stored in the S3 bucket is securely encrypted at rest` check until bridgecrew will support dynamic blocks (https://github.com/bridgecrewio/checkov/issues/776).
#bridgecrew:skip=CKV_AWS_52:Skipping `Ensure S3 bucket has MFA delete enabled` due to issue in terraform (https://github.com/hashicorp/terraform-provider-aws/issues/629).
count = module.this.enabled && local.create_s3_cache_bucket ? 1 : 0
bucket = local.cache_bucket_name_normalised
force_destroy = true
tags = module.this.tags
}

resource "random_string" "bucket_prefix" {
count = module.this.enabled ? 1 : 0
length = 12
Expand Down

0 comments on commit 41b7d76

Please sign in to comment.