将 MFA 与 EKS kubectl & aws-iam-authenticator 结合使用



我一直在尝试让 MFA 与 kubectl 合作,以确保对 AWS 中 EKS 主节点的访问。文档似乎暗示这是可能的,但我遇到了问题,我无法弄清楚。

没有任何特殊之处,我可以连接到我的 EKS 集群:

kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   172.20.0.1   <none>        443/TCP   2d

如果我将 MFA 添加到 AWS CLI,请遵循以下帖子:

如何将 MFA 与 AWS CLI 结合使用?

https://docs.aws.amazon.com/cli/latest/userguide/cli-roles.html

我像这样设置了我的本地凭据:

[default]
aws_access_key_id = **********************
aws_secret_access_key = **********************
region = us-west-2
[default_role]
mfa_serial = arn:aws:iam::1234567890:mfa/spanktar
role_arn = arn:aws:iam::1234567890:role/test_assumeRole
source_profile = default

。然后我可以看到 AWS CLI 具有 MFA:

aws sts get-caller-identity --profile default_role
Enter MFA code for arn:aws:iam::1234567890:mfa/spanktar: 123456
{
"UserId": "**********************:botocore-session-1234567890",
"Account": "1234567890",
"Arn": "arn:aws:sts::1234567890:assumed-role/test_assumeRole/botocore-session-1234567890"
}

然后我可以验证:

aws sts assume-role --role-arn arn:aws:iam::1234567890:role/test_assumeRole --role-session-name default_role
{
"Credentials": {
"AccessKeyId": "**********************",
"SecretAccessKey": "**********************",
"SessionToken": "FOO",
"Expiration": "2018-10-11T21:19:20Z"
},
"AssumedRoleUser": {
"AssumedRoleId": "**********************:default_role",
"Arn": "arn:aws:sts::1234567890:assumed-role/test_assumeRole/default_role"
}
}

Kubectl 仍然正常工作,因为它尚未使用配置文件:

kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   172.20.0.1   <none>        443/TCP   2d

然后,我按照本文档设置 k8s 以使用该配置文件

users:
- name: aws
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
command: aws-iam-authenticator
env:
- name: "AWS_PROFILE"
value: "default_role"
args:
- "token"
- "-i"
- "test-eks-cluster"

所以现在当我尝试kubectl它要求MFA,但它永远无法满足:

kubectl get svc
Assume Role MFA token code: 123456
Assume Role MFA token code: 123456
could not get token: AccessDenied: MultiFactorAuthentication failed with invalid MFA one time pass code.
status code: 403, request id: 123456-cd93-11e8-80a5-1234567898765
E1011 13:22:07.385200   28191 exec.go:230] refreshing credentials: exec: exit status 1
No resources found.
error: You must be logged in to the server (Unauthorized)

AWS 角色test_assumeRole如下所示:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::1234567890:user/spanktar"
},
"Action": "sts:AssumeRole"
}
]
}

相关策略test_assumePolicy如下所示:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::1234567890:role/test_assumeRole"
}
]
}

所以,问题:

  • 我是否需要将其中任何内容附加到 IAM 用户?文档似乎没有建议。
  • 我在这里错过了什么来让它运行?它似乎几乎正确连接起来。

我遇到了同样的问题,并通过将-r选项添加到kubectl配置文件来管理它。

在您的情况下,它看起来像这样:

users:
- name: aws
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
command: aws-iam-authenticator
env:
- name: "AWS_PROFILE"
value: "default_role"
args:
- "token"
- "-i"
- "test-eks-cluster"
- "-r"
- "arn:aws:iam::1234567890:role/test_assumeRole"

我希望这对你也有用。

相关内容

  • 没有找到相关文章

最新更新