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

Better support for aws-iam-authenticator #376

Merged
merged 4 commits into from
Feb 12, 2019
Merged

Conversation

joshmyers
Copy link
Contributor

@joshmyers joshmyers commented Feb 5, 2019

what

This commit adds a command kopsctl login to build a kubecfg that optionally can use
aws-iam-authenticator, given we have the KOPS_AWS_IAM_AUTHENTICATOR_ENABLED set to true

why

We want to enable RBAC using aws-iam-authenticator to auth users against the Kops cluster via IAM roles.

Note that we have to fetch the CA data using openssl s_client as we don’t have a kubecfg to pull it from yet and we don't want to have to access the Kops state store S3 bucket where this information is stored.

Note this is a non breaking change and while we can use the templated kubecfg, as admin operators, we can still run kops export kubectl.

kopsctl login should be run after assuming an AWS role within Geodesic.

There will be further PRs to create specific IAM roles for KubernetesAdmin and KubernetesRead See: cloudposse/terraform-aws-components#111

Testing

Given an aws-iam-authenticator configMap of:

clusterID: us-west-2.testing.cloudposse.co
    server:
      mapRoles:
      - roleARN: arn:aws:iam::126450723953:role/KubernetesAdmin
        username: kubernetes-admin
        groups: ["system:masters"]
      - roleARN: arn:aws:iam::126450723953:role/KubernetesReadOnly
        username: kubernetes-readonly
        groups: ["system:authenticated"]

and ~/.aws/config containing:

[profile cpco-testing-admin]
region         = us-west-2
role_arn       = arn:aws:iam::126450723953:role/OrganizationAccountAccessRole
mfa_serial     = arn:aws:iam::323330167063:mfa/josh
source_profile = cpco

[profile cpco-testing-admin-kubernetes]
region         = us-west-2
role_arn       = arn:aws:iam::126450723953:role/KubernetesAdmin
mfa_serial     = arn:aws:iam::323330167063:mfa/josh
source_profile = cpco

[profile cpco-testing-readonly-kubernetes]
region         = us-west-2
role_arn       = arn:aws:iam::126450723953:role/KubernetesReadOnly
mfa_serial     = arn:aws:iam::323330167063:mfa/josh
source_profile = cpco

[profile cpco-root-admin]
region         = us-west-2
role_arn       = arn:aws:iam::323330167063:role/cpco-root-admin
mfa_serial     = arn:aws:iam::323330167063:mfa/josh
source_profile = cpco

[profile cpco]

cp


login to testing.cloudposse.co geodesic

Try and kubectl get nodes:

 ✗   (none) ~ ⨠  kubectl get nodes
The connection to the server localhost:8080 was refused - did you specify the right host or port?

Build our kubecfg:

 ✗   (none) ~ ⨠  kopsctl cluster kubecfg build
kopsctl ≫ starting task cluster.kubecfg.build
Wrote configuration to /dev/shm/kubecfg...
 ⧉  testing (⎈ |us-west-2.testing.cloudposse.co:default)

Check our kubecfg:

 ✗   (none) ~ ⨠  cat /dev/shm/kubecfg
apiVersion: v1
kind: Config
preferences: {}

clusters:
- cluster:
    server: https://api.us-west-2.testing.cloudposse.co
    certificate-authority-data: <<REDACTED>>
  name: us-west-2.testing.cloudposse.co

contexts:
- context:
    cluster: us-west-2.testing.cloudposse.co
    user: us-west-2.testing.cloudposse.co
  name: us-west-2.testing.cloudposse.co

current-context: us-west-2.testing.cloudposse.co
users:
- name: us-west-2.testing.cloudposse.co
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1alpha1
      command: aws-iam-authenticator
      args:
        - token
        - -i
        - us-west-2.testing.cloudposse.co

Try and get nodes without having assumed our IAM role:

 ✗   (none) ~ ⨠  kubectl get nodes
could not get token: could not create session: SharedConfigAssumeRoleError: failed to load assume role for arn:aws:iam::126450723953:role/OrganizationAccountAccessRole, source profile has no shared credentials
could not get token: could not create session: SharedConfigAssumeRoleError: failed to load assume role for arn:aws:iam::126450723953:role/OrganizationAccountAccessRole, source profile has no shared credentials
could not get token: could not create session: SharedConfigAssumeRoleError: failed to load assume role for arn:aws:iam::126450723953:role/OrganizationAccountAccessRole, source profile has no shared credentials
could not get token: could not create session: SharedConfigAssumeRoleError: failed to load assume role for arn:aws:iam::126450723953:role/OrganizationAccountAccessRole, source profile has no shared credentials
could not get token: could not create session: SharedConfigAssumeRoleError: failed to load assume role for arn:aws:iam::126450723953:role/OrganizationAccountAccessRole, source profile has no shared credentials
Unable to connect to the server: getting credentials: exec: exit status 1

Assume IAM role so we can successfully run some kubectl commands:

 ⧉  testing (⎈ |us-west-2.testing.cloudposse.co:default)
 ✗   (none) ~ ⨠  assume-role
Enter passphrase to unlock /conf/.awsvault/keys/:
* Assumed role arn:aws:iam::126450723953:role/OrganizationAccountAccessRole
 ✓   (cpco-testing-admin) ~ ⨠  kubectl get nodes
NAME                                           STATUS     ROLES    AGE   VERSION
ip-172-20-100-72.us-west-2.compute.internal    Ready      master   2d    v1.10.11
ip-172-20-111-167.us-west-2.compute.internal   Ready      node     25d   v1.10.11
ip-172-20-35-199.us-west-2.compute.internal    Ready      node     15d   v1.10.11
ip-172-20-41-111.us-west-2.compute.internal    Ready      master   2d    v1.10.11
ip-172-20-45-164.us-west-2.compute.internal    Ready      node     58d   v1.10.11
ip-172-20-92-206.us-west-2.compute.internal    Ready      node     3d    v1.10.11
ip-172-20-94-81.us-west-2.compute.internal     NotReady   master   2d    v1.10.11

@osterman
Copy link
Member

osterman commented Feb 5, 2019

Run make bash/fmt/check

@osterman
Copy link
Member

osterman commented Feb 5, 2019

please add links to your research in the PR description
also, if there were any other influences/examples for taking this approach.

@@ -0,0 +1,38 @@
#!/bin/bash
Copy link
Member

Choose a reason for hiding this comment

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

Something about this should tie it to being necessary for usage with aws-iam-authenticator (like a comment) =)

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 thought was that this would be the way to generate kubecfg for all usage (where folks don't want to kops export kubecfg) not necessarily just for the aws-iam-authenticator use case

@osterman osterman changed the title Add build-kube-cfg Better support for aws-iam-authenticator Feb 8, 2019
Dockerfile Outdated Show resolved Hide resolved
Copy link
Member

@osterman osterman left a comment

Choose a reason for hiding this comment

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

See comments

@joshmyers joshmyers force-pushed the kops_iam_authenticator branch 5 times, most recently from 0462d63 to 23a90a6 Compare February 9, 2019 19:21
This commit adds a command to build a kubecfg that optionally can use
aws-iam-authenticator, given we have the
`KOPS_AWS_IAM_AUTHENTICATOR_ENABLED` set to true

We want to enable RBAC using aws-iam-authenticator to auth users against
the Kops cluster via IAM roles.

Note that we have to fetch the CA data using `openssl s_client` as we
don’t have a kubecfg to pull it from yet and we don't want to have to
access the Kops state store S3 bucket where this information is stored.

The build-kubecfg command has a check_env_vars function so that we don't
have to have already declared the cluster_ca function, which keeps
things a little cleaner.

`kopsctl cluster kubecfg build` should be run after assuming an AWS role
within Geodesic.
Rather than default the assumed role to AWS_DEFAULT_PROFILE use fzf to
create a menu for all profiles to do with an AWS organisation (read:
namespace) based off AWS_CONFIG_FILE entries. Example:

Given the below ~/.aws/config file:

```
[profile cpco-testing-admin]
region         = us-west-2
role_arn       = arn:aws:iam::126450723953:role/OrganizationAccountAccessRole
mfa_serial     = arn:aws:iam::323330167063:mfa/josh
source_profile = cpco

[profile cpco-testing-admin-kubernetes]
region         = us-west-2
role_arn       = arn:aws:iam::126450723953:role/KubernetesAdmin
mfa_serial     = arn:aws:iam::323330167063:mfa/josh
source_profile = cpco

[profile cpco-testing-readonly-kubernetes]
region         = us-west-2
role_arn       = arn:aws:iam::126450723953:role/KubernetesReadOnly
mfa_serial     = arn:aws:iam::323330167063:mfa/josh
source_profile = cpco

[profile cpco]
```

We will end up creating an fzf choice menu of:

```
cpco-testing-admin
cpco-testing-admin-kubernetes
cpco-testing-readonly-kubernetes
```

This makes switching between roles significantly less cumbersome. Note
that we still disallow nested assuming roles, so we need to ‘exit’ in
between switching roles.
No point letting the user select a role if already assumed.
Instead of `kopsctl cluster kubecfg build` because typing!
Copy link
Member

@osterman osterman left a comment

Choose a reason for hiding this comment

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

🥇

@joshmyers joshmyers merged commit 6ccebe6 into master Feb 12, 2019
@joshmyers joshmyers deleted the kops_iam_authenticator branch February 12, 2019 09:33
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