Skip to content

Commit a8dc58b

Browse files
Merge pull request #16 from clouddrove/feature/updated_cloudtrail
[feature/updated_cloudtrail]Fix : cloudtrail IAM policy and s3 bucket policy, update the variables.
2 parents 22f99af + c7bfa87 commit a8dc58b

File tree

5 files changed

+166
-95
lines changed

5 files changed

+166
-95
lines changed

README.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ usage : |-
7171
iam_role_name = "CloudTrail-CloudWatch-Delivery-Role"
7272
iam_role_policy_name = "CloudTrail-CloudWatch-Delivery-Policy"
7373
account_type = "master"
74+
is_organization_trail = true
7475
key_deletion_window_in_days = 10
7576
cloudwatch_logs_retention_in_days = 365
7677
cloudwatch_logs_group_name = "cloudtrail-log-group"

_example/master/example.tf

+103-64
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,122 @@
11
provider "aws" {
2-
region = "eu-west-1"
2+
region = "us-east-1"
33
}
44

55
data "aws_caller_identity" "current" {}
66
data "aws_region" "current" {}
77

8-
module "cloudtrail" {
9-
source = "./../../"
10-
11-
name = "trails"
12-
environment = "test"
13-
label_order = ["environment", "name"]
14-
15-
enabled = true
16-
iam_role_name = "CloudTrail-CloudWatch-Delivery-Role"
17-
iam_role_policy_name = "CloudTrail-CloudWatch-Delivery-Policy"
18-
account_type = "master"
19-
key_deletion_window_in_days = 10
20-
cloudwatch_logs_retention_in_days = 365
21-
cloudwatch_logs_group_name = "cloudtrail-log-group"
22-
EVENT_IGNORE_LIST = jsonencode(["^Describe*", "^Assume*", "^List*", "^Get*", "^Decrypt*", "^Lookup*", "^BatchGet*", "^CreateLogStream$", "^RenewRole$", "^REST.GET.OBJECT_LOCK_CONFIGURATION$", "TestEventPattern", "TestScheduleExpression", "CreateNetworkInterface", "ValidateTemplate"])
23-
EVENT_ALERT_LIST = jsonencode(["DetachRolePolicy", "ConsoleLogin"])
24-
USER_IGNORE_LIST = jsonencode(["^awslambda_*", "^aws-batch$", "^bamboo*", "^i-*", "^[0-9]*$", "^ecs-service-scheduler$", "^AutoScaling$", "^AWSCloudFormation$", "^CloudTrailBot$", "^SLRManagement$"])
25-
SOURCE_LIST = jsonencode(["aws-sdk-go"])
26-
27-
s3_bucket_name = "logs-bucket-cd"
28-
secure_s3_enabled = false
29-
s3_log_bucket_name = "logs-bucket-cd-logs"
30-
sse_algorithm = "aws:kms"
31-
slack_webhook = "https://hooks.slack.com/services/TEE0GHDK0F0QZ/B015frHRDBEUFHEVEG/dfdrfrefrwewqe"
32-
slack_channel = "testing"
33-
additional_member_root_arn = ["arn:aws:iam::xxxxxxxxxxxx:root"]
34-
additional_member_trail = ["arn:aws:cloudtrail:*:xxxxxxxxxxxx:trail/*"]
35-
additional_member_account_id = ["xxxxxxxxxxxx"]
36-
additional_s3_account_path_arn = ["arn:aws:s3:::logs-bucket-clouddrove/AWSLogs/xxxxxxxxxxxx/*"]
37-
s3_policy = data.aws_iam_policy_document.default.json
38-
}
8+
module "cloudtrail" {
9+
source = "./../../"
10+
name = "test-cloudtrail"
11+
environment = "main"
12+
label_order = ["environment", "name"]
13+
enabled = true
14+
iam_role_name = "CloudTrail-CloudWatch-Delivery-Role"
15+
iam_role_policy_name = "CloudTrail-CloudWatch-Delivery-Policy"
16+
account_type = "master"
17+
key_deletion_window_in_days = 10
18+
cloudwatch_logs_retention_in_days = 365
19+
cloudwatch_logs_group_name = "cloudtrail-log-group"
20+
EVENT_IGNORE_LIST = jsonencode(["^Describe*", "^Assume*", "^List*", "^Get*", "^Decrypt*", "^Lookup*", "^BatchGet*", "^CreateLogStream$", "^RenewRole$", "^REST.GET.OBJECT_LOCK_CONFIGURATION$", "TestEventPattern", "TestScheduleExpression", "CreateNetworkInterface", "ValidateTemplate"])
21+
EVENT_ALERT_LIST = jsonencode(["DetachRolePolicy", "ConsoleLogin"])
22+
USER_IGNORE_LIST = jsonencode(["^awslambda_*", "^aws-batch$", "^bamboo*", "^i-*", "^[0-9]*$", "^ecs-service-scheduler$", "^AutoScaling$", "^AWSCloudFormation$", "^CloudTrailBot$", "^SLRManagement$"])
23+
SOURCE_LIST = jsonencode(["aws-sdk-go"])
24+
s3_bucket_name = "test-cloudtrail-bucket"
25+
secure_s3_enabled = false
26+
s3_log_bucket_name = "test-clouddtrail-logs"
27+
sse_algorithm = "aws:kms"
28+
additional_member_root_arn = ["arn:aws:iam::xxxxxxxxxxxx:root"]
29+
additional_member_trail = ["arn:aws:cloudtrail:*:xxxxxxxxxxxx:trail/*"]
30+
additional_member_account_id = ["xxxxxxxxxxxx"]
31+
additional_s3_account_path_arn = ["arn:aws:s3:::test-cloudtrail-bucket/AWSLogs/xxxxxxxxxxxx/*"]
32+
s3_policy = data.aws_iam_policy_document.default.json
33+
}
3934

40-
data "aws_iam_policy_document" "default" {
41-
statement {
42-
sid = "AWSCloudTrailAclCheck"
4335

44-
principals {
45-
type = "Service"
46-
identifiers = ["cloudtrail.amazonaws.com"]
47-
}
36+
data "aws_iam_policy_document" "default" {
37+
statement {
38+
sid = "AWSCloudTrailAclCheck20150319"
4839

49-
actions = [
50-
"s3:GetBucketAcl"
51-
]
40+
principals {
41+
type = "Service"
42+
identifiers = ["cloudtrail.amazonaws.com"]
43+
}
5244

53-
resources = ["arn:aws:s3:::logs-bucket-cd"]
54-
}
45+
actions = [
46+
"s3:GetBucketAcl"
47+
]
5548

56-
statement {
57-
sid = "AWSCloudTrailWrite"
49+
resources = ["arn:aws:s3:::test-cloudtrail-bucket"]
50+
condition {
51+
test = "StringEquals"
52+
variable = "AWS:SourceArn"
5853

59-
principals {
60-
type = "Service"
61-
identifiers = ["cloudtrail.amazonaws.com"]
54+
values = [
55+
"arn:aws:cloudtrail:us-east-1:xxxxxxxxxxxx:trail/xcheck-trails"
56+
]
57+
}
6258
}
59+
statement {
60+
sid = "AWSCloudTrailWrite20150319"
6361

64-
actions = [
65-
"s3:PutObject"
66-
]
62+
principals {
63+
type = "Service"
64+
identifiers = ["cloudtrail.amazonaws.com"]
65+
}
6766

68-
resources = compact(
69-
concat(
70-
[format("arn:aws:s3:::logs-bucket-cd/AWSLogs/%s/*", data.aws_caller_identity.current.account_id), "arn:aws:s3:::logs-bucket-cd/AWSLogs/xxxxxxxxxxxx/*"]
71-
)
72-
)
67+
actions = [
68+
"s3:PutObject"
69+
]
7370

74-
condition {
75-
test = "StringEquals"
76-
variable = "s3:x-amz-acl"
71+
resources = ["arn:aws:s3:::test-cloudtrail-bucket/AWSLogs/xxxxxxxxxxxx/*"]
7772

78-
values = [
79-
"bucket-owner-full-control"
80-
]
73+
condition {
74+
test = "StringEquals"
75+
variable = "s3:x-amz-acl"
76+
77+
values = [
78+
"bucket-owner-full-control"
79+
]
80+
}
81+
condition {
82+
test = "StringEquals"
83+
variable = "AWS:SourceArn"
84+
85+
values = [
86+
"arn:aws:cloudtrail:us-east-1:xxxxxxxxxxxx:trail/xcheck-trails"
87+
]
88+
}
8189
}
90+
91+
statement {
92+
sid = "AWSCloudTrailWrite2015031"
93+
94+
principals {
95+
type = "Service"
96+
identifiers = ["cloudtrail.amazonaws.com"]
97+
}
98+
99+
actions = [
100+
"s3:PutObject"
101+
]
102+
103+
resources = ["arn:aws:s3:::logs-bucket-cd-test/AWSLogs/<AWS_ORGANIZATION_ID>/*"]
104+
105+
condition {
106+
test = "StringEquals"
107+
variable = "s3:x-amz-acl"
108+
109+
values = [
110+
"bucket-owner-full-control"
111+
]
112+
}
113+
condition {
114+
test = "StringEquals"
115+
variable = "AWS:SourceArn"
116+
117+
values = [
118+
"arn:aws:cloudtrail:us-east-1:xxxxxxxxxxxx:trail/xcheck-trails"
119+
]
120+
}
121+
}
82122
}
83-
}

main.tf

+29-29
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module "s3_log_bucket" {
3434
create_bucket = local.is_cloudtrail_enabled
3535
bucket_enabled = var.enabled
3636
versioning = true
37-
acl = "log-delivery-write"
37+
acl = "private"
3838
}
3939

4040
module "s3_bucket" {
@@ -47,7 +47,7 @@ module "s3_bucket" {
4747
create_bucket = local.is_cloudtrail_enabled && var.secure_s3_enabled == false
4848
bucket_logging_enabled = var.enabled && var.secure_s3_enabled == false
4949
versioning = true
50-
acl = "log-delivery-write"
50+
acl = "private"
5151
bucket_policy = true
5252
aws_iam_policy_document = var.s3_policy
5353
force_destroy = true
@@ -66,7 +66,7 @@ module "secure_s3_bucket" {
6666
create_bucket = local.is_cloudtrail_enabled && var.secure_s3_enabled
6767
bucket_logging_encryption_enabled = var.enabled && var.secure_s3_enabled
6868
versioning = true
69-
acl = "log-delivery-write"
69+
acl = "private"
7070
bucket_policy = true
7171
aws_iam_policy_document = var.s3_policy
7272
force_destroy = true
@@ -141,7 +141,7 @@ module "kms_key" {
141141
description = "KMS key for cloudtrail"
142142
deletion_window_in_days = 7
143143
enable_key_rotation = true
144-
alias = "alias/cloudtrail1"
144+
alias = "alias/cloudtrail"
145145
policy = data.aws_iam_policy_document.cloudtrail_key_policy.json
146146
}
147147

@@ -177,11 +177,17 @@ data "aws_iam_policy_document" "cloudtrail_key_policy" {
177177
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
178178
values = compact(
179179
concat(
180-
[format("arn:aws:cloudtrail:*:%s:trail/*", data.aws_caller_identity.current.account_id)],
181-
var.additional_member_trail
180+
[format("arn:aws:cloudtrail:*:%s:trail/*", data.aws_caller_identity.current.account_id)]
182181
)
183182
)
184183
}
184+
condition {
185+
test = "StringEquals"
186+
variable = "AWS:SourceArn"
187+
values = [
188+
"arn:aws:cloudtrail:us-east-1:156873913342:trail/<trail_Name>"
189+
]
190+
}
185191
}
186192

187193
statement {
@@ -210,8 +216,7 @@ data "aws_iam_policy_document" "cloudtrail_key_policy" {
210216
variable = "kms:CallerAccount"
211217
values = compact(
212218
concat(
213-
[data.aws_caller_identity.current.account_id],
214-
var.additional_member_account_id
219+
[data.aws_caller_identity.current.account_id]
215220
)
216221
)
217222
}
@@ -245,8 +250,7 @@ data "aws_iam_policy_document" "cloudtrail_key_policy" {
245250
values = compact(
246251
concat(
247252
[
248-
data.aws_caller_identity.current.account_id],
249-
var.additional_member_account_id
253+
data.aws_caller_identity.current.account_id]
250254
)
251255
)
252256
}
@@ -267,8 +271,7 @@ data "aws_iam_policy_document" "cloudtrail_key_policy" {
267271
variable = "kms:CallerAccount"
268272
values = compact(
269273
concat(
270-
[data.aws_caller_identity.current.account_id],
271-
var.additional_member_account_id
274+
[data.aws_caller_identity.current.account_id]
272275
)
273276
)
274277
}
@@ -277,8 +280,7 @@ data "aws_iam_policy_document" "cloudtrail_key_policy" {
277280
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
278281
values = compact(
279282
concat(
280-
[format("arn:aws:cloudtrail:*:%s:trail/*", data.aws_caller_identity.current.account_id)],
281-
var.additional_member_trail
283+
[format("arn:aws:cloudtrail:*:%s:trail/*", data.aws_caller_identity.current.account_id)]
282284
)
283285
)
284286
}
@@ -290,17 +292,16 @@ data "aws_iam_policy_document" "cloudtrail_key_policy" {
290292
type = "AWS"
291293
identifiers = compact(
292294
concat(
293-
[format("arn:aws:iam::%s:root", data.aws_caller_identity.current.account_id)],
294-
var.additional_member_root_arn
295+
[format("arn:aws:iam::%s:root", data.aws_caller_identity.current.account_id)]
295296
)
296297
)
297298
}
298299
actions = [
299-
"kms:Encrypt",
300-
"kms:Decrypt",
301-
"kms:ReEncrypt*",
302-
"kms:GenerateDataKey*",
303-
"kms:DescribeKey"
300+
"kms:ReEncrypt*",
301+
"kms:GenerateDataKey*",
302+
"kms:Encrypt",
303+
"kms:DescribeKey",
304+
"kms:Decrypt"
304305
]
305306
resources = ["*"]
306307
}
@@ -311,8 +312,7 @@ data "aws_iam_policy_document" "cloudtrail_key_policy" {
311312
type = "AWS"
312313
identifiers = compact(
313314
concat(
314-
[format("arn:aws:iam::%s:root", data.aws_caller_identity.current.account_id)],
315-
var.additional_member_root_arn
315+
[format("arn:aws:iam::%s:root", data.aws_caller_identity.current.account_id)]
316316
)
317317
)
318318
}
@@ -345,17 +345,17 @@ locals {
345345
module "cloudtrail" {
346346
source = "git::https://github.com/clouddrove/terraform-aws-cloudtrail.git?ref=tags/0.14.0"
347347

348-
name = "cloudtrail"
348+
name = var.name
349349
environment = var.environment
350350
label_order = ["name", "environment"]
351351
managedby = var.managedby
352352
enabled_cloudtrail = var.enabled
353353
s3_bucket_name = format("%s", var.s3_bucket_name)
354-
enable_logging = true
355-
enable_log_file_validation = true
356-
include_global_service_events = true
357-
is_multi_region_trail = true
358-
is_organization_trail = false
354+
enable_logging = var.enable_logging
355+
enable_log_file_validation = var.enable_log_file_validation
356+
include_global_service_events = var.include_global_service_events
357+
is_multi_region_trail = var.is_multi_region_trail
358+
is_organization_trail = var.is_organization_trail
359359
kms_key_id = var.key_arn == "" ? module.kms_key.key_arn : var.key_arn
360360
cloud_watch_logs_group_arn = join("", aws_cloudwatch_log_group.cloudtrail_events.*.arn)
361361
cloud_watch_logs_role_arn = join("", aws_iam_role.cloudwatch_delivery.*.arn)

variables.tf

+24
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,30 @@ variable "is_organization_trail" {
173173
description = "Specifies whether the trail is an AWS Organizations trail. Organization trails log events for the master account and all member accounts. Can only be created in the organization master account."
174174
}
175175

176+
variable "is_multi_region_trail" {
177+
type = bool
178+
default = true
179+
description = "Specifies whether the trail is an AWS Organizations trail. Organization trails log events for the master account and all member accounts. Can only be created in the organization master account."
180+
}
181+
182+
variable "include_global_service_events" {
183+
type = bool
184+
default = true
185+
description = "Specifies whether the trail is an AWS Organizations trail. Organization trails log events for the master account and all member accounts. Can only be created in the organization master account."
186+
}
187+
188+
variable "enable_log_file_validation" {
189+
type = bool
190+
default = true
191+
description = "Specifies whether the trail is an AWS Organizations trail. Organization trails log events for the master account and all member accounts. Can only be created in the organization master account."
192+
}
193+
194+
variable "enable_logging" {
195+
type = bool
196+
default = true
197+
description = "Specifies whether the trail is an AWS Organizations trail. Organization trails log events for the master account and all member accounts. Can only be created in the organization master account."
198+
}
199+
176200
variable "account_type" {
177201
type = string
178202
default = "individual"

versions.tf

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# Terraform version
22
terraform {
3-
required_version = ">= 0.12"
4-
}
3+
required_version = ">= 1.3.6"
4+
5+
required_providers {
6+
aws = {
7+
source = "hashicorp/aws"
8+
version = ">= 4.48.0"
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)