forked from nozaq/terraform-aws-secure-baseline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.tf
260 lines (225 loc) · 9.62 KB
/
bucket.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
# --------------------------------------------------------------------------------------------------
# Configure the S3 bucket to store audit logs.
# --------------------------------------------------------------------------------------------------
locals {
use_external_bucket = var.use_external_audit_log_bucket
audit_log_bucket_id = local.use_external_bucket ? data.aws_s3_bucket.external[0].id : module.audit_log_bucket.this_bucket.id
audit_log_bucket_arn = local.use_external_bucket ? data.aws_s3_bucket.external[0].arn : module.audit_log_bucket.this_bucket.arn
audit_log_cloudtrail_destination = join("/", [local.audit_log_bucket_arn, trim(var.cloudtrail_s3_key_prefix, "/")])
audit_log_config_destination = join("/", [local.audit_log_bucket_arn, trim(var.config_s3_bucket_key_prefix, "/")])
audit_log_flow_logs_destination = join("/", [local.audit_log_bucket_arn, trim(var.vpc_flow_logs_s3_key_prefix, "/")])
flow_logs_use_s3 = var.vpc_flow_logs_destination_type == "s3"
}
# --------------------------------------------------------------------------------------------------
# Case 1. Use the external S3 bucket.
# --------------------------------------------------------------------------------------------------
data "aws_s3_bucket" "external" {
count = local.use_external_bucket ? 1 : 0
bucket = var.audit_log_bucket_name
}
# --------------------------------------------------------------------------------------------------
# Case 2. Create a new S3 bucket.
#
# Create a S3 bucket to store various audit logs.
# Bucket policies are derived from the default bucket policy and official AWS documents.
# --------------------------------------------------------------------------------------------------
module "audit_log_bucket" {
source = "./modules/secure-bucket"
bucket_name = var.audit_log_bucket_name
log_bucket_name = "${var.audit_log_bucket_name}-access-logs"
lifecycle_glacier_transition_days = var.audit_log_lifecycle_glacier_transition_days
force_destroy = var.audit_log_bucket_force_destroy
enabled = !local.use_external_bucket
tags = var.tags
depends_on = [module.s3_baseline]
}
data "aws_organizations_organization" "org" {
count = local.is_individual_account ? 0 : 1
}
# --------------------------------------------------------------------------------------------------
# Apply policies to enforce SSL connections.
# https://docs.aws.amazon.com/config/latest/developerguide/s3-bucket-ssl-requests-only.html
# --------------------------------------------------------------------------------------------------
data "aws_iam_policy_document" "audit_log_base" {
count = local.use_external_bucket ? 0 : 1
statement {
actions = ["s3:*"]
effect = "Deny"
resources = [
module.audit_log_bucket.this_bucket.arn,
"${module.audit_log_bucket.this_bucket.arn}/*"
]
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
principals {
type = "*"
identifiers = ["*"]
}
}
}
# --------------------------------------------------------------------------------------------------
# Apply policies for CloudTrail log delivery based on AWS CloudTrail User Guide.
# https://docs.aws.amazon.com/awscloudtrail/latest/userguide/create-s3-bucket-policy-for-cloudtrail.html
# --------------------------------------------------------------------------------------------------
data "aws_iam_policy_document" "audit_log_cloud_trail" {
count = local.use_external_bucket ? 0 : 1
source_json = data.aws_iam_policy_document.audit_log_base[0].json
statement {
sid = "AWSCloudTrailAclCheck20150319"
actions = ["s3:GetBucketAcl"]
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
resources = [module.audit_log_bucket.this_bucket.arn]
}
statement {
sid = "AWSCloudTrailWrite20150319"
actions = ["s3:PutObject"]
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
resources = concat(
["${local.audit_log_cloudtrail_destination}/AWSLogs/${var.aws_account_id}/*"],
local.is_master_account ? ["${local.audit_log_cloudtrail_destination}/AWSLogs/${data.aws_organizations_organization.org[0].id}/*"] : []
)
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
}
# --------------------------------------------------------------------------------------------------
# Apply policies for AWS Config log delivery based on AWS Config Developer Guide.
# https://docs.aws.amazon.com/config/latest/developerguide/s3-bucket-policy.html
# --------------------------------------------------------------------------------------------------
data "aws_iam_policy_document" "audit_log_config" {
count = local.use_external_bucket ? 0 : 1
source_json = data.aws_iam_policy_document.audit_log_cloud_trail[0].json
statement {
sid = "AWSConfigBucketPermissionsCheck"
actions = ["s3:GetBucketAcl"]
principals {
type = "Service"
identifiers = ["config.amazonaws.com"]
}
resources = [module.audit_log_bucket.this_bucket.arn]
}
statement {
sid = "AWSConfigBucketExistenceCheck"
actions = ["s3:ListBucket"]
principals {
type = "Service"
identifiers = ["config.amazonaws.com"]
}
resources = [module.audit_log_bucket.this_bucket.arn]
}
statement {
sid = "AWSConfigBucketDelivery"
actions = ["s3:PutObject"]
principals {
type = "Service"
identifiers = ["config.amazonaws.com"]
}
resources = concat(
["${local.audit_log_config_destination}/AWSLogs/${var.aws_account_id}/Config/*"],
local.is_master_account ? [for account in var.member_accounts : "${local.audit_log_config_destination}/AWSLogs/${account.account_id}/Config/*"] : []
)
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
dynamic "statement" {
for_each = local.is_master_account && length(var.member_accounts) > 0 ? [var.member_accounts] : []
content {
sid = "AWSConfigBucketPermissionsCheckForMemberAccounts"
principals {
type = "AWS"
identifiers = [for account in statement.value : "arn:aws:iam::${account.account_id}:root"]
}
actions = ["s3:GetBucketAcl"]
resources = [module.audit_log_bucket.this_bucket.arn]
}
}
dynamic "statement" {
for_each = local.is_master_account && length(var.member_accounts) > 0 ? [var.member_accounts] : []
content {
sid = "AWSConfigBucketExistenceCheckForMemberAccounts"
principals {
type = "AWS"
identifiers = [for account in statement.value : "arn:aws:iam::${account.account_id}:root"]
}
actions = ["s3:ListBucket", "s3:GetBucketLocation"]
resources = [module.audit_log_bucket.this_bucket.arn]
}
}
dynamic "statement" {
for_each = local.is_master_account && length(var.member_accounts) > 0 ? [var.member_accounts] : []
content {
sid = "AWSConfigBucketDeliveryForMemberAccounts"
principals {
type = "AWS"
identifiers = [for account in statement.value : "arn:aws:iam::${account.account_id}:root"]
}
actions = ["s3:PutObject"]
resources = [for account in statement.value : "${local.audit_log_config_destination}/AWSLogs/${account.account_id}/Config/*"]
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
}
}
# --------------------------------------------------------------------------------------------------
# Apply policies for AWS Config log delivery based on Amazon Virtual Private Cloud User Guide.
# This policy is necessary only when the log destination of VPC Flow Logs is set to S3.
# https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs-s3.html#flow-logs-s3-permissions
# --------------------------------------------------------------------------------------------------
data "aws_iam_policy_document" "audit_log_flow_logs" {
count = !local.use_external_bucket && local.flow_logs_use_s3 ? 1 : 0
source_json = data.aws_iam_policy_document.audit_log_config[0].json
statement {
sid = "AWSLogDeliveryAclCheck"
actions = ["s3:GetBucketAcl"]
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
resources = [module.audit_log_bucket.this_bucket.arn]
}
statement {
sid = "AWSLogDeliveryWrite"
actions = ["s3:PutObject"]
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
resources = concat(
["${local.audit_log_flow_logs_destination}/AWSLogs/${var.aws_account_id}/*"],
local.is_master_account ? [for account in var.member_accounts : "${local.audit_log_flow_logs_destination}/AWSLogs/${account.account_id}/*"] : []
)
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
}
data "aws_iam_policy_document" "audit_log" {
count = local.use_external_bucket ? 0 : 1
source_json = local.flow_logs_use_s3 ? data.aws_iam_policy_document.audit_log_flow_logs[0].json : data.aws_iam_policy_document.audit_log_config[0].json
override_json = var.audit_log_bucket_custom_policy_json
}
resource "aws_s3_bucket_policy" "audit_log" {
count = local.use_external_bucket ? 0 : 1
bucket = module.audit_log_bucket.this_bucket.id
policy = data.aws_iam_policy_document.audit_log[0].json
}