Skip to content

Commit 919cc4f

Browse files
ludookarpok78
authored andcommitted
Add optional support for fw policies via new vpc_configs variable, refactor factories variable in net stages (GoogleCloudPlatform#2801)
* net a * extend change to other networking stages * refactor factories config variable in net a * net b and c * complete net b * fix errors, add mtu * fix * fix * fix errors
1 parent 43be363 commit 919cc4f

34 files changed

+945
-306
lines changed

fast/stages/2-networking-a-simple/README.md

+12-12
Large diffs are not rendered by default.

fast/stages/2-networking-a-simple/dns-landing.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module "landing-dns-policy-googleapis" {
6868
project_id = module.landing-project.project_id
6969
name = "googleapis"
7070
factories_config = {
71-
rules = var.factories_config.dns_policy_rules_file
71+
rules = var.factories_config.dns_policy_rules
7272
}
7373
networks = {
7474
landing = module.landing-vpc.self_link

fast/stages/2-networking-a-simple/main.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ module "folder" {
6767

6868
module "firewall-policy-default" {
6969
source = "../../../modules/net-firewall-policy"
70-
name = var.factories_config.firewall_policy_name
70+
name = var.factories_config.firewall.hierarchical.policy_name
7171
parent_id = module.folder.id
7272
factories_config = {
73-
cidr_file_path = "${var.factories_config.data_dir}/cidrs.yaml"
74-
ingress_rules_file_path = "${var.factories_config.data_dir}/hierarchical-ingress-rules.yaml"
73+
cidr_file_path = var.factories_config.firewall.cidr_file
74+
ingress_rules_file_path = var.factories_config.firewall.hierarchical.ingress_rules
7575
}
7676
}
7777

fast/stages/2-networking-a-simple/monitoring.tf

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
# tfdoc:file:description Network monitoring dashboards.
1818

1919
locals {
20-
dashboard_path = "${var.factories_config.data_dir}/dashboards"
21-
dashboard_files = fileset(local.dashboard_path, "*.json")
20+
dashboard_files = fileset(var.factories_config.dashboards, "*.json")
2221
dashboards = {
2322
for filename in local.dashboard_files :
24-
filename => "${local.dashboard_path}/${filename}"
23+
filename => "${var.factories_config.dashboards}/${filename}"
2524
}
2625
}
2726

fast/stages/2-networking-a-simple/net-dev.tf

+51-13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616

1717
# tfdoc:file:description Dev spoke VPC and related resources.
1818

19+
locals {
20+
# streamline VPC configuration conditionals for modules by moving them here
21+
dev_cfg = {
22+
cloudnat = var.vpc_configs.dev.cloudnat.enable == true
23+
dns_logging = var.vpc_configs.dev.dns.enable_logging == true
24+
dns_policy = var.vpc_configs.dev.dns.create_inbound_policy == true
25+
fw_classic = var.vpc_configs.dev.firewall.use_classic == true
26+
fw_order = (
27+
var.vpc_configs.dev.firewall.policy_has_priority == true
28+
? "BEFORE_CLASSIC_FIREWALL"
29+
: "AFTER_CLASSIC_FIREWALL"
30+
)
31+
fw_policy = var.vpc_configs.dev.firewall.create_policy == true
32+
}
33+
}
34+
1935
module "dev-spoke-project" {
2036
source = "../../../modules/project"
2137
billing_account = var.billing_account.id
@@ -67,24 +83,26 @@ module "dev-spoke-project" {
6783
}
6884

6985
module "dev-spoke-vpc" {
70-
source = "../../../modules/net-vpc"
71-
project_id = module.dev-spoke-project.project_id
72-
name = "dev-spoke-0"
73-
mtu = 1500
74-
dns_policy = {
75-
logging = var.dns.enable_logging
86+
source = "../../../modules/net-vpc"
87+
project_id = module.dev-spoke-project.project_id
88+
name = "dev-spoke-0"
89+
mtu = var.vpc_configs.dev.mtu
90+
delete_default_routes_on_create = true
91+
dns_policy = !local.dev_cfg.dns_policy ? {} : {
92+
inbound = true
93+
logging = local.dev_cfg.dns_logging
7694
}
7795
factories_config = {
7896
context = { regions = var.regions }
79-
subnets_folder = "${var.factories_config.data_dir}/subnets/dev"
97+
subnets_folder = "${var.factories_config.subnets}/dev"
8098
}
81-
psa_configs = var.psa_ranges.dev
99+
firewall_policy_enforcement_order = local.dev_cfg.fw_order
100+
psa_configs = var.psa_ranges.dev
82101
# set explicit routes for googleapis in case the default route is deleted
83102
create_googleapis_routes = {
84103
private = true
85104
restricted = true
86105
}
87-
delete_default_routes_on_create = true
88106
routes = {
89107
default = {
90108
dest_range = "0.0.0.0/0"
@@ -97,20 +115,40 @@ module "dev-spoke-vpc" {
97115

98116
module "dev-spoke-firewall" {
99117
source = "../../../modules/net-vpc-firewall"
118+
count = local.dev_cfg.fw_classic ? 1 : 0
100119
project_id = module.dev-spoke-project.project_id
101120
network = module.dev-spoke-vpc.name
102121
default_rules_config = {
103122
disabled = true
104123
}
105124
factories_config = {
106-
cidr_tpl_file = "${var.factories_config.data_dir}/cidrs.yaml"
107-
rules_folder = "${var.factories_config.data_dir}/firewall-rules/dev"
125+
cidr_tpl_file = var.factories_config.firewall.cidr_file
126+
rules_folder = "${var.factories_config.firewall.classic_rules}/dev"
127+
}
128+
}
129+
130+
module "dev-firewall-policy" {
131+
source = "../../../modules/net-firewall-policy"
132+
count = local.dev_cfg.fw_policy ? 1 : 0
133+
name = "dev-spoke-0"
134+
parent_id = module.dev-spoke-project.project_id
135+
region = "global"
136+
attachments = {
137+
dev-spoke-0 = module.dev-spoke-vpc.id
138+
}
139+
# TODO: add context for security groups
140+
factories_config = {
141+
cidr_file_path = var.factories_config.firewall.cidr_file
142+
egress_rules_file_path = "${var.factories_config.firewall.policy_rules}/dev/egress.yaml"
143+
ingress_rules_file_path = "${var.factories_config.firewall.policy_rules}/dev/ingress.yaml"
108144
}
109145
}
110146

111147
module "dev-spoke-cloudnat" {
112-
source = "../../../modules/net-cloudnat"
113-
for_each = toset(var.enable_cloud_nat ? values(module.dev-spoke-vpc.subnet_regions) : [])
148+
source = "../../../modules/net-cloudnat"
149+
for_each = toset(
150+
local.dev_cfg.cloudnat ? values(module.dev-spoke-vpc.subnet_regions) : []
151+
)
114152
project_id = module.dev-spoke-project.project_id
115153
region = each.value
116154
name = "dev-nat-${local.region_shortnames[each.value]}"

fast/stages/2-networking-a-simple/net-landing.tf

+55-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@
1616

1717
# tfdoc:file:description Landing VPC and related resources.
1818

19+
locals {
20+
# streamline VPC configuration conditionals for modules by moving them here
21+
landing_cfg = {
22+
cloudnat = (
23+
local.spoke_connection != "ncc" &&
24+
var.vpc_configs.landing.cloudnat.enable == true
25+
)
26+
dns_logging = var.vpc_configs.landing.dns.enable_logging == true
27+
dns_policy = var.vpc_configs.landing.dns.create_inbound_policy == true
28+
fw_classic = (
29+
local.spoke_connection != "ncc" &&
30+
var.vpc_configs.landing.firewall.use_classic == true
31+
)
32+
fw_order = (
33+
var.vpc_configs.landing.firewall.policy_has_priority == true
34+
? "BEFORE_CLASSIC_FIREWALL"
35+
: "AFTER_CLASSIC_FIREWALL"
36+
)
37+
fw_policy = (
38+
local.spoke_connection != "ncc" &&
39+
var.vpc_configs.landing.firewall.create_policy == true
40+
)
41+
}
42+
}
43+
1944
module "landing-project" {
2045
source = "../../../modules/project"
2146
billing_account = var.billing_account.id
@@ -42,19 +67,20 @@ module "landing-project" {
4267
}
4368

4469
module "landing-vpc" {
45-
source = "../../../modules/net-vpc"
46-
project_id = module.landing-project.project_id
47-
name = "prod-landing-0"
48-
mtu = 1500
49-
dns_policy = {
70+
source = "../../../modules/net-vpc"
71+
project_id = module.landing-project.project_id
72+
name = "prod-landing-0"
73+
mtu = var.vpc_configs.landing.mtu
74+
delete_default_routes_on_create = true
75+
dns_policy = !local.landing_cfg.dns_policy ? {} : {
5076
inbound = true
51-
logging = var.dns.enable_logging
77+
logging = local.landing_cfg.dns_logging
5278
}
5379
factories_config = {
5480
context = { regions = var.regions }
55-
subnets_folder = "${var.factories_config.data_dir}/subnets/landing"
81+
subnets_folder = "${var.factories_config.subnets}/landing"
5682
}
57-
delete_default_routes_on_create = true
83+
firewall_policy_enforcement_order = local.landing_cfg.fw_order
5884
routes = {
5985
default = {
6086
dest_range = "0.0.0.0/0"
@@ -66,22 +92,39 @@ module "landing-vpc" {
6692
}
6793

6894
module "landing-firewall" {
69-
count = local.spoke_connection != "ncc" ? 1 : 0
7095
source = "../../../modules/net-vpc-firewall"
96+
count = local.landing_cfg.fw_classic ? 1 : 0
7197
project_id = module.landing-project.project_id
7298
network = module.landing-vpc.name
7399
default_rules_config = {
74100
disabled = true
75101
}
76102
factories_config = {
77-
cidr_tpl_file = "${var.factories_config.data_dir}/cidrs.yaml"
78-
rules_folder = "${var.factories_config.data_dir}/firewall-rules/landing"
103+
cidr_tpl_file = var.factories_config.firewall.cidr_file
104+
rules_folder = "${var.factories_config.firewall.classic_rules}/landing"
105+
}
106+
}
107+
108+
module "landing-firewall-policy" {
109+
source = "../../../modules/net-firewall-policy"
110+
count = local.landing_cfg.fw_policy ? 1 : 0
111+
name = "prod-landing-0"
112+
parent_id = module.landing-project.project_id
113+
region = "global"
114+
attachments = {
115+
landing-0 = module.landing-vpc.id
116+
}
117+
# TODO: add context for security groups
118+
factories_config = {
119+
cidr_file_path = var.factories_config.firewall.cidr_file
120+
egress_rules_file_path = "${var.factories_config.firewall.policy_rules}/landing/egress.yaml"
121+
ingress_rules_file_path = "${var.factories_config.firewall.policy_rules}/landing/ingress.yaml"
79122
}
80123
}
81124

82125
module "landing-nat-primary" {
83126
source = "../../../modules/net-cloudnat"
84-
count = var.enable_cloud_nat && local.spoke_connection != "ncc" ? 1 : 0
127+
count = local.landing_cfg.cloudnat ? 1 : 0
85128
project_id = module.landing-project.project_id
86129
region = var.regions.primary
87130
name = local.region_shortnames[var.regions.primary]

fast/stages/2-networking-a-simple/net-prod.tf

+51-13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616

1717
# tfdoc:file:description Production spoke VPC and related resources.
1818

19+
locals {
20+
# streamline VPC configuration conditionals for modules by moving them here
21+
prod_cfg = {
22+
cloudnat = var.vpc_configs.prod.cloudnat.enable == true
23+
dns_logging = var.vpc_configs.prod.dns.enable_logging == true
24+
dns_policy = var.vpc_configs.prod.dns.create_inbound_policy == true
25+
fw_classic = var.vpc_configs.prod.firewall.use_classic == true
26+
fw_order = (
27+
var.vpc_configs.prod.firewall.policy_has_priority == true
28+
? "BEFORE_CLASSIC_FIREWALL"
29+
: "AFTER_CLASSIC_FIREWALL"
30+
)
31+
fw_policy = var.vpc_configs.prod.firewall.create_policy == true
32+
}
33+
}
34+
1935
module "prod-spoke-project" {
2036
source = "../../../modules/project"
2137
billing_account = var.billing_account.id
@@ -67,19 +83,21 @@ module "prod-spoke-project" {
6783
}
6884

6985
module "prod-spoke-vpc" {
70-
source = "../../../modules/net-vpc"
71-
project_id = module.prod-spoke-project.project_id
72-
name = "prod-spoke-0"
73-
mtu = 1500
74-
dns_policy = {
75-
logging = var.dns.enable_logging
86+
source = "../../../modules/net-vpc"
87+
project_id = module.prod-spoke-project.project_id
88+
name = "prod-spoke-0"
89+
mtu = var.vpc_configs.prod.mtu
90+
delete_default_routes_on_create = true
91+
dns_policy = !local.prod_cfg.dns_policy ? {} : {
92+
inbound = true
93+
logging = local.prod_cfg.dns_logging
7694
}
7795
factories_config = {
7896
context = { regions = var.regions }
79-
subnets_folder = "${var.factories_config.data_dir}/subnets/prod"
97+
subnets_folder = "${var.factories_config.subnets}/prod"
8098
}
81-
psa_configs = var.psa_ranges.prod
82-
delete_default_routes_on_create = true
99+
firewall_policy_enforcement_order = local.prod_cfg.fw_order
100+
psa_configs = var.psa_ranges.prod
83101
routes = {
84102
default = {
85103
dest_range = "0.0.0.0/0"
@@ -92,20 +110,40 @@ module "prod-spoke-vpc" {
92110

93111
module "prod-spoke-firewall" {
94112
source = "../../../modules/net-vpc-firewall"
113+
count = local.prod_cfg.fw_classic ? 1 : 0
95114
project_id = module.prod-spoke-project.project_id
96115
network = module.prod-spoke-vpc.name
97116
default_rules_config = {
98117
disabled = true
99118
}
100119
factories_config = {
101-
cidr_tpl_file = "${var.factories_config.data_dir}/cidrs.yaml"
102-
rules_folder = "${var.factories_config.data_dir}/firewall-rules/prod"
120+
cidr_tpl_file = var.factories_config.firewall.cidr_file
121+
rules_folder = "${var.factories_config.firewall.classic_rules}/prod"
122+
}
123+
}
124+
125+
module "prod-firewall-policy" {
126+
source = "../../../modules/net-firewall-policy"
127+
count = local.prod_cfg.fw_policy ? 1 : 0
128+
name = "prod-spoke-0"
129+
parent_id = module.prod-spoke-project.project_id
130+
region = "global"
131+
attachments = {
132+
prod-spoke-0 = module.prod-spoke-vpc.id
133+
}
134+
# TODO: add context for security groups
135+
factories_config = {
136+
cidr_file_path = var.factories_config.firewall.cidr_file
137+
egress_rules_file_path = "${var.factories_config.firewall.policy_rules}/prod/egress.yaml"
138+
ingress_rules_file_path = "${var.factories_config.firewall.policy_rules}/prod/ingress.yaml"
103139
}
104140
}
105141

106142
module "prod-spoke-cloudnat" {
107-
source = "../../../modules/net-cloudnat"
108-
for_each = toset(var.enable_cloud_nat ? values(module.prod-spoke-vpc.subnet_regions) : [])
143+
source = "../../../modules/net-cloudnat"
144+
for_each = toset(
145+
local.prod_cfg.cloudnat ? values(module.prod-spoke-vpc.subnet_regions) : []
146+
)
109147
project_id = module.prod-spoke-project.project_id
110148
region = each.value
111149
name = "prod-nat-${local.region_shortnames[each.value]}"

0 commit comments

Comments
 (0)