Skip to content

feat!: add custom egress rules to worker security groups #1222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 52 commits into from
Feb 9, 2025

Conversation

ikarlashov
Copy link
Contributor

@ikarlashov ikarlashov commented Jan 6, 2025

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
   }
]

Copy link
Contributor

github-actions bot commented Jan 6, 2025

Hey @ikarlashov! 👋

Thank you for your contribution to the project. Please refer to the contribution rules for a quick overview of the process.

Make sure that this PR clearly explains:

  • the problem being solved
  • the best way a reviewer and you can test your changes

With submitting this PR you confirm that you hold the rights of the code added and agree that it will published under this LICENSE.

The following ChatOps commands are supported:

  • /help: notifies a maintainer to help you out

Simply add a comment with the command in the first line. If you need to pass more information, separate it with a blank line from the command.

This message was generated automatically. You are welcome to improve it.

@ikarlashov ikarlashov changed the title Add custom egress rules to docker-autoscaler security group feat: add custom egress rules to docker-autoscaler security group Jan 6, 2025
…Don't provision docker-machine security group when docker-autoscaler is used.

Signed-off-by: Yevgen Karlashov <[email protected]>
@ikarlashov ikarlashov force-pushed the custom_sg_docker_autoscaler branch from b2e2ef5 to bfa1b36 Compare January 6, 2025 19:47
@kayman-mk kayman-mk changed the title feat: add custom egress rules to docker-autoscaler security group feat!: add custom egress rules to docker-autoscaler security group Jan 10, 2025
Signed-off-by: Yevgen Karlashov <[email protected]>
Copy link
Collaborator

@kayman-mk kayman-mk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work to improve this module. I noticed the major change, but I think we can go on as it is easy to handle for the users.

Signed-off-by: Yevgen Karlashov <[email protected]>
…r manager and Runner workers ASGs

Signed-off-by: Yevgen Karlashov <[email protected]>
…cker-autoscaler workers' ASG

Signed-off-by: Yevgen Karlashov <[email protected]>
Signed-off-by: Yevgen Karlashov <[email protected]>
Signed-off-by: Yevgen Karlashov <[email protected]>
Signed-off-by: Yevgen Karlashov <[email protected]>
@ikarlashov
Copy link
Contributor Author

It would be nice to allow traffic within runner-manager ASG and Docker-autoscaler workers ASG. Basically add this to security_groups.tf:

resource "aws_vpc_security_group_egress_rule" "runner_manager_to_docker_autoscaler_egress" {
  count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0

  security_group_id            = aws_security_group.runner.id
  from_port                    = 0
  to_port                      = 0
  ip_protocol                  = "-1"
  description                  = "Allow ALL Egress traffic between Runner Manager and Docker-autoscaler workers security group"
  referenced_security_group_id = aws_security_group.docker_autoscaler[0].id
}

But if I add it, terraform doesn't detect any changes for runner's SG.

Signed-off-by: Yevgen Karlashov <[email protected]>
@kayman-mk
Copy link
Collaborator

It would be nice to allow traffic within runner-manager ASG and Docker-autoscaler workers ASG. Basically add this to security_groups.tf:

resource "aws_vpc_security_group_egress_rule" "runner_manager_to_docker_autoscaler_egress" {
  count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0

  security_group_id            = aws_security_group.runner.id
  from_port                    = 0
  to_port                      = 0
  ip_protocol                  = "-1"
  description                  = "Allow ALL Egress traffic between Runner Manager and Docker-autoscaler workers security group"
  referenced_security_group_id = aws_security_group.docker_autoscaler[0].id
}

But if I add it, terraform doesn't detect any changes for runner's SG.

Perhaps it is already in place?

Signed-off-by: Yevgen Karlashov <[email protected]>
Signed-off-by: Yevgen Karlashov <[email protected]>
Signed-off-by: Yevgen Karlashov <[email protected]>
Signed-off-by: Yevgen Karlashov <[email protected]>
Signed-off-by: Yevgen Karlashov <[email protected]>
Signed-off-by: Yevgen Karlashov <[email protected]>
@kayman-mk
Copy link
Collaborator

Did a refactoring and notice that we also have aws_security_group.docker_machine which has the same problems. I think we should have a runner_worker_ingress_rules/runner_worker_egress_rules without specifying the machine type as the meaning is the same.

@kayman-mk
Copy link
Collaborator

Verification

runner_worker_docker_autoscaler_ingress_rules =  [
    {
      cidr_block      = "10.0.0.0/8"  # Example CIDR block for a private network in Amazon
      from_port        = 22
      protocol         = "tcp"
      to_port          = 22
      description      = "Allow SSH Ingress traffic for private network."
    }
]

runner_worker_docker_autoscaler_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
   }
]

@kayman-mk kayman-mk self-requested a review January 30, 2025 15:36
kayman-mk
kayman-mk previously approved these changes Jan 30, 2025
Copy link
Collaborator

@kayman-mk kayman-mk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@kayman-mk
Copy link
Collaborator

I'd like to test this with my current setup before merging it.

ikarlashov and others added 4 commits January 31, 2025 09:25
@kayman-mk
Copy link
Collaborator

Checked with docker+machine.

  1. agent was not able to contact the workers --> security group rule added.
  2. workers were not able to clone via SSH --> default egress rule extended

@ikarlashov Could you please have a look at my commits?

@kayman-mk kayman-mk changed the title feat!: add custom egress rules to docker-autoscaler security group feat!: add custom egress rules to worker security groups Feb 6, 2025
@kayman-mk kayman-mk self-requested a review February 6, 2025 10:07
@kayman-mk kayman-mk merged commit a197e4f into cattle-ops:main Feb 9, 2025
18 checks passed
kayman-mk added a commit that referenced this pull request Feb 13, 2025
🤖 I have created a release *beep* *boop*
---


##
[9.0.0](8.1.0...9.0.0)
(2025-02-09)


### ⚠ BREAKING CHANGES

* remove declaration of unused variables for docker-autoscaler setup
([#1223](#1223))
* add custom egress rules to worker security groups
([#1222](#1222))

### Features

* add custom egress rules to worker security groups
([#1222](#1222))
([a197e4f](a197e4f))
* enabled usage of private key with docker autoscaler
([#1232](#1232))
([95c7ea6](95c7ea6))


### Miscellaneous Chores

* remove declaration of unused variables for docker-autoscaler setup
([#1223](#1223))
([41058c8](41058c8))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Co-authored-by: cattle-ops-releaser-2[bot] <134548870+cattle-ops-releaser-2[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Matthias Kay <[email protected]>
@Alexandre-Orisha
Copy link

Did a refactoring and notice that we also have aws_security_group.docker_machine which has the same problems. I think we should have a runner_worker_ingress_rules/runner_worker_egress_rules without specifying the machine type as the meaning is the same.

Hello, my setup is using docker-autoscaler and I just upgraded from 8.1.0 to 9.0.0.

I think specifying the machine type would be better because now Terraform is trying to create egress rules for a docker+machine security group that doesn't exist.

The only way to make the module work is to explicitly give an empty runner_worker_egress_rules which is quite unhelpful because my workers won't be able to connect to the internet.

Please correct me if I'm wrong.

@kayman-mk
Copy link
Collaborator

@Alexandre-Orisha Perfectly right! Fixed in 9.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants