Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Heroku Config Var with contents from Google Cloud Service Account Keyfile

I have a web app that I'm trying to deploy with Heroku. I'm using the Google Cloud Node.js library for accessing google cloud storage. I have the following at the top of my server file:

const gcs = require('@google-cloud/storage')({
  projectId: 'my-project-ID',
  credentials: process.env.GCS_KEYFILE
});

GCS_KEYFILE is an configuration variable that I've set using the Heroku command line tools using heroku config:set GCS_KEYFILE="$(< /my/file.json)"

Checking the dashboard to make sure that worked confirms the contents of the JSON file have been set to a config var. Screenshot looks like this: enter image description here

The error I get when I try to do anything with gcs is:

Error: Could not authenticate request 
The incoming JSON object does not contain a client_email field

Which makes no sense because it clearly does. The json itself is fine; I took the download directly from Google Cloud without modifying it. I've tried using keyFileName in my const gcs declaration but I get a ENAMETOOLONG error (plus the docs say thats for specifying the path to the JSON file anyway). It works locally when I use keyFileName and specify the path to the JSON file so I'm pretty sure the JSON itself not the issue.

Any ideas as to why I'm getting this error? Or is there a better way to handle JSON Keyfiles from Google on Heroku?

like image 569
quicklikerabbit Avatar asked Jun 17 '17 01:06

quicklikerabbit


People also ask

How do I change my environment variables in Heroku?

One way of setting the environment variables on Heroku is to use the web interface. The first step is to log into your account and go to the Heroku dashboard. Figure 1 illustrates my dashboard. Choose the application for which you want to set the environment variables.

How do I access Heroku config vars in Python?

Heroku's environment variables are called config vars. To set a config var, navigate to the Settings tab of your app's dashboard, and click Reveal Config Vars.


2 Answers

Environment variables on heroku, or anywhere, are strings.

If that google api constructor takes a JSON object you may need to do something like this to convert it back to JSON:

const gcs = require('@google-cloud/storage')({
  projectId: 'my-project-ID',
  credentials: JSON.parse(process.env.GCS_KEYFILE)
});
like image 126
Robert Moskal Avatar answered Nov 15 '22 02:11

Robert Moskal


There is a helpful heroku buildpack for this

heroku buildpacks:add --index 1 https://github.com/buyersight/heroku-google-application-credentials-buildpack.git

--index 1 is because "The buildpack for the primary language of your app should be the last buildpack added," so if you have other buildpacks be careful of that.

like image 36
Sylvia Pap Avatar answered Nov 15 '22 02:11

Sylvia Pap