/
compute.sp
728 lines (706 loc) · 29.3 KB
/
compute.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
query "compute_instance_oslogin_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'metadata') is null then 'alarm'
when (attributes_std -> 'metadata' -> 'enable-oslogin') is null then 'alarm'
when (attributes_std -> 'metadata' ->> 'enable-oslogin') = 'true' then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'metadata') is null then ' ''metadata'' property is not defined'
when (attributes_std -> 'metadata' -> 'enable-oslogin') is null then ' ''enable-oslogin'' property is not defined'
when (attributes_std -> 'metadata' ->> 'enable-oslogin') = 'true' then ' has OS login enabled'
else ' has OS login disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_instance';
EOQ
}
query "compute_instance_with_no_default_service_account" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'service_account') is null and (attributes_std -> 'source_instance_template') is null then 'skip'
when (attributes_std -> 'service_account') is null and (attributes_std -> 'source_instance_template') is not null then 'skip'
when (attributes_std -> 'service_account') is not null and (attributes_std -> 'service_account' ->> 'email') is null then 'alarm'
when (attributes_std ->> 'name') like 'gke-%' and (attributes_std -> 'service_account' ->> 'email') like '%-compute@developer.gserviceaccount.com' then 'ok'
when (attributes_std -> 'service_account' ->> 'email') not like '%-compute@developer.gserviceaccount.com' then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'service_account') is null and (attributes_std -> 'source_instance_template') is null then ' no service account configured'
when (attributes_std -> 'service_account') is null and (attributes_std -> 'source_instance_template') is not null then ' has no service account in the instance template'
when (attributes_std -> 'service_account') is not null and (attributes_std -> 'service_account' ->> 'email') is null then ' does not have email configured in service account'
when (attributes_std ->> 'name') like 'gke-%' then ' configured to use default service account'
when (attributes_std -> 'service_account' ->> 'email') not like '%-compute@developer.gserviceaccount.com' then ' not use default service account'
else ' use default service account'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type in ('google_compute_instance', 'google_compute_instance_from_template', 'google_compute_instance_template');
EOQ
}
query "compute_instance_with_no_default_service_account_with_full_access" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'service_account') is null then 'alarm'
when (attributes_std -> 'service_account' ->> 'email') not like '%-compute@developer.gserviceaccount.com' then 'alarm'
when (attributes_std -> 'service_account' ->> 'email') like '%-compute@developer.gserviceaccount.com' and (attributes_std -> 'service_account' ->> 'scopes') like '%cloud-platform%' then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'service_account') is null then ' not configured with default service account'
when (attributes_std -> 'service_account' ->> 'email') not like '%-compute@developer.gserviceaccount.com' then ' not configured with default service account'
when (attributes_std -> 'service_account' ->> 'email') like '%-compute@developer.gserviceaccount.com' and (attributes_std -> 'service_account' ->> 'scopes') like '%cloud-platform%' then ' configured to use default service account with full access'
else ' not configured with default service account with full access'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_instance';
EOQ
}
query "compute_network_contains_no_default_network" {
sql = <<-EOQ
select
address as resource,
case
when name not ilike 'default' then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when name not ilike 'default' and (attributes_std ->> 'project') is not null then ' ' || (attributes_std ->> 'project') || ' is not using default network'
when name not ilike 'default' and (attributes_std ->> 'project') is null then ' provider project is not using default network'
when name ilike 'default' and (attributes_std ->> 'project') is null then ' provider project is using default network'
when name ilike 'default' and (attributes_std ->> 'project') is not null then ' ' || (attributes_std ->> 'project') || ' is using default network'
end || '.' reason
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_network';
EOQ
}
query "compute_disk_encrypted_with_csk" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'disk_encryption_key') is null then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'disk_encryption_key') is null then 'not encrypted with Customer Supplied Key'
else ' encrypted with Customer Supplied Key'
end || '.' reason
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_disk';
EOQ
}
query "compute_network_contains_no_legacy_network" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'auto_create_subnetworks') is null then 'ok'
when (attributes_std -> 'auto_create_subnetworks')::bool then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'auto_create_subnetworks') is null then ' is not a legacy network'
when (attributes_std -> 'auto_create_subnetworks')::bool then ' is not a legacy network'
else ' is a legacy network'
end || '.' reason
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_network';
EOQ
}
query "compute_subnetwork_flow_log_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'purpose') in ('REGIONAL_MANAGED_PROXY', 'GLOBAL_MANAGED_PROXY') then 'skip'
when (attributes_std -> 'log_config') is not null then 'ok'
else 'alarm'
end as status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'purpose') in ('REGIONAL_MANAGED_PROXY', 'GLOBAL_MANAGED_PROXY') then ' flow logging not supported'
when (attributes_std -> 'log_config') is not null then ' flow logging enabled'
else ' flow logging disabled'
end || '.' reason
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_subnetwork';
EOQ
}
query "compute_instance_confidential_computing_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'confidential_instance_config') is null then 'alarm'
when (attributes_std -> 'confidential_instance_config' -> 'enable_confidential_compute')::bool then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'confidential_instance_config') is null then ' confidential computing disabled'
when (attributes_std -> 'confidential_instance_config' -> 'enable_confidential_compute')::bool then ' confidential computing enabled'
else ' confidential computing disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_instance';
EOQ
}
query "compute_subnetwork_private_ip_google_access" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'private_ip_google_access')::boolean then 'ok' else 'alarm'
end as status,
split_part(address, '.', 2) || case
when (attributes_std -> 'private_ip_google_access') is null then ' ''private_ip_google_access'' is not defined'
when (attributes_std ->> 'private_ip_google_access')::boolean then ' private Google Access is enabled'
else ' private Google Access is disabled'
end || '.' reason
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_subnetwork';
EOQ
}
query "compute_instance_serial_port_connection_disabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'metadata') is null then 'alarm'
when (attributes_std -> 'metadata' -> 'serial-port-enable') is null then 'alarm'
when (attributes_std -> 'metadata' ->> 'serial-port-enable') = 'true' then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'metadata') is null then ' ''metadata'' property is not defined'
when (attributes_std -> 'metadata' -> 'serial-port-enable') is null then ' ''serial-port-enable'' property is not defined'
when (attributes_std -> 'metadata' ->> 'serial-port-enable') = 'true' then ' has serial port connections enabled'
else ' has serial port connections disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_instance';
EOQ
}
query "compute_instance_shielded_vm_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'shielded_instance_config') is null then 'alarm'
when (attributes_std -> 'shielded_instance_config' -> 'enable_integrity_monitoring')::bool then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'shielded_instance_config') is null then ' shielded VM disabled'
when (attributes_std -> 'shielded_instance_config' -> 'enable_integrity_monitoring')::bool then ' shielded VM enabled'
else ' shielded VM disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_instance';
EOQ
}
query "compute_instance_ip_forwarding_disabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'can_ip_forward') is null then 'alarm'
when (attributes_std -> 'can_ip_forward')::bool then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'can_ip_forward') is null then ' IP forwarding disabled'
when (attributes_std -> 'can_ip_forward')::bool then ' IP forwarding enabled'
else ' IP forwarding disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_instance';
EOQ
}
query "compute_instance_with_no_public_ip_addresses" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'network_interface' -> 'access_config') is null or (attributes_std -> 'network_interface' ->> 'access_config') like '{}' then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'network_interface' -> 'access_config') is null or (attributes_std -> 'network_interface' ->> 'access_config') like '{}' then ' not associated with public IP addresses'
else ' associated with public IP addresses'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_instance';
EOQ
}
query "compute_instance_boot_disk_encryption_enabled" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std -> 'boot_disk' -> 'disk_encryption_key_raw') is not null or (attributes_std -> 'boot_disk' -> 'kms_key_self_link') is not null then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when (attributes_std -> 'boot_disk' -> 'disk_encryption_key_raw') is not null or (attributes_std -> 'boot_disk' -> 'kms_key_self_link') is not null then ' boot disk encryption enabled'
else ' boot disk encryption disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_instance';
EOQ
}
query "compute_instance_block_project_wide_ssh_enabled" {
sql = <<-EOQ
select
address as resource,
case
when ((attributes_std ->> 'source_instance_template') is not null and (attributes_std ->> 'metadata') is null) then 'skip'
when ((attributes_std ->> 'source_instance_template') is not null and (attributes_std -> 'metadata' ->> 'block-project-ssh-keys') is null) then 'skip'
when ((attributes_std ->> 'source_instance_template') is null and (attributes_std ->> 'metadata') is null) then 'skip'
when ((attributes_std ->> 'source_instance_template') is null and (attributes_std -> 'metadata' ->> 'block-project-ssh-keys') is null) then 'skip'
when (attributes_std -> 'metadata' ->> 'block-project-ssh-keys')::bool then 'ok'
else 'alarm'
end status,
split_part(address, '.', 2) || case
when ((attributes_std ->> 'source_instance_template') is not null and (attributes_std ->> 'metadata') is null) then ' underlying source template is unknown'
when ((attributes_std ->> 'source_instance_template') is not null and (attributes_std -> 'metadata' ->> 'block-project-ssh-keys') is null) then ' underlying source template is unknown'
when ((attributes_std ->> 'source_instance_template') is null and (attributes_std ->> 'metadata') is null) then ' block project-wide SSH keys not set'
when ((attributes_std ->> 'source_instance_template') is null and (attributes_std -> 'metadata' ->> 'block-project-ssh-keys') is null) then ' block project-wide SSH keys not set'
when (attributes_std -> 'metadata' ->> 'block-project-ssh-keys')::bool then ' block project-wide SSH keys enabled'
else ' block project-wide SSH keys disabled'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type in ('google_compute_instance', 'google_compute_instance_template', 'google_compute_instance_from_template');
EOQ
}
query "compute_security_policy_prevent_message_lookup" {
sql = <<-EOQ
select
address as resource,
case
when jsonb_typeof(attributes_std -> 'rule') = 'object' and ((attributes_std -> 'rule' -> 'match' -> 'expr' ->> 'expression' <> 'evaluatePreconfiguredExpr(''cve-canary'')') or (attributes_std -> 'rule' ->> 'action' = 'allow') or (attributes_std -> 'rule' ->> 'preview' = 'true')) then 'alarm'
when jsonb_typeof(attributes_std -> 'rule') = 'array' and exists(select 1 from jsonb_array_elements(attributes_std -> 'rule') as rules where ((rules -> 'match' -> 'expr' ->> 'expression' <> 'evaluatePreconfiguredExpr(''cve-canary'')') or (rules ->> 'action' = 'allow') or (rules ->> 'preview' = 'true'))) then 'alarm'
else 'ok'
end status,
split_part(address, '.', 2) || case
when jsonb_typeof(attributes_std -> 'rule') = 'object' and ((attributes_std -> 'rule' -> 'match' -> 'expr' ->> 'expression' <> 'evaluatePreconfiguredExpr(''cve-canary'')') or (attributes_std -> 'rule' ->> 'action' = 'allow') or (attributes_std -> 'rule' ->> 'preview' = 'true')) then ' does not prevent message lookup'
when jsonb_typeof(attributes_std -> 'rule') = 'array' and exists(select 1 from jsonb_array_elements(attributes_std -> 'rule') as rules where ((rules -> 'match' -> 'expr' ->> 'expression' <> 'evaluatePreconfiguredExpr(''cve-canary'')') or (rules ->> 'action' = 'allow') or (rules ->> 'preview' = 'true'))) then ' does not prevent message lookup'
else ' prevents message lookup'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_security_policy';
EOQ
}
query "compute_subnetwork_private_ipv6_google_access" {
sql = <<-EOQ
select
address as resource,
case
when (attributes_std ->> 'private_ipv6_google_access') in ('ENABLE_OUTBOUND_VM_ACCESS_TO_GOOGLE', 'ENABLE_BIDIRECTIONAL_ACCESS_TO_GOOGLE') then 'ok'
else 'alarm'
end as status,
split_part(address, '.', 2) || case
when (attributes_std ->> 'private_ipv6_google_access') in ('ENABLE_OUTBOUND_VM_ACCESS_TO_GOOGLE', 'ENABLE_BIDIRECTIONAL_ACCESS_TO_GOOGLE') then ' private IPv6 Google Access is enabled'
else ' private IPv6 Google Access is disabled'
end || '.' reason
${local.common_dimensions_sql}
from
terraform_resource
where
type = 'google_compute_subnetwork';
EOQ
}
query "compute_firewall_allow_http_port_80_ingress" {
sql = <<-EOQ
with rules as (
select
distinct address
from
terraform_resource,
jsonb_array_elements(
case jsonb_typeof(attributes_std -> 'allow')
when 'array' then (attributes_std -> 'allow')
when 'object' then jsonb_build_array(attributes_std -> 'allow')
else null end
) allow,
jsonb_array_elements_text(
case
when ((allow -> 'ports') != 'null') and jsonb_array_length(allow -> 'ports') > 0 then (allow -> 'ports')
else jsonb_build_array(allow -> 'ports')
end) as port,
jsonb_array_elements_text(
case
when ((attributes_std -> 'source_ranges') != 'null') and jsonb_array_length(attributes_std -> 'source_ranges') > 0 then (attributes_std -> 'source_ranges')
else jsonb_build_array(attributes_std -> 'source_ranges')
end) as sip
where
type = 'google_compute_firewall'
and (attributes_std ->> 'direction' is null or lower(attributes_std ->> 'direction') = 'ingress')
and lower(sip) in ('*', '0.0.0.0', '0.0.0.0/0', 'internet', 'any', '<nw>/0', '/0')
and (
port in ('80', '*')
or (
port like '%-%'
and split_part(port, '-', 1) :: integer <= 80
and split_part(port, '-', 2) :: integer >= 80
)
)
)
select
r.address as resource,
case
when g.address is null then 'ok'
else 'alarm'
end as status,
split_part(r.address, '.', 2) || case
when g.address is null then ' restricts HTTP access from internet through port 80'
else ' allows HTTP access from internet through port 80'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource as r
left join rules as g on g.address = r.address
where
type = 'google_compute_firewall';
EOQ
}
query "compute_firewall_allow_ftp_port_20_ingress" {
sql = <<-EOQ
with rules as (
select
distinct address
from
terraform_resource,
jsonb_array_elements(
case jsonb_typeof(attributes_std -> 'allow')
when 'array' then (attributes_std -> 'allow')
when 'object' then jsonb_build_array(attributes_std -> 'allow')
else null end
) allow,
jsonb_array_elements_text(
case
when ((allow -> 'ports') != 'null') and jsonb_array_length(allow -> 'ports') > 0 then (allow -> 'ports')
else jsonb_build_array(allow -> 'ports')
end) as port,
jsonb_array_elements_text(
case
when ((attributes_std -> 'source_ranges') != 'null') and jsonb_array_length(attributes_std -> 'source_ranges') > 0 then (attributes_std -> 'source_ranges')
else jsonb_build_array(attributes_std -> 'source_ranges')
end) as sip
where
type = 'google_compute_firewall'
and (attributes_std ->> 'direction' is null or lower(attributes_std ->> 'direction') = 'ingress')
and lower(sip) in ('*', '0.0.0.0', '0.0.0.0/0', 'internet', 'any', '<nw>/0', '/0')
and (
port in ('20', '*')
or (
port like '%-%'
and split_part(port, '-', 1) :: integer <= 20
and split_part(port, '-', 2) :: integer >= 20
)
)
)
select
r.address as resource,
case
when g.address is null then 'ok'
else 'alarm'
end as status,
split_part(r.address, '.', 2) || case
when g.address is null then ' restricts FTP access from internet through port 20'
else ' allows FTP access from internet through port 20'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource as r
left join rules as g on g.address = r.address
where
type = 'google_compute_firewall';
EOQ
}
query "compute_firewall_allow_ftp_port_21_ingress" {
sql = <<-EOQ
with rules as (
select
distinct address
from
terraform_resource,
jsonb_array_elements(
case jsonb_typeof(attributes_std -> 'allow')
when 'array' then (attributes_std -> 'allow')
when 'object' then jsonb_build_array(attributes_std -> 'allow')
else null end
) allow,
jsonb_array_elements_text(
case
when ((allow -> 'ports') != 'null') and jsonb_array_length(allow -> 'ports') > 0 then (allow -> 'ports')
else jsonb_build_array(allow -> 'ports')
end) as port,
jsonb_array_elements_text(
case
when ((attributes_std -> 'source_ranges') != 'null') and jsonb_array_length(attributes_std -> 'source_ranges') > 0 then (attributes_std -> 'source_ranges')
else jsonb_build_array(attributes_std -> 'source_ranges')
end) as sip
where
type = 'google_compute_firewall'
and (attributes_std ->> 'direction' is null or lower(attributes_std ->> 'direction') = 'ingress')
and lower(sip) in ('*', '0.0.0.0', '0.0.0.0/0', 'internet', 'any', '<nw>/0', '/0')
and (
port in ('21', '*')
or (
port like '%-%'
and split_part(port, '-', 1) :: integer <= 21
and split_part(port, '-', 2) :: integer >= 21
)
)
)
select
r.address as resource,
case
when g.address is null then 'ok'
else 'alarm'
end as status,
split_part(r.address, '.', 2) || case
when g.address is null then ' restricts FTP access from internet through port 21'
else ' allows FTP access from internet through port 21'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource as r
left join rules as g on g.address = r.address
where
type = 'google_compute_firewall';
EOQ
}
query "compute_firewall_allow_ssh_port_22_ingress" {
sql = <<-EOQ
with rules as (
select
distinct address
from
terraform_resource,
jsonb_array_elements(
case jsonb_typeof(attributes_std -> 'allow')
when 'array' then (attributes_std -> 'allow')
when 'object' then jsonb_build_array(attributes_std -> 'allow')
else null end
) allow,
jsonb_array_elements_text(
case
when ((allow -> 'ports') != 'null') and jsonb_array_length(allow -> 'ports') > 0 then (allow -> 'ports')
else jsonb_build_array(allow -> 'ports')
end) as port,
jsonb_array_elements_text(
case
when ((attributes_std -> 'source_ranges') != 'null') and jsonb_array_length(attributes_std -> 'source_ranges') > 0 then (attributes_std -> 'source_ranges')
else jsonb_build_array(attributes_std -> 'source_ranges')
end) as sip
where
type = 'google_compute_firewall'
and (attributes_std ->> 'direction' is null or lower(attributes_std ->> 'direction') = 'ingress')
and lower(sip) in ('*', '0.0.0.0', '0.0.0.0/0', 'internet', 'any', '<nw>/0', '/0')
and (
port in ('22', '*')
or (
port like '%-%'
and split_part(port, '-', 1) :: integer <= 22
and split_part(port, '-', 2) :: integer >= 22
)
)
)
select
r.address as resource,
case
when g.address is null then 'ok'
else 'alarm'
end as status,
split_part(r.address, '.', 2) || case
when g.address is null then ' restricts FTP access from internet through port 22'
else ' allows FTP access from internet through port 22'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource as r
left join rules as g on g.address = r.address
where
type = 'google_compute_firewall';
EOQ
}
query "compute_firewall_allow_rdp_port_3389_ingress" {
sql = <<-EOQ
with rules as (
select
distinct address
from
terraform_resource,
jsonb_array_elements(
case jsonb_typeof(attributes_std -> 'allow')
when 'array' then (attributes_std -> 'allow')
when 'object' then jsonb_build_array(attributes_std -> 'allow')
else null end
) allow,
jsonb_array_elements_text(
case
when ((allow -> 'ports') != 'null') and jsonb_array_length(allow -> 'ports') > 0 then (allow -> 'ports')
else jsonb_build_array(allow -> 'ports')
end) as port,
jsonb_array_elements_text(
case
when ((attributes_std -> 'source_ranges') != 'null') and jsonb_array_length(attributes_std -> 'source_ranges') > 0 then (attributes_std -> 'source_ranges')
else jsonb_build_array(attributes_std -> 'source_ranges')
end) as sip
where
type = 'google_compute_firewall'
and (attributes_std ->> 'direction' is null or lower(attributes_std ->> 'direction') = 'ingress')
and lower(sip) in ('*', '0.0.0.0', '0.0.0.0/0', 'internet', 'any', '<nw>/0', '/0')
and (
port in ('3389', '*')
or (
port like '%-%'
and split_part(port, '-', 1) :: integer <= 3389
and split_part(port, '-', 2) :: integer >= 3389
)
)
)
select
r.address as resource,
case
when g.address is null then 'ok'
else 'alarm'
end as status,
split_part(r.address, '.', 2) || case
when g.address is null then ' restricts RDP access from internet through port 3389'
else ' allows RDP access from internet through port 3389'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource as r
left join rules as g on g.address = r.address
where
type = 'google_compute_firewall';
EOQ
}
query "compute_firewall_allow_mysql_port_3306_ingress" {
sql = <<-EOQ
with rules as (
select
distinct address
from
terraform_resource,
jsonb_array_elements(
case jsonb_typeof(attributes_std -> 'allow')
when 'array' then (attributes_std -> 'allow')
when 'object' then jsonb_build_array(attributes_std -> 'allow')
else null end
) allow,
jsonb_array_elements_text(
case
when ((allow -> 'ports') != 'null') and jsonb_array_length(allow -> 'ports') > 0 then (allow -> 'ports')
else jsonb_build_array(allow -> 'ports')
end) as port,
jsonb_array_elements_text(
case
when ((attributes_std -> 'source_ranges') != 'null') and jsonb_array_length(attributes_std -> 'source_ranges') > 0 then (attributes_std -> 'source_ranges')
else jsonb_build_array(attributes_std -> 'source_ranges')
end) as sip
where
type = 'google_compute_firewall'
and (attributes_std ->> 'direction' is null or lower(attributes_std ->> 'direction') = 'ingress')
and lower(sip) in ('*', '0.0.0.0', '0.0.0.0/0', 'internet', 'any', '<nw>/0', '/0')
and (
port in ('3306', '*')
or (
port like '%-%'
and split_part(port, '-', 1) :: integer <= 3306
and split_part(port, '-', 2) :: integer >= 3306
)
)
)
select
r.address as resource,
case
when g.address is null then 'ok'
else 'alarm'
end as status,
split_part(r.address, '.', 2) || case
when g.address is null then ' restricts MySQL access from internet through port 3306'
else ' allows MySQL access from internet through port 3306'
end || '.' reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
terraform_resource as r
left join rules as g on g.address = r.address
where
type = 'google_compute_firewall';
EOQ
}