Skip to content

Commit d5dde32

Browse files
author
erastus.ndi
committed
fix: Fixed Access Logging
1 parent aa402e1 commit d5dde32

File tree

9 files changed

+137
-15
lines changed

9 files changed

+137
-15
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repos:
88
files: \.(tf|tfvars)$
99

1010
- repo: https://github.com/bridgecrewio/checkov.git
11-
rev: '3.2.483'
11+
rev: '3.2.484'
1212
hooks:
1313
- id: checkov
1414
name: checkov-aws

aws/modules/s3/main.tf

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ locals {
1111
#checkov:skip=CKV_AWS_144:Not required for this project
1212
resource "aws_s3_bucket" "this" {
1313
bucket = var.bucket_name
14-
object_lock_enabled = var.enable_object_lock
14+
object_lock_enabled = var.enable_object_lock && !var.is_logging_target # Cannot have object lock on logging destination
1515
tags = local.tags
1616
}
1717

@@ -25,9 +25,9 @@ resource "aws_s3_bucket_versioning" "this" {
2525
}
2626
}
2727

28-
#Object Lock (if enabled)
28+
#Object Lock (if enabled and not a logging target)
2929
resource "aws_s3_bucket_object_lock_configuration" "this" {
30-
count = var.enable_object_lock ? 1 : 0
30+
count = var.enable_object_lock && !var.is_logging_target ? 1 : 0
3131
bucket = aws_s3_bucket.this.id
3232

3333
rule {
@@ -67,6 +67,11 @@ resource "aws_s3_bucket_logging" "this" {
6767
bucket = aws_s3_bucket.this.id
6868
target_bucket = var.logging_target_bucket
6969
target_prefix = var.logging_target_prefix != null ? var.logging_target_prefix : "${var.environment}-bucket-logs/"
70+
71+
depends_on = [
72+
aws_s3_bucket.this,
73+
aws_s3_bucket_ownership_controls.this
74+
]
7075
}
7176

7277
# Server-Side Encryption
@@ -110,7 +115,7 @@ resource "aws_s3_bucket_ownership_controls" "this" {
110115
bucket = aws_s3_bucket.this.id
111116

112117
rule {
113-
object_ownership = var.s3_object_ownership
118+
object_ownership = var.is_logging_target ? "BucketOwnerPreferred" : var.s3_object_ownership
114119
}
115120
}
116121

aws/modules/s3/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ variable "s3_object_ownership" {
115115
default = "BucketOwnerPreferred"
116116
}
117117

118+
variable "is_logging_target" {
119+
description = "Whether this bucket is used as a logging target for other S3 buckets"
120+
type = bool
121+
default = false
122+
}
123+
118124
variable "additional_bucket_policy_documents" {
119125
description = "Optional list of JSON policy documents to merge with the module's bucket policy"
120126
type = list(string)

aws/non-prod-infra/dev/s3-policy.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,37 @@ data "aws_iam_policy_document" "s3_core_audit_logs_policy" {
5252
variable = "s3:x-amz-acl"
5353
values = ["bucket-owner-full-control"]
5454
}
55+
condition {
56+
test = "StringEquals"
57+
variable = "aws:SourceAccount"
58+
values = [data.aws_caller_identity.current.account_id]
59+
}
60+
condition {
61+
test = "ArnLike"
62+
variable = "aws:SourceArn"
63+
values = [
64+
"arn:aws:s3:::${local.s3_bucket_kfd_name}",
65+
"arn:aws:s3:::${local.s3_bucket_static_files_name}",
66+
"arn:aws:s3:::${local.s3_bucket_submitted_form_data_name}",
67+
"arn:aws:s3:::${local.s3_bucket_zip_files_name}"
68+
]
69+
}
70+
}
71+
statement {
72+
sid = "AllowS3LoggingGetBucketAcl"
73+
effect = "Allow"
74+
principals {
75+
type = "Service"
76+
identifiers = ["logging.s3.amazonaws.com"]
77+
}
78+
actions = ["s3:GetBucketAcl"]
79+
resources = [
80+
"arn:aws:s3:::${local.s3_bucket_core_audit_logs_name}"
81+
]
82+
condition {
83+
test = "StringEquals"
84+
variable = "aws:SourceAccount"
85+
values = [data.aws_caller_identity.current.account_id]
86+
}
5587
}
5688
}

aws/non-prod-infra/dev/s3.tf

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,23 @@ module "s3_core_audit_logs" {
115115
kms_key_id = data.aws_kms_alias.s3.target_key_arn
116116
enable_public_access_block = true
117117
enable_mfa_delete = false
118-
enable_object_lock = true
118+
enable_object_lock = false
119119
object_lock_mode = "GOVERNANCE"
120120
object_lock_retention_days = 30
121-
enable_lifecycle_expiration = true
121+
enable_lifecycle_expiration = false
122122
expiration_days = 30
123123
additional_bucket_policy_documents = [data.aws_iam_policy_document.s3_core_audit_logs_policy.json]
124+
is_logging_target = true
124125
# checkov:CKV2_AWS_62: Not required for this bucket
125126
# checkov:CKV2_AWS_61: Not required for this bucket
126127
# checkov:CKV_AWS_144: Not required for this project
127128
}
128129

129-
resource "aws_s3_bucket_acl" "cloudfront_acl" {
130-
bucket = local.s3_bucket_core_audit_logs_name
130+
resource "aws_s3_bucket_acl" "core_audit_logs_acl" {
131+
bucket = module.s3_core_audit_logs.bucket_id
131132
acl = "log-delivery-write"
133+
134+
depends_on = [
135+
module.s3_core_audit_logs
136+
]
132137
}

aws/non-prod-infra/staging/s3-policy.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,37 @@ data "aws_iam_policy_document" "s3_core_audit_logs_policy" {
5151
variable = "s3:x-amz-acl"
5252
values = ["bucket-owner-full-control"]
5353
}
54+
condition {
55+
test = "StringEquals"
56+
variable = "aws:SourceAccount"
57+
values = [data.aws_caller_identity.current.account_id]
58+
}
59+
condition {
60+
test = "ArnLike"
61+
variable = "aws:SourceArn"
62+
values = [
63+
"arn:aws:s3:::${local.s3_bucket_kfd_name}",
64+
"arn:aws:s3:::${local.s3_bucket_static_files_name}",
65+
"arn:aws:s3:::${local.s3_bucket_submitted_form_data_name}",
66+
"arn:aws:s3:::${local.s3_bucket_zip_files_name}"
67+
]
68+
}
69+
}
70+
statement {
71+
sid = "AllowS3LoggingGetBucketAcl"
72+
effect = "Allow"
73+
principals {
74+
type = "Service"
75+
identifiers = ["logging.s3.amazonaws.com"]
76+
}
77+
actions = ["s3:GetBucketAcl"]
78+
resources = [
79+
"arn:aws:s3:::${local.s3_bucket_core_audit_logs_name}"
80+
]
81+
condition {
82+
test = "StringEquals"
83+
variable = "aws:SourceAccount"
84+
values = [data.aws_caller_identity.current.account_id]
85+
}
5486
}
5587
}

aws/non-prod-infra/staging/s3.tf

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,23 @@ module "s3_core_audit_logs" {
115115
kms_key_id = data.aws_kms_alias.s3.target_key_arn
116116
enable_public_access_block = true
117117
enable_mfa_delete = false
118-
enable_object_lock = true
118+
enable_object_lock = false
119119
object_lock_mode = "GOVERNANCE"
120120
object_lock_retention_days = 30
121121
enable_lifecycle_expiration = false
122122
expiration_days = 30
123123
additional_bucket_policy_documents = [data.aws_iam_policy_document.s3_core_audit_logs_policy.json]
124+
is_logging_target = true
124125
# checkov:CKV2_AWS_62: Not required for this bucket
125126
# checkov:CKV2_AWS_61: Not required for this bucket
126127
# checkov:CKV_AWS_144: Not required for this project
127128
}
128129

129-
resource "aws_s3_bucket_acl" "cloudfront_acl" {
130-
bucket = local.s3_bucket_core_audit_logs_name
130+
resource "aws_s3_bucket_acl" "core_audit_logs_acl" {
131+
bucket = module.s3_core_audit_logs.bucket_id
131132
acl = "log-delivery-write"
133+
134+
depends_on = [
135+
module.s3_core_audit_logs
136+
]
132137
}

aws/prod-infra/prod/s3-policy.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,37 @@ data "aws_iam_policy_document" "s3_core_audit_logs_policy" {
5151
variable = "s3:x-amz-acl"
5252
values = ["bucket-owner-full-control"]
5353
}
54+
condition {
55+
test = "StringEquals"
56+
variable = "aws:SourceAccount"
57+
values = [data.aws_caller_identity.current.account_id]
58+
}
59+
condition {
60+
test = "ArnLike"
61+
variable = "aws:SourceArn"
62+
values = [
63+
"arn:aws:s3:::${local.s3_bucket_kfd_name}",
64+
"arn:aws:s3:::${local.s3_bucket_static_files_name}",
65+
"arn:aws:s3:::${local.s3_bucket_submitted_form_data_name}",
66+
"arn:aws:s3:::${local.s3_bucket_zip_files_name}"
67+
]
68+
}
69+
}
70+
statement {
71+
sid = "AllowS3LoggingGetBucketAcl"
72+
effect = "Allow"
73+
principals {
74+
type = "Service"
75+
identifiers = ["logging.s3.amazonaws.com"]
76+
}
77+
actions = ["s3:GetBucketAcl"]
78+
resources = [
79+
"arn:aws:s3:::${local.s3_bucket_core_audit_logs_name}"
80+
]
81+
condition {
82+
test = "StringEquals"
83+
variable = "aws:SourceAccount"
84+
values = [data.aws_caller_identity.current.account_id]
85+
}
5486
}
5587
}

aws/prod-infra/prod/s3.tf

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,23 @@ module "s3_core_audit_logs" {
115115
kms_key_id = data.aws_kms_alias.s3.target_key_arn
116116
enable_public_access_block = true
117117
enable_mfa_delete = false
118-
enable_object_lock = true
118+
enable_object_lock = false
119119
object_lock_mode = "GOVERNANCE"
120120
object_lock_retention_days = 30
121121
enable_lifecycle_expiration = false
122122
expiration_days = 30
123123
additional_bucket_policy_documents = [data.aws_iam_policy_document.s3_core_audit_logs_policy.json]
124+
is_logging_target = true
124125
# checkov:CKV2_AWS_62: Not required for this bucket
125126
# checkov:CKV2_AWS_61: Not required for this bucket
126127
# checkov:CKV_AWS_144: Not required for this project
127128
}
128129

129-
resource "aws_s3_bucket_acl" "cloudfront_acl" {
130-
bucket = local.s3_bucket_core_audit_logs_name
130+
resource "aws_s3_bucket_acl" "core_audit_logs_acl" {
131+
bucket = module.s3_core_audit_logs.bucket_id
131132
acl = "log-delivery-write"
133+
134+
depends_on = [
135+
module.s3_core_audit_logs
136+
]
132137
}

0 commit comments

Comments
 (0)