diff --git a/README.md b/README.md index 4a628fa..5e45884 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/docs/terraform.md b/docs/terraform.md index e867b8a..650f149 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -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 | diff --git a/main.tf b/main.tf index 8630905..fab8a3c 100644 --- a/main.tf +++ b/main.tf @@ -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