1

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

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

cloudbuild.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'
    ]

更新: 不需要使用卷,因为/workspaceCloud 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'
    ]
4

2 回答 2

1

As of 2021 February 10, you can access Secret Manager secrets directly from Cloud Build using the availableSecrets field:

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'

Documentation

于 2021-02-10T15:05:13.063 回答
1

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

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

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

请参阅: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)。

于 2020-02-10T22:40:02.483 回答