-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_network_detail.sp
718 lines (641 loc) · 16.6 KB
/
compute_network_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
dashboard "compute_network_detail" {
title = "GCP Compute Network Detail"
documentation = file("./dashboards/compute/docs/compute_network_detail.md")
tags = merge(local.compute_common_tags, {
type = "Detail"
})
input "network_id" {
title = "Select a network:"
query = query.compute_network_input
width = 4
}
container {
card {
width = 2
query = query.compute_network_mtu
args = [self.input.network_id.value]
}
card {
width = 2
query = query.compute_network_routing_mode
args = [self.input.network_id.value]
}
card {
width = 2
query = query.compute_network_subnet_count
args = [self.input.network_id.value]
}
card {
width = 2
query = query.compute_network_is_default
args = [self.input.network_id.value]
}
card {
width = 2
query = query.network_firewall_rules_count
args = [self.input.network_id.value]
}
card {
width = 2
query = query.auto_create_subnetwork
args = [self.input.network_id.value]
}
}
with "compute_vpn_gateways_for_compute_network" {
query = query.compute_vpn_gateways_for_compute_network
args = [self.input.network_id.value]
}
with "compute_backend_services_for_compute_network" {
query = query.compute_backend_services_for_compute_network
args = [self.input.network_id.value]
}
with "compute_firewalls_for_compute_network" {
query = query.compute_firewalls_for_compute_network
args = [self.input.network_id.value]
}
with "compute_forwarding_rules_for_compute_network" {
query = query.compute_forwarding_rules_for_compute_network
args = [self.input.network_id.value]
}
with "compute_instances_for_compute_network" {
query = query.compute_instances_for_compute_network
args = [self.input.network_id.value]
}
with "compute_routers_for_compute_network" {
query = query.compute_routers_for_compute_network
args = [self.input.network_id.value]
}
with "compute_subnetworks_for_compute_network" {
query = query.compute_subnetworks_for_compute_network
args = [self.input.network_id.value]
}
with "dns_policies_for_compute_network" {
query = query.dns_policies_for_compute_network
args = [self.input.network_id.value]
}
with "kubernetes_clusters_for_compute_network" {
query = query.kubernetes_clusters_for_compute_network
args = [self.input.network_id.value]
}
with "sql_database_instances_for_compute_network" {
query = query.sql_database_instances_for_compute_network
args = [self.input.network_id.value]
}
container {
graph {
title = "Relationships"
type = "graph"
node {
base = node.compute_backend_service
args = {
compute_backend_service_ids = with.compute_backend_services_for_compute_network.rows[*].service_id
}
}
node {
base = node.compute_firewall
args = {
compute_firewall_ids = with.compute_firewalls_for_compute_network.rows[*].firewall_id
}
}
node {
base = node.compute_forwarding_rule
args = {
compute_forwarding_rule_ids = with.compute_forwarding_rules_for_compute_network.rows[*].rule_id
}
}
node {
base = node.compute_instance
args = {
compute_instance_ids = with.compute_instances_for_compute_network.rows[*].instance_id
}
}
node {
base = node.compute_network
args = {
compute_network_ids = [self.input.network_id.value]
}
}
node {
base = node.compute_network_peers
args = {
compute_network_ids = [self.input.network_id.value]
}
}
node {
base = node.compute_router
args = {
compute_router_ids = with.compute_routers_for_compute_network.rows[*].router_id
}
}
node {
base = node.compute_subnetwork
args = {
compute_subnetwork_ids = with.compute_subnetworks_for_compute_network.rows[*].subnetwork_id
}
}
node {
base = node.compute_vpn_gateway
args = {
compute_vpn_gateway_ids = with.compute_vpn_gateways_for_compute_network.rows[*].gateway_id
}
}
node {
base = node.dns_policy
args = {
dns_policy_ids = with.dns_policies_for_compute_network.rows[*].policy_id
}
}
node {
base = node.kubernetes_cluster
args = {
kubernetes_cluster_ids = with.kubernetes_clusters_for_compute_network.rows[*].cluster_id
}
}
node {
base = node.sql_database_instance
args = {
database_instance_self_links = with.sql_database_instances_for_compute_network.rows[*].self_link
}
}
edge {
base = edge.compute_network_to_compute_backend_service
args = {
compute_network_ids = [self.input.network_id.value]
}
}
edge {
base = edge.compute_network_to_compute_firewall
args = {
compute_network_ids = [self.input.network_id.value]
}
}
edge {
base = edge.compute_network_to_compute_forwarding_rule
args = {
compute_network_ids = [self.input.network_id.value]
}
}
edge {
base = edge.compute_subnetwork_to_compute_instance
args = {
compute_subnetwork_ids = with.compute_subnetworks_for_compute_network.rows[*].subnetwork_id
}
}
edge {
base = edge.compute_network_to_compute_network_peers
args = {
compute_network_ids = [self.input.network_id.value]
}
}
edge {
base = edge.compute_network_to_compute_router
args = {
compute_network_ids = [self.input.network_id.value]
}
}
edge {
base = edge.compute_network_to_compute_subnetwork
args = {
compute_network_ids = [self.input.network_id.value]
}
}
edge {
base = edge.compute_network_to_dns_policy
args = {
compute_network_ids = [self.input.network_id.value]
}
}
edge {
base = edge.compute_subnetwork_to_kubernetes_cluster
args = {
compute_subnetwork_ids = with.compute_subnetworks_for_compute_network.rows[*].subnetwork_id
}
}
edge {
base = edge.compute_network_to_sql_database_instance
args = {
compute_network_ids = [self.input.network_id.value]
}
}
edge {
base = edge.compute_vpn_gateway_to_compute_network
args = {
compute_vpn_gateway_ids = with.compute_vpn_gateways_for_compute_network.rows[*].gateway_id
}
}
}
}
container {
container {
table {
title = "Overview"
width = 4
type = "line"
query = query.compute_network_overview
args = [self.input.network_id.value]
}
table {
title = "Peering Details"
width = 8
query = query.compute_network_peering
args = [self.input.network_id.value]
}
}
container {
table {
title = "Subnet Details"
query = query.compute_network_subnet
args = [self.input.network_id.value]
}
}
}
}
# Input queries
query "compute_network_input" {
sql = <<-EOQ
select
name as label,
id::text || '/' || project as value,
json_build_object(
'project', project,
'id', id::text
) as tags
from
gcp_compute_network
order by
title;
EOQ
}
# Card queries
query "compute_network_mtu" {
sql = <<-EOQ
select
'MTU (Bytes)' as label,
mtu as value
from
gcp_compute_network
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2);
EOQ
}
query "compute_network_subnet_count" {
sql = <<-EOQ
with compute_subnetwork as (
select
network_name,
project
from
gcp_compute_subnetwork
), compute_network as (
select
name,
project,
id
from
gcp_compute_network
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2)
)
select
'Subnets' as label,
count(*) as value,
case when count(*) > 0 then 'ok' else 'alert' end as type
from
compute_subnetwork s,
compute_network n
where
s.network_name = n.name
and s.project = n.project;
EOQ
}
query "compute_network_is_default" {
sql = <<-EOQ
select
'Default Network' as label,
case when name <> 'default' then 'ok' else 'Default Network' end as value,
case when name <> 'default' then 'ok' else 'alert' end as type
from
gcp_compute_network
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2);
EOQ
}
query "compute_network_routing_mode" {
sql = <<-EOQ
select
'Routing Mode' as label,
initcap(routing_mode) as value
from
gcp_compute_network
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2);
EOQ
}
query "network_firewall_rules_count" {
sql = <<-EOQ
with compute_firewall as (
select
network,
project
from
gcp_compute_firewall
), compute_network as (
select
name,
project,
id
from
gcp_compute_network
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2)
)
select
'Firewall Rules' as label,
count(f.*) as value,
case when count(f.*) > 0 then 'ok' else 'alert' end as type
from
compute_firewall f,
compute_network n
where
split_part(f.network, 'networks/', 2) = n.name
and f.project = n.project;
EOQ
}
query "auto_create_subnetwork" {
sql = <<-EOQ
select
'Auto Create Subnetwork' as label,
case when auto_create_subnetworks then 'Enabled' else 'Disabled' end as value,
case when auto_create_subnetworks and name = 'default' then 'ok' else 'alert' end as type
from
gcp_compute_network
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2);
EOQ
}
# With queries
query "compute_vpn_gateways_for_compute_network" {
sql = <<-EOQ
with compute_ha_vpn_gateway as (
select
id,
network
from
gcp_compute_ha_vpn_gateway
), compute_network as (
select
self_link,
id
from
gcp_compute_network
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2)
)
select
g.id::text as gateway_id
from
compute_ha_vpn_gateway g,
compute_network n
where
g.network = n.self_link;
EOQ
}
query "compute_backend_services_for_compute_network" {
sql = <<-EOQ
with compute_backend_service as (
select
id,
network
from
gcp_compute_backend_service
), compute_network as (
select
self_link,
id
from
gcp_compute_network
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2)
)
select
bs.id::text as service_id
from
compute_backend_service bs,
compute_network n
where
bs.network = n.self_link;
EOQ
}
query "compute_firewalls_for_compute_network" {
sql = <<-EOQ
with compute_firewall as (
select
id,
network
from
gcp_compute_firewall
), compute_network as (
select
self_link,
id
from
gcp_compute_network
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2)
)
select
f.id::text as firewall_id
from
compute_firewall f,
compute_network n
where
f.network = n.self_link;
EOQ
}
query "compute_forwarding_rules_for_compute_network" {
sql = <<-EOQ
select
fr.id::text as rule_id
from
gcp_compute_forwarding_rule fr,
gcp_compute_network n
where
split_part(fr.network, 'networks/', 2) = n.name
and fr.project = n.project
and n.id = (split_part($1, '/', 1))::bigint
and n.project = split_part($1, '/', 2)
union
select
fr.id::text as rule_id
from
gcp_compute_global_forwarding_rule fr,
gcp_compute_network n
where
split_part(fr.network, 'networks/', 2) = n.name
and fr.project = n.project
and n.id = (split_part($1, '/', 1))::bigint
and n.project = split_part($1, '/', 2);
EOQ
}
query "compute_instances_for_compute_network" {
sql = <<-EOQ
select
i.id::text || '/' || i.project as instance_id
from
gcp_compute_instance i,
gcp_compute_network n,
jsonb_array_elements(network_interfaces) as ni
where
n.self_link = ni ->> 'network'
and n.id = (split_part($1, '/', 1))::bigint
and n.project = split_part($1, '/', 2);
EOQ
}
query "compute_routers_for_compute_network" {
sql = <<-EOQ
select
r.id::text as router_id
from
gcp_compute_router r,
gcp_compute_network n
where
r.network = n.self_link
and n.id = (split_part($1, '/', 1))::bigint
and n.project = split_part($1, '/', 2);
EOQ
}
query "compute_subnetworks_for_compute_network" {
sql = <<-EOQ
with compute_subnetwork as (
select
id,
network,
project
from
gcp_compute_subnetwork
), compute_network as (
select
id,
self_link
from
gcp_compute_network
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2)
)
select
s.id::text || '/' || s.project as subnetwork_id
from
compute_subnetwork s,
compute_network n
where
s.network = n.self_link;
EOQ
}
query "dns_policies_for_compute_network" {
sql = <<-EOQ
select
p.id::text as policy_id
from
gcp_dns_policy p,
jsonb_array_elements(p.networks) pn,
gcp_compute_network n
where
pn ->> 'networkUrl' = n.self_link
and n.id = (split_part($1, '/', 1))::bigint
and n.project = split_part($1, '/', 2);
EOQ
}
query "kubernetes_clusters_for_compute_network" {
sql = <<-EOQ
select
c.id::text || '/' || c.project as cluster_id
from
gcp_kubernetes_cluster c,
gcp_compute_network n
where
c.network = n.name
and c.project = n.project
and n.id = (split_part($1, '/', 1))::bigint;
EOQ
}
query "sql_database_instances_for_compute_network" {
sql = <<-EOQ
select
i.self_link as self_link
from
gcp_sql_database_instance i,
gcp_compute_network n
where
n.self_link like '%' || (i.ip_configuration ->> 'privateNetwork') || '%'
and n.id = (split_part($1, '/', 1))::bigint
and n.project = split_part($1, '/', 2);
EOQ
}
# Other queries
query "compute_network_overview" {
sql = <<-EOQ
select
name as "Name",
id::text as "ID",
creation_timestamp as "Creation Time",
mtu as "MTU",
routing_mode as "Routing Mode",
location as "Location",
project as "Project"
from
gcp_compute_network
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2);
EOQ
}
query "compute_network_peering" {
sql = <<-EOQ
select
p ->> 'name' as "Name",
p ->> 'state' as "State",
p ->> 'stateDetails' as "State Details",
p ->> 'autoCreateRoutes' as "Auto Create Routes",
p ->> 'exchangeSubnetRoutes' as "Exchange Subnet Routes",
p ->> 'exportSubnetRoutesWithPublicIp' as "Export Subnet Routes With Public IP"
from
gcp_compute_network,
jsonb_array_elements(peerings) as p
where
id = (split_part($1, '/', 1))::bigint
and project = split_part($1, '/', 2);
EOQ
}
query "compute_network_subnet" {
sql = <<-EOQ
select
s.name as "Name",
s.id::text as "ID",
s.creation_timestamp as "Creation Time",
s.enable_flow_logs as "Enable Flow Logs",
s.log_config_enable as "Log Config Enabled",
s.gateway_address as "Gateway Address",
s.ip_cidr_range as "IPv4 CIDR Range",
s.ipv6_cidr_range as "IPv6 CIDR Range",
s.private_ip_google_access as "Private IPv4 Google Access",
s.private_ipv6_google_access as "Private IPv6 Google Access"
from
gcp_compute_subnetwork s,
gcp_compute_network n
where
s.network_name = n.name
and s.project = n.project
and n.id = (split_part($1, '/', 1))::bigint
and n.project = split_part($1, '/', 2);
EOQ
}