【问题标题】:Why do we need Assume Role for Unit Test in GitHub and how to use it?为什么我们需要在 GitHub 中进行单元测试的 Assume Role 以及如何使用它?
【发布时间】:2022-11-13 22:08:25
【问题描述】:

我想知道为什么我们在 GitHub 中创建测试时需要使用 AWS Assume Role。

请通过屏幕截图和链接给我更多信息。

【问题讨论】:

    标签: amazon-web-services unit-testing github identity-management assume-role


    【解决方案1】:

    GitHub 正在构建一台新机器来测试我们的代码。该机器在我们的 VPC 之外,因此它需要凭据才能登录 AWS 产品。一种选择是在代码中隐式写入凭据,但最佳做法是将存储库与 AWS 角色连接,该角色将专门为测试而创建。此担任角色的权限有限;因此它只能在 GitHub 中进行测试。

    请按照以下步骤操作:

    1.在身份提供者中创建提供者

    1.1 打开 IAM 控制台https://console.aws.amazon.com/iam/
    1.2 在导航窗格中,选择身份提供者,然后选择添加提供者。
    1.3 对于配置提供程序,选择 OpenID Connect。
    1.4 对于提供商 URL:使用https://token.actions.githubusercontent.com
    1.5 选择获取指纹以验证您的 IdP 的服务器证书。
    1.6 对于“观众”:使用sts.amazonaws.com

    2. 创建 IAM 角色

    2.1 在 AWS IAM 中创建角色。我建议以隐式名称调用它,例如“github-actions-role”。
    2.2 给它运行测试所需的最少权限。常见权限是 S3 权限、DynamoDB 权限等。
    2.3 配置角色和信任策略。编辑信任策略以将子字段添加到验证条件。例如:

    "Condition": {
                    "StringLike": {
                        "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
                        "token.actions.githubusercontent.com:sub": "repo:Lend-AI/repo-name:*"
                    }
                }
    

    2.4 如果您希望条件准确,请使用“StringEquals”而不是“StringLike”。
    2.5 如果您想指定分支,请替换此行:

    "token.actions.githubusercontent.com:sub": "repo:Lend-AI/ repo-name:*"
    

    和:

    "token.actions.githubusercontent.com:sub": "repo:Lend-AI/repo-name:ref:refs/heads/branch-name"
    

    2.6 您可以使用通配符 (*) 替换存储库名称,以包含 Lend-AI 组织中的所有存储库。
    2.7 您的信任政策应如下所示:

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Principal": {
                    "Federated": "arn:aws:iam::123456123456:oidc-provider/token.actions.githubusercontent.com"
                },
                "Action": "sts:AssumeRoleWithWebIdentity",
                "Condition": {
                    "StringLike": {
                        "token.actions.githubusercontent.com:sub": "repo:octo-org/octo-repo:*"
                    },
                    "ForAllValues:StringEquals": {
                        "token.actions.githubusercontent.com:iss": "https://token.actions.githubusercontent.com",
                        "token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
                    }
                }
            }
        ]
    }
    
    

    2.8 注意IAM创建页面顶部的“Maximum session duration”。这将定义您的角色可以执行测试多长时间。如果您的测试可以运行超过一小时,请更改默认定义。
    2.9 回到“身份提供者”,点击刚刚创建的提供者(token.actions.githubusercontent.com)。然后,单击“分配角色”>>使用现有角色>>选择您在此步骤中创建的角色。

    assign-role-bottom-image

    3. 更新 GitHub 动作

    更新您的单元测试 Yaml 文件。它应该如下所示:

    
    on: [push]
    
    permissions:
      id-token: write
      contents: read
    
    jobs:
      build:
    
        runs-on: ubuntu-latest
    
        steps:
        - name: Configure AWS Credentials
          uses: aws-actions/configure-aws-credentials@v1
          with:
            role-to-assume: arn:aws:iam:YOUR_AWS_ACCOUNT_ID:role/github-actions-role
            aws-region: YOUR_REGION
        - uses: actions/checkout@v2
        - name: Set up Python 3.9
          uses: actions/setup-python@v2
          with:
            python-version: 3.9
        - name: Install dependencies
          run: |
            python -m pip install --upgrade pip
            pip install -r requirements.txt
        - name: Running Python Unit Tests
          run: python -m unittest discover tests
    

    进一步阅读:
    https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services

    https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc.html

    https://github.com/aws-actions/configure-aws-credentials#assuming-a-role

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 2018-01-07
      • 2017-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多