CodeBuild 支持从 AWS Secrets Manager 中的密钥或通过 AWS CodeConnections 连接获取第三方提供商的访问令牌。您可以将密钥或连接设置为与指定第三方提供商(例如 GitHub、GitHub Enterprise 或 Bitbucket)进行交互时的默认凭证。
您可以将您的源凭证设置为三个不同级别:
-
适合所有项目的账户级别凭证:这些是 AWS 账户中所有项目的默认凭证。如果未指定项目或源级别凭证,则凭证将用于项目。
-
特定存储库的源级别凭证:这是在项目源上定义了 Secrets Manager 密钥或 CodeConnections 连接时使用的凭证。这些凭证将仅用于指定源存储库上的操作。这样您就可以在同一个项目中设置具有不同权限范围的多个访问令牌,而不使用默认的账户级别凭证。
-
项目级别的备用凭证:您可以使用
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 角色授予权限
-
按照 允许 CodeBuild 与其他 AWS 服务进行交互 中的说明为您的 CodeBuild 项目创建 IAM 角色。
-
请执行以下操作之一:
-
向您的 CodeBuild 项目角色添加以下 IAM 策略,以便授予对密钥的访问权限。
{ "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>
" } } } ] } -
向您的 CodeBuild 项目角色添加以下 IAM 策略,以便授予对连接的访问权限。
{ "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 中将连接配置为账户级别凭证
-
对于源提供商,选择 Bitbucket、GitHub 或 GitHub Enterprise。
-
对于凭证,执行以下操作之一:
-
选择默认来源凭证,使用您账户的默认来源凭证应用于所有项目。
-
如果未连接到源提供商,请选择管理默认来源凭证。
-
在凭证类型中,选择一个凭证类型。
-
如果您选择 CodeConnections,则要选择使用现有连接或创建新连接。
如果您选择了另一个凭证类型,请在服务中选择要用于存储令牌的服务,然后执行以下操作:
-
如果您选择使用 Secrets Manager,则可以选择使用现有密钥连接或创建新密钥并选择保存。有关如何创建新密钥的更多信息,请参阅在 Secrets Manager 密钥中创建和存储令牌。
-
如果您选择使用 CodeBuild,请输入您的令牌或用户名和应用程序密码,然后选择保存。
-
-
-
选择自定义来源凭证,以便使用自定义来源凭证来覆盖您账户的默认设置。
-
在凭证类型中,选择一个凭证类型。
-
在连接中,选择使用现有连接或创建新连接。
-
-
现在,您可以在构建项目中使用该令牌并运行它。有关更多信息,请参阅在 AWS CodeBuild 中创建构建项目 和手动运行 AWS CodeBuild 构建。
将多个令牌配置为源级别凭证
要使用 Secrets Manager 密钥或 CodeConnections 连接作为源级别凭证,请直接在 CodeBuild 项目中引用令牌,然后开始构建。
在 AWS Management Console 中将多个令牌配置为源级别凭证
-
对于源提供商,选择 GitHub。
-
对于凭证,执行以下操作之一:
-
选择默认来源凭证,使用您账户的默认来源凭证应用于所有项目。
-
如果未连接到 GitHub,请选择管理默认来源凭证。
-
对于凭证类型,选择 GitHub 应用程序。
-
在连接中,选择使用现有连接或创建新连接。
-
-
选择自定义来源凭证,以便使用自定义来源凭证来覆盖您账户的默认设置。
-
对于凭证类型,选择 GitHub 应用程序。
-
在连接中,选择使用现有连接或创建新连接。
-
-
-
选择添加源,然后重复选择源提供商和凭证的过程。
设置项目级别源凭证回退
要设置项目级别源凭证回退,请使用项目主源的 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,所以通常在源模型中提供一个 buildspec。通常,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