Firebase secrets not defined in process.env
S

2

5

I'm writing a Firebase function with Cloud Storage trigger. Like this

const functions = require('firebase-functions')
const doSomethingWithSecrets = require('./doSomethingWithSecrets')

const doSomethingWhenUploaded = functions.runWith({
  secrets: ["MY_SECRET_1", "MY_SECRET_2", "MY_SECRET_3"]
}).storage.object().onFinalize(o => {
  functions.logger.debug([
    process.env.MY_SECRET_1  // undefined
    process.env.MY_SECRET_2  // undefined
    process.env.MY_SECRET_3  // undefined
  ])

  doSomethingWithSecrets(process.env.MY_SECRET_1, process.env.MY_SECRET_2, process.env.MY_SECRET_3)
  // Error: Invalid secret.
})

All three of them returns undefined. I've made sure that they're properly set. They show up both when using firebase functions:secret:accesss MY_SECRET_1 and in Google Cloud Console.

What's wrong?

Additional info

I previously used it with only one secret and it worked. I don't know what happened, I'm using nvm and lost track of which Node version I used when it worked, so it may be a clue.

process.env returns all the env like normal and none of my secrets shows up.

Seafood answered 12/5, 2022 at 0:27 Comment(2)
Have you tried debugging only the process.env? Since upon replicating your code on my end, it returned properly. Also, do you have any guides/documentation that you follow, if so, include it in your question.Malayalam
@Malayalam I added the information you requested.Seafood
R
4

Update your firebase-tools and the issue will resolve itself. I was dealing with this issue all day today and found a git hub issue that fixed the problem in the latest release of 10.9.2.

npm install -g firebase-tools

https://github.com/firebase/firebase-tools/issues/4540 https://github.com/firebase/firebase-tools/issues/4459

Russi answered 14/5, 2022 at 2:12 Comment(0)
S
2

I encountered the exact same issue. After much suffering I finally got it to work. When you start using secrets in your functions, you need to redeploy all functions and NOT just the individual function.

e.g

firebase deploy --only functions:myFunc

results in my secret values coming through as 'undefined' when accessed from my function using runWith(), however after doing a full functions deploy

firebase deploy --only functions

everything worked.

Sorel answered 9/9, 2022 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.