本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
里面有多个访问令牌 CodeBuild
CodeBuild 支持从您的密钥中 Amazon Secrets Manager 或通过 Amazon CodeConnections 连接向第三方提供商获取访问令牌。您可以将您的密钥或连接设置为与指定第三方提供商(例如 En GitHub terprise 或 Bitbucket)进行交互的默认凭据。 GitHub
您可以将源证书设置为三个不同的级别:
-
所有项目的账户级别凭证:这些是 Amazon 账户中所有项目的默认凭证。当未指定项目或源代码级别凭据时,它们将用于项目。
-
特定存储库的源代码级别凭据:这是在项目源上定义 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 项目的说明 CodeBuild 允许与其他 Amazon 服务进行交互为 CodeBuild 项目创建IAM角色。
-
请执行以下操作之一:
-
将以下IAM策略添加到您的 CodeBuild 项目角色以授予对您的密钥的访问权限。
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "secretsmanager:GetSecretValue" ], "Resource": [ "
<secret-arn>
" ] } ] }(可选)如果您使用 Amazon 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 连接配置为账户级凭证,并在项目中使用。
现在,您可以在构建项目中使用该令牌并运行它。有关更多信息,请参阅在中创建构建项目 Amazon CodeBuild 和手动运行 Amazon CodeBuild 构建。
将多个令牌配置为源代码级凭证
要使用 Secrets Manager 密钥或 CodeConnections 连接作为源代码级凭据,请直接在 CodeBuild 项目中引用该令牌,然后开始构建。
设置项目级别的源代码凭据备用
要设置项目级别的源代码凭据备用,请使用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 请求。
其他设置选项
您可以使用 Amazon CloudFormation 模板配置 Secrets Manager 账户级别的证书。您可以使用以下 Amazon CloudFormation 模板来设置账户级别证书:
Parameters: GitHubToken: Type: String NoEcho: true Default: placeholder Resources: CodeBuildAuthTokenSecret: Type: Amazon::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: Amazon::CodeBuild::SourceCredential Properties: ServerType: GITHUB AuthType: SECRETS_MANAGER Token: !Ref CodeBuildAuthTokenSecret
注意
如果您还在同一个堆栈中创建项目,请使用 Amazon CloudFormation 属性DependsOn来确保在项目之前创建AccountCredential
的。
您还可以使用 Amazon CloudFormation 模板配置 Secrets Manager 多个源代码级别的证书。您可以使用以下 Amazon CloudFormation 模板使用多个令牌来提取多个来源:
Parameters: GitHubTokenOne: Type: String NoEcho: true Default: placeholder GitHubTokenTwo: Type: String NoEcho: true Default: placeholder Resources: CodeBuildSecretsManagerProject: Type: Amazon::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: Amazon::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: Amazon::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: Amazon::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