里面有多个访问令牌 CodeBuild - AWS CodeBuild

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

里面有多个访问令牌 CodeBuild

CodeBuild 支持从您的密钥中 AWS Secrets Manager 或通过 AWS CodeConnections 连接向第三方提供商获取访问令牌。您可以将您的密钥或连接设置为与指定第三方提供商(例如 En GitHub terprise 或 Bitbucket)进行交互的默认凭据。 GitHub

您可以将源证书设置为三个不同的级别:

  1. 所有项目的账户级别凭证:这些是 AWS 账户中所有项目的默认凭证。当未指定项目或源代码级别凭据时,它们将用于项目。

  2. 特定存储库的源代码级别凭据:这是在项目源上定义 Secrets Manager 密钥或 CodeConnections 连接的时候。这些凭据将仅用于对指定源存储库的操作。这允许您在同一个项目中设置具有不同权限范围的多个访问令牌,而不必使用默认的账户级别证书。

  3. 项目级别的备用凭据:您可以使用NO_SOURCE作为主要来源类型来设置项目级别的备用凭证,并在其上定义密钥或连接。当你在一个项目上有多个来源,但想对它们使用相同的凭证,或者你不想为项目使用默认的账户级别凭证时,可以使用这个选项。

步骤 1:创建 Secrets Manager 密钥或 CodeConnections 连接

按照以下说明创建 Secrets Manager 密钥或 CodeConnections 连接:

第 2 步:向 CodeBuild 项目IAM角色授予对 Secrets Manager 密钥的访问权限

注意

在继续操作之前,你必须有权访问在 Secrets Manager 中创建的令牌或者 CodeConnections。

要授予 CodeBuild 项目IAM角色对 Secrets Manager 或的访问权限 CodeConnections,您必须添加以下IAM策略。

授予 CodeBuild 项目IAM角色访问权限
  1. 按照 CodeBuild 项目的说明 CodeBuild 允许与其他 AWS 服务进行交互为 CodeBuild 项目创建IAM角色。

  2. 请执行以下操作之一:

    • 将以下IAM策略添加到您的 CodeBuild 项目角色以授予对您的密钥的访问权限。

      { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "secretsmanager:GetSecretValue" ], "Resource": [ "<secret-arn>" ] } ] }

      (可选)如果您使用 AWS KMS 客户管理的密钥来加密 Secrets Manager 密钥,则可以添加以下政策声明来授予访问权限。

      { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "kms:Decrypt" ], "Resource": "<kms-key-arn>", "Condition": { "StringEquals": { "kms:EncryptionContext:SecretARN": "<secret-arn>" } } } ] }
    • 将以下IAM策略添加到您的 CodeBuild 项目角色以授予对连接的访问权限。

      { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "codeconnections:GetConnectionToken", "codeconnections:GetConnection" ], "Resource": [ <connection-arn> ] } ] }

第 3 步:配置 Secrets Manager 或 CodeConnections 令牌

您可以使用 Secrets Manager 或 CodeConnections 令牌将源证书设置为三个不同的级别。

将 Secrets Manager 或 CodeConnections 令牌配置为账户级别证书

您可以将 Secrets Manager 密钥或 CodeConnections 连接配置为账户级凭证,并在项目中使用。

AWS Management Console
要将连接配置为账户级别的凭证,请参阅 AWS Management Console
  1. 对于源提供商,请选择 Bitbucket 或 E GitHub nter prise。GitHub

  2. 对于凭证,请执行以下任一操作:

    • 选择 “默认来源凭据”,使用您账户的默认来源凭据应用于所有项目。

      1. 如果您未连接到源提供商,请选择 “管理默认来源凭据”。

      2. 对于凭证类型,请选择一种凭证类型。

      3. 如果您选择 CodeConnections,请选择使用现有连接或创建新连接。

        如果您选择了其他凭证类型,请为服务选择要用于存储令牌的服务,然后执行以下操作:

        • 如果您选择使用 S ecrets Manager,则可以选择使用现有密钥连接或创建新密钥并选择保存。有关如何创建新密钥的更多信息,请参阅创建令牌并将其存储在 Secrets Manager 密钥中

        • 如果您选择使用 CodeBuild,请输入您的令牌或用户名和应用程序密码,然后选择保存

    • 选择 “自定义来源凭据”,使用自定义来源凭据来覆盖您账户的默认设置。

      1. 对于凭证类型,请选择一种凭证类型。

      2. 在 “连接” 中,选择使用现有连接或创建新连接。

AWS CLI
要将连接配置为账户级别的凭证,请参阅 AWS CLI
  • 打开终端(Linux、macOS 或 Unix)或命令提示符(Windows)。 AWS CLI 使用运行import-source-credentials命令。

    使用以下命令配置 Secrets Manager 密钥:

    aws codebuild import-source-credentials \ --token "<secret-arn>" \ --server-type <source-provider> \ --auth-type SECRETS_MANAGER \ --region <aws-region>

    使用以下命令配置 CodeConnections 连接:

    aws codebuild import-source-credentials \ --token "<connection-arn>" \ --server-type <source-provider> \ --auth-type CODECONNECTIONS \ --region <aws-region>

    此命令允许您导入令牌作为账户级别的默认源凭证。使用导入凭证时 ImportSourceCredentialsAPI,除非在项目中配置了一组更具体的凭证,否则 CodeBuild 将使用该令牌进行与源提供商的所有交互,例如 webhook、构建状态报告和 git clone 操作。

现在,您可以在构建项目中使用该令牌并运行它。有关更多信息,请参阅在中创建构建项目 AWS CodeBuild手动运行 AWS CodeBuild 构建

将多个令牌配置为源代码级凭证

要使用 Secrets Manager 密钥或 CodeConnections 连接作为源代码级凭据,请直接在 CodeBuild 项目中引用该令牌,然后开始构建。

AWS Management Console
要在中将多个令牌配置为源级凭证 AWS Management Console
  1. 对于源提供商,选择GitHub

  2. 对于凭证,请执行以下任一操作:

    • 选择 “默认来源凭据”,使用您账户的默认来源凭据应用于所有项目。

      1. 如果您未连接到 GitHub,请选择 “管理默认来源凭据”。

      2. 对于凭据类型,请选择GitHub 应用程序

      3. 在 “连接” 中,选择使用现有连接或创建新连接。

    • 选择 “自定义来源凭据”,使用自定义来源凭据来覆盖您账户的默认设置。

      1. 对于凭据类型,请选择GitHub 应用程序

      2. 在 “连接” 中,选择使用现有连接或创建新连接。

  3. 选择 “添加来源”,然后重复选择来源提供商和凭据的过程。

AWS CLI
要在中将多个令牌配置为源级凭证 AWS CLI
  • 打开终端(Linux、macOS 或 Unix)或命令提示符(Windows)。 AWS CLI 使用运行create-project命令。

    使用以下命令:

    aws codebuild create-project --region <aws-region> \ --name <project-name> \ --artifacts type=NO_ARTIFACTS \ --environment "type=LINUX_CONTAINER, computeType=BUILD_GENERAL1_SMALL, image=aws/codebuild/amazonlinux2-x86_64-standard:5.0" \ --service-role <service-role-name> \ --source "type=GITHUB, location=<github-repository-1>, auth={type=SECRETS_MANAGER,resource=<secret-or-connection-arn-1>}" \ --secondary-sources "type=GITHUB, location=<github-repository-2>, auth={type=SECRETS_MANAGER,resource=<secret-or-connection-arn-2>}, sourceIdentifier=secondary" aws codebuild start-build --region <aws-region> --project-name <project-name>

设置项目级别的源代码凭据备用

要设置项目级别的源代码凭据备用,请使用NO_SOURCE项目的主要来源并引用令牌。

aws codebuild create-project \ --name <project-name> \ --service-role <service-role-name> \ --artifacts type=NO_ARTIFACTS \ --environment "type=LINUX_CONTAINER, computeType=BUILD_GENERAL1_SMALL, image=aws/codebuild/amazonlinux2-x86_64-standard:5.0" \ --service-role <service-role-name> \ --source "type=NO_SOURCE, auth={type=SECRETS_MANAGER,resource=<secret-or-connection-arn>}, buildspec=<buildspec>" --secondary-sources "type=GITHUB, location=<github-repository>, sourceIdentifier=secondary" aws codebuild start-build --region <aws-region> --project-name <project_name>

使用时NO_SOURCE,通常在源模型中提供 buildspec,因为它没有直接配置为使用外部源来获取 build spec。通常,NO_SOURCE源代码将处理从 buildspec 中克隆所有相关存储库。为确保配置的凭据可用于这些操作,您可以在 buildspec 中启用该git-credential-helper选项。

env: git-credential-helper: yes

在构建过程中, CodeBuild 将从配置的令牌中读取该AuthServer字段,并将令牌凭据用于向该特定第三方源提供商发出的所有 git 请求。

其他设置选项

您可以使用 AWS CloudFormation 模板配置 Secrets Manager 账户级别的证书。您可以使用以下 AWS CloudFormation 模板来设置账户级别证书:

Parameters: GitHubToken: Type: String NoEcho: true Default: placeholder Resources: CodeBuildAuthTokenSecret: Type: AWS::SecretsManager::Secret Properties: Description: CodeBuild auth token Name: codebuild-auth-token SecretString: !Join - '' - - '{"ServerType":"GITHUB","AuthType":"PERSONAL_ACCESS_TOKEN","Token":"' - !Ref GitHubToken - '"}' Tags: - Key: codebuild:source:provider Value: github - Key: codebuild:source:type Value: personal_access_token CodeBuildSecretsManagerAccountCredential: Type: AWS::CodeBuild::SourceCredential Properties: ServerType: GITHUB AuthType: SECRETS_MANAGER Token: !Ref CodeBuildAuthTokenSecret
注意

如果您还在同一个堆栈中创建项目,请使用 AWS CloudFormation 属性DependsOn来确保在项目之前创建AccountCredential的。

您还可以使用 AWS CloudFormation 模板配置 Secrets Manager 多个源代码级别的证书。您可以使用以下 AWS CloudFormation 模板使用多个令牌来提取多个来源:

Parameters: GitHubTokenOne: Type: String NoEcho: true Default: placeholder GitHubTokenTwo: Type: String NoEcho: true Default: placeholder Resources: CodeBuildSecretsManagerProject: Type: AWS::CodeBuild::Project Properties: Name: codebuild-multitoken-example ServiceRole: <service-role> Environment: Type: LINUX_CONTAINER ComputeType: BUILD_GENERAL1_SMALL Image: aws/codebuild/amazonlinux2-x86_64-standard:5.0 Source: Type: GITHUB Location: <github-repository-one> Auth: Type: SECRETS_MANAGER Resource: !Ref CodeBuildAuthTokenSecretOne SecondarySources: - Type: GITHUB Location: <github-repository-two> Auth: Type: SECRETS_MANAGER Resource: !Ref CodeBuildAuthTokenSecretTwo SourceIdentifier: secondary Artifacts: Type: NO_ARTIFACTS LogsConfig: CloudWatchLogs: Status: ENABLED CodeBuildProjectIAMRoleSecretAccess: Type: AWS::IAM::RolePolicy Properties: RoleName: <role-name> PolicyName: CodeBuildProjectIAMRoleSecretAccessPolicy PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - secretsmanager:GetSecretValue Resource: - !Ref CodeBuildAuthTokenSecretOne - !Ref CodeBuildAuthTokenSecretTwo CodeBuildAuthTokenSecretOne: Type: AWS::SecretsManager::Secret Properties: Description: CodeBuild auth token one Name: codebuild-auth-token-one SecretString: !Join - '' - - '{"ServerType":"GITHUB","AuthType":"PERSONAL_ACCESS_TOKEN","Token":"' - !Ref GitHubTokenOne - '"}' Tags: - Key: codebuild:source:provider Value: github - Key: codebuild:source:type Value: personal_access_token CodeBuildAuthTokenSecretTwo: Type: AWS::SecretsManager::Secret Properties: Description: CodeBuild auth token two Name: codebuild-auth-token-two SecretString: !Join - '' - - '{"ServerType":"GITHUB","AuthType":"PERSONAL_ACCESS_TOKEN","Token":"' - !Ref GitHubTokenTwo - '"}' Tags: - Key: codebuild:source:provider Value: github - Key: codebuild:source:type Value: personal_access_token