如何从GitHub Actions中使用AWS Code Artifact存储库进行身份验证?

huangapple go评论79阅读模式
英文:

How to authenticate with AWS Code Artifact Repository from GitHub Actions?

问题

我在AWS CodeArtifact中配置了一个存储库。我可以在本地计算机上作为管理员使用dotnet restore访问它。现在我正在尝试在GitHub actions中进行部署。

我有以下工作流程:

jobs:
  build-app:
    runs-on: ubuntu-latest
    steps:
    - name: Configure AWS CLI
      uses: aws-actions/configure-aws-credentials@v2
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: eu-west-2
    - name: Checkout
      uses: actions/checkout@v3.5.0
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v3.2.0
      with:
        dotnet-version: 6.0.x
    - name: Login to AWS CodeArtifact
      run: |
                    aws codeartifact login --tool dotnet --repository ${{ secrets.AWS_CODEARTIFACT_REPOSITORY }} --domain ${{ secrets.AWS_CODEARTIFACT_DOMAIN }}

然而,我收到以下错误:

在调用GetAuthorizationToken操作时发生错误(AccessDeniedException):用户arn:aws:iam::***:user/username未被授权执行codeartifact:GetAuthorizationToken操作,因为没有基于身份的策略允许codeartifact:GetAuthorizationToken操作
错误:进程以退出码254完成。

需要知道的关键事项。Code Artifact位于帐户A中,用户位于帐户B中。

IAM

用户的策略

{
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sts:GetServiceBearerToken",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "sts:AWSServiceName": "codeartifact.amazonaws.com"
                }
            }
        }
    ],
    "Version": "2012-10-17"
}

Code Artifact域

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::account-b-id:root"
            },
            "Action": "codeartifact:GetAuthorizationToken",
            "Resource": "arn:aws:codeartifact:eu-west-2:289579680938:domain/afterlife"
        }
    ]
}

CodeArtifact存储库

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::account-b-id:root"
            },
            "Action": [
                "codeartifact:ReadFromRepository",
                "codeartifact:ListPackages",
                "codeartifact:ListPackageVersions",
                "codeartifact:ListPackageVersionDependencies",
                "codeartifact:ListPackageVersionAssets",
                "codeartifact:GetRepositoryEndpoint",
                "codeartifact:GetPackageVersionReadme",
                "codeartifact:GetAuthorizationToken",
                "codeartifact:DescribeRepository",
                "codeartifact:DescribePackageVersion"
            ],
            "Resource": "arn:aws:codeartifact:eu-west-2:289579680938:repository/afterlife/nuget-mirror"
        }
    ]
}

我做错了什么?

英文:

I have a repository configured in AWS CodeArtifact. I can access it as an administrator from my local machine with dotnet restore. Now I am trying to get it to work with GitHub actions for deployment.

I have the following workflow:

jobs:
  build-app:
    runs-on: ubuntu-latest
    steps:
    - name: Configure AWS CLI
      uses: aws-actions/configure-aws-credentials@v2
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: eu-west-2
    - name: Checkout
      uses: actions/checkout@v3.5.0
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v3.2.0
      with:
        dotnet-version: 6.0.x
    - name: Login to AWS CodeArtifact
      run: |
                    aws codeartifact login --tool dotnet --repository ${{ secrets.AWS_CODEARTIFACT_REPOSITORY }} --domain ${{ secrets.AWS_CODEARTIFACT_DOMAIN }}

However, I get the error:

An error occurred (AccessDeniedException) when calling the GetAuthorizationToken operation: User: arn:aws:iam::***:user/username is not authorized to perform: codeartifact:GetAuthorizationToken on resource: arn:aws:codeartifact:eu-west-2:***:domain/*** because no identity-based policy allows the codeartifact:GetAuthorizationToken action
Error: Process completed with exit code 254.

Key things to know. Code Artifact is in Account A, and the User is in Account B.

IAM

Policy on the User

{
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sts:GetServiceBearerToken",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "sts:AWSServiceName": "codeartifact.amazonaws.com"
                }
            }
        }
    ],
    "Version": "2012-10-17"
}

Code Artifact Domain

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::account-b-id:root"
            },
            "Action": "codeartifact:GetAuthorizationToken",
            "Resource": "arn:aws:codeartifact:eu-west-2:289579680938:domain/afterlife"
        }
    ]
}

CodeArtifact Repository

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::account-b-id:root"
            },
            "Action": [
                "codeartifact:ReadFromRepository",
                "codeartifact:ListPackages",
                "codeartifact:ListPackageVersions",
                "codeartifact:ListPackageVersionDependencies",
                "codeartifact:ListPackageVersionAssets",
                "codeartifact:GetRepositoryEndpoint",
                "codeartifact:GetPackageVersionReadme",
                "codeartifact:GetAuthorizationToken",
                "codeartifact:DescribeRepository",
                "codeartifact:DescribePackageVersion"
            ],
            "Resource": "arn:aws:codeartifact:eu-west-2:289579680938:repository/afterlife/nuget-mirror"
        }
    ]
}

What am I doing wrong?

答案1

得分: 0

我已解决。

关键变化是我需要将以下策略添加到调用 aws codeartifact login 时活跃的 IAM 用户中。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "codeartifact:Describe*",
                "codeartifact:Get*",
                "codeartifact:List*",
                "codeartifact:ReadFromRepository"
            ],
            "Effect": "Allow",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "sts:GetServiceBearerToken",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "sts:AWSServiceName": "codeartifact.amazonaws.com"
                }
            }
        }
    ]
}

我还需要在 aws codeartifact login 命令中添加 --owner <code-artifact-所部署的账户-id>

英文:

I worked it out.

The key change was that I needed to add the following policy to the IAM user that was active when calling aws codeartifact login.

{
    &quot;Version&quot;: &quot;2012-10-17&quot;,
    &quot;Statement&quot;: [
        {
            &quot;Action&quot;: [
                &quot;codeartifact:Describe*&quot;,
                &quot;codeartifact:Get*&quot;,
                &quot;codeartifact:List*&quot;,
                &quot;codeartifact:ReadFromRepository&quot;
            ],
            &quot;Effect&quot;: &quot;Allow&quot;,
            &quot;Resource&quot;: &quot;*&quot;
        },
        {
            &quot;Effect&quot;: &quot;Allow&quot;,
            &quot;Action&quot;: &quot;sts:GetServiceBearerToken&quot;,
            &quot;Resource&quot;: &quot;*&quot;,
            &quot;Condition&quot;: {
                &quot;StringEquals&quot;: {
                    &quot;sts:AWSServiceName&quot;: &quot;codeartifact.amazonaws.com&quot;
                }
            }
        }
    ]
}

I also needed to add the --owner &lt;account-id-of-where-code-artifact-is-deployed&gt; to the aws codeartifact login command.

huangapple
  • 本文由 发表于 2023年7月12日 21:58:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定