forked from lacework/terraform-aws-cloudtrail-controltower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
229 lines (205 loc) · 6.47 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
locals {
sqs_queue_name = length(var.sqs_queue_name) > 0 ? var.sqs_queue_name : "${var.prefix}-sqs-${random_id.uniq.hex}"
s3_logs_location = "${var.s3_bucket_arn}/${data.aws_organizations_organization.main.id}/*AWSLogs/*"
cross_account_policy_name = (
length(var.cross_account_policy_name) > 0 ? var.cross_account_policy_name : "${var.prefix}-cross-acct-policy-${random_id.uniq.hex}"
)
iam_role_arn = module.lacework_ct_iam_role.created ? module.lacework_ct_iam_role.arn : var.iam_role_arn
iam_role_external_id = module.lacework_ct_iam_role.created ? module.lacework_ct_iam_role.external_id : var.iam_role_external_id
iam_role_name = var.use_existing_iam_role ? var.iam_role_name : (
length(var.iam_role_name) > 0 ? var.iam_role_name : "${var.prefix}-iam-${random_id.uniq.hex}"
)
version_file = "${abspath(path.module)}/VERSION"
lacework_integration_guid = lacework_integration_aws_ct.default.id
module_name = "terraform-aws-cloudtrail-controltower"
module_version = fileexists(local.version_file) ? file(local.version_file) : ""
}
data "aws_organizations_organization" "main" {
provider = aws.audit
}
resource "random_id" "uniq" {
byte_length = 4
}
resource "aws_sqs_queue" "lacework_cloudtrail_sqs_queue" {
provider = aws.audit
name = local.sqs_queue_name
tags = var.tags
}
resource "aws_sqs_queue_policy" "lacework_sqs_queue_policy" {
provider = aws.audit
depends_on = [time_sleep.wait_time]
queue_url = aws_sqs_queue.lacework_cloudtrail_sqs_queue.id
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "lacework_sqs_policy_${random_id.uniq.hex}",
"Statement": [
{
"Sid": "AllowLaceworkSNSTopicToSendMessage",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "SQS:SendMessage",
"Resource": "${aws_sqs_queue.lacework_cloudtrail_sqs_queue.arn}",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "${var.sns_topic_arn}"
}
}
},
{
"Sid": "AllowLaceworkCrossAccountRoleToAccessSQSQueue",
"Effect": "Allow",
"Principal": {
"AWS": "${local.iam_role_arn}"
},
"Action": [
"sqs:DeleteMessage",
"sqs:ReceiveMessage",
"sqs:GetQueueAttributes"
],
"Resource": "${aws_sqs_queue.lacework_cloudtrail_sqs_queue.arn}"
}
]
}
POLICY
}
resource "aws_sns_topic_subscription" "lacework_sns_topic_sub" {
provider = aws.audit
topic_arn = var.sns_topic_arn
protocol = "sqs"
endpoint = aws_sqs_queue.lacework_cloudtrail_sqs_queue.arn
}
data "aws_iam_policy_document" "read_logs" {
provider = aws.log_archive
version = "2012-10-17"
statement {
sid = "ReadLogFiles"
actions = ["s3:Get*"]
resources = ["${local.s3_logs_location}"]
}
statement {
sid = "GetAccountAlias"
actions = ["iam:ListAccountAliases"]
resources = ["*"]
}
statement {
sid = "ListLogFiles"
resources = ["${local.s3_logs_location}"]
actions = ["s3:ListBucket"]
condition {
test = "StringLike"
variable = "s3:prefix"
values = ["*AWSLogs/"]
}
}
statement {
sid = "ConsumeNotifications"
resources = ["${aws_sqs_queue.lacework_cloudtrail_sqs_queue.arn}"]
actions = [
"sqs:GetQueueAttributes",
"sqs:GetQueueUrl",
"sqs:DeleteMessage",
"sqs:ReceiveMessage"
]
}
statement {
sid = "Debug"
resources = ["*"]
actions = [
"cloudtrail:DescribeTrails",
"cloudtrail:GetTrail",
"cloudtrail:GetTrailStatus",
"cloudtrail:ListPublicKeys",
"s3:GetBucketAcl",
"s3:GetBucketPolicy",
"s3:ListAllMyBuckets",
"s3:GetBucketLocation",
"s3:GetBucketLogging",
"sns:GetSubscriptionAttributes",
"sns:GetTopicAttributes",
"sns:ListSubscriptions",
"sns:ListSubscriptionsByTopic",
"sns:ListTopics"
]
}
}
data "aws_iam_policy_document" "kms_decrypt" {
provider = aws.log_archive
version = "2012-10-17"
statement {
sid = "DecryptLogFiles"
actions = ["kms:Decrypt"]
resources = [var.kms_key_arn]
}
}
data "aws_iam_policy_document" "cross_account_policy" {
provider = aws.log_archive
version = "2012-10-17"
source_policy_documents = var.kms_key_arn == "" ? [
data.aws_iam_policy_document.read_logs.json,
] : [
data.aws_iam_policy_document.read_logs.json,
data.aws_iam_policy_document.kms_decrypt.json
]
}
resource "aws_iam_policy" "cross_account_policy" {
provider = aws.log_archive
name = local.cross_account_policy_name
description = "A cross account policy to allow Lacework to pull config and cloudtrail"
policy = data.aws_iam_policy_document.cross_account_policy.json
}
module "lacework_ct_iam_role" {
providers = {
aws = aws.log_archive
}
source = "lacework/iam-role/aws"
version = "0.4.2"
create = var.use_existing_iam_role ? false : true
iam_role_name = local.iam_role_name
lacework_aws_account_id = var.lacework_aws_account_id
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "lacework_cross_account_iam_role_policy" {
provider = aws.log_archive
role = local.iam_role_name
policy_arn = aws_iam_policy.cross_account_policy.arn
depends_on = [module.lacework_ct_iam_role]
}
# wait for X seconds for things to settle down in the AWS side
# before trying to create the Lacework external integration
resource "time_sleep" "wait_time" {
create_duration = var.wait_time
depends_on = [
aws_iam_role_policy_attachment.lacework_cross_account_iam_role_policy,
aws_sns_topic_subscription.lacework_sns_topic_sub
]
}
resource "lacework_integration_aws_ct" "default" {
name = var.lacework_integration_name
queue_url = aws_sqs_queue.lacework_cloudtrail_sqs_queue.id
credentials {
role_arn = local.iam_role_arn
external_id = local.iam_role_external_id
}
dynamic "org_account_mappings" {
for_each = var.org_account_mappings
content {
default_lacework_account = org_account_mappings.value["default_lacework_account"]
dynamic "mapping" {
for_each = org_account_mappings.value["mapping"]
content {
lacework_account = mapping.value["lacework_account"]
aws_accounts = mapping.value["aws_accounts"]
}
}
}
}
depends_on = [time_sleep.wait_time]
}
data "lacework_metric_module" "lwmetrics" {
count = var.enable_metric_module ? 1 : 0
name = local.module_name
version = local.module_version
}