Reputation: 754
I have a json config file with secure values that I have in a google cloud secret that follows (some of the values have been removed or reduced for security):
Secret code:
{
"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"
}
However, the deploy fails and I get this error:
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)
At this line:
"type": "service_account",
And this is the code where I import the secret file and use it:
var serviceAccount = require("./secrets/video-ipod-firebase-adminsdk-wnvid-cc7ced9a5a.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
And here is the configuration where it is mounted:
This does appear to be correct JSON syntax and it was uploaded directly from the json file on my local machine that works during development, so I'm not sure how this is incorrect syntax.
Upvotes: 2
Views: 470
Reputation: 1346
The error that you are getting is basically a parsing error for variable values,which are stored in a configuration file and being called when the Cloud Instance is deployed.
The official document Setting Up Authentication for Production Applications mentions that if the environment variable isn't set, ADC uses the default service account that Compute Engine, Google Kubernetes Engine, Cloud Run, App Engine, and Cloud Functions provide, for applications that run on those services.Therefore to access the Secret Manager from Cloud Run, Application Default Credentials (ADC) will use the default service account of Cloud Run.
I would suggest you put the json in the same folder as your index.js and try the reference as below and after that you initialize the app.
var serviceAccount = require("./myfirebaseapp-firebase-adminsdk-my-secret-key.json");
The references for path and relative path on your local machine in the code deployed to Cloud Run will not work and thus may resulting in parsing parameter error.It's even worth if you just use the default service account if possible, and give it the necessary permissions to do what it needs to do if necessary.
Also check these similar examples:
Upvotes: 1