-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc_detail.sp
1036 lines (889 loc) · 22 KB
/
vpc_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
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
dashboard "vpc_detail" {
title = "AliCloud VPC Detail"
documentation = file("./dashboards/vpc/docs/vpc_detail.md")
tags = merge(local.vpc_common_tags, {
type = "Detail"
})
input "vpc_id" {
title = "Select a VPC:"
query = query.vpc_input
width = 4
}
container {
card {
width = 3
query = query.vpc_ipv4_count
args = [self.input.vpc_id.value]
}
card {
width = 3
query = query.vpc_ipv6_count
args = [self.input.vpc_id.value]
}
card {
width = 3
query = query.vpc_vswitch_count
args = [self.input.vpc_id.value]
}
card {
width = 3
query = query.vpc_is_default
args = [self.input.vpc_id.value]
}
}
with "ecs_instances_for_vpc" {
query = query.ecs_instances_for_vpc
args = [self.input.vpc_id.value]
}
with "ecs_network_interfaces_for_vpc" {
query = query.ecs_network_interfaces_for_vpc
args = [self.input.vpc_id.value]
}
with "ecs_security_groups_for_vpc" {
query = query.ecs_security_groups_for_vpc
args = [self.input.vpc_id.value]
}
with "rds_instances_for_vpc" {
query = query.rds_instances_for_vpc
args = [self.input.vpc_id.value]
}
with "vpc_dhcp_options_sets_for_vpc" {
query = query.vpc_dhcp_options_sets_for_vpc
args = [self.input.vpc_id.value]
}
with "vpc_flow_logs_for_vpc" {
query = query.vpc_flow_logs_for_vpc
args = [self.input.vpc_id.value]
}
with "vpc_nat_gateways_for_vpc" {
query = query.vpc_nat_gateways_for_vpc
args = [self.input.vpc_id.value]
}
with "vpc_route_tables_for_vpc" {
query = query.vpc_route_tables_for_vpc
args = [self.input.vpc_id.value]
}
with "vpc_vswitches_for_vpc" {
query = query.vpc_vswitches_for_vpc
args = [self.input.vpc_id.value]
}
container {
graph {
title = "Relationships"
type = "graph"
node {
base = node.ecs_instance
args = {
ecs_instance_arns = with.ecs_instances_for_vpc.rows[*].instance_arn
}
}
node {
base = node.ecs_network_interface
args = {
ecs_network_interface_ids = with.ecs_network_interfaces_for_vpc.rows[*].eni_id
}
}
node {
base = node.ecs_security_group
args = {
ecs_security_group_ids = with.ecs_security_groups_for_vpc.rows[*].security_group_id
}
}
node {
base = node.rds_instance
args = {
rds_instance_arns = with.rds_instances_for_vpc.rows[*].rds_instance_arn
}
}
node {
base = node.vpc_availability_zone
args = {
vpc_vpc_ids = [self.input.vpc_id.value]
}
}
node {
base = node.vpc_dhcp_option_set
args = {
vpc_dhcp_option_set_ids = with.vpc_dhcp_options_sets_for_vpc.rows[*].dhcp_options_set_id
}
}
node {
base = node.vpc_flow_log
args = {
vpc_flow_log_ids = with.vpc_flow_logs_for_vpc.rows[*].flow_log_id
}
}
node {
base = node.vpc_nat_gateway
args = {
vpc_nat_gateway_ids = with.vpc_nat_gateways_for_vpc.rows[*].gateway_id
}
}
node {
base = node.vpc_route_table
args = {
vpc_route_table_ids = with.vpc_route_tables_for_vpc.rows[*].route_table_id
}
}
node {
base = node.vpc_vpc
args = {
vpc_vpc_ids = [self.input.vpc_id.value]
}
}
node {
base = node.vpc_vpn_gateway
args = {
vpc_vpc_ids = [self.input.vpc_id.value]
}
}
node {
base = node.vpc_vswitch
args = {
vpc_vswitch_ids = with.vpc_vswitches_for_vpc.rows[*].vswitch_id
}
}
edge {
base = edge.vpc_availability_zone_to_vpc_vswitch
args = {
vpc_vpc_ids = [self.input.vpc_id.value]
}
}
edge {
base = edge.vpc_vpc_to_vpc_availability_zone
args = {
vpc_vpc_ids = [self.input.vpc_id.value]
}
}
edge {
base = edge.vpc_vpc_to_vpc_route_table
args = {
vpc_vpc_ids = [self.input.vpc_id.value]
}
}
edge {
base = edge.vpc_vpc_to_ecs_security_group
args = {
vpc_vpc_ids = [self.input.vpc_id.value]
}
}
edge {
base = edge.vpc_vpc_to_vpc_dhcp_option_set
args = {
vpc_dhcp_option_set_ids = with.vpc_dhcp_options_sets_for_vpc.rows[*].dhcp_options_set_id
}
}
edge {
base = edge.vpc_vpc_to_vpc_flow_log
args = {
vpc_vpc_ids = [self.input.vpc_id.value]
}
}
edge {
base = edge.vpc_vpc_to_vpc_vpn_gateway
args = {
vpc_vpc_ids = [self.input.vpc_id.value]
}
}
edge {
base = edge.vpc_vswitch_to_ecs_instance
args = {
vpc_vswitch_ids = with.vpc_vswitches_for_vpc.rows[*].vswitch_id
}
}
edge {
base = edge.vpc_vswitch_to_ecs_network_interface
args = {
vpc_vswitch_ids = with.vpc_vswitches_for_vpc.rows[*].vswitch_id
}
}
edge {
base = edge.vpc_vswitch_to_nat_gateway
args = {
vpc_nat_gateway_ids = with.vpc_nat_gateways_for_vpc.rows[*].gateway_id
}
}
edge {
base = edge.vpc_vswitch_to_rds_instance
args = {
vpc_vswitch_ids = with.vpc_vswitches_for_vpc.rows[*].vswitch_id
}
}
edge {
base = edge.vpc_vswitch_to_vpc_route_table
args = {
vpc_vswitch_ids = with.vpc_vswitches_for_vpc.rows[*].vswitch_id
}
}
}
}
container {
container {
width = 6
table {
title = "Overview"
type = "line"
width = 6
query = query.vpc_overview
args = [self.input.vpc_id.value]
}
table {
title = "Tags"
width = 6
query = query.vpc_tags
args = [self.input.vpc_id.value]
}
}
container {
width = 6
table {
title = "CIDR Blocks"
query = query.vpc_cidr_blocks
args = [self.input.vpc_id.value]
}
table {
title = "DHCP Options"
query = query.vpc_dhcp_options
args = [self.input.vpc_id.value]
}
}
}
container {
title = "vSwitches"
chart {
title = "vSwitches by Zone"
type = "column"
width = 4
query = query.vpc_vswitch_by_az
args = [self.input.vpc_id.value]
}
table {
query = query.vpc_vswitches_detail
width = 8
column "vSwitch ID" {
href = "${dashboard.vpc_vswitch_detail.url_path}?input.vswitch_id={{.properties.'vSwitch ID' | @uri}}"
}
args = [self.input.vpc_id.value]
}
}
container {
table {
title = "Route Tables"
query = query.vpc_route_tables_detail
width = 6
args = [self.input.vpc_id.value]
}
table {
title = "Routes"
query = query.vpc_routes_detail
width = 6
args = [self.input.vpc_id.value]
}
}
container {
title = "NACLs"
flow {
base = flow.nacl_flow
title = "Ingress NACLs"
width = 6
query = query.vpc_ingress_nacl_sankey
args = [self.input.vpc_id.value]
}
flow {
base = flow.nacl_flow
title = "Egress NACLs"
width = 6
query = query.vpc_egress_nacl_sankey
args = [self.input.vpc_id.value]
}
}
container {
table {
title = "Security Groups"
query = query.vpc_security_groups_detail
width = 6
args = [self.input.vpc_id.value]
}
table {
title = "Gateways"
query = query.vpc_gateways_detail
width = 6
args = [self.input.vpc_id.value]
}
}
}
flow "nacl_flow" {
width = 6
type = "sankey"
category "drop" {
color = "alert"
}
category "accept" {
color = "ok"
}
}
query "vpc_input" {
sql = <<-EOQ
select
title as label,
vpc_id as value,
json_build_object(
'account_id', account_id,
'region', region,
'vpc_id', vpc_id
) as tags
from
alicloud_vpc
order by
title;
EOQ
}
query "vpc_ipv4_count" {
sql = <<-EOQ
with cidrs as (
select
power(2, 32 - masklen(cidr_block)) as num_ips
from
alicloud_vpc
where
vpc_id = $1
union all
select
power(2, 32 - masklen(b:: cidr)) as num_ips
from
alicloud_vpc,
jsonb_array_elements_text(secondary_cidr_blocks) as b
where
vpc_id = $1
)
select
sum(num_ips) as "IPv4 Addresses"
from
cidrs;
EOQ
}
query "vpc_ipv6_count" {
sql = <<-EOQ
with cidrs as (
select
power(2, 128 - masklen((b ->> 'Ipv6CidrBlock'):: cidr)) as num_ips
from
alicloud_vpc,
jsonb_array_elements(ipv6_cidr_blocks) as b
where
vpc_id = $1
)
select
sum(num_ips) as "IPv6 Addresses"
from
cidrs;
EOQ
}
query "vpc_vswitch_count" {
sql = <<-EOQ
select
'vSwitches' as label,
count(*) as value,
case when count(*) > 0 then 'ok' else 'alert' end as type
from
alicloud_vpc_vswitch
where
vpc_id = $1;
EOQ
}
query "vpc_is_default" {
sql = <<-EOQ
select
'Default VPC' as label,
case when not is_default then 'ok' else 'Default VPC' end as value,
case when not is_default then 'ok' else 'alert' end as type
from
alicloud_vpc
where
vpc_id = $1;
EOQ
}
# with queries
query "ecs_instances_for_vpc" {
sql = <<-EOQ
select
arn as instance_arn
from
alicloud_ecs_instance as i
where
vpc_id = $1;
EOQ
}
query "ecs_network_interfaces_for_vpc" {
sql = <<-EOQ
select
network_interface_id as eni_id
from
alicloud_ecs_network_interface
where
vpc_id = $1;
EOQ
}
query "rds_instances_for_vpc" {
sql = <<-EOQ
select
arn as rds_instance_arn
from
alicloud_rds_instance
where
vpc_id = $1;
EOQ
}
query "vpc_dhcp_options_sets_for_vpc" {
sql = <<-EOQ
with vpcs as (
select
jsonb_array_elements(associate_vpcs)->> 'VpcId' as vpc_id
from
alicloud_vpc_dhcp_options_set
)
select
d.dhcp_options_set_id
from
alicloud_vpc_dhcp_options_set as d,
vpcs as v
where
v.vpc_id = $1;
EOQ
}
query "vpc_flow_logs_for_vpc" {
sql = <<-EOQ
select
flow_log_id as flow_log_id
from
alicloud_vpc_flow_log
where
resource_id = $1;
EOQ
}
query "vpc_nat_gateways_for_vpc" {
sql = <<-EOQ
select
nat_gateway_id as gateway_id
from
alicloud_vpc_nat_gateway
where
vpc_id = $1;
EOQ
}
query "ecs_security_groups_for_vpc" {
sql = <<-EOQ
select
security_group_id as security_group_id
from
alicloud_ecs_security_group
where
vpc_id = $1;
EOQ
}
query "vpc_vswitches_for_vpc" {
sql = <<-EOQ
select
vswitch_id as vswitch_id
from
alicloud_vpc_vswitch
where
vpc_id = $1;
EOQ
}
query "vpc_route_tables_for_vpc" {
sql = <<-EOQ
select
route_table_id as route_table_id
from
alicloud_vpc_route_table
where
vpc_id = $1;
EOQ
}
query "vpc_overview" {
sql = <<-EOQ
select
vpc_id as "VPC ID",
title as "Title",
region as "Region",
account_id as "Account ID"
from
alicloud_vpc
where
vpc_id = $1
EOQ
}
query "vpc_tags" {
sql = <<-EOQ
select
tag ->> 'Key' as "Key",
tag ->> 'Value' as "Value"
from
alicloud_vpc,
jsonb_array_elements(tags_src) as tag
where
vpc_id = $1
order by
tag ->> 'Key';
EOQ
}
query "vpc_cidr_blocks" {
sql = <<-EOQ
select
cidr_block as "CIDR Block",
power(2, 32 - masklen(cidr_block)) as "Total IPs"
from
alicloud_vpc
where
vpc_id = $1
union all
select
b::cidr as "CIDR Block",
power(2, 32 - masklen(b:: cidr)) as "Total IPs"
from
alicloud_vpc,
jsonb_array_elements_text(secondary_cidr_blocks) as b
where
vpc_id = $1
union all
select
(b ->> 'Ipv6CidrBlock'):: cidr as "CIDR Block",
power(2, 128 - masklen((b ->> 'Ipv6CidrBlock'):: cidr)) as "Total IPs"
from
alicloud_vpc,
jsonb_array_elements(ipv6_cidr_blocks) as b
where
vpc_id = $1;
EOQ
}
query "vpc_dhcp_options" {
sql = <<-EOQ
select
distinct dhcp_options_set_id as "DHCP Options Set ID",
name as "Name",
status as "Status",
domain_name as "Domain Name",
domain_name_servers as "Domain Name Servers",
boot_file_name as "Boot File Name",
tftp_server_name as "TFTP Server Name"
from
alicloud_vpc_dhcp_options_set,
jsonb_array_elements(associate_vpcs) as v
where
v ->> 'VpcId' = $1
order by
dhcp_options_set_id;
EOQ
}
query "vpc_vswitch_by_az" {
sql = <<-EOQ
select
zone_id,
count(*)
from
alicloud_vpc_vswitch
where
vpc_id = $1
group by
zone_id
order by
zone_id;
EOQ
}
query "vpc_vswitches_detail" {
sql = <<-EOQ
with vSwitches as (
select
vswitch_id,
name,
cidr_block,
zone_id,
available_ip_address_count,
power(2, 32 - masklen(cidr_block :: cidr)) -1 as raw_size
from
alicloud_vpc_vswitch
where
vpc_id = $1
)
select
vswitch_id as "vSwitch ID",
name as "Name",
cidr_block as "CIDR Block",
zone_id as "Zone ID",
available_ip_address_count as "Available IPs",
power(2, 32 - masklen(cidr_block :: cidr)) -1 as "Total IPs",
round(100 * (available_ip_address_count / (raw_size))::numeric, 2) as "% Free"
from
vSwitches
order by
vswitch_id;
EOQ
}
query "vpc_route_tables_detail" {
sql = <<-EOQ
select
route_table_id as "Route Table ID",
name as "Name"
from
alicloud_vpc_route_table
where
vpc_id = $1
order by
route_table_id;
EOQ
}
query "vpc_routes_detail" {
sql = <<-EOQ
select
route_table_id as "Route Table ID",
name as "Name",
r ->> 'Status' as "Status",
r ->> 'InstanceId' as "Gateway",
r ->> 'DestinationCidrBlock' as "Destination CIDR",
case
when v is null then vpc_id
else v
end as "Associated To"
from
alicloud_vpc_route_table,
jsonb_array_elements(route_entries) as r,
jsonb_array_elements_text(vswitch_ids) as v
where
vpc_id = $1
order by
route_table_id,
"Associated To";
EOQ
}
query "vpc_security_groups_detail" {
sql = <<-EOQ
select
name as "Group Name",
security_group_id as "Group ID",
creation_time as "Creation Time",
description as "Description"
from
alicloud_ecs_security_group
where
vpc_id = $1;
EOQ
}
query "vpc_gateways_detail" {
sql = <<-EOQ
select
vpn_gateway_id as "ID",
name as "Name",
'alicloud_vpc_vpn_gateway' as "Type",
status as "Status"
from
alicloud_vpc_vpn_gateway
where
vpc_id = $1
union all
select
nat_gateway_id as "ID",
name as "Name",
'alicloud_vpc_nat_gateway' as "Type",
status as "Status"
from
alicloud_vpc_nat_gateway
where
vpc_id = $1;
EOQ
}
query "vpc_ingress_nacl_sankey" {
sql = <<-EOQ
with aces as (
select
title,
network_acl_id,
e ->> 'SourceCidrIp' as cidr_block,
SPLIT_PART(e ->> 'Port','/',2) as to_port,
SPLIT_PART(e ->> 'Port','/',1) as from_port,
e ->> 'Policy' as rule_action,
e ->> 'NetworkAclEntryName' as rule_name,
e ->> 'Protocol' as protocol,
case when e ->> 'Policy' = 'accept' then 'Accept ' else 'Drop ' end ||
case
when e->>'Protocol' = 'all' then 'All Traffic'
when e->>'Protocol' = 'icmp' then 'All ICMP'
when e->>'Protocol' = 'tcp' and (e ->> 'Port' = '1/1' or e ->> 'Port' = '1/65535') then 'All TCP'
when e->>'Protocol' = 'udp' and (e ->> 'Port' = '1/1' or e ->> 'Port' = '1/65535') then 'All UDP'
when e->>'Protocol' = 'tcp' and SPLIT_PART(e ->> 'Port','/',2) = SPLIT_PART(e ->> 'Port','/',1)
then concat(SPLIT_PART(e ->> 'Port','/',2), '/TCP')
when e->>'Protocol' = 'udp' and SPLIT_PART(e ->> 'Port','/',2) = SPLIT_PART(e ->> 'Port','/',1)
then concat(SPLIT_PART(e ->> 'Port','/',2), '/UDP')
when e->>'Protocol' = 'tcp' and SPLIT_PART(e ->> 'Port','/',2) <> SPLIT_PART(e ->> 'Port','/',1)
then concat(SPLIT_PART(e ->> 'Port','/',2), '-', SPLIT_PART(e ->> 'Port','/',1), '/TCP')
when e->>'Protocol' = 'udp' and SPLIT_PART(e ->> 'Port','/',2) <> SPLIT_PART(e ->> 'Port','/',1)
then concat(SPLIT_PART(e ->> 'Port','/',2), '-', SPLIT_PART(e ->> 'Port','/',1), '/UDP')
else concat('Procotol: ', e->>'Protocol')
end as rule_description,
r ->> 'ResourceId' as associated_id
from
alicloud_vpc_network_acl,
jsonb_array_elements(ingress_acl_entries) as e,
jsonb_array_elements(resources) as r
where
vpc_id = $1
)
-- CIDR Nodes
select
distinct cidr_block as id,
cidr_block as title,
'cidr_block' as category,
null as from_id,
null as to_id
from aces
-- Rule Nodes
union select
concat(network_acl_id,'_',rule_name) as id,
rule_description as title,
'rule' as category,
null as from_id,
null as to_id
from aces
-- ACL Nodes
union select
distinct network_acl_id as id,
network_acl_id as title,
'nacl' as category,
null as from_id,
null as to_id
from aces
-- vswitch node
union select
distinct associated_id as id,
associated_id as title,
'vswitch' as category,
null as from_id,
null as to_id
from aces
-- ip -> rule edge
union select
null as id,
null as title,
rule_action as category,
cidr_block as from_id,
concat(network_acl_id,'_',rule_name) as to_id
from aces
-- rule -> NACL edge
union select
null as id,
null as title,
rule_action as category,
concat(network_acl_id,'_',rule_name) as from_id,
network_acl_id as to_id
from aces
-- nacl -> vswitch edge
union select
null as id,
null as title,
'attached' as category,
network_acl_id as from_id,
associated_id as to_id
from aces
EOQ
}
query "vpc_egress_nacl_sankey" {
sql = <<-EOQ
with aces as (
select
title,
network_acl_id,
e ->> 'SourceCidrIp' as cidr_block,
SPLIT_PART(e ->> 'Port','/',2) as to_port,
SPLIT_PART(e ->> 'Port','/',1) as from_port,
e ->> 'Policy' as rule_action,
e ->> 'NetworkAclEntryName' as rule_name,
e ->> 'Protocol' as protocol,
case when e ->> 'Policy' = 'accept' then 'Accept ' else 'Drop ' end ||
case
when e->>'Protocol' = 'all' then 'All Traffic'
when e->>'Protocol' = 'icmp' then 'All ICMP'
when e->>'Protocol' = 'tcp' and (e ->> 'Port' = '1/1' or e ->> 'Port' = '1/65535') then 'All TCP'
when e->>'Protocol' = 'udp' and (e ->> 'Port' = '1/1' or e ->> 'Port' = '1/65535') then 'All UDP'
when e->>'Protocol' = 'tcp' and SPLIT_PART(e ->> 'Port','/',2) = SPLIT_PART(e ->> 'Port','/',1)
then concat(SPLIT_PART(e ->> 'Port','/',2), '/TCP')
when e->>'Protocol' = 'udp' and SPLIT_PART(e ->> 'Port','/',2) = SPLIT_PART(e ->> 'Port','/',1)
then concat(SPLIT_PART(e ->> 'Port','/',2), '/UDP')
when e->>'Protocol' = 'tcp' and SPLIT_PART(e ->> 'Port','/',2) <> SPLIT_PART(e ->> 'Port','/',1)
then concat(SPLIT_PART(e ->> 'Port','/',2), '-', SPLIT_PART(e ->> 'Port','/',1), '/TCP')
when e->>'Protocol' = 'udp' and SPLIT_PART(e ->> 'Port','/',2) <> SPLIT_PART(e ->> 'Port','/',1)
then concat(SPLIT_PART(e ->> 'Port','/',2), '-', SPLIT_PART(e ->> 'Port','/',1), '/UDP')
else concat('Procotol: ', e->>'Protocol')
end as rule_description,
r ->> 'ResourceId' as associated_id
from
alicloud_vpc_network_acl,
jsonb_array_elements(ingress_acl_entries) as e,
jsonb_array_elements(resources) as r
where
vpc_id = $1
)
-- Subnet Nodes
select
distinct associated_id as id,
associated_id as title,
'vswitch' as category,
null as from_id,
null as to_id,
0 as depth
from aces
-- ACL Nodes
union select
distinct network_acl_id as id,
network_acl_id as title,
'nacl' as category,
null as from_id,
null as to_id,
1 as depth
from aces
-- Rule Nodes
union select
concat(network_acl_id, '_',rule_name) as id,
rule_description as title,
'rule' as category,
null as from_id,
null as to_id,
2 as depth
from aces
-- CIDR Nodes
union select
distinct cidr_block as id,
cidr_block as title,
'cidr_block' as category,
null as from_id,
null as to_id,
3 as depth