-
Notifications
You must be signed in to change notification settings - Fork 52
/
backup.sp
313 lines (292 loc) · 11.6 KB
/
backup.sp
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
locals {
conformance_pack_backup_common_tags = merge(local.aws_compliance_common_tags, {
service = "AWS/Backup"
})
}
control "backup_recovery_point_manual_deletion_disabled" {
title = "Backup recovery points manual deletion should be disabled"
description = "Checks if a backup vault has an attached resource-based policy which prevents deletion of recovery points. The rule is non-compliant if the Backup Vault does not have resource-based policies or has policies without a suitable 'Deny' statement."
query = query.backup_recovery_point_manual_deletion_disabled
tags = merge(local.conformance_pack_backup_common_tags, {
cisa_cyber_essentials = "true"
ffiec = "true"
gxp_21_cfr_part_11 = "true"
gxp_eu_annex_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
soc_2 = "true"
})
}
control "backup_plan_min_retention_35_days" {
title = "Backup plan min frequency and min retention check"
description = "Checks if a backup plan has a backup rule that satisfies the required frequency and retention period(35 Days). The rule is non-compliant if recovery points are not created at least as often as the specified frequency or expire before the specified period."
query = query.backup_plan_min_retention_35_days
tags = merge(local.conformance_pack_backup_common_tags, {
cisa_cyber_essentials = "true"
fedramp_low_rev_4 = "true"
fedramp_moderate_rev_4 = "true"
ffiec = "true"
gxp_21_cfr_part_11 = "true"
gxp_eu_annex_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
soc_2 = "true"
})
}
control "backup_recovery_point_encryption_enabled" {
title = "Backup recovery points should be encrypted"
description = "Ensure that a recovery point is encrypted. The rule is non-compliant if the recovery point is not encrypted."
query = query.backup_recovery_point_encryption_enabled
tags = merge(local.conformance_pack_backup_common_tags, {
cisa_cyber_essentials = "true"
ffiec = "true"
gxp_21_cfr_part_11 = "true"
gxp_eu_annex_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
soc_2 = "true"
})
}
control "backup_recovery_point_min_retention_35_days" {
title = "Backup recovery points should not expire before retention period"
description = "Ensure a recovery point expires no earlier than after the specified period. The rule is non-compliant if the recovery point has a retention point less than 35 days."
query = query.backup_recovery_point_min_retention_35_days
tags = merge(local.conformance_pack_backup_common_tags, {
cisa_cyber_essentials = "true"
ffiec = "true"
gxp_21_cfr_part_11 = "true"
gxp_eu_annex_11 = "true"
hipaa_final_omnibus_security_rule_2013 = "true"
hipaa_security_rule_2003 = "true"
nist_800_171_rev_2 = "true"
nist_csf = "true"
pci_dss_v321 = "true"
soc_2 = "true"
})
}
control "backup_plan_region_configured" {
title = "Backup plan should exist in a region"
description = "Ensure that there exists at least one backup plan in a region. The rule is non-compliant if there are no backup plans in a region."
query = query.backup_plan_region_configured
tags = local.conformance_pack_backup_common_tags
}
control "backup_vault_region_configured" {
title = "Backup vaults should exist in a region"
description = "Ensure that there exists at least one backup vault in a region. The rule is non-compliant if there are no backup vaults in a region."
query = query.backup_vault_region_configured
tags = local.conformance_pack_backup_common_tags
}
control "backup_report_plan_configured" {
title = "Backup report plan should exist in a region where backup plan is enabled"
description = "Ensure that there is a minimum of one backup report plan in each region. The rule will be considered non-compliant if a region with backup plans does not have any backup report plans."
query = query.backup_report_plan_configured
tags = local.conformance_pack_backup_common_tags
}
query "backup_recovery_point_manual_deletion_disabled" {
sql = <<-EOQ
with recovery_point_manual_deletion_disabled as (
select
arn
from
aws_backup_vault,
jsonb_array_elements(policy -> 'Statement') as s
where
s ->> 'Effect' = 'Deny' and
s -> 'Action' @> '["backup:DeleteRecoveryPoint","backup:UpdateRecoveryPointLifecycle","backup:PutBackupVaultAccessPolicy"]'
and s ->> 'Resource' = '*'
group by
arn
)
select
v.arn as resource,
case
when d.arn is not null then 'ok'
else 'alarm'
end as status,
case
when d.arn is not null then v.title || ' recovery point manual deletion disabled.'
else v.title || ' recovery point manual deletion not disabled.'
end as reason
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "v.")}
from
aws_backup_vault as v
left join recovery_point_manual_deletion_disabled as d on v.arn = d.arn;
EOQ
}
query "backup_plan_min_retention_35_days" {
sql = <<-EOQ
with all_plans as (
select
arn,
r as Rules,
title,
region,
account_id,
_ctx
from
aws_backup_plan,
jsonb_array_elements(backup_plan -> 'Rules') as r
)
select
-- The resource ARN can be duplicate as we are checking all the associated rules to the backup-plan
-- Backup plans are composed of one or more backup rules.
-- https://docs.aws.amazon.com/aws-backup/latest/devguide/creating-a-backup-plan.html
r.arn as resource,
case
when r.Rules is null then 'alarm'
when r.Rules ->> 'Lifecycle' is null then 'ok'
when (r.Rules -> 'Lifecycle' ->> 'DeleteAfterDays')::int >= 35 then 'ok'
else 'alarm'
end as status,
case
when r.Rules is null then r.title || ' retention period not set.'
when r.Rules ->> 'Lifecycle' is null then (r.Rules ->> 'RuleName') || ' retention period set to never expire.'
else (r.Rules ->> 'RuleName') || ' retention period set to ' || (r.Rules -> 'Lifecycle' ->> 'DeleteAfterDays') || ' days.'
end as reason
${local.common_dimensions_sql}
from
all_plans as r;
EOQ
}
query "backup_recovery_point_encryption_enabled" {
sql = <<-EOQ
select
recovery_point_arn as resource,
case
when is_encrypted then 'ok'
else 'alarm'
end as status,
case
when is_encrypted then recovery_point_arn || ' encryption enabled.'
else recovery_point_arn || ' encryption disabled.'
end as reason
${local.common_dimensions_sql}
from
aws_backup_recovery_point;
EOQ
}
query "backup_recovery_point_min_retention_35_days" {
sql = <<-EOQ
select
recovery_point_arn as resource,
case
when (lifecycle -> 'DeleteAfterDays') is null then 'ok'
when (lifecycle -> 'DeleteAfterDays')::int >= 35 then 'ok'
else 'alarm'
end as status,
case
when (lifecycle -> 'DeleteAfterDays') is null then split_part(recovery_point_arn, ':', -1) || ' retention period set to never expire.'
else split_part(recovery_point_arn, ':', -1) || ' recovery point has a retention period of ' || (lifecycle -> 'DeleteAfterDays')::int || ' days.'
end as reason
${local.common_dimensions_sql}
from
aws_backup_recovery_point;
EOQ
}
query "backup_plan_region_configured" {
sql = <<-EOQ
with count_plans as (
select
region,
account_id,
count(*) as count
from
aws_backup_plan
group by
region,
account_id
)
select
'arn:' || r.partition || '::' || r.region || ':' || r.account_id as resource,
case
when cp.count > 0 then 'ok'
else 'alarm'
end as status,
case
when cp.count > 0 then cp.count || ' backup plan(s) exist in region ' || r.region || '.'
else 'No backup plans exist in region ' || r.region || '.'
end as reason
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "r.")}
from
aws_region as r
left join count_plans as cp on r.account_id = cp.account_id and r.region = cp.region;
EOQ
}
query "backup_vault_region_configured" {
sql = <<-EOQ
with count_vaults as (
select
region,
account_id,
count(*) as count
from
aws_backup_vault
group by
region,
account_id
)
select
'arn:' || r.partition || '::' || r.region || ':' || r.account_id as resource,
case
when v.count > 0 then 'ok'
else 'alarm'
end as status,
case
when v.count > 0 then v.count || ' backup vault(s) exist in region ' || r.region || '.'
else 'No backup vault exists in region ' || r.region || '.'
end as reason
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "r.")}
from
aws_region as r
left join count_vaults as v on r.account_id = v.account_id and r.region = v.region;
EOQ
}
query "backup_report_plan_configured" {
sql = <<-EOQ
with backup_plan_configured_regions as (
select
distinct region,
account_id
from
aws_backup_plan
group by
region,
account_id
), backup_report_plan_configured as (
select
distinct region,
account_id
from
aws_backup_report_plan
group by
region,
account_id
)
select
'arn:' || r.partition || '::' || r.region || ':' || r.account_id as resource,
case
when cp.region is not null and rp.region is not null then 'ok'
when cp.region is not null and rp.region is null then 'alarm'
else 'info'
end as status,
case
when cp.region is not null and rp.region is not null then 'Backup report plan(s) exist in region ' || r.region || '.'
when cp.region is not null and rp.region is null then 'No backup report plan(s) exist in region ' || r.region || '.'
else 'No backup plan(s) configured in region ' || r.region || '.'
end as reason
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "r.")}
from
aws_region as r
left join backup_plan_configured_regions as cp on r.account_id = cp.account_id and r.region = cp.region
left join backup_report_plan_configured as rp on r.account_id = rp.account_id and r.region = rp.region;
EOQ
}