【问题标题】:AWS Code Commit Cross Account IAM RolesAWS Codecommit 跨账户 IAM 角色
【发布时间】:2017-03-16 06:32:27
【问题描述】:

我有两个 AWS 账户:

DEV:  111111111111
PROD: 999999999999

我在名为 prodRepo 的 prod 帐户中创建了一个代码提交仓库。

我想要做的是允许 DEVPROD 帐户上的 ec2 实例对此 repo 具有只读访问权限。所以git clonegit pull等等……

我可以使用名为 codecommit-tester 的以下 IAM 实例配置文件在我的 PROD 帐户上轻松完成此操作

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "codecommit:BatchGetRepositories",
        "codecommit:Get*",
        "codecommit:GitPull",
        "codecommit:List*"
      ],
      "Resource": "arn:aws:codecommit:us-east-1:999999999999:prodRepo"
    }
  ]
}

信任关系政策是:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
}

然后我使用 git config 中的 aws 凭证助手来执行只读 git 操作,而无需在我的机器上存储凭证(它从实例元数据中获取代码提交的凭证)。

$ cat ~/.gitconfig
[credential]
    helper = !aws codecommit credential-helper $@
    UseHttpPath = true

我遇到的问题是在 DEV 帐户上创建一个 IAM 策略/角色来执行与 PROD 帐户相同的操作。这是我尝试过的。

我编辑了 PROD 帐户上的信任关系以信任 DEV 帐户:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    },
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::111111111111:root"
      },
      "Action": "sts:AssumeRole"
    }
}

现在我认为这意味着DEV 帐户可以担任此角色。在 DEV 帐户上,我创建了这些附加到角色的 IAM 策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codecommit:BatchGetRepositories",
                "codecommit:Get*",
                "codecommit:GitPull",
                "codecommit:List*"
            ],
            "Resource": "arn:aws:codecommit:us-east-1:999999999999:prodRepo"
        }
    ]
}

{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": "sts:AssumeRole",
        "Resource": "arn:aws:iam::999999999999:role/codecommit-tester"
    }
}

在使用此 IAM 实例配置文件启动 ec2 实例后,我在 DEV 账户上使用凭证助手,执行git clone 时出现此错误:

$ git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/prodRepo
Cloning into 'prodRepo'...
fatal: unable to access 'https://git-codecommit.us-east-1.amazonaws.com/v1/repos/prodRepo/': The requested URL returned error: 403

那么,我在DEV 上的 IAM 角色/策略中遗漏了什么来完成这项工作?

【问题讨论】:

    标签: amazon-web-services amazon-ec2 amazon-iam aws-codecommit


    【解决方案1】:

    我认为您不需要您提到的 dev 中的 iam 角色(在 DEV 帐户上,我创建了附加到角色的这些 IAM 策略)....尚未尝试使用实例跨帐户担任角色。。 但是如果您可以使用

    在 prod 帐户中创建新的 IAM 角色
    {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sts:AssumeRole",
            "Resource": "arn:aws:iam::999999999999:role/codecommit-tester"
        }
    ]
    }
    

    信任关系类似于

    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::111111111111:root"
      },
      "Action": "sts:AssumeRole"
    }
    

    并在 dev ec2 角色中承担新的 IAM ARN。

    【讨论】:

    • 是的,这有帮助。谢谢!
    猜你喜欢
    • 2015-07-11
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多