Skip to content

Commit 92265ba

Browse files
authored
fix: added rule for internal lb. (#504)
1 parent cbcd425 commit 92265ba

File tree

2 files changed

+44
-81
lines changed

2 files changed

+44
-81
lines changed

modules/network/locals.tf

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -247,28 +247,28 @@ locals {
247247
]
248248

249249
# Combine supplied allow list and the public load balancer subnet
250-
internal_lb_allowed_cidrs = concat(var.internal_lb_allowed_cidrs, tolist([local.pub_lb_subnet]))
250+
internal_lb_allowed_cidrs = var.load_balancers == "both"? concat(var.internal_lb_allowed_cidrs, tolist([local.pub_lb_subnet])) : var.internal_lb_allowed_cidrs
251251

252252
# Create a Cartesian product of allowed cidrs and ports
253253
internal_lb_allowed_cidrs_and_ports = setproduct(local.internal_lb_allowed_cidrs, var.internal_lb_allowed_ports)
254254

255255
pub_lb_egress = [
256-
{
257-
description = "Allow stateful egress to internal load balancers subnet on port 80",
258-
destination = local.int_lb_subnet,
259-
destination_type = "CIDR_BLOCK",
260-
protocol = local.tcp_protocol,
261-
port = 80
262-
stateless = false
263-
},
264-
{
265-
description = "Allow stateful egress to internal load balancers subnet on port 443",
266-
destination = local.int_lb_subnet,
267-
destination_type = "CIDR_BLOCK",
268-
protocol = local.tcp_protocol,
269-
port = 443
270-
stateless = false
271-
},
256+
# {
257+
# description = "Allow stateful egress to internal load balancers subnet on port 80",
258+
# destination = local.int_lb_subnet,
259+
# destination_type = "CIDR_BLOCK",
260+
# protocol = local.tcp_protocol,
261+
# port = 80
262+
# stateless = false
263+
# },
264+
# {
265+
# description = "Allow stateful egress to internal load balancers subnet on port 443",
266+
# destination = local.int_lb_subnet,
267+
# destination_type = "CIDR_BLOCK",
268+
# protocol = local.tcp_protocol,
269+
# port = 443
270+
# stateless = false
271+
# },
272272
{
273273
description = "Allow stateful egress to workers. Required for NodePorts",
274274
destination = local.workers_subnet,

modules/network/nsgs.tf

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ resource "oci_core_network_security_group_security_rule" "cp_egress" {
3838

3939
count = length(local.cp_egress)
4040

41-
lifecycle {
42-
ignore_changes = [destination, destination_type, direction, protocol, tcp_options]
43-
}
4441
}
4542

4643
resource "oci_core_network_security_group_security_rule" "cp_ingress" {
@@ -73,9 +70,6 @@ resource "oci_core_network_security_group_security_rule" "cp_ingress" {
7370

7471
count = length(local.cp_ingress)
7572

76-
lifecycle {
77-
ignore_changes = [source, source_type, direction, protocol, tcp_options]
78-
}
7973
}
8074

8175
resource "oci_core_network_security_group_security_rule" "cp_ingress_additional_cidrs" {
@@ -102,9 +96,6 @@ resource "oci_core_network_security_group_security_rule" "cp_ingress_additional_
10296

10397
count = length(var.control_plane_allowed_cidrs)
10498

105-
lifecycle {
106-
ignore_changes = [source, source_type, direction, protocol, tcp_options]
107-
}
10899
}
109100

110101
# workers nsg and rules
@@ -144,9 +135,6 @@ resource "oci_core_network_security_group_security_rule" "workers_egress" {
144135

145136
count = length(local.workers_egress)
146137

147-
lifecycle {
148-
ignore_changes = [destination, destination_type, direction, protocol, tcp_options]
149-
}
150138
}
151139

152140
# add this rule separately so it can be controlled independently
@@ -162,9 +150,6 @@ resource "oci_core_network_security_group_security_rule" "workers_egress_interne
162150

163151
count = var.allow_worker_internet_access == true ? 1 : 0
164152

165-
lifecycle {
166-
ignore_changes = [destination, destination_type, direction, protocol, tcp_options]
167-
}
168153
}
169154

170155
resource "oci_core_network_security_group_security_rule" "workers_ingress" {
@@ -197,9 +182,6 @@ resource "oci_core_network_security_group_security_rule" "workers_ingress" {
197182

198183
count = length(local.workers_ingress)
199184

200-
lifecycle {
201-
ignore_changes = [source, source_type, direction, protocol, tcp_options]
202-
}
203185
}
204186

205187
# add the next 4 rules separately so it can be controlled independently based on which lbs are created
@@ -222,9 +204,6 @@ resource "oci_core_network_security_group_security_rule" "workers_ingress_from_i
222204

223205
count = var.load_balancers == "internal" || var.load_balancers == "both" ? 1 : 0
224206

225-
lifecycle {
226-
ignore_changes = [source, source_type, direction, protocol, tcp_options]
227-
}
228207
}
229208

230209
resource "oci_core_network_security_group_security_rule" "workers_healthcheck_ingress_from_int_lb" {
@@ -246,9 +225,6 @@ resource "oci_core_network_security_group_security_rule" "workers_healthcheck_in
246225

247226
count = var.load_balancers == "internal" || var.load_balancers == "both" ? 1 : 0
248227

249-
lifecycle {
250-
ignore_changes = [source, source_type, direction, protocol, tcp_options]
251-
}
252228
}
253229

254230
resource "oci_core_network_security_group_security_rule" "workers_ingress_from_pub_lb" {
@@ -270,9 +246,6 @@ resource "oci_core_network_security_group_security_rule" "workers_ingress_from_p
270246

271247
count = var.load_balancers == "public" || var.load_balancers == "both" ? 1 : 0
272248

273-
lifecycle {
274-
ignore_changes = [source, source_type, direction, protocol, tcp_options]
275-
}
276249
}
277250

278251
resource "oci_core_network_security_group_security_rule" "workers_healthcheck_ingress_from_pub_lb" {
@@ -294,9 +267,6 @@ resource "oci_core_network_security_group_security_rule" "workers_healthcheck_in
294267

295268
count = var.load_balancers == "public" || var.load_balancers == "both" ? 1 : 0
296269

297-
lifecycle {
298-
ignore_changes = [source, source_type, direction, protocol, tcp_options]
299-
}
300270
}
301271

302272
resource "oci_core_network_security_group_security_rule" "workers_ssh_ingress_from_bastion" {
@@ -357,38 +327,50 @@ resource "oci_core_network_security_group_security_rule" "int_lb_egress" {
357327
}
358328
}
359329

360-
lifecycle {
361-
ignore_changes = [destination, destination_type, direction, protocol, tcp_options]
362-
}
363-
364330
count = var.load_balancers == "internal" || var.load_balancers == "both" ? length(local.int_lb_egress) : 0
365331
}
366332

367-
# add this rule separately so it can be controlled independently
368-
resource "oci_core_network_security_group_security_rule" "int_lb_healthcheck_ingress_from_pub_lb" {
333+
resource "oci_core_network_security_group_security_rule" "int_lb_ingress" {
369334
network_security_group_id = oci_core_network_security_group.int_lb[0].id
370-
description = "Allow healthchecks from public load balancers"
335+
description = "Allow stateful ingress from ${element(element(local.internal_lb_allowed_cidrs_and_ports, count.index), 0)} on port ${element(element(local.internal_lb_allowed_cidrs_and_ports, count.index), 1)}"
371336
direction = "INGRESS"
372337
protocol = local.tcp_protocol
373-
source = local.pub_lb_subnet
338+
source = element(element(local.internal_lb_allowed_cidrs_and_ports, count.index), 0)
374339
source_type = "CIDR_BLOCK"
375340

376341
stateless = false
377342

378343
tcp_options {
379344
destination_port_range {
380-
min = length(regexall("-", element(var.internal_lb_allowed_ports, count.index))) > 0 ? tonumber(element(split("-", element(var.internal_lb_allowed_ports, count.index)), 0)) : element(var.internal_lb_allowed_ports, count.index)
381-
max = length(regexall("-", element(var.internal_lb_allowed_ports, count.index))) > 0 ? tonumber(element(split("-", element(var.internal_lb_allowed_ports, count.index)), 1)) : element(var.internal_lb_allowed_ports, count.index)
345+
min = length(regexall("-", element(element(local.internal_lb_allowed_cidrs_and_ports, count.index), 1))) > 0 ? element(split("-", element(element(local.internal_lb_allowed_cidrs_and_ports, count.index), 1)), 0) : element(element(local.internal_lb_allowed_cidrs_and_ports, count.index), 1)
346+
max = length(regexall("-", element(element(local.internal_lb_allowed_cidrs_and_ports, count.index), 1))) > 0 ? element(split("-", element(element(local.internal_lb_allowed_cidrs_and_ports, count.index), 1)), 1) : element(element(local.internal_lb_allowed_cidrs_and_ports, count.index), 1)
382347
}
383348
}
384349

385-
lifecycle {
386-
ignore_changes = [source, source_type, direction, protocol, tcp_options, icmp_options]
387-
}
388-
389-
count = var.load_balancers == "both" ? length(var.internal_lb_allowed_ports) : 0
350+
count = var.load_balancers == "internal" || var.load_balancers == "both" ? length(local.internal_lb_allowed_cidrs_and_ports) : 0
390351
}
391352

353+
# add this rule separately so it can be controlled independently
354+
# resource "oci_core_network_security_group_security_rule" "int_lb_healthcheck_ingress_from_pub_lb" {
355+
# network_security_group_id = oci_core_network_security_group.int_lb[0].id
356+
# description = "Allow healthchecks from public load balancers"
357+
# direction = "INGRESS"
358+
# protocol = local.tcp_protocol
359+
# source = local.pub_lb_subnet
360+
# source_type = "CIDR_BLOCK"
361+
362+
# stateless = false
363+
364+
# tcp_options {
365+
# destination_port_range {
366+
# min = length(regexall("-", element(var.internal_lb_allowed_ports, count.index))) > 0 ? tonumber(element(split("-", element(var.internal_lb_allowed_ports, count.index)), 0)) : element(var.internal_lb_allowed_ports, count.index)
367+
# max = length(regexall("-", element(var.internal_lb_allowed_ports, count.index))) > 0 ? tonumber(element(split("-", element(var.internal_lb_allowed_ports, count.index)), 1)) : element(var.internal_lb_allowed_ports, count.index)
368+
# }
369+
# }
370+
371+
# count = var.load_balancers == "both" ? length(var.internal_lb_allowed_ports) : 0
372+
# }
373+
392374
# public lb nsg and rules
393375
resource "oci_core_network_security_group" "pub_lb" {
394376
compartment_id = var.compartment_id
@@ -426,10 +408,6 @@ resource "oci_core_network_security_group_security_rule" "pub_lb_egress" {
426408
}
427409
}
428410

429-
lifecycle {
430-
ignore_changes = [destination, destination_type, direction, protocol, tcp_options]
431-
}
432-
433411
count = var.load_balancers == "public" || var.load_balancers == "both" ? length(local.pub_lb_egress) : 0
434412
}
435413

@@ -450,10 +428,6 @@ resource "oci_core_network_security_group_security_rule" "pub_lb_egress_health_c
450428
}
451429
}
452430

453-
lifecycle {
454-
ignore_changes = [destination, destination_type, direction, protocol, tcp_options]
455-
}
456-
457431
count = var.load_balancers == "public" || var.load_balancers == "both" ? 1 : 0
458432
}
459433

@@ -474,10 +448,6 @@ resource "oci_core_network_security_group_security_rule" "pub_lb_egress_health_c
474448
}
475449
}
476450

477-
lifecycle {
478-
ignore_changes = [destination, destination_type, direction, protocol, tcp_options]
479-
}
480-
481451
count = var.load_balancers == "both" ? length(var.internal_lb_allowed_ports) : 0
482452
}
483453

@@ -498,10 +468,6 @@ resource "oci_core_network_security_group_security_rule" "pub_lb_ingress" {
498468
}
499469
}
500470

501-
lifecycle {
502-
ignore_changes = [source, source_type, direction, protocol, tcp_options, icmp_options]
503-
}
504-
505471
count = var.load_balancers == "public" || var.load_balancers == "both" ? length(local.public_lb_allowed_cidrs_and_ports) : 0
506472
}
507473

@@ -532,9 +498,6 @@ resource "oci_core_network_security_group_security_rule" "waf_ingress" {
532498
}
533499
}
534500

535-
lifecycle {
536-
ignore_changes = [source, source_type, direction, protocol, tcp_options, icmp_options]
537-
}
538501
}
539502

540503
## fss : instance network security group rules

0 commit comments

Comments
 (0)