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
|
# GAC policy evaluations =======================================================
# Unique GAC Controls:
# GAC-003, GAC-008, GAC-012, GAC-013, GAC-014, GAC-015, GAC-016, GAC-022,
# GAC-023, GAC-025, GAC-026
#
# Overlap GAC controls from existing policies:
# GAC-001 → STR-001 S3 Block Public Access
# GAC-002 → STR-004 S3 Server-Side Encryption
# GAC-004 → STR-005 S3 TLS-Only Access
# GAC-005 → CMP-009 EBS Volume Encryption
# GAC-006 → CMP-010 EBS Default Encryption
# GAC-007 → CMP-001 IMDSv2 on EC2
# GAC-009 → DAT-002 RDS Storage Encryption
# GAC-010 → DAT-001 RDS Not Publicly Accessible
# GAC-011 → DAT-004 RDS Deletion Protection
# GAC-017 → CMP-005 Unrestricted SSH Ingress
# GAC-018 → CMP-006 Unrestricted RDP Ingress
# GAC-019 → CMP-008 Default Security Group Rules
# GAC-020 → NCD-007 ALB HTTP to HTTPS Redirect
# GAC-021 → NCD-008 CloudFront HTTPS to Viewers
# GAC-024 → DAT-006 DynamoDB Point-in-Time Recovery
package controls.aws.gac
import data.common.definitions as defs
import data.common.exemptions as exemptions
import data.common.outputs as outputs
import data.common.policydoc as policydoc
# GAC-003 — S3 buckets must have versioning enabled ---------------------------
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-003"
some _, rc in input.resource_changes
rc.type == "aws_s3_bucket"
rc.change.actions[_] in ["create", "update"]
not _versioning_exists(rc.name)
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("S3 bucket '%s' does not have versioning enabled", [rc.name]),
)
}
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-003"
some _, rc in input.resource_changes
rc.type == "aws_s3_bucket_versioning"
rc.change.actions[_] in ["create", "update"]
some vc in rc.change.after.versioning_configuration
vc.status != "Enabled"
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("S3 bucket versioning '%s' is not set to Enabled", [rc.name]),
)
}
_versioning_exists(bucket_name) if {
some r in input.configuration.root_module.resources
r.type == "aws_s3_bucket_versioning"
some ref in r.expressions.bucket.references
ref == concat(".", ["aws_s3_bucket", bucket_name])
}
# GAC-008 — EBS snapshots must not be shared publicly -------------------------
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-008"
some _, rc in input.resource_changes
rc.type == "aws_snapshot_create_volume_permission"
rc.change.actions[_] in ["create", "update"]
rc.change.after.account_id == "all"
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("EBS snapshot permission '%s' shares snapshot publicly (account_id = all)", [rc.name]),
)
}
# GAC-012 — RDS DB instances must have adequate backup retention --------------
# RDS Automated backups is capped at 35 days. HCA's requirement of 365 days
# shall be set up with AWS Backups
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-012"
some _, rc in input.resource_changes
rc.type == "aws_db_instance"
rc.change.actions[_] in ["create", "update"]
not _adequate_backup_retention(rc.change.after.backup_retention_period)
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("RDS instance '%s' does not have max retention configured (expected 35 days)", [rc.name]),
)
}
_adequate_backup_retention(days) if {
days != null
days >= 35
}
# GAC-013 — RDS snapshots must not be shared publicly -------------------------
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-013"
some _, rc in input.resource_changes
rc.type in {"aws_db_snapshot", "aws_db_cluster_snapshot"}
rc.change.actions[_] in ["create", "update"]
"all" in rc.change.after.shared_accounts
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("RDS snapshot '%s' is shared publicly (shared_accounts contains 'all')", [rc.name]),
)
}
# GAC-014 — IAM policies must not grant full administrative privileges ---------
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-014"
some _, rc in input.resource_changes
rc.type in {"aws_iam_policy", "aws_iam_role_policy", "aws_iam_user_policy", "aws_iam_group_policy"}
rc.change.actions[_] in ["create", "update"]
doc := json.unmarshal(rc.change.after.policy)
some stmt in doc.Statement
stmt.Effect == "Allow"
some action in policydoc.as_list(stmt.Action)
action == "*"
some resource in policydoc.as_list(stmt.Resource)
resource == "*"
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("'%s.%s' grants full administrative privileges (Action * on Resource *)", [rc.type, rc.name]),
)
}
# GAC-015 — IAM roles must not use wildcard trust principals or actions --------
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-015"
some _, rc in input.resource_changes
rc.type == "aws_iam_role"
rc.change.actions[_] in ["create", "update"]
doc := json.unmarshal(rc.change.after.assume_role_policy)
some stmt in doc.Statement
stmt.Effect == "Allow"
policydoc.public_principal(stmt)
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("IAM role '%s' trust policy uses a wildcard principal", [rc.name]),
)
}
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-015"
some _, rc in input.resource_changes
rc.type == "aws_iam_role"
rc.change.actions[_] in ["create", "update"]
doc := json.unmarshal(rc.change.after.assume_role_policy)
some stmt in doc.Statement
stmt.Effect == "Allow"
some action in policydoc.as_list(stmt.Action)
action == "*"
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("IAM role '%s' trust policy uses a wildcard action", [rc.name]),
)
}
# GAC-016 — IAM account password policy must enforce strong requirements -------
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-016"
some _, rc in input.resource_changes
rc.type == "aws_iam_account_password_policy"
rc.change.actions[_] in ["create", "update"]
not _strong_password_policy(rc.change.after)
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("IAM account password policy '%s' does not meet strong requirements", [rc.name]),
)
}
_strong_password_policy(after) if {
after.minimum_password_length >= 14
after.require_symbols == true
after.require_numbers == true
after.require_uppercase_characters == true
after.require_lowercase_characters == true
after.password_reuse_prevention >= 24
after.max_password_age <= 90
}
# GAC-022 — CloudFront must use modern TLS minimum protocol version ------------
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-022"
some _, rc in input.resource_changes
rc.type == "aws_cloudfront_distribution"
rc.change.actions[_] in ["create", "update"]
not _modern_tls(rc.change.after)
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("CloudFront distribution '%s' does not use a modern TLS minimum protocol version", [rc.name]),
)
}
_modern_tls(after) if {
some cert in after.viewer_certificate
_tls_version_ok(cert.minimum_protocol_version)
}
_tls_version_ok(v) if startswith(v, "TLSv1.2")
_tls_version_ok(v) if startswith(v, "TLSv1.3")
# GAC-023 — EKS cluster API endpoint public access must be disabled or restricted
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-023"
some _, rc in input.resource_changes
rc.type == "aws_eks_cluster"
rc.change.actions[_] in ["create", "update"]
not _eks_endpoint_safe(rc.change.after)
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("EKS cluster '%s' has public API endpoint access enabled without CIDR restrictions", [rc.name]),
)
}
_eks_endpoint_safe(after) if {
some vpc in after.vpc_config
vpc.endpoint_public_access == false
}
_eks_endpoint_safe(after) if {
some vpc in after.vpc_config
vpc.endpoint_public_access == true
not "0.0.0.0/0" in vpc.public_access_cidrs
}
# GAC-025 — Provider default_tags must include mandatory tags ------------------
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-025"
some name, provider in input.configuration.provider_config
startswith(name, "aws")
not provider.expressions.default_tags[0].tags.constant_value
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("AWS provider '%s' has no default_tags configured", [name]),
)
}
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-025"
some name, provider in input.configuration.provider_config
startswith(name, "aws")
provider.expressions.default_tags[0].tags.constant_value
some tag in defs.mandatory_provider_tags
not tag in object.keys(provider.expressions.default_tags[0].tags.constant_value)
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("AWS provider '%s' is missing mandatory default tag '%s'", [name, tag]),
)
}
# GAC-026 — Taggable resources must carry all baseline tags --------------------
deny contains msg if {
policy_id := "ICP-TF-AWS-GAC-026"
some _, rc in input.resource_changes
rc.change.actions != ["delete"]
_is_taggable(rc)
real_tags := _as_object(object.get(rc.change.after, "tags", null))
problems := array.concat(
[sprintf("missing '%s'", [tag]) |
some tag in object.keys(defs.mandatory_baseline_tags)
not tag in object.keys(real_tags)
],
[sprintf("'%s' has invalid value '%s'", [tag, real_tags[tag]]) |
some tag, allowed in defs.mandatory_baseline_tags
tag in object.keys(real_tags)
not "*" in allowed
not real_tags[tag] in allowed
],
)
count(problems) > 0
meta := data.policies[policy_id]
meta.status == "active"
not exemptions.policy_exemption(policy_id)
not exemptions.severity_exemption(meta.severity)
msg := outputs.violation(
policy_id, meta,
sprintf("Resource '%s.%s' tag issues: %s", [rc.type, rc.name, concat("; ", sort(problems))]),
)
}
_is_taggable(rc) if {
"tags" in object.keys(rc.change.after)
}
_is_taggable(rc) if {
"tags" in object.keys(rc.change.after_unknown)
}
_as_object(tags) := tags if tags != null
_as_object(tags) := {} if tags == null
|