lom.rego
  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
# LOM policy evaluations =======================================================
# Controls: LOM-001 through LOM-009

package controls.aws.lom

import data.common.exemptions as exemptions
import data.common.outputs as outputs

# Scope guard ------------------------------------------------------------------

_config_in_scope if {
	some _, rc in input.resource_changes
	rc.type in {"aws_config_configuration_recorder", "aws_config_delivery_channel"}
}

# LOM-001 — CloudTrail must be multi-region and capture all management events --

deny contains msg if {
	policy_id := "ICP-TF-AWS-LOM-001"

	some _, rc in input.resource_changes
	rc.type == "aws_cloudtrail"
	rc.change.actions[_] in ["create", "update"]

	not _cloudtrail_compliant(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("CloudTrail trail '%s' is not multi-region or does not capture all management events", [rc.name]),
	)
}

_cloudtrail_compliant(after) if {
	after.is_multi_region_trail == true
	after.include_global_service_events == true
	some sel in after.event_selector
	sel.read_write_type == "All"
	sel.include_management_events == true
}

# LOM-002 — CloudTrail log file integrity validation must be enabled -----------

deny contains msg if {
	policy_id := "ICP-TF-AWS-LOM-002"

	some _, rc in input.resource_changes
	rc.type == "aws_cloudtrail"
	rc.change.actions[_] in ["create", "update"]

	rc.change.after.enable_log_file_validation != true

	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("CloudTrail trail '%s' does not have log file validation enabled", [rc.name]),
	)
}

# LOM-003 — CloudTrail logs must be encrypted with a CMK ----------------------

deny contains msg if {
	policy_id := "ICP-TF-AWS-LOM-003"

	some _, rc in input.resource_changes
	rc.type == "aws_cloudtrail"
	rc.change.actions[_] in ["create", "update"]

	not _has_kms_key(rc.change.after.kms_key_id)

	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("CloudTrail trail '%s' is not encrypted with a KMS CMK", [rc.name]),
	)
}

# LOM-004 — CloudWatch Log Groups must be encrypted with a CMK ----------------

deny contains msg if {
	policy_id := "ICP-TF-AWS-LOM-004"

	some _, rc in input.resource_changes
	rc.type == "aws_cloudwatch_log_group"
	rc.change.actions[_] in ["create", "update"]

	not _has_kms_key(rc.change.after.kms_key_id)

	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("CloudWatch Log Group '%s' is not encrypted with a KMS CMK", [rc.name]),
	)
}

# LOM-005 — CloudWatch Log Groups must define an explicit retention period -----
# HCA minimum retention: 365 days

deny contains msg if {
	policy_id := "ICP-TF-AWS-LOM-005"

	some _, rc in input.resource_changes
	rc.type == "aws_cloudwatch_log_group"
	rc.change.actions[_] in ["create", "update"]

	not _retention_compliant(rc.change.after.retention_in_days)

	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("CloudWatch Log Group '%s' does not have a retention period of at least 365 days", [rc.name]),
	)
}

_retention_compliant(days) if {
	days != null
	days >= 365
}

# LOM-006 — VPC Flow Logs must be enabled for every VPC -----------------------

deny contains msg if {
	policy_id := "ICP-TF-AWS-LOM-006"

	some _, rc in input.resource_changes
	rc.type == "aws_vpc"
	rc.change.actions[_] in ["create", "update"]

	not _flow_log_exists_by_name(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("VPC '%s' has no flow log configured", [rc.name]),
	)
}

_flow_log_exists_by_name(vpc_name) if {
	some r in _all_configuration_resources
	r.type == "aws_flow_log"
	some ref in r.expressions.vpc_id.references
	ref == concat(".", ["aws_vpc", vpc_name])
}

_all_configuration_resources contains r if {
	some r in input.configuration.root_module.resources
}

_all_configuration_resources contains r if {
	some _, mc in input.configuration.root_module.module_calls
	some r in mc.module.resources
}

# LOM-007 — ALB/NLB must have access logging enabled --------------------------

deny contains msg if {
	policy_id := "ICP-TF-AWS-LOM-007"

	some _, rc in input.resource_changes
	rc.type == "aws_lb"
	rc.change.actions[_] in ["create", "update"]

	not _access_logging_enabled(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("Load balancer '%s' does not have access logging enabled", [rc.name]),
	)
}

_access_logging_enabled(after) if {
	some log in after.access_logs
	log.enabled == true
	log.bucket != null
	log.bucket != ""
}

# LOM-008 — RDS DB instances must export engine logs to CloudWatch -------------

deny contains msg if {
	policy_id := "ICP-TF-AWS-LOM-008"

	some _, rc in input.resource_changes
	rc.type == "aws_db_instance"
	rc.change.actions[_] in ["create", "update"]

	not _has_log_exports(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("RDS instance '%s' has no CloudWatch log exports configured", [rc.name]),
	)
}

_has_log_exports(after) if {
	count(after.enabled_cloudwatch_logs_exports) > 0
}

# LOM-009 — AWS Config recorder and delivery channel must be defined ----------

deny contains msg if {
	policy_id := "ICP-TF-AWS-LOM-009"

	_config_in_scope
	not _config_recorder_exists
	not _config_delivery_channel_exists

	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,
		"No aws_config_configuration_recorder or aws_config_delivery_channel found in plan",
	)
}

deny contains msg if {
	policy_id := "ICP-TF-AWS-LOM-009"

	_config_in_scope
	_config_recorder_exists
	not _config_delivery_channel_exists

	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,
		"aws_config_configuration_recorder exists but aws_config_delivery_channel is missing",
	)
}

deny contains msg if {
	policy_id := "ICP-TF-AWS-LOM-009"

	_config_in_scope
	not _config_recorder_exists
	_config_delivery_channel_exists

	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,
		"aws_config_delivery_channel exists but aws_config_configuration_recorder is missing",
	)
}

_config_recorder_exists if {
	some _, rc in input.resource_changes
	rc.type == "aws_config_configuration_recorder"
	rc.change.actions[_] in ["create", "update"]
}

_config_delivery_channel_exists if {
	some _, rc in input.resource_changes
	rc.type == "aws_config_delivery_channel"
	rc.change.actions[_] in ["create", "update"]
}

# Helpers ----------------------------------------------------------------------

_has_kms_key(key) if {
	key != null
	key != ""
}