繁体   English   中英

如何通过 Cloud Build 访问 GSM 机密并传递给 Cloud Function

[英]How to access GSM secrets through Cloud Build and pass to Cloud Function

使用 Cloud Build 时,如何将机密从 Google Secrets Manager (GSM) 传递到 Cloud Function? 下面的cloudbuild.yaml包含三个步骤。 此外,我正在使用volumes在构建步骤之间创建永久存储。 我可以通过 Cloud Build 确认 GSM 检索。 但是,当我尝试使用--env-vars-file以 yaml 格式传递机密时,遇到以下错误...

Already have image (with digest): gcr.io/cloud-builders/gcloud
ERROR: gcloud crashed (AttributeError): 'str' object has no attribute 'items'

云构建.yaml:

steps:
  - name: 'gcr.io/cloud-builders/gcloud'
    volumes:
    - name: 'secrets'
      path: '/secrets'
    entrypoint: "bash"
    args:
      - "-c"
      - |
        echo -n 'gsm_secret:' > /secrets/my-secret-file.txt
  - name: 'gcr.io/cloud-builders/gcloud'
    volumes:
    - name: 'secrets'
      path: '/secrets'
    entrypoint: "bash"
    args:
      - "-c"
      - |
        gcloud components update
        gcloud beta secrets versions access --secret=MySecret latest >> /secrets/my-secret-file.txt
        cat /secrets/my-secret-file.txt
  - name: 'gcr.io/cloud-builders/gcloud'
    volumes:
      - name: 'secrets'
        path: '/secrets'
    args: [
      'functions', 'deploy', 'gsm-foobar',
      '--project=[...]',
      '--trigger-http',
      '--runtime=go111',
      '--region=us-central1',
      '--memory=256MB',
      '--timeout=540',
      '--entry-point=GSM',
      '--allow-unauthenticated',
      '--source=https://source.developers.google.com/[...]',
      '--service-account', '[...]@appspot.gserviceaccount.com',
      '--env-vars-file', '/secrets/my-secret-file.txt'
    ]

更新:不需要使用卷,因为/workspace是 Cloud Build 中步骤之间的永久存储。 此外,不再需要gcloud components update ,因为截至今天,默认 Cloud SDK 版本为 279.0.0

一个办法:

steps:
  - name: 'gcr.io/cloud-builders/gcloud'
    entrypoint: "bash"
    args:
      - "-c"
      - |
        echo "gsm_secret: $(gcloud beta secrets versions access --secret=MySecret latest)" > /workspace/my-secret-file.txt
        cat /workspace/my-secret-file.txt
  - name: 'gcr.io/cloud-builders/gcloud'
    args: [
      'functions', 'deploy', 'gsm-foobar',
      [...]
      '--entry-point=GSM',
      '--allow-unauthenticated',
      '--source=https://source.developers.google.com/[...]',
      '--service-account', '[...]@appspot.gserviceaccount.com',
      '--env-vars-file=/workspace/my-secret-file.txt'
    ]

第二次阅读时,我意识到您的第二步将秘密值放入文件中。 我认为你错过了换行符。

注意我自己没有试过这个!

确保您的机密文件末尾有一个换行符。

请参阅: https : //cloud.google.com/functions/docs/env-var

更新:试过了;-)

我认为你的问题是最后的换行符。

在部署之前的一个步骤中使用以下内容,有效:

echo "gsm_secret: $(gcloud beta secrets versions access --secret=MySecret latest)" > /secrets/my-secret-file.txt

或者,更简单地说,也许:

steps:
  - name: "gcr.io/cloud-builders/gcloud"
    entrypoint: /bin/bash
    args:
      - "-c"
      - |
        gcloud functions deploy ... \
        --set-env-vars=NAME=$(gcloud beta secrets versions access --secret=name latest)

另请参阅secretEnv 这是一种更优雅的机制。Google 或许应该增强此功能以支持秘密管理器(除了 KMS)。

自 2021 年 2 月 10 日起,您可以使用availableSecrets字段直接从 Cloud Build 访问 Secret Manager 机密:

steps:
- id: 'deploy'
  name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: 'bash'
  args:
  - '-c'
  - 'gcloud functions deploy --set-env-vars=SECRET=$$MY_SECRET'
  secretEnv: ['MY_SECRET']
availableSecrets:
  secretManager:
  - versionName: 'projects/my-project/secrets/my-secret/versions/latest'
    env: 'MY_SECRET'

文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM