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(eks): IAM roles for service accounts #6062

Merged
merged 28 commits into from
May 18, 2020
Merged

Conversation

vlesierse
Copy link
Contributor

@vlesierse vlesierse commented Feb 2, 2020

Commit Message

feat(eks): IAM roles for service accounts (#6062)

Adds support for IAM roles for service account which allows pods the assume IAM roles.

NOTE: currently there are no condition set on the IAM Role which results that there are no restrictions on other pods to assume the role. This will be fixed in a subsequent PR.

See README for details.

Fixes #5388
Fixes #3949

End of Commit Message

  • Enable OpenID Connect Provider
  • Service Account construct
  • Role constraints
  • Add cluster.addServiceAccount convenience method
  • Integration Tests
  • Unit Tests
  • Update README.md

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@vlesierse
Copy link
Contributor Author

@eladb question regarding CDK as related to this PR.

Following to the EKS Documentation I'm trying create an IAM role with an assume policy which contains a condition. This is something that can easily be done with CDK. However the condition's key needs to be a calculated value of the custom resource, which is a token in CDK. CloudFormation doesn't support this. Is there are way to resolve this before passing it through?

const condition: { [id: string]: any; } = {};
condition[this.openIDConnectSubject!] = `system:serviceaccount:${namespace}:${name}`;

*
* @example https://5E1D0CEXAMPLEA591B746AFC5AB30262.yl4.us-west-2.eks.amazonaws.com
*/
public readonly openIDConnectIssuerUrl: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is an attribute it should have a prefix “cluster”. Rename to: “clusterOpenIdConnectIssueUrl”

@eladb
Copy link
Contributor

eladb commented Feb 10, 2020

CloudFormation doesn't support this. Is there are way to resolve this before passing it through?

Yes, indeed, CloudFormation doesn't support using intrinsic functions as map keys... Can you provide a bit more context into your code? I am not sure I fully see where you are using this.

@vlesierse
Copy link
Contributor Author

I've pushed the code how it should work as you can see in the service-account.ts. My alternative is to create Customer Resource to swap the condition, but it feels a bit heavy for something this small..

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 96a142f
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: f4059d7
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: db0abca
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@vlesierse
Copy link
Contributor Author

@eladb the initial implementation is working. Eventually I've solved the problem if IAM condition keys with a CustomerResource. It's not the most elegant solution, but I think the only one available at the moment.

I'm currently focusing on the tests.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 2c97cca
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

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

Initial round of comments.

Would be nice to validate this logic against https://github.com/smaato/cfn-resource-provider.

I also feel like perhaps we should actually move these two new resources to the IAM module instead of the EKS module... This might be a bit of a hassle given the IAM module can't use the custom resource provider framework at the moment (i.e. it needs to use "raw" custom resources).

Comment on lines 41 to 43
throw new Error(
`Cannot delete a profile without a physical id`
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
throw new Error(
`Cannot delete a profile without a physical id`
);
throw new Error(`Cannot delete an OIDC provider without a physical id`);

Comment on lines 51 to 53
const deleteOpenIDConnectProviderResponse = await this.eks.deleteOpenIDConnectProvider(
deleteOpenIDConnectProvider
);
Copy link
Contributor

Choose a reason for hiding this comment

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

style nit:

Suggested change
const deleteOpenIDConnectProviderResponse = await this.eks.deleteOpenIDConnectProvider(
deleteOpenIDConnectProvider
);
const deleteOpenIDConnectProviderResponse = await this.eks.deleteOpenIDConnectProvider(deleteOpenIDConnectProvider);

} catch (getOpenIDConnectProviderError) {
if (getOpenIDConnectProviderError.code === "NoSuchEntity") {
this.log(
"received NoSuchEntityFoundException, this means the profile has been deleted (or never existed)"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"received NoSuchEntityFoundException, this means the profile has been deleted (or never existed)"
"received NoSuchEntityFoundException, this means the provider has been deleted (or never existed)"

if (!statement.Condition) {
statement.Condition = {};
}
statement.Condition.StringEquals = condition;
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens if the policy already has a statement.Condition.StringEquals?


const role = await this.eks.getRole({ RoleName: this.physicalResourceId});
if (!role) {
throw new Error(`no role found with name ${this.physicalResourceId}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we just consider this a success? (idempotency)

const deleteOpenIDConnectProvider: aws.IAM.DeleteOpenIDConnectProviderRequest = {
OpenIDConnectProviderArn: this.physicalResourceId
};

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe call "checkResource" first and return success if it's not found (idempotency)

@RafalWilinski
Copy link
Contributor

Do you need any help with this PR? Happy to contribute some code in order to push this forward.

@vlesierse
Copy link
Contributor Author

Thanks @RafalWilinski your help would be appreciated. The coming week I might have some more time to finish this. The code is working but needs a bit of refactoring and the addition of tests.

@eladb I'm wondering if the code needs to move the to aws-iam as the logic is pretty EKS specific. Updating the assume role policy might be.

@eladb
Copy link
Contributor

eladb commented Apr 16, 2020

@eladb I'm wondering if the code needs to move the to aws-iam as the logic is pretty EKS specific. Updating the assume role policy might be.

Yes, I think it needs to move to IAM, which makes this dramatically more complex. Let me pick this task up and then you'll be able to leverage this work here. OK?

@vlesierse
Copy link
Contributor Author

@eladb sure, that will be great. Last night I was struggling a bit how to put this in aws-iam. I will focus today on some of the testing and documentation and adjust the code once you are done.

@vlesierse
Copy link
Contributor Author

Would be nice to validate this logic against https://github.com/smaato/cfn-resource-provider.

I have used the same logic as what they regarding creating the provider. However their update logic is more sophisticated which I can change in my implementation to be more resilient to to changes from EKS. (Client ID or Certificate changes)

});
```

The `OpenIdConnectPrincipal` class can be used as a principal used with a `OpenIdConnectProvider`, for example:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move this to the open is connect section please?

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 1d3f8e0
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@eladb
Copy link
Contributor

eladb commented May 18, 2020

You'll need to update the EKS integration tests by running npm run integ integ.eks-cluster.js as the error suggests.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: f90bf1f
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 909b778
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@vlesierse
Copy link
Contributor Author

You'll need to update the EKS integration tests by running npm run integ integ.eks-cluster.js as the error suggests.

Got it 👍 Took some time to get it run correctly on one of my AWS accounts.

@eladb eladb changed the title feat(eks): least privilege service accounts feat(eks): IAM roles for service accounts May 18, 2020
@eladb
Copy link
Contributor

eladb commented May 18, 2020

@vlesierse updated PR title & message

@pahud
Copy link
Contributor

pahud commented May 18, 2020

Amazing! Thank you @vlesierse and @eladb for all the great work.

@mergify
Copy link
Contributor

mergify bot commented May 18, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 34d029a
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented May 18, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 1f303da
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented May 18, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: f069c4e
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented May 18, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: afa1075
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented May 18, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 3f0d2c8 into aws:master May 18, 2020
karupanerura pushed a commit to karupanerura/aws-cdk that referenced this pull request May 22, 2020
## Commit Message
feat(eks): IAM roles for service accounts (aws#6062)

Adds support for IAM roles for service account which allows pods the assume IAM roles.

NOTE: currently there are no condition set on the IAM Role which results that there are no restrictions on other pods to assume the role. This will be fixed in a subsequent PR.

See README for details.

Fixes aws#5388 
Fixes aws#3949 

## End of Commit Message

- [x] Enable OpenID Connect Provider
- [x] Service Account construct
- [ ] Role constraints
- [x] Add `cluster.addServiceAccount` convenience method
- [x] Integration Tests
- [x] Unit Tests
- [x] Update README.md

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*

<!-- 
Please read the contribution guidelines and follow the pull-request checklist:
https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md
 -->
@pahud pahud mentioned this pull request Jun 11, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants