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

feat: allow bucket policies to be externally applied #328

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion bucket.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module "audit_log_bucket" {
log_bucket_name = var.audit_log_bucket_access_logs_name != "" ? var.audit_log_bucket_access_logs_name : "${var.audit_log_bucket_name}-access-logs"
lifecycle_glacier_transition_days = var.audit_log_lifecycle_glacier_transition_days
force_destroy = var.audit_log_bucket_force_destroy
use_external_log_bucket_policy = var.use_external_audit_access_log_bucket_policy

tags = var.tags

Expand Down Expand Up @@ -248,7 +249,7 @@ data "aws_iam_policy_document" "audit_log" {
}

resource "aws_s3_bucket_policy" "audit_log" {
count = local.use_external_bucket ? 0 : 1
count = local.use_external_bucket || var.use_external_audit_log_bucket_policy ? 0 : 1

bucket = module.audit_log_bucket[0].this_bucket.id
policy = data.aws_iam_policy_document.audit_log[0].json
Expand Down
2 changes: 2 additions & 0 deletions modules/secure-bucket/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ resource "aws_s3_bucket_lifecycle_configuration" "access_log" {
}

resource "aws_s3_bucket_policy" "access_log_policy" {
count = var.use_external_log_bucket_policy ? 0 : 1

bucket = aws_s3_bucket.access_log.id
policy = data.aws_iam_policy_document.access_log_policy.json

Expand Down
12 changes: 7 additions & 5 deletions modules/secure-bucket/migrations.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ moved {
to = aws_s3_bucket.access_log
}

moved {
from = aws_s3_bucket_policy.access_log_policy[0]
to = aws_s3_bucket_policy.access_log_policy
}
# Migrations to 2.2.0-custom
# Allowing bucket policy to be exported out and not be applied

# moved {
# from = aws_s3_bucket_policy.access_log_policy[0]
# to = aws_s3_bucket_policy.access_log_policy
# }

moved {
from = aws_s3_bucket_public_access_block.access_log[0]
Expand All @@ -27,4 +30,3 @@ moved {
from = aws_s3_bucket_public_access_block.content[0]
to = aws_s3_bucket_public_access_block.content
}

5 changes: 5 additions & 0 deletions modules/secure-bucket/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ output "log_bucket" {
description = "The S3 bucket used for storing access logs of this bucket."
value = aws_s3_bucket.access_log
}

output "log_bucket_policy" {
description = "Bucket policy to use on the bucket for the access logs."
value = data.aws_iam_policy_document.access_log_policy
}
6 changes: 6 additions & 0 deletions modules/secure-bucket/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ variable "bucket_key_enabled" {
type = bool
default = false
}

variable "use_external_log_bucket_policy" {
description = "Whether or not to apply bucket policy onto log bucket directly."
type = bool
default = false
}
14 changes: 14 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ output "audit_bucket" {
value = one(module.audit_log_bucket[*].this_bucket)
}

output "audit_bucket_policy" {
description = "Bucket policy of the audit logs bucket."
value = one(data.aws_iam_policy_document.audit_log[*])
}

# --------------------------------------------------------------------------------------------------
# Outputs from alarm-baseline module.
# --------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -137,6 +142,15 @@ output "support_iam_role" {
value = one(module.iam_baseline[*].support_iam_role)
}

# --------------------------------------------------------------------------------------------------
# Outputs from secure-bucket module.
# --------------------------------------------------------------------------------------------------

output "access_log_bucket_policy" {
description = "Bucket policy of the access logs bucket of audit logs."
value = one(module.audit_log_bucket[*].log_bucket_policy)
}

# --------------------------------------------------------------------------------------------------
# Outputs from vpc-baseline module.
# --------------------------------------------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ variable "use_external_audit_log_bucket" {
default = false
}

variable "use_external_audit_log_bucket_policy" {
description = "Whether or not to apply bucket policy onto audit log bucket directly."
type = bool
default = false
}

variable "use_external_audit_access_log_bucket_policy" {
description = "Whether or not to apply bucket policy onto the access logs bucket corresponding to audit log bucket directly."
type = bool
default = false
}

# --------------------------------------------------------------------------------------------------
# Variables for iam-baseline module.
# --------------------------------------------------------------------------------------------------
Expand Down