generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 4
/
replication.tf
288 lines (243 loc) · 8.26 KB
/
replication.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
locals {
replication_bucket = "arn:aws:s3:::${var.replication_bucket}/*"
}
data "aws_caller_identity" "current" {}
resource "aws_s3_bucket_notification" "bucket_notification_replication" {
count = var.replication_enabled && var.notification_enabled ? 1 : 0
provider = aws.bucket-replication
bucket = aws_s3_bucket.replication[count.index]
topic {
topic_arn = var.notification_sns_arn
events = var.notification_events
}
}
# Replication S3 bucket, to replicate to (rather than from)
# Logging not deemed required for replication bucket
# tfsec:ignore:aws-s3-enable-bucket-logging
resource "aws_s3_bucket" "replication" {
#checkov:skip=CKV_AWS_144: "Replication not required on replication bucket"
#checkov:skip=CKV_AWS_18: "Logging handled in logging configuration resource"
#checkov:skip=CKV_AWS_21: "Versioning handled in versioning configuration resource"
#checkov:skip=CKV_AWS_145: "Encryption handled in encryption configuration resource"
count = var.replication_enabled ? 1 : 0
provider = aws.bucket-replication
bucket = var.bucket_name != null ? "${var.bucket_name}-replication" : null
bucket_prefix = var.bucket_prefix != null ? "${var.bucket_prefix}-replication" : null
force_destroy = var.force_destroy
tags = var.tags
}
resource "aws_s3_bucket_ownership_controls" "replication" {
count = var.replication_enabled ? 1 : 0
provider = aws.bucket-replication
bucket = aws_s3_bucket.replication[0].id
rule {
object_ownership = var.ownership_controls
}
}
# Configure bucket ACL
resource "aws_s3_bucket_acl" "replication" {
count = var.replication_enabled && var.ownership_controls != "BucketOwnerEnforced" ? 1 : 0
provider = aws.bucket-replication
bucket = length(aws_s3_bucket.replication) > 0 ? aws_s3_bucket.replication[0].id : ""
acl = var.acl
depends_on = [
aws_s3_bucket_ownership_controls.replication
]
}
# Configure bucket lifecycle rules
resource "aws_s3_bucket_lifecycle_configuration" "replication" {
#checkov:skip=CKV_AWS_300: "Ensure S3 lifecycle configuration sets period for aborting failed uploads"
count = var.replication_enabled ? 1 : 0
provider = aws.bucket-replication
bucket = aws_s3_bucket.replication[count.index].id
rule {
id = "main"
status = "Enabled"
transition {
days = 90
storage_class = "STANDARD_IA"
}
transition {
days = 365
storage_class = "GLACIER"
}
expiration {
days = 730
}
noncurrent_version_transition {
noncurrent_days = 90
storage_class = "STANDARD_IA"
}
noncurrent_version_transition {
noncurrent_days = 365
storage_class = "GLACIER"
}
noncurrent_version_expiration {
noncurrent_days = 730
}
}
}
# Block public access policies to the replication bucket
resource "aws_s3_bucket_public_access_block" "replication" {
count = var.replication_enabled ? 1 : 0
provider = aws.bucket-replication
bucket = aws_s3_bucket.replication[count.index].bucket
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
# Attach policies to the S3 bucket
# This ensures every bucket created via this module
# doesn't allow any actions that aren't over SecureTransport methods (i.e. HTTP)
resource "aws_s3_bucket_policy" "replication" {
count = var.replication_enabled ? 1 : 0
provider = aws.bucket-replication
bucket = aws_s3_bucket.replication[count.index].id
policy = data.aws_iam_policy_document.replication[count.index].json
# Create the Public Access Block before the policy is added
depends_on = [aws_s3_bucket_public_access_block.replication]
}
resource "aws_s3_bucket_server_side_encryption_configuration" "replication" {
#checkov:skip=CKV2_AWS_67: "Ensure AWS S3 bucket encrypted with Customer Managed Key (CMK) has regular rotation"
count = var.replication_enabled ? 1 : 0
provider = aws.bucket-replication
bucket = aws_s3_bucket.replication[count.index].id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = var.sse_algorithm
kms_master_key_id = (var.custom_replication_kms_key != "") ? var.custom_replication_kms_key : ""
}
}
}
resource "aws_s3_bucket_versioning" "replication" {
count = var.replication_enabled ? 1 : 0
provider = aws.bucket-replication
bucket = aws_s3_bucket.replication[count.index].id
versioning_configuration {
status = "Enabled"
}
}
data "aws_iam_policy_document" "replication" {
count = var.replication_enabled ? 1 : 0
provider = aws.bucket-replication
statement {
effect = "Deny"
actions = ["s3:*"]
resources = [
aws_s3_bucket.replication[count.index].arn,
"${aws_s3_bucket.replication[count.index].arn}/*"
]
principals {
identifiers = ["*"]
type = "AWS"
}
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
}
}
# S3 bucket replication: role
resource "aws_iam_role" "replication_role" {
provider = aws.bucket-replication
count = var.replication_enabled ? 1 : 0
name = "AWSS3BucketReplication${var.suffix_name}"
assume_role_policy = data.aws_iam_policy_document.s3-assume-role-policy.json
tags = var.tags
}
# S3 bucket replication: assume role policy
data "aws_iam_policy_document" "s3-assume-role-policy" {
version = "2012-10-17"
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["s3.amazonaws.com"]
}
}
}
resource "aws_iam_policy" "replication_policy" {
# name = "AWSS3BucketReplicatioPolicy${var.suffix_name}"
count = var.replication_enabled ? 1 : 0
provider = aws.bucket-replication
name = "AWSS3BucketReplication${var.suffix_name}"
policy = data.aws_iam_policy_document.replication-policy.json
}
# S3 bucket replication: role policy
# tfsec:ignore:aws-iam-no-policy-wildcards
data "aws_iam_policy_document" "replication-policy" {
version = "2012-10-17"
statement {
effect = "Allow"
actions = [
"s3:GetReplicationConfiguration",
"s3:ListBucket"
]
resources = [aws_s3_bucket.default.arn]
}
statement {
effect = "Allow"
actions = [
"s3:GetObjectVersion",
"s3:GetObjectVersionAcl",
"s3:GetObjectVersionForReplication",
"s3:GetObjectLegalHold",
"s3:GetObjectRetention",
"s3:GetObjectVersionTagging"
]
resources = ["${aws_s3_bucket.default.arn}/*"]
}
statement {
effect = "Allow"
actions = [
"s3:ReplicateObject",
"s3:ReplicateDelete",
"s3:ReplicateTags",
"s3:GetObjectVersionTagging",
"s3:ObjectOwnerOverrideToBucketOwner"
]
resources = [var.replication_bucket != "" ? local.replication_bucket : "*"]
condition {
test = "StringLikeIfExists"
variable = "s3:x-amz-server-side-encryption"
values = [
"aws:kms",
"AES256"
]
}
}
}
resource "aws_iam_role_policy_attachment" "replication" {
count = var.replication_enabled ? 1 : 0
provider = aws.bucket-replication
role = aws_iam_role.replication_role[count.index].name
policy_arn = aws_iam_policy.replication_policy[count.index].arn
}
resource "aws_s3_bucket_replication_configuration" "default" {
for_each = var.replication_enabled ? toset(["run"]) : []
bucket = aws_s3_bucket.default.id
role = aws_iam_role.replication_role[0].arn
rule {
id = "SourceToDestinationReplication"
status = var.replication_enabled ? "Enabled" : "Disabled"
priority = 0
destination {
bucket = var.replication_enabled ? aws_s3_bucket.replication[0].arn : aws_s3_bucket.replication[0].arn
storage_class = "STANDARD"
encryption_configuration {
replica_kms_key_id = (var.custom_replication_kms_key != "") ? var.custom_replication_kms_key : "arn:aws:kms:${var.replication_region}:${data.aws_caller_identity.current.account_id}:alias/aws/s3"
}
}
source_selection_criteria {
sse_kms_encrypted_objects {
status = (var.replication_enabled != false) ? "Enabled" : "Disabled"
}
}
}
depends_on = [
aws_s3_bucket_versioning.default
]
}