-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
318 lines (263 loc) · 8.96 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
# Terraform module which creates S3 Bucket resources for Load Balancer Access Logs on AWS.
module "s3-bucket" {
count = var.existing_bucket_name == "" ? 1 : 0
source = "github.com/ministryofjustice/modernisation-platform-terraform-s3-bucket?ref=8688bc15a08fbf5a4f4eef9b7433c5a417df8df1" # v7.0.0
providers = {
aws.bucket-replication = aws.bucket-replication
}
bucket_prefix = "${var.application_name}-ssm${var.suffix}"
bucket_policy = [data.aws_iam_policy_document.bucket_policy.json]
replication_enabled = false
versioning_enabled = true
force_destroy = var.force_destroy_bucket
lifecycle_rule = [
{
id = "main"
enabled = "Enabled"
prefix = ""
tags = {
rule = "log"
autoclean = "true"
}
transition = [
{
days = 90
storage_class = "STANDARD_IA"
}, {
days = 365
storage_class = "GLACIER"
}
]
expiration = {
days = 730
}
noncurrent_version_transition = [
{
days = 90
storage_class = "STANDARD_IA"
}, {
days = 365
storage_class = "GLACIER"
}
]
noncurrent_version_expiration = {
days = 730
}
}
]
tags = var.tags
}
data "aws_iam_policy_document" "bucket_policy" {
# Ignore this check on tfsec - it causes a fail on resources *. The resource is required for patching purposes
#tfsec:ignore:aws-iam-no-policy-wildcards
statement {
effect = "Allow"
actions = [
"s3:PutObject"
]
resources = [var.existing_bucket_name != "" ? "arn:aws:s3:::${var.existing_bucket_name}/${var.application_name}/AWSLogs/${var.account_number}/*" : "${module.s3-bucket[0].bucket.arn}/${var.application_name}/AWSLogs/${var.account_number}/*"]
principals {
type = "AWS"
identifiers = [data.aws_elb_service_account.default.arn]
}
}
statement {
sid = "AWSLogDeliveryWrite"
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
actions = [
"s3:PutObject"
]
resources = [var.existing_bucket_name != "" ? "arn:aws:s3:::${var.existing_bucket_name}/${var.application_name}/AWSLogs/${var.account_number}/*" : "${module.s3-bucket[0].bucket.arn}/${var.application_name}/AWSLogs/${var.account_number}/*"]
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = [
"bucket-owner-full-control"
]
}
}
statement {
sid = "AWSLogDeliveryAclCheck"
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
actions = [
"s3:GetBucketAcl"
]
resources = [
var.existing_bucket_name != "" ? "arn:aws:s3:::${var.existing_bucket_name}" : module.s3-bucket[0].bucket.arn
]
}
}
data "aws_elb_service_account" "default" {}
###################################################################
############################# SSM #################################
###################################################################
###### IAM #####
data "aws_iam_policy_document" "ssm-admin-policy-doc" {
# Not relevant to what we are doing. This sets a high level access policy
#checkov:skip=CKV_AWS_110: "Ensure IAM policies does not allow privilege escalation"
#checkov:skip=CKV_AWS_109: "Ensure IAM policies does not allow permissions management / resource exposure without constraints"
#checkov:skip=CKV_AWS_107: "Ensure IAM policies does not allow credentials exposure"
#checkov:skip=CKV_AWS_108: "Ensure IAM policies does not allow data exfiltration"
#checkov:skip=CKV_AWS_356: "Ensure no IAM policies documents allow "*" as a statement's resource for restrictable actions"
#checkov:skip=CKV_AWS_111: "Ensure IAM policies does not allow write access without constraints"
#checkov:skip=CKV_TF_1: "Ensure Terraform module sources use a commit hash"
statement {
# Ignore this check on tfsec - it causes a fail on resources *. The resource is required for patching purposes
#tfsec:ignore:aws-iam-no-policy-wildcards
actions = ["s3:*",
"ec2:*",
"ssm:*",
"cloudwatch:*",
"cloudformation:*",
"iam:*",
"lambda:*"
]
# Ignore this check on tfsec - it causes a fail on resources *. The resource is required for patching purposes
#tfsec:ignore:aws-iam-no-policy-wildcards
resources = ["*"]
}
}
resource "aws_iam_policy" "ssm-patching-iam-policy" {
name = "ssm-patching-iam-policy${var.suffix}"
description = "IAM Policy for the AWS-PatchAsgInstance automation script that runs as part of the module"
path = "/"
policy = data.aws_iam_policy_document.ssm-admin-policy-doc.json
}
resource "aws_iam_role" "ssm-patching-iam-role" {
# Ignore this check on tfsec - it causes a fail on resources *. The resource is required for patching purposes
#tfsec:ignore:aws-iam-no-policy-wildcards
name = "ssm-patching-iam-role${var.suffix}"
assume_role_policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {
"Service" : [
"ssm.amazonaws.com",
"iam.amazonaws.com"
]
},
"Action" : "sts:AssumeRole"
}
]
})
}
resource "aws_iam_role_policy_attachment" "ssm-admin-automation" {
role = aws_iam_role.ssm-patching-iam-role.name
policy_arn = aws_iam_policy.ssm-patching-iam-policy.arn
}
###### ssm maintenance window #####
resource "aws_ssm_maintenance_window" "ssm-maintenance-window" {
name = "${var.application_name}-maintenance-window${var.suffix}"
schedule = var.patch_schedule
duration = 4 # These could be made into variables if required, however 4 hours seems a long enough duration for patching.
cutoff = 3
}
###### ssm maintenance target #####
resource "aws_ssm_maintenance_window_target" "ssm-maintenance-window-target" {
window_id = aws_ssm_maintenance_window.ssm-maintenance-window.id
name = "maintenance-window-target${var.suffix}"
description = "This is a maintenance window target"
resource_type = "INSTANCE"
targets {
key = "tag:${var.patch_key}"
values = [var.patch_tag]
}
}
###### ssm automation task #####
resource "aws_ssm_maintenance_window_task" "ssm-maintenance-window-automation-task" {
name = "${var.application_name}-automation-patching-task${var.suffix}"
description = "${var.application_name}-automation-patching-task${var.suffix}"
max_concurrency = 20
max_errors = 10
priority = 1
task_type = "AUTOMATION"
task_arn = "AWS-PatchInstanceWithRollback"
window_id = aws_ssm_maintenance_window.ssm-maintenance-window.id
service_role_arn = aws_iam_role.ssm-patching-iam-role.arn
targets {
key = "WindowTargetIds"
values = aws_ssm_maintenance_window_target.ssm-maintenance-window-target[*].id
}
task_invocation_parameters {
automation_parameters {
document_version = "$LATEST"
parameter {
name = "InstanceId"
values = ["{{RESOURCE_ID}}"]
}
parameter {
name = "ReportS3Bucket"
values = [var.existing_bucket_name != "" ? "arn:aws:s3:::${var.existing_bucket_name}" : "${module.s3-bucket[0].bucket.id}"]
}
}
}
}
########### Optionals ############
###### Resource Group #####
resource "aws_resourcegroups_group" "patch-resource-group" {
name = "${var.application_name}-patch-group${var.suffix}"
resource_query {
query = <<JSON
{
"ResourceTypeFilters":[
"AWS::EC2::Instance"
],
"TagFilters":[
{
"Key":"Patching",
"Values":["Yes"]
}
]
}
JSON
}
}
###### Approval rule #####
#resource "aws_ssm_patch_baseline" "patch-baseline-poc" {
# name = "${var.application_name}-baseline"
# operating_system = var.operating_system
#
# approval_rule {
# approve_after_days = var.approval_days
# compliance_level = var.compliance_level
#
# patch_filter {
# key = "CLASSIFICATION"
# values = var.patch_classification
# }
# }
#}
resource "aws_ssm_patch_baseline" "ssm-patch-baseline" {
name = "${var.application_name}-baseline${var.suffix}"
description = "${var.application_name}-baseline${var.suffix}"
operating_system = var.operating_system
rejected_patches = var.rejected_patches
approval_rule {
approve_after_days = var.approval_days
compliance_level = var.compliance_level
patch_filter {
key = "PRODUCT"
values = var.product
}
patch_filter {
key = "CLASSIFICATION"
values = var.patch_classification
}
patch_filter {
key = var.operating_system == "WINDOWS" ? "MSRC_SEVERITY" : "SEVERITY"
values = var.severity
}
}
}
resource "aws_ssm_default_patch_baseline" "ssm-default-patch-baseline" {
baseline_id = aws_ssm_patch_baseline.ssm-patch-baseline.id
operating_system = var.operating_system
}