AWS CodeArtifact与Dependabot的集成

6

我正尝试使用Dependabot与AWS CodeArtifact一起使用,但我不断遇到身份验证问题。

Dependabot无法对私有软件包注册表进行身份验证: 使用以下私有软件包注册表导致更新失败:###-###.d.codeartifact..amazonaws.com/npm/private。

日志:

proxy | 2022/06/29 16:52:05 [022] GET https://###-###.d.codeartifact.###.amazonaws.com:443/npm/private/hermes-engine proxy | 2022/06/29 16:52:06 [022] 401 https://###-###.d.codeartifact.###.amazonaws.com:443/npm/private/hermes-engine proxy | 2022/06/29 16:52:06 [024] GET https://###-###.d.codeartifact.###.amazonaws.com:443/npm/private/hermes-engine proxy | 2022/06/29 16:52:06 [024] 401 https://###-###.d.codeartifact.###.amazonaws.com:443/npm/private/hermes-engine updater | INFO <job_408163671> 在更新hermes-engine时处理了错误:private_source_authentication_failure {:source=>"###-###.d.codeartifact.###.amazonaws.com/npm/private"}

我尝试通过更改不同的dependabot.yml配置来解决问题,例如使用带路径的完整CodeArtifact URL或者使用token而不是用户名和密码。

我的dependabot.yml

version: 2
registries:
  npm-codeartifact:
    type: npm-registry
    url: https://<registry>-<account>.d.codeartifact.<region>.amazonaws.com
    username: aws
    password: ${{secrets.CODEARTIFACT_TOKEN}}

  - package-ecosystem: "npm"
    schedule:
      interval: "daily"
    registries:
      - npm-codeartifact

我应该使用什么来让Dependabot与CodeArtifact配合工作?

3个回答

2

这个回答基于@aphexlog的回答以及@Andrea De Luisi关于如何更新 Code Artifact 令牌的评论,因为它在12小时后会过期。我的解决方案以 Nuget Package Manager 为例,并使用一个独立的 GitHub Action (.yml文件)来运行cron作业,与dependabot配置分开。


前提条件

  • dependabot.yml 文件位于 YourProject/.github/dependabot.yml
  • aws-code-artifact-updatetoken.yml 文件(可以随意命名)位于 YourProject\.github\workflows\aws-code-artifact-updatetoken.yml
  • AWS 凭证作为存储库/组织机密
  • 个人访问令牌经典版(PAT),有效期为 30-90 天,如果您需要更安全的话。但是对于这种用例,我认为没有过期或 365 天过期已足够安全。

要创建您的 PAT(经典版),请转到您的个人帐户设置,向下滚动直到看到 <> 开发者设置,然后转到个人访问令牌下拉菜单并选择令牌(经典版)。生成一个新令牌,并确保单击生成新令牌(经典版)。如果您不属于任何组织,请单击 workflow 范围(这将强制使用 repo 范围)。如果您属于组织,请还单击 admin:org 范围

选项 1

  • 创建或加入GitHub组织
  • 进入组织设置,向下滚动至"secrets",选择"dependabot"并创建一个Dependabot组织密码

选项2

  • 进入您的存储库,单击"设置"选项卡
  • 向下滚动至"secrets",选择"dependabot"并创建一个新的Dependabot存储库密码

我已经将我的机密创建为CODE_ARTIFACT_TOKEN_PRIVATE_REPOS的dependabot组织机密,并且仅可访问私有存储库。我的初始值是一个虚拟值"test",因为它将在工作流程运行后被运行和更新为正确的令牌。

aws-code-artifact-updatetoken.yml

name: Update AWS Code Artifact Token

on:
  schedule:
    # Runs every 10 hours
    - cron: "0 */10 * * *"
  workflow_dispatch:

这将为工作流创建名称,并设置一个cron-job以使GitHub Actions自动每10小时运行一次

jobs:
  update-code-artifact-token:
    runs-on: ubuntu-latest
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      AWS_DEFAULT_REGION: us-west-2
      GH_TOKEN: ${{ secrets.ORG_SECRETS }}
    steps:
      - name: Get Code Artifact Token
        run: |
          export CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain fullstackindie --domain-owner 896630178141 --query authorizationToken --output text`

创建一个在Ubuntu上运行的作业。以一种格式创建环境变量,可以自动地被安装在GitHub Action runner上的aws cligh cli (github)使用。我正在将CODEARTIFACT_AUTH_TOKEN导出为当前shell中的变量,在稍后的步骤中使用。这个变量得到了一个正确的CodeArtifact令牌。请用您的AWS CodeArtifact帐户信息替换--domain fullstackindie --domain-owner 896630178141。
  - name: Update Code Artifact Token
    run: |
      gh secret set CODE_ARTIFACT_TOKEN_PRIVATE_REPOS --org FullStackIndieLLC --visibility private --app dependabot --body "$CODEARTIFACT_AUTH_TOKEN"

这最后一步使用github cli来设置之前为您的repo或组织创建的Dependabot Repository/Organization secret。您可以为gh secret set设置不同的标志/参数,但您必须包括--app dependabot以更新Dependabot secrets。您还必须包括包含令牌的--body参数。当使用环境变量时,必须使用"$MY_ENV"格式的--body。因为我的dependabot组织机密仅适用于私有存储库,所以我将可见性设置为私有。我还使用--org flag来更改我的Dependabot组织机密。如果使用Dependabot Repository secret,则您的令牌将类似于此

  - name: Update Code Artifact Token
    run: |
      gh secret set CODE_ARTIFACT_TOKEN_PRIVATE_REPOS --repo MyRepo --visibility private --app dependabot --body "$CODEARTIFACT_AUTH_TOKEN"

我建议通过进入您的存储库中的“操作”选项卡,单击工作流程,然后在下拉菜单中单击“运行工作流程”来强制执行工作流程。否则,在第一次运行之前,您的存储库可能需要大约10个小时左右才能更新令牌。

1

@aphexlog 错过了一个让 dependabot 与 poetry 协同工作的关键点。

你需要在令牌值之前添加 aws:

version: 2
registries:
  npm-codeartifact:
    type: npm-registry
    url: https://<registry>-<account>.d.codeartifact.<region>.amazonaws.com
    token: aws:${{secrets.CODEARTIFACT_TOKEN}}
updates:
  # Keep npm dependencies up to date
    - package-ecosystem: "npm"
      schedule:
        interval: "daily"
      registries:
      - npm-codeartifact

0

如果您正在使用令牌,您不需要指定用户名:

version: 2
registries:
  npm-codeartifact:
    type: npm-registry
    url: https://<registry>-<account>.d.codeartifact.<region>.amazonaws.com
    token: ${{secrets.CODEARTIFACT_TOKEN}}
updates:
  # Keep npm dependencies up to date
    - package-ecosystem: "npm"
      schedule:
        interval: "daily"
      registries:
      - npm-codeartifact

嗨,谢谢分享。CodeArtifact令牌在12小时后过期。您是否找到了一种方法来更新或每次操作运行时直接重新生成它?(例如使用CodeArtifact访问/secretKey) - Andrea De Luisi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接