Skip to content
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

feat: Add load_balancer_controller targetgroup binding only role #199

Merged
merged 11 commits into from
Mar 25, 2022

Conversation

cdhesse
Copy link
Contributor

@cdhesse cdhesse commented Mar 4, 2022

Description

This implements a least privilege role for load_balancer_controller when using TargetGroup binding only as documented here.

Motivation and Context

The current module has a role for load_balancer_controller, but it is the full set of permissions, which are not required when implementing TargetGroup binding only.

Breaking Changes

No

How Has This Been Tested?

  • I have tested and validated these changes using one or more of the provided examples/* projects

I tested the changes by executing the examples/iam-role-for-service-accounts-eks, and added a module call there to create the new role.

@cdhesse cdhesse changed the title add load_balancer_controller targetgroup binding only role feat: add load_balancer_controller targetgroup binding only role Mar 4, 2022
@cdhesse cdhesse changed the title feat: add load_balancer_controller targetgroup binding only role feat: Add load_balancer_controller targetgroup binding only role Mar 4, 2022
@bryantbiggs bryantbiggs self-requested a review March 5, 2022 16:32
cdhesse and others added 4 commits March 10, 2022 01:30
In extensive testing, I realized the documentation had missed the permission "ec2:DescribeVpcs" so the aws-load-balancer-controller was throwing unauthorized errors as a result.  After adding "ec2:DescribeVpcs" everything is working as expected.
@cdhesse
Copy link
Contributor Author

cdhesse commented Mar 12, 2022

kubernetes-sigs/aws-load-balancer-controller#2542 was also created to address the missing describevps iam permission.

@cdhesse
Copy link
Contributor Author

cdhesse commented Mar 15, 2022

kubernetes-sigs/aws-load-balancer-controller#2542 was also created to address the missing describevps iam permission.

Issue 2542 has been closed as of today in the aws-load-balancer-controller repo.

"ec2:DescribeVpcs",
"elasticloadbalancing:DescribeTargetGroups",
"elasticloadbalancing:DescribeTargetHealth",
"elasticloadbalancing:ModifyTargetGroup",
Copy link
Member

@bryantbiggs bryantbiggs Mar 18, 2022

Choose a reason for hiding this comment

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

For the Modify*/Register*/Deregeister* actions, should we not break these permissions out to avoid the wildcard, similar to the full controller policy. Something like

  statement {
    actions = [
      "ec2:DescribeSecurityGroups",
      "ec2:DescribeInstances",
      "ec2:DescribeVpcs",
      "elasticloadbalancing:DescribeTargetGroups",
      "elasticloadbalancing:DescribeTargetHealth",
    ]

    resources = ["*"]
  }

  statement {
    actions = [
      "elasticloadbalancing:ModifyTargetGroup",
      "elasticloadbalancing:ModifyTargetGroupAttributes",
    ]
    resources = ["*"]

    condition {
      test     = "Null"
      variable = "aws:ResourceTag/elbv2.k8s.aws/cluster"
      values   = ["false"]
    }
  }

  statement {
    actions = [
      "elasticloadbalancing:RegisterTargets",
      "elasticloadbalancing:DeregisterTargets",
    ]
    resources = ["arn:${local.partition}:elasticloadbalancing:*:*:targetgroup/*/*"]
  }

With that we could even go one step further and allow users to restrict which target groups with

  statement {
    actions = [
      "ec2:DescribeSecurityGroups",
      "ec2:DescribeInstances",
      "ec2:DescribeVpcs",
      "elasticloadbalancing:DescribeTargetGroups",
      "elasticloadbalancing:DescribeTargetHealth",
    ]

    resources = ["*"]
  }

  statement {
    actions = [
      "elasticloadbalancing:ModifyTargetGroup",
      "elasticloadbalancing:ModifyTargetGroupAttributes",
    ]
    resources = ["*"]

    condition {
      test     = "Null"
      variable = "aws:ResourceTag/elbv2.k8s.aws/cluster"
      values   = ["false"]
    }
  }

  statement {
    actions = [
      "elasticloadbalancing:RegisterTargets",
      "elasticloadbalancing:DeregisterTargets",
    ]
    resources = [for name in var.load_balancer_controller_targetgroup_names :
      "arn:${local.partition}:elasticloadbalancing:*:*:targetgroup/${name}/*"
    ]
  }

Where the variable load_balancer_controller_targetgroup_names defaults to *. What do you think?

Copy link
Contributor Author

@cdhesse cdhesse Mar 19, 2022

Choose a reason for hiding this comment

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

The use case for TargetGroupBinding only mode, vs. full ALB, is that you want to create (or already have pre-existing) TargetGroups outside of the kubernetes environment. This change would require the TargetGroup in question to have the elbv2.k8s.aws/cluster tag. While on one hand this solution ensures that at least the owner/creator of the TargetGroup intended for it to be updated from a load-balancer-controller, the downside is there may be valid reasons for that tag to not exist.

The second thought - your proposed change is a better version than what is documented as part of the load-balancer-controller intro guide. A person trying for the first time to use this module would quickly realize it didn't work, until they reverse engineer looking at this policy, then see that they need to go back and tag their TargetGroups with this k8s tag. I suggest that if you implement it this way, the ideal would be that the TargetBingingOnly documentation should include this more restricted version of the role vs. their more permissive one. The load-balancer-controller documentation could then describe the requirement to tag the TargetGroup.

However, the Register/Deregister change I think is brilliant. We could make an option as well for var.load_balancer_controller_targetgroup_arns if we wanted to, and then you could pass name or arn...?

Copy link
Member

Choose a reason for hiding this comment

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

For now we'll proceed with what is defined by the upstream documentation. We can revisit later for any additional policy scoping

@bryantbiggs
Copy link
Member

@cdhesse if you could make the requested tweaks, update your branch with the latest, and re-run pre-commit run -a, we should be set to merge this then. Thank you for the PR!

@cdhesse
Copy link
Contributor Author

cdhesse commented Mar 25, 2022

@cdhesse if you could make the requested tweaks, update your branch with the latest, and re-run pre-commit run -a, we should be set to merge this then. Thank you for the PR!

I've done as you requested, should be ready to merge now, thank you!

Copy link
Member

@bryantbiggs bryantbiggs left a comment

Choose a reason for hiding this comment

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

thanks again for the PR @cdhesse - good to go @antonbabenko 👍🏽

@antonbabenko antonbabenko merged commit e00526e into terraform-aws-modules:master Mar 25, 2022
antonbabenko pushed a commit that referenced this pull request Mar 25, 2022
## [4.16.0](v4.15.1...v4.16.0) (2022-03-25)

### Features

* Add load_balancer_controller targetgroup binding only role ([#199](#199)) ([e00526e](e00526e))
@antonbabenko
Copy link
Member

This PR is included in version 4.16.0 🎉

@github-actions
Copy link

github-actions bot commented Nov 8, 2022

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants