/
containerregistry.sp
306 lines (294 loc) · 9.91 KB
/
containerregistry.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
query "container_registry_encrypted_with_cmk" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'encryption' ) is null then 'alarm'
when (attributes_std -> 'encryption' ->> 'enabled')::boolean then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'encryption' ) is null then ' ''encryption'' not defined'
when (attributes_std -> 'encryption' ->> 'enabled')::boolean 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_container_registry';
EOQ
}
query "container_registry_azure_defender_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'resource_type') = 'AppServices' and (attributes_std ->> 'tier') = 'Standard' then 'ok'
else 'info'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'resource_type') = 'ContainerRegistry' and (attributes_std ->> 'tier') = 'Standard' then ' Azure Defender on for Container Registry'
else ' Azure Defender off for Container Registry'
end || '.' reason
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_security_center_subscription_pricing';
EOQ
}
query "container_registry_restrict_public_access" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'network_rule_set') is null then 'alarm'
when (attributes_std -> 'network_rule_set' ->> 'default_action')::text = 'Deny' then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'network_rule_set') is null then ' ''network_rule_set'' not defined'
when (attributes_std -> 'network_rule_set' ->> 'default_action')::text = 'Deny' then ' not publicly accessible'
else ' publicly accessible'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_container_registry';
EOQ
}
query "container_registry_use_virtual_service_endpoint" {
sql = <<-EOQ
with container_registry as (
select
*
from
terraform_resource
where
type = 'azurerm_container_registry'
), container_registry_subnet as (
select
distinct address
from
container_registry as a,
jsonb_array_elements(attributes_std -> 'network_rule_set' -> 'virtual_network') as rule
)
select
a.address as resource,
case
when (attributes_std -> 'network_rule_set' ->> 'default_action')::text <> 'Deny' then 'alarm'
when s.address is null then 'alarm'
else 'ok'
end as status,
case
when (attributes_std -> 'network_rule_set' ->> 'default_action')::text <> 'Deny' then ' not configured with virtual service endpoint'
when s.address is null then ' not configured with virtual service endpoint'
else ' configured with virtual service endpoint'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
container_registry as a
left join container_registry_subnet as s on a.address = s.address;
EOQ
}
query "container_registry_admin_user_disabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'admin_enabled')::boolean then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'admin_enabled')::boolean then ' admin user enabled'
else ' admin user disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_container_registry';
EOQ
}
query "container_registry_anonymous_pull_disabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'sku') in ('Standard', 'Premium') and (attributes_std ->> 'anonymous_pull_enabled')::boolean then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'sku') in ('Standard', 'Premium') and (attributes_std ->> 'anonymous_pull_enabled')::boolean then ' anonymous pull enabled'
else ' anonymous pull disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_container_registry';
EOQ
}
query "container_registry_image_scan_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'sku') in ('Standard', 'Premium') then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'sku') in ('Standard', 'Premium') then ' image scan enabled'
else ' image scan disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_container_registry';
EOQ
}
query "container_registry_quarantine_policy_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'sku') = 'Premium' and (attributes_std ->> 'quarantine_policy_enabled')::bool then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'sku') = 'Premium' and (attributes_std ->> 'quarantine_policy_enabled')::bool then ' quarantine policy enabled'
else ' quarantine policy disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_container_registry';
EOQ
}
query "container_registry_retention_policy_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'sku') = 'Premium' and (attributes_std -> 'retention_policy' ->> 'enabled')::bool then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'sku') = 'Premium' and (attributes_std -> 'retention_policy' ->> 'enabled')::bool then ' retention policy enabled'
else ' retention policy disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_container_registry';
EOQ
}
query "container_registry_geo_replication_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'sku') = 'Premium' and (attributes_std ->> 'georeplications') is not null then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'sku') = 'Premium' and (attributes_std ->> 'georeplications') is not null then ' geo-replication enabled'
else ' geo-replication disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_container_registry';
EOQ
}
query "container_registry_public_network_access_disabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'public_network_access_enabled')::bool or (attributes_std ->> 'public_network_access_enabled') is null then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'public_network_access_enabled')::bool or (attributes_std ->> 'public_network_access_enabled') is null then ' public network access enabled'
else ' public network access disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_container_registry';
EOQ
}
query "container_registry_trust_policy_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'trust_policy' ->> 'enabled')::boolean then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'trust_policy' ->> 'enabled')::boolean then ' trust policy enabled'
else ' trust policy disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'azurerm_container_registry';
EOQ
}
query "container_registry_zone_redundant_enabled" {
sql = <<-EOQ
with geo_replication_zone_redundant as (
select
distinct name
from
terraform_resource
where
type = 'azurerm_container_registry'
and
(not (attributes_std -> 'georeplications' -> 'zone_redundancy_enabled')::bool
or attributes_std -> 'georeplications' -> 'zone_redundancy_enabled' is null)
)
select
address as resource,
case
when (r.attributes_std -> 'georeplications') is null then 'alarm'
when not (attributes_std -> 'zone_redundancy_enabled')::boolean then 'alarm'
when g.name is not null then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (r.attributes_std -> 'georeplications') is null then ' geo replication not defined'
when not (attributes_std -> 'zone_redundancy_enabled')::boolean then ' not zone redundant'
when g.name is not null then ' not zone redundant'
else ' zone redundant'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource as r
left join geo_replication_zone_redundant as g on g.name = r.name
where
type = 'azurerm_container_registry';
EOQ
}