/
storage.sp
444 lines (427 loc) · 15.9 KB
/
storage.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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
query "storage_azure_defender_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'tier') = 'Standard' and (attributes_std ->> 'resource_type') = 'StorageAccounts' then 'ok'
else 'skip'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'tier') = 'Standard' and (attributes_std ->> 'resource_type') = 'StorageAccounts' then ' Azure defender enabled for StorageAccount(s)'
else ' Azure defender enabled for ' || (attributes_std ->> 'resource_type') || ''
end || '.' reason
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_security_center_subscription_pricing';
EOQ
}
query "storage_account_block_public_access" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'allow_blob_public_access') is null then 'ok'
when (attributes_std -> 'allow_blob_public_access')::bool then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'allow_blob_public_access') is null then ' does not allow public access to the blobs or containers'
when (attributes_std -> 'allow_blob_public_access')::bool then ' allows public access to all the blobs or containers'
else ' does not allow public access to the blobs or containers'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_queue_services_logging_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'queue_properties') is null or (attributes_std -> 'queue_properties' -> 'logging') is null then 'alarm'
when not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Read')::boolean or not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Write')::boolean or not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Delete')::boolean then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'queue_properties') is null or (attributes_std -> 'queue_properties' -> 'logging') is null then ' blob service logging not enabled'
when not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Read')::boolean or not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Write')::boolean or not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Delete')::boolean then ' blob service logging not enabled'
else ' blob service logging enabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_use_virtual_service_endpoint" {
sql = <<-EOQ
with storage_account_network_rules as (
select
name,
address,
type,
path,
start_line,
split_part((attributes_std ->> 'storage_account_name'), '.',2) as storage_account_name
from
terraform_resource
where
type = 'azurerm_storage_account_network_rules'
and (attributes_std ->> 'default_action') = 'Deny'
), storage_account_name as (
select
name,
address,
type,
path,
_ctx,
start_line
from
terraform_resource
where
type = 'azurerm_storage_account'
)
select
san.address as resource,
case
when sanr.address is null then 'alarm'
else 'ok'
end status,
split_part(san.address, '.', 2) || case
when sanr.address is null then ' does not use virtual service endpoint'
else ' uses virtual service endpoint'
end || '.' reason
${replace(local.tag_dimensions_qualifier_sql, "__QUALIFIER__", "san.")}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "san.")}
from
storage_account_name as san
left join storage_account_network_rules as sanr on sanr.storage_account_name = san.name;
EOQ
}
query "storage_account_encryption_at_rest_using_cmk" {
sql = <<-EOQ
select
address as resource,
case
when name in (select split_part((attributes_std ->> 'storage_account_id'), '.', 2) from terraform_resource where type = 'azurerm_storage_account_customer_managed_key') then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when name in (select split_part((attributes_std ->> 'storage_account_id'), '.', 2) from terraform_resource where type = 'azurerm_storage_account_customer_managed_key') then ' encrypted with CMK'
else ' not encrypted with CMK'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_infrastructure_encryption_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'infrastructure_encryption_enabled') is null then 'alarm'
when (attributes_std -> 'infrastructure_encryption_enabled')::bool then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'infrastructure_encryption_enabled') is null then ' ''infrastructure_encryption_enabled'' parameter not defined'
when (attributes_std -> 'infrastructure_encryption_enabled')::bool then ' infrastructure encryption enabled'
else ' infrastructure encryption disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_blob_containers_public_access_private" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'allow_blob_public_access') is null then 'ok'
when (attributes_std -> 'allow_blob_public_access')::bool then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'allow_blob_public_access') is null then ' does not allow public access to the blobs'
when (attributes_std -> 'allow_blob_public_access')::bool then ' allows public access to all the blobs'
else ' does not allow public access to the blobs'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_default_network_access_rule_denied" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'network_rules') is null then 'alarm'
when (attributes_std -> 'network_rules' ->> 'default_action') = 'Allow' then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'network_rules') is null then ' has no network rules defined'
when (attributes_std -> 'network_rules' ->> 'default_action') = 'Allow' then ' allows traffic from specific networks'
else ' allows network access'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_trusted_microsoft_services_enabled" {
sql = <<-EOQ
with storage_account_network_rules as (
select
name,
address,
type,
path,
start_line,
split_part((attributes_std ->> 'storage_account_name'), '.',2) as storage_account_name
from
terraform_resource
where
type = 'azurerm_storage_account_network_rules'
and (attributes_std ->> 'bypass') like '%AzureServices%'
), storage_account_name as (
select
name,
address,
type,
path,
_ctx,
start_line
from
terraform_resource
where
type = 'azurerm_storage_account'
)
select
san.type || ' ' || san.name as resource,
case
when sanr.name is null then 'alarm'
else 'ok'
end status,
split_part(san.address, '.', 2) || case
when sanr.name is null then ' trusted Microsoft services not enabled'
else ' trusted Microsoft services enabled'
end || '.' reason
${replace(local.tag_dimensions_qualifier_sql, "__QUALIFIER__", "san.")}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "san.")}
from
storage_account_name as san
left join storage_account_network_rules as sanr on sanr.storage_account_name = san.name;
EOQ
}
query "storage_account_blob_service_logging_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'queue_properties') is null or (attributes_std -> 'queue_properties' -> 'logging') is null then 'alarm'
when not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Read')::boolean or not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Write')::boolean or not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Delete')::boolean then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'queue_properties') is null or (attributes_std -> 'queue_properties' -> 'logging') is null then ' blob service logging not enabled'
when not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Read')::boolean or not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Write')::boolean or not (attributes_std -> 'queue_properties' -> 'logging' ->> 'Delete')::boolean then ' blob service logging not enabled'
else ' blob service logging enabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_uses_private_link" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'private_link_access') is null then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'private_link_access') is null then ' does not use private link'
else ' uses private link'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_encryption_scopes_encrypted_at_rest_with_cmk" {
sql = <<-EOQ
select
address as resource,
case
when name in (select split_part((attributes_std ->> 'storage_account_id'), '.', 2) from terraform_resource where type = 'azurerm_storage_encryption_scope' and (attributes_std ->> 'source') = 'Microsoft.KeyVault') then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when name in (select split_part((attributes_std ->> 'storage_account_id'), '.', 2) from terraform_resource where type = 'azurerm_storage_encryption_scope' and (attributes_std ->> 'source') = 'Microsoft.KeyVault') then ' encrypted with CMK'
else ' not encrypted with CMK'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_restrict_network_access" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'network_rules') is null then 'alarm'
when (attributes_std -> 'network_rules' ->> 'default_action') = 'Deny' then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'network_rules') is null then ' allows network access'
when (attributes_std -> 'network_rules' ->> 'default_action') = 'Deny' then ' restricts network access'
else ' allows network access'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_secure_transfer_required_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'enable_https_traffic_only') is null then 'ok'
when (attributes_std -> 'enable_https_traffic_only')::bool then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'enable_https_traffic_only') is null then ' encryption in transit enabled'
when (attributes_std -> 'enable_https_traffic_only')::bool then ' encryption in transit enabled'
else ' encryption in transit not enabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_uses_azure_resource_manager" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'resource_group_name') is null then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'resource_group_name') is null then ' does not use azure resource group manager'
else ' uses azure resource group manager'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_uses_latest_minimum_tls_version" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'min_tls_version') in ('TLS1_2', 'TLS1_3') then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'min_tls_version') in ('TLS1_2', 'TLS1_3') then ' use latest version of TLS encryption'
when (attributes_std ->> 'min_tls_version') is null then ' version of TLS encryption is not set'
else ' does not uses latest version of TLS encryption'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_account_replication_type_set" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'account_replication_type') in ('GRS', 'RAGRS', 'GZRS', 'RAGZRS') then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'account_replication_type') in ('GRS', 'RAGRS', 'GZRS', 'RAGZRS') then ' replication type set correctly'
when (attributes_std ->> 'account_replication_type') is null then ' replication type not set'
else ' replication type not set correctly'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_account';
EOQ
}
query "storage_container_restrict_public_access" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'container_access_type') = 'private' or (attributes_std ->> 'container_access_type') is null then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'container_access_type') = 'private' or (attributes_std ->> 'container_access_type') is null then ' not publicly accessible'
else ' publicly accessible'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_storage_container';
EOQ
}