Skip to content

Commit a197e4f

Browse files
feat!: add custom egress rules to worker security groups (#1222)
Add custom egress rules to docker-autoscaler security group and remove condition to provision unused docker-machine security group. ## Description 1. By default, the module provisions a security group for Docker Autoscaler workers with egress rules that allow ALL traffic. Unlike ingress rules, egress rules are not customizable, which poses a significant security concern. This PR introduces the ability to customize egress rules for the Docker Autoscaler workers' security group by declaring a separate variable for docker-autoscaler egress rules. 2. `var.runner_worker_docker_autoscaler_asg` becomes bulky and hard to read. Considering the complexity of the ingress rules structure, it makes sense to create a separate variable `var.runner_worker_docker_autoscaler_ingress_rules` for ingress rules. This way we will follow variable convention for existing security group rules variables, i.e. `var.runner_worker_docker_machine_extra_egress_rules`. In the result of the change: `var.runner_worker_docker_autoscaler_asg.sg_ingresses` is removed and its content should be moved to `var.runner_worker_ingress_rules`. 3. Adding `var.runner_ingress_rules` to manage Ingress rules for runner-manager security group. 4. Changed runner-manager egress rules' var name and its spec. `var.runner_networking_egress_rules` has migrated to `var.runner_egress_rules` with a new spec. 5. Additionally, PR removes the condition that provisions an unused security group intended solely for Docker Machine setup. ## Migrations required 1. Move all docker-autoscaler ingress rules declaration from `var.runner_worker_docker_autoscaler_asg.sg_ingresses` to `var.runner_worker_ingress_rules`. 2. `var.runner_networking_egress_rules` migrated to `var.runner_manager_egress_rules` with a new spec. 3. Move `var.runner_worker_docker_machine_extra_egress_rules` to `var.runner_worker_egress_rules`. In case you used multiple cidr_blocks, ... you have to create multiple rules. Attention: The default value for `var.runner_worker_docker_machine_extra_egress_rules` allowing all egress traffic has been replaced by a rule allowing traffic to port 22/443 only. Don't forget to add the rules you need to `var.runner_worker_egress_rules`. Attention: Due to the resource replacement you might see inconsistencies and disappearing security group rules. In that case delete all rules from the Runner and Runner Workers and apply the module again. Sample egress rule: ``` runner_worker_egress_rules = [ { cidr_block = "0.0.0.0/0" from_port = 443 protocol = "tcp" to_port = 443 description = "Allow HTTPS egress traffic." }, { ipv6_cidr_block = "::/0" description = "Allow HTTPS Egress everywhere" from_port = 443 protocol = "tcp" to_port = 443 } ] ``` --------- Signed-off-by: Yevgen Karlashov <[email protected]> Co-authored-by: Matthias Kay <[email protected]>
1 parent 6fdaf06 commit a197e4f

File tree

4 files changed

+389
-193
lines changed

4 files changed

+389
-193
lines changed

.cspell.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"glrt",
2525
"glrunners",
2626
"hmarr",
27+
"icmpv6",
2728
"instancelifecycle",
2829
"keyrings",
2930
"kics",
@@ -83,7 +84,7 @@
8384
"rebalance",
8485
"signoff",
8586
"typecheck",
86-
"userdata",
87+
"userdata"
8788
],
8889
"flagWords": []
8990
}

docker_autoscaler.tf

+43-30
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
# outdated docker+machine driver. The docker+machine driver is a legacy driver that is no longer maintained by GitLab.
44
#
55

6-
resource "aws_security_group" "docker_autoscaler" {
7-
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
6+
########################################
7+
###### Security Group and SG rules #####
8+
########################################
89

9-
description = "Docker autoscaler security group"
10+
# Base security group
11+
resource "aws_security_group" "docker_autoscaler" {
12+
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
13+
name_prefix = "${local.name_sg}-docker-autoscaler"
1014
vpc_id = var.vpc_id
11-
name = "${local.name_sg}-docker-autoscaler"
15+
description = "Docker-autoscaler security group"
1216

1317
tags = merge(
1418
local.tags,
@@ -18,40 +22,49 @@ resource "aws_security_group" "docker_autoscaler" {
1822
)
1923
}
2024

21-
resource "aws_security_group_rule" "autoscaler_egress" {
22-
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
25+
# Ingress rules
26+
resource "aws_vpc_security_group_ingress_rule" "docker_autoscaler_ingress" {
27+
for_each = var.runner_worker.type == "docker-autoscaler" ? var.runner_worker_ingress_rules : {}
2328

24-
description = "All egress traffic docker autoscaler"
25-
type = "egress"
26-
from_port = 0
27-
to_port = 0
28-
protocol = "-1"
29-
cidr_blocks = ["0.0.0.0/0"]
30-
security_group_id = join("", aws_security_group.docker_autoscaler[*].id)
29+
security_group_id = aws_security_group.docker_autoscaler[0].id
30+
31+
from_port = each.value.from_port
32+
to_port = each.value.to_port
33+
ip_protocol = each.value.protocol
34+
35+
description = each.value.description
36+
prefix_list_id = each.value.prefix_list_id
37+
referenced_security_group_id = each.value.security_group
38+
cidr_ipv4 = each.value.cidr_block
39+
cidr_ipv6 = each.value.ipv6_cidr_block
3140
}
3241

33-
resource "aws_security_group_rule" "autoscaler_ingress" {
42+
resource "aws_vpc_security_group_ingress_rule" "docker_autoscaler_internal_traffic" {
3443
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
3544

36-
description = "All ingress traffic from runner security group"
37-
type = "ingress"
38-
from_port = 0
39-
to_port = 0
40-
protocol = "-1"
41-
source_security_group_id = aws_security_group.runner.id
42-
security_group_id = join("", aws_security_group.docker_autoscaler[*].id)
45+
security_group_id = aws_security_group.docker_autoscaler[0].id
46+
from_port = -1
47+
to_port = -1
48+
ip_protocol = "-1"
49+
description = "Allow ALL Ingress traffic between Runner Manager and Docker-autoscaler workers security group"
50+
referenced_security_group_id = aws_security_group.runner.id
4351
}
4452

45-
resource "aws_security_group_rule" "extra_autoscaler_ingress" {
46-
count = var.runner_worker.type == "docker-autoscaler" ? length(var.runner_worker_docker_autoscaler_asg.sg_ingresses) : 0
53+
# Egress rules
54+
resource "aws_vpc_security_group_egress_rule" "docker_autoscaler_egress" {
55+
for_each = var.runner_worker.type == "docker-autoscaler" ? var.runner_worker_egress_rules : {}
56+
57+
security_group_id = aws_security_group.docker_autoscaler[0].id
58+
59+
from_port = each.value.from_port
60+
to_port = each.value.to_port
61+
ip_protocol = each.value.protocol
4762

48-
description = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].description
49-
type = "ingress"
50-
from_port = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].from_port
51-
to_port = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].to_port
52-
protocol = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].protocol
53-
cidr_blocks = var.runner_worker_docker_autoscaler_asg.sg_ingresses[count.index].cidr_blocks
54-
security_group_id = join("", aws_security_group.docker_autoscaler[*].id)
63+
description = each.value.description
64+
prefix_list_id = each.value.prefix_list_id
65+
referenced_security_group_id = each.value.security_group
66+
cidr_ipv4 = each.value.cidr_block
67+
cidr_ipv6 = each.value.ipv6_cidr_block
5568
}
5669

5770
####################################

0 commit comments

Comments
 (0)