mar*_*uru 4 continuous-integration secret-key google-cloud-kms google-cloud-build google-secret-manager
如何从我的 Google Cloud Build Pipeline访问我在Google Secret Manager 中定义的变量?
您可以使用标准Cloud Builder gcloud从 Cloud Build 访问机密
但是,有两个问题:
steps:
- name: gcr.io/cloud-builders/gcloud
entrypoint: "bash"
args:
- "-c"
- |
gcloud components update
# Store the secret is a temporary file
gcloud beta secrets versions access --secret=MySecretName latest > my-secret-file.txt
- name: AnotherCloudBuildStepImage
entrypoint: "bash"
args:
- "-c"
- |
# For getting the secret and pass it to a command/script
./my-script.sh $(cat my-secret-file.txt)
Run Code Online (Sandbox Code Playgroud)
考虑将 Secret Manager Secret Accessor 角色授予roles/secretmanager.secretAccessorCloud Build 默认服务帐户<PROJECT_ID>@cloudbuild.gserviceaccount.com
编辑
您可以通过安装 gcloud CLI(并使用授权访问机密的服务帐户进行初始化)或通过 API 调用从任何地方访问机密
curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://secretmanager.googleapis.com/v1beta1/projects/PROJECT_ID/secrets/MySecretName/versions/latest:access
Run Code Online (Sandbox Code Playgroud)
注意:您会在数据字段中收到 base64 编码格式的密钥。使用前别忘了解码!
您必须在授予正确角色的服务帐户上生成访问令牌。这里我再次使用 gcloud,因为它更容易。但是根据您的平台,使用最合适的方法。python 脚本也可以完成这项工作。
编辑 2
Cloud Build 中现在提供了一种获取机密的新方法。更少的锅炉板,更安全。现在看看并使用这种方式。
| 归档时间: |
|
| 查看次数: |
3350 次 |
| 最近记录: |