/
compute_virtual_machine_detail.sp
955 lines (855 loc) · 26.9 KB
/
compute_virtual_machine_detail.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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
dashboard "compute_virtual_machine_detail" {
title = "Azure Compute Virtual Machine Detail"
documentation = file("./dashboards/compute/docs/compute_virtual_machine_detail.md")
tags = merge(local.compute_common_tags, {
type = "Detail"
})
input "vm_id" {
title = "Select a virtual machine:"
query = query.compute_virtual_machine_input
width = 4
}
container {
card {
width = 2
query = query.compute_virtual_machine_status
args = [self.input.vm_id.value]
}
card {
width = 2
query = query.compute_virtual_machine_encryption_status
args = [self.input.vm_id.value]
}
card {
width = 2
query = query.compute_virtual_machine_disaster_recovery_status
args = [self.input.vm_id.value]
}
card {
width = 2
query = query.compute_virtual_machine_ingress_access
args = [self.input.vm_id.value]
}
card {
width = 2
query = query.compute_virtual_machine_vulnerability_assessment_solution
args = [self.input.vm_id.value]
}
}
with "compute_disks_for_compute_virtual_machine" {
query = query.compute_disks_for_compute_virtual_machine
args = [self.input.vm_id.value]
}
with "compute_images_for_compute_virtual_machine" {
query = query.compute_images_for_compute_virtual_machine
args = [self.input.vm_id.value]
}
with "network_application_gateways_for_compute_virtual_machine" {
query = query.network_application_gateways_for_compute_virtual_machine
args = [self.input.vm_id.value]
}
with "network_load_balancer_backend_address_pools_for_compute_virtual_machine" {
query = query.network_load_balancer_backend_address_pools_for_compute_virtual_machine
args = [self.input.vm_id.value]
}
with "network_load_balancers_for_compute_virtual_machine" {
query = query.network_load_balancers_for_compute_virtual_machine
args = [self.input.vm_id.value]
}
with "network_network_interfaces_for_compute_virtual_machine" {
query = query.network_network_interfaces_for_compute_virtual_machine
args = [self.input.vm_id.value]
}
with "network_public_ips_for_compute_virtual_machine" {
query = query.network_public_ips_for_compute_virtual_machine
args = [self.input.vm_id.value]
}
with "network_security_groups_for_compute_virtual_machine" {
query = query.network_security_groups_for_compute_virtual_machine
args = [self.input.vm_id.value]
}
with "network_subnets_for_compute_virtual_machine" {
query = query.network_subnets_for_compute_virtual_machine
args = [self.input.vm_id.value]
}
with "network_virtual_networks_for_compute_virtual_machine" {
query = query.network_virtual_networks_for_compute_virtual_machine
args = [self.input.vm_id.value]
}
container {
graph {
title = "Relationships"
type = "graph"
direction = "TD"
node {
base = node.compute_disk
args = {
compute_disk_ids = with.compute_disks_for_compute_virtual_machine.rows[*].disk_id
}
}
node {
base = node.compute_image
args = {
compute_image_ids = with.compute_images_for_compute_virtual_machine.rows[*].compute_image_id
}
}
node {
base = node.compute_virtual_machine
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
node {
base = node.compute_virtual_machine_application_gateway_backend_address_pool
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
node {
base = node.network_application_gateway
args = {
network_application_gateway_ids = with.network_application_gateways_for_compute_virtual_machine.rows[*].application_gateway_id
}
}
node {
base = node.network_load_balancer
args = {
network_load_balancer_ids = with.network_load_balancers_for_compute_virtual_machine.rows[*].load_balancer_id
}
}
node {
base = node.network_load_balancer_backend_address_pool
args = {
network_load_balancer_backend_address_pool_ids = with.network_load_balancer_backend_address_pools_for_compute_virtual_machine.rows[*].pool_id
}
}
node {
base = node.network_network_interface
args = {
network_network_interface_ids = with.network_network_interfaces_for_compute_virtual_machine.rows[*].network_interface_id
}
}
node {
base = node.network_network_security_group
args = {
network_network_security_group_ids = with.network_security_groups_for_compute_virtual_machine.rows[*].nsg_id
}
}
node {
base = node.network_public_ip
args = {
network_public_ip_ids = with.network_public_ips_for_compute_virtual_machine.rows[*].public_ip_id
}
}
node {
base = node.network_subnet
args = {
network_subnet_ids = with.network_subnets_for_compute_virtual_machine.rows[*].subnet_id
}
}
node {
base = node.network_virtual_network
args = {
network_virtual_network_ids = with.network_virtual_networks_for_compute_virtual_machine.rows[*].virtual_network_id
}
}
edge {
base = edge.compute_virtual_machine_to_compute_data_disk
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
edge {
base = edge.compute_virtual_machine_to_compute_image
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
edge {
base = edge.compute_virtual_machine_to_compute_os_disk
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
edge {
base = edge.compute_virtual_machine_to_network_network_interface
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
edge {
base = edge.compute_virtual_machine_to_network_public_ip
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
edge {
base = edge.compute_virtual_machine_to_network_security_group
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
edge {
base = edge.compute_virtual_machine_to_network_subnet
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
edge {
base = edge.compute_virtual_machine_to_network_virtual_network
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
edge {
base = edge.network_application_gateway_backend_address_pool_to_compute_virtual_machine
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
edge {
base = edge.network_application_gateway_to_compute_virtual_machine
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
edge {
base = edge.network_load_balancer_backend_address_pool_to_compute_virtual_machine
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
edge {
base = edge.network_load_balancer_to_compute_virtual_machine_backend_address_pool
args = {
compute_virtual_machine_ids = [self.input.vm_id.value]
}
}
}
}
container {
container {
width = 6
table {
title = "Overview"
type = "line"
width = 6
query = query.compute_virtual_machine_overview
args = [self.input.vm_id.value]
}
table {
title = "Tags"
width = 6
query = query.compute_virtual_machine_tags
args = [self.input.vm_id.value]
}
}
container {
width = 6
table {
title = "Storage Profile"
query = query.compute_virtual_machine_storage_profile
args = [self.input.vm_id.value]
column "Disk Name" {
href = "/azure_insights.dashboard.compute_disk_detail?input.disk_id={{ .ID | @uri }}"
}
}
table {
title = "Image"
query = query.compute_virtual_machine_image
args = [self.input.vm_id.value]
}
}
}
container {
width = 12
table {
title = "Security Groups"
query = query.compute_virtual_machine_security_groups
args = [self.input.vm_id.value]
column "Name" {
href = "/azure_insights.dashboard.network_security_group_detail?input.nsg_id={{ .'Security Group ID' | @uri }}"
}
}
}
container {
width = 12
table {
title = "Network Interfaces"
query = query.compute_virtual_machine_network_interfaces
args = [self.input.vm_id.value]
column "Name" {
href = "/azure_insights.dashboard.network_interface_detail?input.nic_id={{ .'Network Interface ID' | @uri }}"
}
}
}
container {
width = 12
table {
title = "Guest Configuration"
query = query.compute_virtual_machine_guest_configuration_assignments
args = [self.input.vm_id.value]
}
}
container {
width = 12
table {
title = "Data Disks"
query = query.compute_virtual_machine_data_disks
args = [self.input.vm_id.value]
column "Name" {
href = "/azure_insights.dashboard.compute_disk_detail?input.disk_id={{ .'Managed Disk ID' | @uri }}"
}
}
}
}
query "compute_virtual_machine_input" {
sql = <<-EOQ
select
v.title as label,
lower(v.id) as value,
json_build_object(
'subscription', s.display_name,
'resource_group', v.resource_group,
'region', v.region,
'vm_id', v.vm_id
) as tags
from
azure_compute_virtual_machine as v,
azure_subscription as s
where
v.subscription_id = s.subscription_id
order by
v.title;
EOQ
}
# Card Queries
query "compute_virtual_machine_status" {
sql = <<-EOQ
select
'Status' as label,
initcap(power_state) as value
from
azure_compute_virtual_machine
where
lower(id) = $1;
EOQ
}
query "compute_virtual_machine_encryption_status" {
sql = <<-EOQ
select
'Host Encryption' as label,
case when security_profile -> 'encryptionAtHost' <> 'true' or security_profile -> 'encryptionAtHost' is null then 'Disabled' else 'Enabled' end as value,
case when security_profile -> 'encryptionAtHost' <> 'true' or security_profile -> 'encryptionAtHost' is null then 'alert' else 'ok' end as type
from
azure_compute_virtual_machine
where
lower(id) = $1;
EOQ
}
query "compute_virtual_machine_disaster_recovery_status" {
sql = <<-EOQ
with vm_dr_enabled as (
select
substr(source_id, 0, length(source_id)) as source_id
from
azure_resource_link as l
left join azure_compute_virtual_machine as vm on lower(substr(source_id, 0, length(source_id)))= lower(vm.id)
where
l.name like 'ASR-Protect-%'
)
select
'Disaster Recovery' as label,
case when source_id is null then 'Disabled' else 'Enabled' end as value,
case when source_id is null then 'alert' else 'ok' end as type
from
azure_compute_virtual_machine as vm
left join vm_dr_enabled as l on lower(vm.id) = lower(l.source_id)
where
lower(id) = $1;
EOQ
}
query "compute_virtual_machine_ingress_access" {
sql = <<-EOQ
with network_sg as (
select
distinct name as sg_name,
network_interfaces
from
azure_network_security_group as nsg,
jsonb_array_elements(security_rules) as sg,
jsonb_array_elements_text(
case
when jsonb_array_length(sg -> 'properties' -> 'destinationPortRanges') > 0 then (sg -> 'properties' -> 'destinationPortRanges')
else jsonb_build_array(sg -> 'properties' -> 'destinationPortRange')
end ) as dport,
jsonb_array_elements_text(
case
when jsonb_array_length(sg -> 'properties' -> 'sourceAddressPrefixes') > 0 then (sg -> 'properties' -> 'sourceAddressPrefixes')
else jsonb_build_array(sg -> 'properties' -> 'sourceAddressPrefix')
end) as sip
where
sg -> 'properties' ->> 'access' = 'Allow'
and sg -> 'properties' ->> 'direction' = 'Inbound'
and sg -> 'properties' ->> 'protocol' in ('TCP','*')
and sip in ('*', '0.0.0.0', '0.0.0.0/0', 'Internet', 'any', '<nw>/0', '/0')
)
select
'Unrestricted Ingress' as label,
case when sg.sg_name is null then 'Restricted' else 'Unrestricted' end as value,
case when sg.sg_name is null then 'ok' else 'alert' end as type
from
azure_compute_virtual_machine as vm
left join network_sg as sg on sg.network_interfaces @> vm.network_interfaces
where
lower(id) = $1;
EOQ
}
query "azure_compute_virtual_machine_egress_access" {
sql = <<-EOQ
with network_sg as (
select
distinct name as sg_name,
network_interfaces
from
azure_network_security_group as nsg,
jsonb_array_elements(security_rules) as sg,
jsonb_array_elements_text(
case
when jsonb_array_length(sg -> 'properties' -> 'destinationPortRanges') > 0 then (sg -> 'properties' -> 'destinationPortRanges')
else jsonb_build_array(sg -> 'properties' -> 'destinationPortRange')
end ) as dport,
jsonb_array_elements_text(
case
when jsonb_array_length(sg -> 'properties' -> 'sourceAddressPrefixes') > 0 then (sg -> 'properties' -> 'sourceAddressPrefixes')
else jsonb_build_array(sg -> 'properties' -> 'sourceAddressPrefix')
end) as sip
where
sg -> 'properties' ->> 'access' = 'Allow'
and sg -> 'properties' ->> 'direction' = 'Outbound'
and sg -> 'properties' ->> 'protocol' in ('TCP','*')
and sip in ('*', '0.0.0.0', '0.0.0.0/0', 'Internet', '<nw>/0', '/0')
)
select
'Unrestricted Egress' as label,
case when sg.sg_name is null then 'Restricted' else 'Unrestricted' end as value,
case when sg.sg_name is null then 'ok' else 'alert' end as type
from
azure_compute_virtual_machine as vm
left join network_sg as sg on sg.network_interfaces @> vm.network_interfaces
where
lower(id) = $1;
EOQ
}
query "compute_virtual_machine_vulnerability_assessment_solution" {
sql = <<-EOQ
with defender_enabled_vms as (
select
distinct a.vm_id as vm_id
from
azure_compute_virtual_machine as a,
jsonb_array_elements(extensions) as b
where
b ->> 'ExtensionType' = any(ARRAY ['MDE.Linux', 'MDE.Windows'])
and b ->> 'ProvisioningState' = 'Succeeded'
),
agent_installed_vm as (
select
distinct a.vm_id as vm_id
from
defender_enabled_vms as a
left join azure_compute_virtual_machine as w on lower(w.vm_id) = lower(a.vm_id),
jsonb_array_elements(extensions) as b
where
b ->> 'Publisher' = 'Qualys'
and b ->> 'ExtensionType' = any(ARRAY ['WindowsAgent.AzureSecurityCenter', 'LinuxAgent.AzureSecurityCenter'])
and b ->> 'ProvisioningState' = 'Succeeded'
)
select
'Vulnerability Assessment' as label,
case when b.vm_id is not null then 'Enabled' else 'Disabled' end as value,
case when b.vm_id is not null then 'ok' else 'alert' end as type
from
azure_compute_virtual_machine as a
left join agent_installed_vm as b on lower(a.vm_id) = lower(b.vm_id)
where
lower(id) = $1;
EOQ
}
# With Queries
query "compute_disks_for_compute_virtual_machine" {
sql = <<-EOQ
select
lower(jsonb_array_elements(data_disks)->'managedDisk'->>'id') as disk_id
from
azure_compute_virtual_machine
where
lower(id) = $1
union
select
lower(d.id) as disk_id
from
azure_compute_virtual_machine as vm
left join azure_compute_disk as d on lower(d.managed_by) = lower(vm.id)
where
lower(vm.id) = $1;
EOQ
}
query "compute_images_for_compute_virtual_machine" {
sql = <<-EOQ
select
lower(i.id) as compute_image_id
from
azure_compute_image as i
left join azure_compute_virtual_machine as v on lower(i.id) = lower(v.image_id)
where
lower(v.id) = $1;
EOQ
}
query "network_application_gateways_for_compute_virtual_machine" {
sql = <<-EOQ
with network_interface as (
select
vm.id,
nic.id,
nic.ip_configurations as ip_configurations
from
azure_compute_virtual_machine as vm,
jsonb_array_elements(network_interfaces) as n
left join azure_network_interface as nic on nic.id = n ->> 'id'
where
lower(vm.id) = $1
),
vm_application_gateway_backend_address_pool as (
select
p ->> 'id' as id
from
network_interface,
jsonb_array_elements(ip_configurations) as i,
jsonb_array_elements(i -> 'properties' -> 'applicationGatewayBackendAddressPools') as p
)
select
lower(g.id) as application_gateway_id
from
azure_application_gateway as g,
jsonb_array_elements(backend_address_pools) as p
where
lower(p ->> 'id') in (select lower(id) from vm_application_gateway_backend_address_pool);
EOQ
}
query "network_load_balancer_backend_address_pools_for_compute_virtual_machine" {
sql = <<-EOQ
with network_interface as (
select
vm.id,
nic.id,
nic.ip_configurations as ip_configurations
from
azure_compute_virtual_machine as vm,
jsonb_array_elements(network_interfaces) as n
left join azure_network_interface as nic on nic.id = n ->> 'id'
where
lower(vm.id) = $1
),
loadBalancerBackendAddressPools as (
select
p ->> 'id' as id
from
network_interface,
jsonb_array_elements(ip_configurations) as i,
jsonb_array_elements(i -> 'properties' -> 'loadBalancerBackendAddressPools') as p
)
select
lower(pool.id) as pool_id
from
loadBalancerBackendAddressPools as p
left join azure_lb_backend_address_pool as pool on lower(pool.id) = lower(p.id);
EOQ
}
query "network_load_balancers_for_compute_virtual_machine" {
sql = <<-EOQ
with network_interface as (
select
vm.id,
nic.id,
nic.ip_configurations as ip_configurations
from
azure_compute_virtual_machine as vm,
jsonb_array_elements(network_interfaces) as n
left join azure_network_interface as nic on nic.id = n ->> 'id'
where
lower(vm.id) = $1
),
loadBalancerBackendAddressPools as (
select
p ->> 'id' as id
from
network_interface,
jsonb_array_elements(ip_configurations) as i,
jsonb_array_elements(i -> 'properties' -> 'loadBalancerBackendAddressPools') as p
)
select
lower(lb.id) as load_balancer_id
from
azure_lb as lb,
jsonb_array_elements(backend_address_pools) as pool
where
lower(pool ->> 'id') in (select lower(id) from loadBalancerBackendAddressPools);
EOQ
}
query "network_network_interfaces_for_compute_virtual_machine" {
sql = <<-EOQ
with network_interface_id as (
select
id as vm_id,
jsonb_array_elements(network_interfaces)->>'id' as n_id
from
azure_compute_virtual_machine
where
lower(id) = $1
)
select
lower(vn.n_id) as network_interface_id
from
network_interface_id as vn
left join azure_network_interface as n on lower(vn.n_id) = lower(n.id)
EOQ
}
query "network_public_ips_for_compute_virtual_machine" {
sql = <<-EOQ
with network_interfaces as (
select
vm.id as vm_id,
nic.id as nic_id,
nic.ip_configurations as ip_configuration
from
azure_compute_virtual_machine as vm,
jsonb_array_elements(network_interfaces) as n
left join azure_network_interface as nic on lower(nic.id) = lower(n ->> 'id')
where
lower(vm.id) = $1
)
select
lower(p.id) as public_ip_id
from
network_interfaces as n,
jsonb_array_elements(ip_configuration) as ip_config
left join azure_public_ip as p on lower(p.id) = lower(ip_config -> 'properties' -> 'publicIPAddress' ->> 'id')
where
p.id is not null;
EOQ
}
query "network_security_groups_for_compute_virtual_machine" {
sql = <<-EOQ
with network_interface_id as (
select
id as vm_id,
jsonb_array_elements(network_interfaces)->>'id' as n_id
from
azure_compute_virtual_machine
where
lower(id) = $1
)
select
lower(s.id) as nsg_id
from
network_interface_id as vn
left join azure_network_interface as n on lower(vn.n_id) = lower(n.id)
left join azure_network_security_group as s on lower(n.network_security_group_id) = lower(s.id)
where
s.id is not null
EOQ
}
query "network_subnets_for_compute_virtual_machine" {
sql = <<-EOQ
with network_interface_id as (
select
id,
jsonb_array_elements(network_interfaces) ->> 'id' as nic_id
from
azure_compute_virtual_machine
where
lower(id) = $1
)
select
lower(s.id) as subnet_id
from
azure_network_interface as nic,
jsonb_array_elements(ip_configurations) as c
left join azure_subnet as s on lower(s.id) = lower(c -> 'properties' -> 'subnet' ->> 'id')
where
lower(nic.id) in (select lower(nic_id) from network_interface_id);
EOQ
}
query "network_virtual_networks_for_compute_virtual_machine" {
sql = <<-EOQ
with network_interface_id as (
select
id,
jsonb_array_elements(network_interfaces)->>'id' as nic_id
from
azure_compute_virtual_machine
where
lower(id) = $1
), subnet_id as (
select
s.id as id,
s.virtual_network_name
from
azure_network_interface as nic,
jsonb_array_elements(ip_configurations) as c
left join azure_subnet as s on lower(s.id) = lower(c -> 'properties' -> 'subnet' ->> 'id')
where
lower(nic.id) in (select lower(nic_id) from network_interface_id)
)
select
lower(vn.id) as virtual_network_id
from
azure_virtual_network as vn,
jsonb_array_elements(subnets) as s
left join subnet_id as sub on lower(sub.id) = lower(s ->> 'id')
where
lower(s ->> 'id') in (select lower(id) from subnet_id);
EOQ
}
# Table Queries
query "compute_virtual_machine_overview" {
sql = <<-EOQ
select
name as "Name",
vm_id as "Virtual Machine ID",
type as "Type",
provisioning_state as "Provisioning State",
size as "Size",
os_type as "OS Type",
identity ->> 'type' as "Identity",
region as "Region",
resource_group as "Resource Group",
subscription_id as "Subscription ID",
id as "ID"
from
azure_compute_virtual_machine
where
lower(id) = $1;
EOQ
}
query "compute_virtual_machine_tags" {
sql = <<-EOQ
select
tag.key as "Key",
tag.value as "Value"
from
azure_compute_virtual_machine,
jsonb_each_text(tags) as tag
where
lower(id) = $1
order by
tag.key;
EOQ
}
query "compute_virtual_machine_storage_profile" {
sql = <<-EOQ
select
os_disk_name as "Disk Name",
os_disk_caching as "Disk Caching",
os_disk_create_option as "Disk Create Option",
os_disk_vhd_uri as "Virtual Hard Disk URI",
lower(d.id) as "ID"
from
azure_compute_virtual_machine as vm
left join azure_compute_disk as d on vm.os_disk_name = d.name and lower(vm.id)= lower(d.managed_by)
where
lower(vm.id) = $1;
EOQ
}
query "compute_virtual_machine_image" {
sql = <<-EOQ
select
image_sku as "SKU",
image_version as "Version",
image_exact_version as "Exact Version",
image_id as "Image ID",
image_offer as "Offer",
image_publisher as "Publisher"
from
azure_compute_virtual_machine
where
lower(id) = $1;
EOQ
}
query "compute_virtual_machine_security_groups" {
sql = <<-EOQ
select
nsg.name as "Name",
nsg.provisioning_state as "Provisioning State",
nsg.region as "Region",
nsg.resource_group as "Resource Group",
lower(nsg.id) as "Security Group ID"
from
azure_network_security_group as nsg
left join azure_compute_virtual_machine as vm on vm.network_interfaces @> nsg.network_interfaces
where
lower(vm.id) = $1;
EOQ
}
query "compute_virtual_machine_data_disks" {
sql = <<-EOQ
select
disk ->> 'name' as "Name",
disk ->> 'caching' as "Caching",
disk ->> 'createOption' as "Create Option",
(disk ->> 'toBeDetached')::boolean as "To Be Detached",
(disk ->> 'writeAcceleratorEnabled')::boolean as "Write Accelerator Enabled",
lower(disk -> 'managedDisk' ->> 'id') as "Managed Disk ID"
from
azure_compute_virtual_machine,
jsonb_array_elements(data_disks) as disk
where
lower(id) = $1;
EOQ
}
query "compute_virtual_machine_network_interfaces" {
sql = <<-EOQ
with vm_interface as (
select
vm.public_ips as public_ips,
vm.private_ips as private_ips,
n ->> 'id' as network_id
from
azure_compute_virtual_machine as vm,
jsonb_array_elements(network_interfaces) as n
where
lower(id) = $1
)
select
i.name as "Name",
i.provisioning_state as "Provisioning State",
vi.public_ips as "Public IPs",
vi.private_ips as "Private IPs",
(ip_config -> 'properties' ->> 'primary')::boolean as "Primary IP Config",
ip_config -> 'properties' ->> 'privateIPAddressVersion' as "Private IP Version",
lower(i.id) as "Network Interface ID"
from
vm_interface vi
left join azure_network_interface as i on lower(i.id) = lower(vi.network_id)
left join jsonb_array_elements(i.ip_configurations) as ip_config on true;
EOQ
}
query "compute_virtual_machine_guest_configuration_assignments" {
sql = <<-EOQ
select
g ->> 'name' as "Name",
g ->> 'complianceStatus' as "Compliance Status",
g -> 'guestConfiguration' -> 'configurationSetting' ->> 'allowModuleOverwrite' as "Allow Module Overwrite",
g -> 'guestConfiguration' -> 'configurationSetting' ->> 'configurationMode' as "Configuration Mode",
g -> 'guestConfiguration' -> 'configurationSetting' ->> 'configurationModeFrequencyMins' as "Configuration Mode Frequency Mins",
(g -> 'guestConfiguration' -> 'configurationSetting' ->> 'rebootIfNeeded')::boolean as "Reboot if Needed",
g -> 'guestConfiguration' -> 'configurationSetting' ->> 'refreshFrequencyMins' as "Refresh Frequency Mins",
g -> 'guestConfiguration' ->> 'version' as "Version"
from
azure_compute_virtual_machine,
jsonb_array_elements(guest_configuration_assignments) as g
where
lower(id) = $1;
EOQ
}