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

Add AWSIAMRole CRD for flexible role assignment #13

Merged
merged 1 commit into from
May 6, 2019

Conversation

mikkeloscar
Copy link
Contributor

@mikkeloscar mikkeloscar commented Jan 29, 2019

This implements support for a simple AWSIAMRole CRD which makes it possible to reference a role name or full arn and get a corresponding secret with IAM role credentials generated, which can be mounted into a pod.

It works like this:

The controller will look for resources of type AWSIAMRole which looks like this:

apiVersion: amazonaws.com/v1
kind: AWSIAMRole
metadata:
  name: my-app-iam-role
spec:
  roleReference: <my-iam-role-name-or-arn>

And it will create a matching secret my-app-iam-role with credentials for the referenced role.

This should solve most of the issues raised in #12

Additionally to being able to referencing a role in the AWSIAMRole resources, I also think about making it possible to specify a full role definition and have the controller provision the role in AWS. This would make it possible to provision AWS IAM roles completely through Kubernetes and would make it possible to hide the complexity of specifying assumeRole relationships between roles from the user. I.e. users would no longer need to define that the kube-aws-iam-controller role should be able to assume the user role as this could be injected transparently.

The reason I think we need to support both a simple roleReference and role provisioning is that it makes it more flexible and potentially easier to migrate from other solutions to kube-aws-iam-controller.

It also allows providing nicer feedback for users, like a status section of the CRD showing the full role arn and expiration date:

$ kubectl get awsiamroles.amazonaws.com                            
NAME              ROLEARN                                          EXPIRATION
my-app-iam-role   arn:aws:iam::012345678910:role/my-app-iam-role   2019-01-29T21:58:57Z

/cc @seh

@coveralls
Copy link

coveralls commented Jan 29, 2019

Pull Request Test Coverage Report for Build 85

  • 257 of 368 (69.84%) changed or added relevant lines in 5 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+6.6%) to 67.627%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pod_watcher.go 2 3 66.67%
secrets_controller.go 6 7 85.71%
main.go 0 20 0.0%
awsiamrole_controller.go 236 325 72.62%
Files with Coverage Reduction New Missed Lines %
main.go 1 0.0%
Totals Coverage Status
Change from base Build 53: 6.6%
Covered Lines: 493
Relevant Lines: 729

💛 - Coveralls

Copy link

@seh seh left a comment

Choose a reason for hiding this comment

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

A few things to consider, that I couldn't comment on at the line level since this patch didn't touch the pertinent areas of the code:

  • Update Secrets more carefully
    When updating a Secret consider constraining the update on matching the resource version, and if it fails due to a version mismatch, reread the Secret and try again (no more than some fixed number of times).
  • Do we watch pods only to post events to them?
    If so, consider removing that capability, so that your controller doesn't need a pod cache (a scaling limitation that presents a headache to cluster operators).
  • Have you considered integrating kubebuilder?
    Though you've already paid the cost of arranging for code generation, integrating kubebuilder could remove some of the boilerplate code here.

Have you been running this controller since posting this patch? How is it working so far?

minLength: 3
roleSessionDuration:
description: |
Specify the tole session duration in seconds. defaults to 3600
Copy link

Choose a reason for hiding this comment

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

s/tole/role/
s/defaults/Defaults/

roleSessionDuration:
description: |
Specify the tole session duration in seconds. defaults to 3600
seconds (1 hour). This value must be less than or equal to the
Copy link

Choose a reason for hiding this comment

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

What happens if this value is greater than "MaxSessionDuration?" Does it get clamped to that value, or does something fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I assume the API call to sts assumeRole would throw an error which would propagate as an event on the AWSIAMRole resource

roleARN:
type: string
expiration:
type: string
Copy link

Choose a reason for hiding this comment

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

What's the format of this value? Presumably it's a time instant formatted as a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, RFC3339 formatted string in UTC (I'm not sure if kubectl can somehow show the local time given a timestamp string?)

type: integer
minimum: 900 # 15 minutes
maximum: 43200 # 12 hours
roleDefinition:
Copy link

Choose a reason for hiding this comment

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

Is this CRD defining a new IAM role, or is this a snapshot of an existing IAM role as read from AWS?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the idea, which is not yet implemented and won't be in this PR, is to allow users to define an IAM role via the CRD as an alternative to referencing it. This would then take care of provisioning the IAM role with the correct assume role configuration etc.

I will remove this from this PR, was just easier to design the whole thing with this idea in mind.

name: kube-aws-iam-controller
rules:
- apiGroups:
- "amazonaws.com"
Copy link

Choose a reason for hiding this comment

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

Can we really claim this group name? I think it's best left for software actually published by the trademark holder. Perhaps something involving "kube," "iam," and "aws" would be safer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I also thought about this but went with it anyway since kube2iam does the same and no complaints from AWS :) But you are right, let's use a less trademark infringing value.

Copy link
Member

Choose a reason for hiding this comment

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

mikkel.labs/aws-iam?

- apiGroups:
- ""
resources:
- pods
Copy link

Choose a reason for hiding this comment

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

Why does the controller watch pods? Is this to help clean up orphaned (no longer referenced) Secrets? Is it to decide whether to keep renewing credentials for Secrets that are no longer referenced?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The old support for generating secrets based on the aws-iam- prefixed secret mount is still supported here. I would remove it in a later PR to not break existing users without a possibility of an upgrade path.


SRC="github.com"
GOPKG="$SRC/mikkeloscar/kube-aws-iam-controller"
CUSTOM_RESOURCE_NAME="amazonaws.com"
Copy link

Choose a reason for hiding this comment

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

Again, I suggest reconsidering use of this name for which we're not the trademark holder.

@mikkeloscar
Copy link
Contributor Author

Thanks a lot for the review, let me answer your questions inline.

A few things to consider, that I couldn't comment on at the line level since this patch didn't touch the pertinent areas of the code:

  • Update Secrets more carefully
    When updating a Secret consider constraining the update on matching the resource version, and if it fails due to a version mismatch, reread the Secret and try again (no more than some fixed number of times).

This should already be handled by client-go. If we get any error on update then it will be retried in the next iteration, IMO no need to add retries directly to this call unless it becomes a problem.

  • Do we watch pods only to post events to them?
    If so, consider removing that capability, so that your controller doesn't need a pod cache (a scaling limitation that presents a headache to cluster operators).

It's to support the old aws-iam- secret prefix behavior for a little longer.

  • Have you considered integrating kubebuilder?
    Though you've already paid the cost of arranging for code generation, integrating kubebuilder could remove some of the boilerplate code here.

I have not really considered it. The code generation as-is is fairly simple IMO and I would avoid adding another build dependency unless it really provides a benefit.

Have you been running this controller since posting this patch? How is it working so far?

Not really, have only been running it for testing but nothing real so far. This is mostly due to other priorities right now, but I didn't find any issues in my tests, just didn't get around to merging yet :)

@mikkeloscar mikkeloscar force-pushed the awsiamrole-crd branch 3 times, most recently from 38f2631 to d5e94eb Compare March 26, 2019 19:38
Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
@mikkeloscar mikkeloscar changed the title [WIP] Add AWSIAMRole CRD for flexible role assignment Add AWSIAMRole CRD for flexible role assignment May 6, 2019
@mikkeloscar mikkeloscar merged commit ee83d04 into master May 6, 2019
@mikkeloscar mikkeloscar deleted the awsiamrole-crd branch May 6, 2019 07:26
@mikkeloscar mikkeloscar mentioned this pull request May 6, 2019
@mikkeloscar mikkeloscar mentioned this pull request May 8, 2019
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.

4 participants