本教程介绍如何在 Cloud Build 流水线中创建 Binary Authorization 证明。此设置有助于确保只有在 Cloud Build 构建过程中构建和签名的容器映像才能自动获得在您的部署环境中运行的授权。
Cloud Build 概览
Cloud Build(概览)会获取存储在托管代码库中的源代码,运行构建和测试,并将生成的软件输出存储在 Container Registry 或 Google Cloud Platform 上的其他存储服务中。
Binary Authorization 概览
Binary Authorization(概览)是一款Google Cloud 产品,用于对应用强制执行部署时限制条件。借助其 Google Kubernetes Engine (GKE) 集成,用户可以强制要求部署到 Kubernetes 集群的容器由可信授权机构进行加密签名,并由 Binary Authorization 证明者进行验证。
您可以将 Binary Authorization 配置为根据源代码所在的位置要求提供证明,以防止部署通过未经授权的来源构建的容器映像。
如需了解详情,请参阅以下内容:
架构
下图显示了 Binary Authorization/Cloud Build 设置中的组件:
此流水线的具体步骤如下:
首先,系统将用于构建容器映像的代码推送到源代码库。
持续集成 (CI) 工具 Cloud Build 将用于构建和测试容器。
该构建会将容器映像推送到 Container Registry 或其他用于存储已构建映像的注册表。
Cloud Key Management Service 用于为加密密钥对提供密钥管理功能,它将负责对容器映像签名。然后,生成的签名将存储在新创建的证明中。
在部署时,证明者会使用密钥对中的公钥来验证证明。Binary Authorization 要求必须具有已签名的证明才能部署容器映像,从而强制执行政策。
通过搭配使用 Cloud Build 与 Cloud Key Management Service 创建证明
本部分介绍了如何实现上述架构,其中使用了 Cloud Build 社区中的开源自定义构建步骤。自定义构建步骤对容器映像进行签名、创建证明,并将其上传到 Binary Authorization。
配置 Identity and Access Management
如需使用此构建步骤,Cloud Build 服务账号需要以下 IAM 角色:
- Binary Authorization Attestor Viewer
roles/binaryauthorization.attestorsViewer
- Cloud KMS CryptoKey Signer/Verifier(要使用 KMS 中的密钥对证明进行签名时)
roles/cloudkms.signerVerifier
- Artifact Analysis Notes Attacher
roles/containeranalysis.notes.attacher
您可以使用以下命令将角色添加到项目的 Cloud Build 服务账号:
启用 Cloud Build:
在目标 Google Cloud 项目中启用 Cloud Build API。
将项目 ID 保存到环境变量中:
PROJECT_ID=PROJECT_ID
其中,PROJECT_ID 是您的 Google Cloud 项目 ID。
设置项目 Google Cloud CLI:
gcloud config set project ${PROJECT_ID}获取项目编号:
PROJECT_NUMBER=$(gcloud projects list --filter="${PROJECT_ID}" --format="value(PROJECT_NUMBER)")将 Binary Authorization Attestor Viewer 角色添加到 Cloud Build 服务账号:
gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member serviceAccount:${PROJECT_NUMBER}@cloudbuild. \ --role roles/binaryauthorization.attestorsViewer将 Cloud KMS CryptoKey Signer/Verifier 角色添加到 Cloud Build 服务账号(基于 KMS 的签名):
gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member serviceAccount:${PROJECT_NUMBER}@cloudbuild. \ --role roles/cloudkms.signerVerifier将 Artifact Analysis Notes Attacher 添加到 Cloud Build 服务账号:
gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member serviceAccount:${PROJECT_NUMBER}@cloudbuild. \ --role roles/containeranalysis.notes.attacher
使用 Cloud Build 构建并注册自定义构建步骤
克隆 Google Cloud Build 社区代码库:
git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git
为 Cloud Build 配置 Binary Authorization 签名者:
在使用前,自定义构建步骤的代码必须构建到容器中并推送到 Cloud Build。为此,请运行以下命令:
cd cloud-builders-community/binauthz-attestation gcloud builds submit . --config cloudbuild.yaml
自定义构建步骤已推送到当前项目的 Google Container Registry,现在可以使用了。
在 Binary Authorization 中创建证明者
创建一个证明者,Binary Authorization 将在部署时使用该证明者来验证证明。
在 Binary Authorization 中设置证明者和 Cloud Key Management Service 密钥对:
请参阅使用 CLI 创建证明者
验证证明者是否已创建
gcloud --project="${ATTESTOR_PROJECT_ID}" container binauthz attestors list
向您的 cloudbuild.yaml 中添加“create-attestation”步骤
如需使用 binauthz-attestation 步骤,您必须添加用于对已推送到 Container Registry 的构建进行签名的步骤,以便更新 cloudbuild.yaml。
下面提供了两种方法:
手动更新
cloudbuild.yaml。使用您之前设置的环境变量运行示例流水线。
手动更新 cloudbuild.yaml
在将容器上传到 Container Registry 的步骤之后,通过添加以下构建步骤来手动更新
cloudbuild.yaml。 注意:您必须手动将 ATTESTOR_NAME、KMS_KEY_LOCATION、KMS_KEYRING_NAME、KMS_KEY_NAME 和 KMS_KEY_VERSION 替换成您自己的值:- id: 'create-attestation' name: 'gcr.io/${PROJECT_ID}/binauthz-attestation:latest' args: - '--artifact-url' - 'gcr.io/${PROJECT_ID}/helloworld:latest' - '--attestor' - 'projects/${PROJECT_ID}/attestors/ATTESTOR_NAME' - '--keyversion' - 'projects/${PROJECT_ID}/locations/KMS_KEY_LOCATION/keyRings/KMS_KEYRING_NAME/cryptoKeys/KMS_KEY_NAME/cryptoKeyVersions/KMS_KEY_VERSION'以下代码也有效:
- id: 'create-attestation' name: 'gcr.io/${PROJECT_ID}/binauthz-attestation:latest' args: - '--artifact-url' - 'gcr.io/${PROJECT_ID}/helloworld:latest' - '--attestor' - 'ATTESTOR_NAME' - '--attestor-project' - '${PROJECT_ID}' - '--keyversion' - 'KEY_VERSION' - '--keyversion-project' - '${PROJECT_ID}' - '--keyversion-location' - 'KEY_LOCATION' - '--keyversion-keyring' - 'KEYRING_NAME' - '--keyversion-key' - 'KEY_NAME'
可选:测试流水线
如需测试示例 Cloud Build 证明流水线,请执行以下步骤:
使用您之前设置的环境变量创建一个
cloudbuild.yaml文件:cd example cat <<EOM > cloudbuild_example.yaml steps: - id: 'build' name: 'gcr.io/cloud-builders/docker' args: - 'build' - '-t' - 'gcr.io/$PROJECT_ID/helloworld:latest' - '.' - id: 'publish' name: 'gcr.io/cloud-builders/docker' args: - 'push' - 'gcr.io/$PROJECT_ID/helloworld:latest' - id: 'create-attestation' name: 'gcr.io/$PROJECT_ID/binauthz-attestation:latest' args: - '--artifact-url' - 'gcr.io/$PROJECT_ID/helloworld:latest' - '--attestor' - 'projects/$PROJECT_ID/attestors/${ATTESTOR_NAME}' - '--keyversion' - 'projects/${PROJECT_ID}/locations/${KMS_KEY_LOCATION}/keyRings/${KMS_KEYRING_NAME}/cryptoKeys/${KMS_KEY_NAME}/cryptoKeyVersions/${KMS_KEY_VERSION}' tags: ['cloud-builders-community'] EOM使用示例
cloudbuild_example.yaml运行 Cloud Build:从
cloud-builders-community/binauthz-attestation/example目录运行以下命令:gcloud builds submit . --config cloudbuild_example.yaml