【问题标题】:Cross account IAM roles for Kubernetes service account - s3 bucketKubernetes 服务账户的跨账户 IAM 角色 - s3 存储桶
【发布时间】:2022-05-10 03:18:57
【问题描述】:

嘿,我正在尝试跨帐户访问某个角色。 我有 2 个帐户:prod 和 non-prod。 和prod 帐户中的bucket,我试图将文件从非prod 角色写入到那里,该角色用作k8s 集群中的服务帐户。

在我配置的 prod 帐户中: 具有以下策略的角色(对存储桶的读写访问权限):

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "ListObjectsInBucket",
        "Effect": "Allow",
        "Action": [
            "s3:ListBucket"
        ],
        "Resource": [
            "arn:aws:s3:::test2"
        ]
    },
    {
        "Sid": "AllObjectActions",
        "Effect": "Allow",
        "Action": "s3:*Object",
        "Resource": [
            "arn:aws:s3:::test2/*"
        ]
    }
]

}

以及以下信任:

{


"Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::non-prod-AccountID:role/name-of-the-non-prod-role"
      },
      "Action": "sts:AssumeRole",
      "Condition": {}
    }
  ]
}

在我配置的非 prod 中:

具有以下政策的角色:

   {
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::prod-Account-ID:role/prod-role-name"
      },
      "Action": "sts:AssumeRole",
      "Condition": {}
    }
  ]
}

并信任如下:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::non-prod-accountID:oidc-provider/oidc.eks.us-east-1.amazonaws.com/id/1111111111111111111"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "oidc.eks.us-east-1.amazonaws.com/id/1111111111111111111:sub": 
            "system:serviceaccount:name-space:name-of-the-service-account"
        }
      }
    }
  ]
}

serviceAccount 注释是:

annotations: 
eks.amazonaws.com/role-arn: arn:aws:iam::non-prod-AccountID:role/non-prod-role-name

使用非产品角色的服务帐户从 pod 内部运行命令时:

aws s3 cp hello.txt s3://test2/hello.txt

我有:

upload failed: ./hello.txt to s3://test2/hello.txt An error occurred (AccessDenied) when calling the PutObject operation: Access Denied

顺便说一下,集群在另一个帐户(devops 帐户)中,如果它相关,肯定将 OIDC 提供者身份添加到非产品和产品帐户作为身份提供者。

【问题讨论】:

  • 嗨 talms1 欢迎来到 SO。您没有在问题中包含 ServiceAccount 的注释。我也很想知道您是否将Action 更改为没有前导通配符,它​​是否开始起作用?最后,不要忽视 aws --debug s3 cp 的强大功能,以了解 awscli 的想法。祝你好运
  • 我的 serviceAccount 注释是:注释:#eks.amazonaws.com/role-arn:arn:aws:iam::non-prod-AccountID:role/non-prod-role-name。你的意思是s3政策的行动?没关系,我什至可以放 s3FullAcess 策略,仍然有同样的错误。
  • 我认为您的“BTW”节是整个问题:您没有将“devops”帐户包含在任何这些信任关系中。您可以通过manually running sts assume-role-with-web-identity 使用--web-identity-token "$(cat /var/run/secrets/eks.amazonaws.com/serviceaccount/token)" 自行确认这一点

标签: amazon-web-services kubernetes amazon-s3 roles service-accounts


【解决方案1】:

如果您在尝试跨账户代入角色时收到错误An error occurred (InvalidIdentityToken) when calling the AssumeRoleWithWebIdentity operation: No OpenIDConnect provider found in your account for $oidc_url,但您可以在集群账户中正常代入角色,请注意以下几点:

EKS 帐户

  1. 创建服务帐户
apiVersion: v1
kind: ServiceAccount
metadata:
  name: $sa_name
  namespace: $eks_ns
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::$resource_account_id:role/$role_name
  1. 注释您的部署
spec.template.spec:
  serviceAccountName: $sa_name
  1. 获取有关您的集群 OIDC 提供商的信息
aws iam get-open-id-connect-provider --open-id-connect-provider-arn arn:aws:iam::$eks_cluster_account_id:oidc-provider/$oidc_provider

3.1。输出将是这样的:

{
    "Url": "...",
    "ClientIDList": ["..."],
    "ThumbprintList": ["..."],
    "CreateDate": "...",
    "Tags": [...]
}

3.2。记下输出(特别是 UrlThumbprintList

资源帐户

  1. 使用集群帐户的输出添加提供程序(如果您还没有)
aws iam create-open-id-connect-provider --url $oidc_url --client-id-list sts.amazonaws.com --thumbprint-list $oidc_thumbprint

这应该足以解决提到的错误停止。如果您现在收到 An error occurred (AccessDenied) when calling the AssumeRoleWithWebIdentity operation: Not authorized to perform sts:AssumeRoleWithWebIdentity,您可能正在使用 Principal.Federated 上的 $eks_cluster_account_id,而不是在上一步中创建的 $resource_account_id。因此,请确保您使用的是分配给资源帐户而非集群帐户的 IP 中的 ARN。

  1. 使用以下受信任实体策略创建角色和策略以访问您的资源:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Federated": "arn:aws:iam::$resource_account_id:oidc-provider/$oidc_provider"
            },
            "Action": "sts:AssumeRoleWithWebIdentity"
        }
    ]
}

此外,没有必要拥有两个角色。一个就够了。

【讨论】:

    猜你喜欢
    • 2020-06-24
    • 2021-04-25
    • 1970-01-01
    • 2020-10-11
    • 2021-12-07
    • 2017-03-16
    • 2018-11-26
    • 2016-03-17
    • 1970-01-01
    相关资源
    最近更新 更多