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

aws_eks: Support AccessConfig on aws_eks.Cluster #28588

Closed
2 tasks
TirTech opened this issue Jan 5, 2024 · 17 comments · Fixed by #30016
Closed
2 tasks

aws_eks: Support AccessConfig on aws_eks.Cluster #28588

TirTech opened this issue Jan 5, 2024 · 17 comments · Fixed by #30016
Assignees
Labels
@aws-cdk/aws-eks Related to Amazon Elastic Kubernetes Service effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p1

Comments

@TirTech
Copy link

TirTech commented Jan 5, 2024

Describe the feature

Support setting AccessConfig (or at least AuthenticationMode) on aws_eks.Cluster.

Use Case

We currently use aws_eks.Cluster to create our clusters, and would like to start using EKS access modes (specifically API_AND_CONFIG_MAP)

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.118.0

Environment details (OS name and version, etc.)

Ubuntu 22.04.3 LTS on Windows 11 x86_64, python 3.10

@TirTech TirTech added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jan 5, 2024
@github-actions github-actions bot added the @aws-cdk/aws-eks Related to Amazon Elastic Kubernetes Service label Jan 5, 2024
@pahud
Copy link
Contributor

pahud commented Jan 5, 2024

Yes this is an awesome feature we just announced and we definitely should support that. Would you tell me more about your expected CDK experience with that?

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jan 5, 2024
@TirTech
Copy link
Author

TirTech commented Jan 5, 2024

Yes this is an awesome feature we just announced and we definitely should support that. Would you tell me more about your expected CDK experience with that?

Sure. When creating an EKS cluster, we would like to opt in to access entries by passing the authentication mode to the constructor:

cluster = aws_eks.Cluster(
            self,
            id="MyCluster",
            # ...snipped
            authentication_mode=aws_eks.AuthenticationMode.API_AND_CONFIG_MAP
        )

with aws_eks.AuthenticationMode being one of the available cluster authentication modes (default CONFIG_MAP).

For now we would go on to create CfnAccessEntry resource for each of the IAM roles we want to grant cluster access to, but in the future we would be looking to get the higher level AccessEntry resource added so we could create these entries for some aws_iam.Role "role" either on their own:

aws_eks.AccessEntry(
            self,
            id="AdministatorAccessEntry",
            cluster_name=cluster.cluster_name,
            principal_arn=role.role_arn,
            # other options
        )

or with a helper method on the cluster:

cluster.add_access_entry(self, id="AdministatorAccessEntry", principal_arn=role.role_arn, ...)

@codeJack
Copy link

codeJack commented Jan 9, 2024

Thanks @TirTech for raising this one.

@pahud to complement what has been shared above around the expected CDK experience with AccessConfig.

  • Ideally, the aws_eks.Cluster construct should also expose the bootstrapClusterCreatorAdminPermissions flag along with AuthenticationMode.
    Today the role which creates the cluster is intrinsically a Kubernetes Cluster Administrator (doesn't even show up in the aws-auth config map). With access entries you might want to have full control and explicitly grant access to IAM entities. The CDK IAM model would need to evolve accordingly as today, by default, the kubectl provider leverages the cluster creation role as kubectl role. I would expect a dedicated IAM identity to be crafted for this specific purpose so that it can be audited and quarantined in case of need.
  • About how to provide access to IAM roles into the cluster, I would expect the aws_eks.Cluster construct to somehow expose grant semantics to be consistent with other CDK libraries.
    Something along these lines, where IGrantable is implemented by IAM roles, users, groups etc:
# Grant cluster admin (system:masters RBAC group)
cluster.grant_admin(IGrantable)
# Grant read only (similarly to what a console user needs to navigate the cluster resources)
cluster.grant_read_only(IGrantable)
# General purpose grant where explicit mapping to RBAC access management can be expressed.
# Eventually this one could also take care of the heavy lifting by creating the Kubernetes' Role/ClusterRole/Binding/Group behind the scenes.
cluster.grant(IGrantable, <rbac mapping>)

@pahud
Copy link
Contributor

pahud commented Jan 9, 2024

This makes sense to me. Thank you for the user experience sharing. I am bumping this issue to p1 now but we still welcome any pull requests from the community.

@akefirad
Copy link

Is this possible/safe to use the escape hatch to enable API_AND_CONFIG mode until this feature is released? Asking because if it’s enabled manually, it cannot be set in the CDK stack anymore since the deployment is going to fail (which seems unnecessary, it could have simply succeeded, but that’s not the point here). Thanks.

@pahud
Copy link
Contributor

pahud commented Mar 6, 2024

Just self-assigned. I will pick up this issue and see if I can submit a PR in the next few days. I can't guarantee any ETA but this is something I am really looking forward to.

@mikelane
Copy link

FYI, I attempted to use an escape hatch like this:

export default class MyEks extends Construct {
  public readonly cluster: eks.Cluster
  
  // other constructs here

  this.cluster = eks.Cluster(this, 'MyEksCluster', {
    // Various props here
  });
  
  const cfnCluster = this.cluster.node.defaultChild as eks.CfnCluster;
  cfnCluster.accessConfig = {authenticationMode: 'API_AND_CONFIG_MAP'};
}

But this did not set the authentication mode value. It doesn't appear in the synthesized cloudformation and the option isn't selected in the console.

@pahud
Copy link
Contributor

pahud commented Mar 13, 2024

We are discussing with the team what is the best option for us to rollout this feature. Will update here in the next few days.

@venkates67
Copy link

looking forward for the update...

@hbjydev
Copy link

hbjydev commented Apr 17, 2024

@pahud Any updates on this? :)

@derbauer97
Copy link

@pahud we are also interested in this feature

@pahud
Copy link
Contributor

pahud commented Apr 30, 2024

Thanks for all the upvotes. We are still discussing with the team as adding this feature is not as simply as just adding the accessConfig prop for the ClusterProps. We probably need to implement the AccessEntry L2 construct as well.

What I am planning for the low hanging fruits is:

  1. add accessConfig in ClusterProps and on cluster creation or update, enable this with SDK call.
  2. make sure we can create the AccessEntry with CfnAccessEntry L1 construct and work well with the cluster.

This should be a small PR to unblock this for now.

And after that, we could have another PR to implement the AccessEntry L2 for better developer experience.

Let me know if you have different thoughts and feel free to chat with me for more details on cdk.dev slack.

@mergify mergify bot closed this as completed in #30016 Jun 12, 2024
mergify bot pushed a commit that referenced this issue Jun 12, 2024
###  Background

Amazon EKS originally uses `ConfigMap` as its access management and in aws-eks we use AwsAuth to leverage the kubectl from kubectl-lambda-layer to create the AwsAuth configmap for that. The ConfigMap has been very difficult to maintain due to its lack support of EKS API but thanks to the AwsAuth class, it's been very smooth in CDK.

In AWS reInvent 2023 we [announced](https://aws.amazon.com/blogs/containers/a-deep-dive-into-simplified-amazon-eks-access-management-controls/) the access API support that simplifies the management as a replacement of the traditional ConfigMap. In CloudFormation we have the [AccessConfig](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eks-cluster.html#cfn-eks-cluster-accessconfig) with [AuthenticationMode](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-eks-cluster-accessconfig.html#cfn-eks-cluster-accessconfig-authenticationmode) and `BootstrapClusterCreatorAdminPermissions` now.

The `AuthenticationMode` supports `CONFIG_MAP`, which is the default, `API_AND_CONFIG_MAP` and `CONFIG_MAP`. It allows users to switch the mode on cluster creation or update. When the mode has API support, users have to define the `AccessEntry` to map the access policies and the IAM principals. This PR introduces the `AccessEntry` and `AccessPolicy` classes for that to simplify it with similar experience just as the [iam.ManagedPolicy ](https://github.com/aws/aws-cdk/blob/3928eae1ee92a03ba9959288f05f59d6bd5edcba/packages/aws-cdk-lib/aws-iam/lib/managed-policy.ts#L104)class. This PR also introduces the `grantAccess()` method that allows a cluster to `grant` its access to a specific principal and abstracts away the complexity.

Overview of the API experience from this PR:

```ts
const cluster = new eks.Cluster(this, 'Cluster', {
  vpc,
  mastersRole: clusterAdminRole,
  version: eks.KubernetesVersion.V1_30,
  kubectlLayer: new KubectlV29Layer(this, 'KubectlLayer'),
  authenticationMode: eks.AuthenticationMode.API_AND_CONFIG_MAP,
});

// Cluster Admin role for this cluster
cluster.grantAccess('clusterAdminAccess', clusterAdminRole.roleArn, [
  eks.AccessPolicy.fromAccessPolicyName('AmazonEKSClusterAdminPolicy', {
    accessScopeType: eks.AccessScopeType.CLUSTER,
  }),
]);

// EKS Admin role for specified namespaces of thie cluster
cluster.grantAccess('eksAdminRoleAccess', eksAdminRole.roleArn, [
  eks.AccessPolicy.fromAccessPolicyName('AmazonEKSAdminPolicy', {
    accessScopeType: eks.AccessScopeType.NAMESPACE,
    namespaces: ['foo', 'bar'],
  }),
]);

// EKS Admin Viewer role for specified namespaces of thie cluster
cluster.grantAccess('eksAdminViewRoleAccess', eksAdminViewRole.roleArn, [
  eks.AccessPolicy.fromAccessPolicyName('AmazonEKSAdminViewPolicy', {
    accessScopeType: eks.AccessScopeType.NAMESPACE,
    namespaces: ['foo', 'bar'],
  }),
]);
```


### Issue # (if applicable)

Closes  #28588

This PR introduces the `authenticationMode`, `AccessEntry` and `AccessPolicy` for both `Cluster` and `FargateCluster` construct.

- [x] bump `@aws-sdk/client-eks` to [v3.476.0](https://github.com/aws/aws-sdk-js-v3/releases/tag/v3.476.0)(the minimal version with EKS Cluster Access Management support)
- [x] make sure it deploys with the new AccessConfig support for a new cluster
- [x] make sure an existing cluster can update by adding this new prop
- [x] make sure it deploys with a new FargateCluster
- [x] make sure an existing FargateCluster can update by adding this new prop
- [x] make sure it works with CfnAccessEntry L1 resources
- [x] AccessEntry L2 construct support
- [x] AccessPolicy class
- [x] bootstrapClusterCreatorAdminPermissions
- [x] unit tests
- [x] integ tests
- [x] update README
- [x] add PR notes

### Notes

1. Switching authentication modes on an existing cluster is a one-way operation like:

undefined(CONFIG_MAP) -> API_AND_CONFIG_MAP -> API

You can switch from undefined or CONFIG_MAP to API_AND_CONFIG_MAP. You can then switch from API_AND_CONFIG_MAP to API. You cannot revert these operations in the opposite direction. Meaning you cannot switch back to CONFIG_MAP or API_AND_CONFIG_MAP from API. And you cannot switch back to CONFIG_MAP from API_AND_CONFIG_MAP. (see [here](https://aws.amazon.com/blogs/containers/a-deep-dive-into-simplified-amazon-eks-access-management-controls/)) This PR adds relevant checks in the custom resource and add docstring in the `authenticationMode` prop.

2. Switching `bootstrapClusterCreatorAdminPermissions` would cause cluster replacement, we callout in the README and construct prop docstring as a headsup. This option is [available](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-eks-cluster-accessconfig.html#cfn-eks-cluster-accessconfig-bootstrapclustercreatoradminpermissions) in CFN which triggers replacement on resource update as well. I have created #30107 for further improvement.

3. This feature does not support AWS China regions at this moment as the JS SDK version of lambda node18 runtime in China regions is `3.462.0` while this feature requires SDK [3.476.0](https://github.com/aws/aws-sdk-js-v3/releases/tag/v3.476.0) or above. It's `3.552.0` in us-east-1. Use [this example](https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html#nodejs-sdk-included) to check the version.


### Reason for this change



### Description of changes



### Description of how you validated changes



### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

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

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@pahud
Copy link
Contributor

pahud commented Jun 12, 2024

Hi folks, this feature was just merged. Let me know if you have any issues and feel free to reach out to me on cdk.dev slack.

@TirTech @codeJack @akefirad @mikelane @venkates67 @hbjydev @derbauer97

@mikelane
Copy link

Hi folks, this feature was just merged. Let me know if you have any issues and feel free to reach out to me on cdk.dev slack.

@TirTech @codeJack @akefirad @mikelane @venkates67 @hbjydev @derbauer97

Somewhat unrelated to this thread, but the CDK.dev slack link is broken.

@Xfel
Copy link

Xfel commented Jun 13, 2024

One thing I noticed is that the L2 construct does not support the KubernetesGroups parameter, which would be very useful for cases where we need more fine-grained options than what the pre-defined AccessPolicies provide.

@pahud
Copy link
Contributor

pahud commented Jun 19, 2024

@mikelane please create a new issue if it's still broken.

@Xfel thank you for your valuable feedback. Can you create a new feat issue for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-eks Related to Amazon Elastic Kubernetes Service effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants