Colons in ENV vars?

I have an env var which represents a JSON value I got from Google’s Application Credentials. It looks right in the Netlify console, but when I log it in my Go Function, it is truncated down to the last key: value pair. For now, I’m working around this by just base64 encoding the whole thing, and that works, but it means I’m getting up closer than I’d like to the 4KB env var limit…

hey carl! thanks for reporting this. We’re going to take a look at this shortly- we haven’t forgotten. :+1:

1 Like

Hey @carlana,
I’m digging into this now and also wanted to surface this thread in the meantime, which I think could be related to what you’re running into:

What do you think- did you hit any of those errors along the way? And are you also working the GOOGLE_APPLICATION_CREDENTIALS json?

Will post again when I have something more conclusive!

Small update: I set up example where I put the contents of the GOOGLE_APPLICATION_CREDENTIALS json into the Netlify UI here:

Example site:
https://romantic-booth-bf6134.netlify.app

If you click that endpoint, the function renders the env variable that was entered (don’t worry, it’s fake!). I don’t see errors in the function logs. I know there are quite a few differences between this test case and your case, but it does at least suggest to me that we won’t break on variables with colons that are entered in the UI, since we seem to just convert the whole thing into a string. Could there be an issue with how you’re parsing the variable inside the function? And I should have asked earlier, but could you please share your Netlify site name where you’re running this function?

Google defaults to just using an env var to set the file path that it loads a JSON file from, but that doesn’t work for me because my project is open source. Instead, I load the JSON out of the environment instead of a file.

You can see it all here: https://github.com/spotlightpa/poor-richard/blob/master/pkg/api/api.go

The site is spotlightpa.org.

Hi, that post was from me about an eon ago. The following comments in this thread describe what I did: assign the JSON object to the GOOGLE_APPLICATION_CREDENTIALS env var. It will then be available as a systems variable in the function’s javascript. JSON.parse that, then you can use the firebase admin API to pass the object to the initializer.

Pertinent code link: GitHub - arjunyel/firestore-apollo-graphql: An example of a GraphQL setup with a Firebase Firestore backend. Uses Apollo Engine/Server 2.0 and deployed to Google App Engine.

1 Like

Nice! @JeffML Thanks so much for jumping back in here and sharing more resources.

I’ve also updated the example site I shared above, https://romantic-booth-bf6134.netlify.app. The key:value pair in the Netlify “Environment Variables” UI is: GOOGLE_APPLICATION_CREDENTIALS, then the entire copy/pasted JSON file from Google:

{   
"type": "service_account",   
"project_id": "fake-thing-harmony-2894735",  
"private_key_id": "91blahblahblahblahblah",
...
}

The JS function/endpoint on the example site isn’t much use in this case but the Go function does two things:

  1. Converts the string in the env variable to JSON
  2. Logs a few of the values (these will be visible in your Function logs in the Netlify UI here when you hit the function endpoint: Netlify App)
  3. Converts the JSON back to a string and sends it as the response body

Function code is here in case it’s helpful:

1 Like

My problem isn’t with using the credential. I figured out how to do that (although not in the standard way that Google expects where the env var contains the name of a file path). The problem is that the credential was being truncated for some reason. I know that it was being truncated because I did a debug commit that just logged my credentials to the Function console:

I see that the festive-edison project is properly echoing back the env var, so the problem is not as simple as colons somehow being mangled in the JSON. If I had to guess what’s different between my code and festive-edison, my next guess is that the real credentials from Google are quite large, more than 2K, whereas the festive-edison credentials are only half the size. If possible can you test with real credentials and see if they also echo correctly (to the Netlify Function console if you’re not comfortable making them public).

Hey @carlana,
I updated the example (on a different site… long boring story! But links updated above) with more representative credentials that are 2344 chars. You can take a look here:

Based on this, I don’t think Netlify is truncating the env var, though I did notice that when printing the private key here, the \n chars are rendered as line breaks and not printed out. I do think the issue here is how the env var is parsed inside the function.

Hope this helps!

1 Like