-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.tf
491 lines (394 loc) · 13.3 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# Get the account id of the AWS ALB and ELB service account in a given region for the
# purpose of whitelisting in a S3 bucket policy.
data "aws_elb_service_account" "main" {
}
# The AWS account id
data "aws_caller_identity" "current" {
}
# The AWS partition for differentiating between AWS commercial and GovCloud
data "aws_partition" "current" {
}
# The region is pulled from the current AWS session you are in
data "aws_region" "current" {
}
locals {
# S3 bucket ARN
bucket_arn = "arn:${data.aws_partition.current.partition}:s3:::${var.s3_bucket_name}"
#
# CloudTrail locals
#
# supports logging to multiple accounts
# doesn't support to multiple prefixes
# allow cloudtrail policies if default_allow or allow_cloudtrail are true
cloudtrail_effect = var.default_allow || var.allow_cloudtrail ? "Allow" : "Deny"
# use the cloudtrail_accounts to grant access if provided, otherwise grant access to the current account id
cloudtrail_accounts = length(var.cloudtrail_accounts) > 0 ? var.cloudtrail_accounts : [data.aws_caller_identity.current.account_id]
# if var.cloudtrail_logs_prefix is empty then be sure to remove // in the path
cloudtrail_logs_path = var.cloudtrail_logs_prefix == "" ? "AWSLogs" : "${var.cloudtrail_logs_prefix}/AWSLogs"
# format the full account resources ARN list
cloudtrail_account_resources = toset(formatlist("${local.bucket_arn}/${local.cloudtrail_logs_path}/%s/*", local.cloudtrail_accounts))
# finally, add the organization id path if one is specified
cloudtrail_resources = var.cloudtrail_org_id == "" ? local.cloudtrail_account_resources : setunion(local.cloudtrail_account_resources, ["${local.bucket_arn}/${local.cloudtrail_logs_path}/${var.cloudtrail_org_id}/*"])
#
# Cloudwatch Logs locals
#
# doesn't support logging to multiple accounts
# doesn't support logging to mulitple prefixes
cloudwatch_effect = var.default_allow || var.allow_cloudwatch ? "Allow" : "Deny"
# region specific logs service principal
cloudwatch_service = "logs.${data.aws_region.current.name}.amazonaws.com"
cloudwatch_resource = "${local.bucket_arn}/${var.cloudwatch_logs_prefix}/*"
#
# Config locals
#
# supports logging to muliple accounts
# doesn't support logging to muliple prefixes
config_effect = var.default_allow || var.allow_config ? "Allow" : "Deny"
config_accounts = length(var.config_accounts) > 0 ? var.config_accounts : [data.aws_caller_identity.current.account_id]
config_logs_path = var.config_logs_prefix == "" ? "AWSLogs" : "${var.config_logs_prefix}/AWSLogs"
# Config does a writability check by writing to key "[prefix]/AWSLogs/[accountId]/Config/ConfigWritabilityCheckFile".
# When there is an oversize configuration item change notification, Config will write the notification to S3 at the path.
# Therefore, you cannot limit the policy to the region.
# For example:
# [prefix]/AWSLogs/[accountId]/Config/global/[year]/[month]/[day]/
# OversizedChangeNotification/AWS::IAM::Policy/
# [accountId]_Config_global_ChangeNotification_AWS::IAM::Policy_[resourceId]_[timestamp]_[configurationStateId].json.gz
# Therefore, do not extend the resource path to include the region as shown in the AWS Console.
config_resources = sort(formatlist("${local.bucket_arn}/${local.config_logs_path}/%s/Config/*", local.config_accounts))
#
# ELB locals
#
# supports logging to muliple accounts
# doesn't support logging to multiple prefixes
elb_effect = var.default_allow || var.allow_elb ? "Allow" : "Deny"
elb_accounts = length(var.elb_accounts) > 0 ? var.elb_accounts : [data.aws_caller_identity.current.account_id]
elb_logs_path = var.elb_logs_prefix == "" ? "AWSLogs" : "${var.elb_logs_prefix}/AWSLogs"
elb_resources = sort(formatlist("${local.bucket_arn}/${local.elb_logs_path}/%s/*", local.elb_accounts))
#
# ALB locals
#
# doesn't support logging to multiple accounts
alb_account = var.alb_account != "" ? var.alb_account : data.aws_caller_identity.current.account_id
# supports logging to multiple prefixes
alb_effect = var.default_allow || var.allow_alb ? "Allow" : "Deny"
# if the list of prefixes contains "", set an append_root_prefix flag
alb_include_root_prefix = contains(var.alb_logs_prefixes, "") ? true : false
# create a list of paths, but remove any prefixes containing "" using compact
alb_logs_path_temp = formatlist("%s/AWSLogs", compact(var.alb_logs_prefixes))
# now append an "AWSLogs" path to the list if alb_include_root_prefix is true
alb_logs_path = local.alb_include_root_prefix ? concat(local.alb_logs_path_temp, ["AWSLogs"]) : local.alb_logs_path_temp
# finally, format the full final resources ARN list
alb_resources = sort(formatlist("${local.bucket_arn}/%s/${local.alb_account}/*", local.alb_logs_path))
#
# NLB locals
#
# doesn't support logging to multiple accounts
nlb_account = var.nlb_account != "" ? var.nlb_account : data.aws_caller_identity.current.account_id
# supports logging to multiple prefixes
nlb_effect = var.default_allow || var.allow_nlb ? "Allow" : "Deny"
nlb_include_root_prefix = contains(var.nlb_logs_prefixes, "") ? true : false
nlb_logs_path_temp = formatlist("%s/AWSLogs", compact(var.nlb_logs_prefixes))
nlb_logs_path = local.nlb_include_root_prefix ? concat(local.nlb_logs_path_temp, ["AWSLogs"]) : local.nlb_logs_path_temp
nlb_resources = sort(formatlist("${local.bucket_arn}/%s/${local.nlb_account}/*", local.nlb_logs_path))
#
# Redshift locals
#
# doesn't support logging to multiple accounts
# doesn't support logging to multiple prefixes
redshift_effect = var.default_allow || var.allow_redshift ? "Allow" : "Deny"
redshift_resource = "${local.bucket_arn}/${var.redshift_logs_prefix}/*"
#
# S3 locals
#
# doesn't support logging to multiple accounts
s3_effect = var.default_allow || var.allow_s3 ? "Allow" : "Deny"
s3_resources = ["${local.bucket_arn}/${var.s3_logs_prefix}/*"]
}
#
# S3 Bucket
#
data "aws_iam_policy_document" "main" {
#
# CloudTrail bucket policies
#
statement {
sid = "cloudtrail-logs-get-bucket-acl"
effect = local.cloudtrail_effect
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
actions = ["s3:GetBucketAcl"]
resources = [local.bucket_arn]
}
statement {
sid = "cloudtrail-logs-put-object"
effect = local.cloudtrail_effect
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
actions = ["s3:PutObject"]
resources = local.cloudtrail_resources
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
#
# CloudWatch bucket policies
#
statement {
sid = "cloudwatch-logs-get-bucket-acl"
effect = local.cloudwatch_effect
principals {
type = "Service"
identifiers = [local.cloudwatch_service]
}
actions = ["s3:GetBucketAcl"]
resources = [local.bucket_arn]
}
statement {
sid = "cloudwatch-logs-put-object"
effect = local.cloudwatch_effect
principals {
type = "Service"
identifiers = [local.cloudwatch_service]
}
actions = ["s3:PutObject"]
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
resources = [local.cloudwatch_resource]
}
#
# Config bucket policies
#
statement {
sid = "config-permissions-check"
effect = local.config_effect
principals {
type = "Service"
identifiers = ["config.amazonaws.com"]
}
actions = ["s3:GetBucketAcl", "s3:ListBucket"]
resources = [local.bucket_arn]
}
dynamic "statement" {
for_each = { for k, v in local.config_accounts : k => v }
content {
sid = "config-bucket-delivery-${statement.key}"
effect = local.config_effect
principals {
type = "Service"
identifiers = ["config.amazonaws.com"]
}
actions = ["s3:PutObject", "s3:PutObjectAcl"]
condition {
test = "StringEquals"
variable = "AWS:SourceAccount"
values = [statement.value]
}
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
resources = ["${local.bucket_arn}/${local.config_logs_path}/${statement.value}/Config/*"]
}
}
#
# ELB bucket policies
#
statement {
sid = "elb-logs-put-object"
effect = local.elb_effect
principals {
type = "AWS"
identifiers = [data.aws_elb_service_account.main.arn]
}
actions = ["s3:PutObject"]
resources = local.elb_resources
}
#
# ALB bucket policies
#
statement {
sid = "alb-logs-put-object"
effect = local.alb_effect
principals {
type = "AWS"
identifiers = [data.aws_elb_service_account.main.arn]
}
actions = ["s3:PutObject"]
resources = local.alb_resources
}
#
# NLB bucket policies
#
statement {
sid = "nlb-logs-put-object"
effect = local.nlb_effect
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
actions = ["s3:PutObject"]
resources = local.nlb_resources
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
statement {
sid = "nlb-logs-acl-check"
effect = local.nlb_effect
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
actions = ["s3:GetBucketAcl"]
resources = [local.bucket_arn]
}
#
# Redshift bucket policies
#
statement {
sid = "redshift-logs-put-object"
effect = local.redshift_effect
principals {
type = "Service"
identifiers = ["redshift.amazonaws.com"]
}
actions = ["s3:PutObject"]
resources = [local.redshift_resource]
}
statement {
sid = "redshift-logs-get-bucket-acl"
effect = local.redshift_effect
principals {
type = "Service"
identifiers = ["redshift.amazonaws.com"]
}
actions = ["s3:GetBucketAcl"]
resources = [local.bucket_arn]
}
#
# S3 server access log bucket policies
#
statement {
sid = "s3-logs-put-object"
effect = local.s3_effect
principals {
type = "Service"
identifiers = ["logging.s3.amazonaws.com"]
}
actions = ["s3:PutObject"]
resources = local.s3_resources
}
#
# Enforce TLS requests only
#
statement {
sid = "enforce-tls-requests-only"
effect = "Deny"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = ["s3:*"]
resources = [
local.bucket_arn,
"${local.bucket_arn}/*"
]
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
}
}
resource "aws_s3_bucket" "aws_logs" {
bucket = var.s3_bucket_name
force_destroy = var.force_destroy
tags = var.tags
}
resource "aws_s3_bucket_policy" "aws_logs" {
bucket = aws_s3_bucket.aws_logs.id
policy = data.aws_iam_policy_document.main.json
lifecycle {
ignore_changes = [
# Allows a user to append a custom policy if needed
policy
]
}
}
resource "aws_s3_bucket_acl" "aws_logs" {
count = var.s3_bucket_acl != null ? 1 : 0
bucket = aws_s3_bucket.aws_logs.id
acl = var.s3_bucket_acl
depends_on = [aws_s3_bucket_ownership_controls.aws_logs]
}
resource "aws_s3_bucket_ownership_controls" "aws_logs" {
count = var.control_object_ownership ? 1 : 0
bucket = aws_s3_bucket.aws_logs.id
rule {
object_ownership = var.object_ownership
}
depends_on = [
aws_s3_bucket_policy.aws_logs,
aws_s3_bucket_public_access_block.public_access_block,
aws_s3_bucket.aws_logs
]
}
resource "aws_s3_bucket_lifecycle_configuration" "aws_logs" {
bucket = aws_s3_bucket.aws_logs.id
rule {
id = "expire_all_logs"
status = var.enable_s3_log_bucket_lifecycle_rule ? "Enabled" : "Disabled"
filter {}
expiration {
days = var.s3_log_bucket_retention
}
noncurrent_version_expiration {
noncurrent_days = var.noncurrent_version_retention
}
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "aws_logs" {
bucket = aws_s3_bucket.aws_logs.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_logging" "aws_logs" {
count = var.logging_target_bucket != "" ? 1 : 0
bucket = aws_s3_bucket.aws_logs.id
target_bucket = var.logging_target_bucket
target_prefix = var.logging_target_prefix
}
resource "aws_s3_bucket_versioning" "aws_logs" {
bucket = aws_s3_bucket.aws_logs.id
versioning_configuration {
status = var.versioning_status
mfa_delete = var.enable_mfa_delete ? "Enabled" : null
}
}
resource "aws_s3_bucket_public_access_block" "public_access_block" {
count = var.create_public_access_block ? 1 : 0
bucket = aws_s3_bucket.aws_logs.id
# Block new public ACLs and uploading public objects
block_public_acls = true
# Retroactively remove public access granted through public ACLs
ignore_public_acls = true
# Block new public bucket policies
block_public_policy = true
# Retroactivley block public and cross-account access if bucket has public policies
restrict_public_buckets = true
}