【问题标题】:Cloud Build - Credentials error using exec wrapper to connect to Cloud SQL Proxy + other GCP resourcesCloud Build - 使用 exec 包装器连接到 Cloud SQL 代理 + 其他 GCP 资源的凭据错误
【发布时间】:2020-11-26 05:57:20
【问题描述】:

我正在尝试在 Cloud Build 步骤中使用 exec-wrapper 来运行 Cloud SQL 代理并运行节点脚本来执行自定义数据库迁移。这是我的云构建配置的样子:

steps:
- name: gcr.io/cloud-builders/docker
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/api-stg', '.']
- name: gcr.io/cloud-builders/docker
  args: ['push', 'gcr.io/$PROJECT_ID/api-stg']
- name: gcr.io/cloud-builders/gcloud
  args: ['app', 'deploy', 'app-stg.yaml', '--image-url=gcr.io/$PROJECT_ID/api-stg']
- name: "gcr.io/google-appengine/exec-wrapper"
  args: ["-i", "gcr.io/$PROJECT_ID/api-stg",
         "-s", "$PROJECT_ID:us-central1:<Cloud SQL Instance Name>",
         "--", "scripts/management/custom_migration"]

images: ['gcr.io/$PROJECT_ID/api-stg']
timeout: 1200s # 20 minutes

在我的custom_migration.js 文件中,我有如下内容:

const {Storage} = require('@google-cloud/storage');

const storage = new Storage();
const bucket = storage.bucket(BUCKET_NAME);
const file = await bucket.file(key);
const result = await new Promise(resolve => file.download((err, data) => {...}));
...

这会导致google-auth-library 出现以下错误:

Error: The file at /root/.google/credentials does not exist, or it is not a file. 
Error: ENOENT: no such file or directory, lstat '/root/.google'

当部署在新版本中时,我的 App Engine 柔性环境能够运行此代码,但 Cloud Build 步骤中的相同代码未正确获得凭据。如何允许 exec-wrapper 使用我的 App Engine 柔性环境的默认凭据?

【问题讨论】:

  • 这里为什么需要一个 exec-wrapper?
  • @guillaumeblaquiere 我需要一个 exec-wrapper 以便通过 Cloud SQL 代理连接到我的 Cloud SQL 实例。看来这就是 exec 包装器的目的
  • 好的,您需要一个 Cloud SQL 连接和 nodeJS 环境来运行您的迁移脚本,对吗?如果我有正确答案,我还会向您解释 exec-wrapper 和 Cloud Build 元数据服务器的问题。
  • @guillaumeblaquiere 是的,没错!
  • 脚本 scripts/management/custom_migration 在你的容器 gcr.io/$PROJECT_ID/api-stg 中还是在外面?

标签: node.js docker google-cloud-platform google-cloud-storage google-cloud-build


【解决方案1】:

这一步对我有用

steps:
  - name: 'node:14-alpine'
    entrypoint: 'sh'
    args:
      - -c
      - |
        wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
        chmod +x cloud_sql_proxy
        ./cloud_sql_proxy -instances=my-project-id:us-central1:vertx=tcp:5432 &
        npm install @google-cloud/storage pg
        node index-test.js

因为我不知道你的自定义容器的内容,你可以尝试适应这样的东西

steps:
  - name: 'gcr.io/$PROJECT_ID/api-stg'
    entrypoint: 'sh'
    args:
      - -c
      - |
        wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
        chmod +x cloud_sql_proxy
        ./cloud_sql_proxy -instances=my-project-id:us-central1:vertx=tcp:5432 &
        npm install @google-cloud/storage pg
        node scripts/management/custom_migration

关于标准库的问题,我的猜测是AppEngine环境和Cloud Build环境冲突。

如果您查看 Google Auth 库,您会看到 App Engine 凭据的一个案例,以及计算凭据的另一个案例。该计算标准用于任何 Google Cloud 服务(Cloud Run、Cloud Functions、Compute Engine、Cloud Build...),但 App Engine 有其特殊性。

【讨论】:

  • 感谢您的建议,让我试试这个并回复您!
猜你喜欢
  • 1970-01-01
  • 2021-03-27
  • 2021-09-08
  • 2019-05-26
  • 2021-11-30
  • 2018-08-04
  • 2021-02-12
  • 2020-08-31
  • 1970-01-01
相关资源
最近更新 更多