繁体   English   中英

搜索到 Cloud Run 实例错误的 Google Cloud Secret:“SyntaxError: Unexpected token ':'”

[英]Google Cloud Secret hunted to Cloud Run Instance Error: "SyntaxError: Unexpected token ':'"

我有一个 json 配置文件,其中包含我在随后的谷歌云机密中拥有的安全值(出于安全考虑,一些值已被删除或减少): 秘密元数据

密码:

{
  "type": "service_account",
  "project_id": "video-ipod",
  "private_key_id": "cc***********72",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMI****************kFPye\n-----END PRIVATE KEY-----\n",
  "client_email": "fire*************nt.com",
  "client_id": "1*************50",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/o*****erts",
  "client_x509_cert_url": "https://www.googl*************sdk-wnvid%40video-ip********unt.com"
}

但是,部署失败,我收到此错误:

SyntaxError: Unexpected token ':'
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1088:15)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/index.js:6:22)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)

在这一行:

"type": "service_account",

这是我导入秘密文件并使用它的代码:

var serviceAccount = require("./secrets/video-ipod-firebase-adminsdk-wnvid-cc7ced9a5a.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

这是安装它的配置: Cloud Run Secret 导入配置

这看起来确实是正确的 JSON 语法,它是直接从我本地计算机上的 json 文件上传的,该文件在开发过程中有效,所以我不确定这是不正确的语法。

您得到的错误基本上是变量值的解析错误,这些值存储在配置文件中并在部署云实例时被调用。
官方文档Setting Up Authentication for Production Applications提到,如果未设置环境变量,ADC 将使用 Compute Engine、Google Kube.netes Engine、Cloud Run、App Engine 和 Cloud Functions 提供的默认服务帐户,用于应用程序在这些服务上运行。因此,要从 Cloud Run 访问 Secret Manager,应用程序默认凭证 (ADC) 将使用 Cloud Run 的默认服务帐户。 我建议您将 json 放在与您的 index.js 相同的文件夹中,然后尝试如下参考,然后初始化应用程序。
var serviceAccount = require("./myfirebaseapp-firebase-adminsdk-my-secret-key.json");

部署到Cloud Run的代码中本地机器上的路径和相对路径的引用将不起作用,从而可能导致解析参数错误。如果可能的话,您甚至值得使用默认服务帐户并为其提供必要的权限在必要时做它需要做的事情。
还要检查这些类似的例子:

暂无
暂无

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

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