Skip to content

Commit 1a431dd

Browse files
authored
feat: Enable CloudTrail log delivery bucket policy option (#332)
1 parent 0d781fb commit 1a431dd

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ No modules.
191191
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
192192
| [aws_canonical_user_id.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/canonical_user_id) | data source |
193193
| [aws_iam_policy_document.access_log_delivery](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
194+
| [aws_iam_policy_document.cloudtrail_log_delivery](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
194195
| [aws_iam_policy_document.combined](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
195196
| [aws_iam_policy_document.deny_incorrect_encryption_headers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
196197
| [aws_iam_policy_document.deny_incorrect_kms_key_sse](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -221,6 +222,7 @@ No modules.
221222
| <a name="input_analytics_source_bucket_arn"></a> [analytics\_source\_bucket\_arn](#input\_analytics\_source\_bucket\_arn) | The analytics source bucket ARN. | `string` | `null` | no |
222223
| <a name="input_attach_access_log_delivery_policy"></a> [attach\_access\_log\_delivery\_policy](#input\_attach\_access\_log\_delivery\_policy) | Controls if S3 bucket should have S3 access log delivery policy attached | `bool` | `false` | no |
223224
| <a name="input_attach_analytics_destination_policy"></a> [attach\_analytics\_destination\_policy](#input\_attach\_analytics\_destination\_policy) | Controls if S3 bucket should have bucket analytics destination policy attached. | `bool` | `false` | no |
225+
| <a name="input_attach_cloudtrail_log_delivery_policy"></a> [attach\_cloudtrail\_log\_delivery\_policy](#input\_attach\_cloudtrail\_log\_delivery\_policy) | Controls if S3 bucket should have CloudTrail log delivery policy attached | `bool` | `false` | no |
224226
| <a name="input_attach_deny_incorrect_encryption_headers"></a> [attach\_deny\_incorrect\_encryption\_headers](#input\_attach\_deny\_incorrect\_encryption\_headers) | Controls if S3 bucket should deny incorrect encryption headers policy attached. | `bool` | `false` | no |
225227
| <a name="input_attach_deny_incorrect_kms_key_sse"></a> [attach\_deny\_incorrect\_kms\_key\_sse](#input\_attach\_deny\_incorrect\_kms\_key\_sse) | Controls if S3 bucket policy should deny usage of incorrect KMS key SSE. | `bool` | `false` | no |
226228
| <a name="input_attach_deny_insecure_transport_policy"></a> [attach\_deny\_insecure\_transport\_policy](#input\_attach\_deny\_insecure\_transport\_policy) | Controls if S3 bucket should have deny non-SSL transport policy attached | `bool` | `false` | no |

examples/complete/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module "log_bucket" {
7373
attach_elb_log_delivery_policy = true
7474
attach_lb_log_delivery_policy = true
7575
attach_access_log_delivery_policy = true
76+
attach_cloudtrail_log_delivery_policy = true
7677
attach_deny_insecure_transport_policy = true
7778
attach_require_latest_tls_policy = true
7879
attach_waf_log_delivery_policy = true

main.tf

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ locals {
1212

1313
create_bucket_acl = (var.acl != null && var.acl != "null") || length(local.grants) > 0
1414

15-
attach_policy = var.attach_require_latest_tls_policy || var.attach_access_log_delivery_policy || var.attach_elb_log_delivery_policy || var.attach_lb_log_delivery_policy || var.attach_deny_insecure_transport_policy || var.attach_inventory_destination_policy || var.attach_deny_incorrect_encryption_headers || var.attach_deny_incorrect_kms_key_sse || var.attach_deny_unencrypted_object_uploads || var.attach_deny_ssec_encrypted_object_uploads || var.attach_policy || var.attach_waf_log_delivery_policy
15+
attach_policy = var.attach_require_latest_tls_policy || var.attach_access_log_delivery_policy || var.attach_elb_log_delivery_policy || var.attach_lb_log_delivery_policy || var.attach_cloudtrail_log_delivery_policy || var.attach_deny_insecure_transport_policy || var.attach_inventory_destination_policy || var.attach_deny_incorrect_encryption_headers || var.attach_deny_incorrect_kms_key_sse || var.attach_deny_unencrypted_object_uploads || var.attach_deny_ssec_encrypted_object_uploads || var.attach_policy || var.attach_waf_log_delivery_policy
1616

1717
# Variables with type `any` should be jsonencode()'d when value is coming from Terragrunt
1818
grants = try(jsondecode(var.grant), var.grant)
@@ -569,6 +569,7 @@ data "aws_iam_policy_document" "combined" {
569569
var.attach_elb_log_delivery_policy ? data.aws_iam_policy_document.elb_log_delivery[0].json : "",
570570
var.attach_lb_log_delivery_policy ? data.aws_iam_policy_document.lb_log_delivery[0].json : "",
571571
var.attach_access_log_delivery_policy ? data.aws_iam_policy_document.access_log_delivery[0].json : "",
572+
var.attach_cloudtrail_log_delivery_policy ? data.aws_iam_policy_document.cloudtrail_log_delivery[0].json : "",
572573
var.attach_require_latest_tls_policy ? data.aws_iam_policy_document.require_latest_tls[0].json : "",
573574
var.attach_deny_insecure_transport_policy ? data.aws_iam_policy_document.deny_insecure_transport[0].json : "",
574575
var.attach_deny_unencrypted_object_uploads ? data.aws_iam_policy_document.deny_unencrypted_object_uploads[0].json : "",
@@ -667,7 +668,7 @@ data "aws_iam_policy_document" "lb_log_delivery" {
667668
count = local.create_bucket && var.attach_lb_log_delivery_policy && !var.is_directory_bucket ? 1 : 0
668669

669670
statement {
670-
sid = "AWSLogDeliveryWrite"
671+
sid = "AlbNlbLogDeliveryWrite"
671672

672673
principals {
673674
type = "Service"
@@ -702,7 +703,7 @@ data "aws_iam_policy_document" "lb_log_delivery" {
702703
}
703704

704705
statement {
705-
sid = "AWSLogDeliveryAclCheck"
706+
sid = "AlbNlbLogDeliveryAclCheck"
706707

707708
effect = "Allow"
708709

@@ -822,7 +823,7 @@ data "aws_iam_policy_document" "waf_log_delivery" {
822823
count = local.create_bucket && var.attach_waf_log_delivery_policy && !var.is_directory_bucket ? 1 : 0
823824

824825
statement {
825-
sid = "AWSLogDeliveryWrite"
826+
sid = "WafLogDeliveryWrite"
826827

827828
effect = "Allow"
828829

@@ -859,7 +860,7 @@ data "aws_iam_policy_document" "waf_log_delivery" {
859860
}
860861

861862
statement {
862-
sid = "AWSLogDeliveryAclCheck"
863+
sid = "WafLogDeliveryAclCheck"
863864

864865
effect = "Allow"
865866

@@ -890,6 +891,46 @@ data "aws_iam_policy_document" "waf_log_delivery" {
890891
}
891892
}
892893

894+
# CloudTrail
895+
data "aws_iam_policy_document" "cloudtrail_log_delivery" {
896+
count = local.create_bucket && var.attach_cloudtrail_log_delivery_policy && !var.is_directory_bucket ? 1 : 0
897+
898+
statement {
899+
sid = "AWSCloudTrailAclCheck"
900+
principals {
901+
type = "Service"
902+
identifiers = ["cloudtrail.amazonaws.com"]
903+
}
904+
actions = [
905+
"s3:GetBucketAcl",
906+
]
907+
resources = [
908+
aws_s3_bucket.this[0].arn,
909+
]
910+
}
911+
912+
statement {
913+
sid = "AWSCloudTrailWrite"
914+
principals {
915+
type = "Service"
916+
identifiers = ["cloudtrail.amazonaws.com"]
917+
}
918+
actions = [
919+
"s3:PutObject",
920+
]
921+
resources = [
922+
"${aws_s3_bucket.this[0].arn}/AWSLogs/*",
923+
]
924+
condition {
925+
test = "StringEquals"
926+
variable = "s3:x-amz-acl"
927+
values = [
928+
"bucket-owner-full-control",
929+
]
930+
}
931+
}
932+
}
933+
893934
data "aws_iam_policy_document" "deny_insecure_transport" {
894935
count = local.create_bucket && var.attach_deny_insecure_transport_policy && !var.is_directory_bucket ? 1 : 0
895936

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ variable "attach_access_log_delivery_policy" {
2222
default = false
2323
}
2424

25+
variable "attach_cloudtrail_log_delivery_policy" {
26+
description = "Controls if S3 bucket should have CloudTrail log delivery policy attached"
27+
type = bool
28+
default = false
29+
}
30+
2531
variable "attach_deny_insecure_transport_policy" {
2632
description = "Controls if S3 bucket should have deny non-SSL transport policy attached"
2733
type = bool

wrappers/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module "wrapper" {
1515
analytics_source_bucket_arn = try(each.value.analytics_source_bucket_arn, var.defaults.analytics_source_bucket_arn, null)
1616
attach_access_log_delivery_policy = try(each.value.attach_access_log_delivery_policy, var.defaults.attach_access_log_delivery_policy, false)
1717
attach_analytics_destination_policy = try(each.value.attach_analytics_destination_policy, var.defaults.attach_analytics_destination_policy, false)
18+
attach_cloudtrail_log_delivery_policy = try(each.value.attach_cloudtrail_log_delivery_policy, var.defaults.attach_cloudtrail_log_delivery_policy, false)
1819
attach_deny_incorrect_encryption_headers = try(each.value.attach_deny_incorrect_encryption_headers, var.defaults.attach_deny_incorrect_encryption_headers, false)
1920
attach_deny_incorrect_kms_key_sse = try(each.value.attach_deny_incorrect_kms_key_sse, var.defaults.attach_deny_incorrect_kms_key_sse, false)
2021
attach_deny_insecure_transport_policy = try(each.value.attach_deny_insecure_transport_policy, var.defaults.attach_deny_insecure_transport_policy, false)

0 commit comments

Comments
 (0)