simple-oauth2 DEBUG in firebase functions - firebase

Does anyone knows how to enable debug in firebase functions node environment for an npm package called simple-oauth2?
The doc says to add DEBUG=simple-oauth2 as an environment variable but I don't believe firebase functions has a way to set environment variables apart from what is available from functions.config()
Any thoughts?

I ended up with a workaround that might help others too.
process.env is just a JSON and you can modify it directly, even though I wouldn't consider it best practice. But it does the job in this specific case.
1: So I ended up adding and hardcoding the DEBUG parameter into the process.env on the server side before importing the simple-oauth2 package. Like this:
index.js:
process.env.DEBUG = "*simple-oauth2*"
2: It appeared one can take more sophisticated and scalable approach by using approach described in this github thread (thanks to #MarcAnthonyB)
What they do is basically create .env file in your functions and import those variables using dotenv package, like below (which internally just modifies process.env json anyway):
require('dotenv').config();

Related

Firebase deploy failed even without changing the function from onCallable to onRequest

Error: [functionName(us-central1)] Changing from a callable function to an HTTPS function is not allowed. Please delete your function and create a new one instead.
Any advice and insight is appreciated.
Which version of Firebase CLI (firebase --version) are you using? Last night I updated firebase-tools package to 10.3.0 and functions deployment started giving me the error you mention. I downgraded to 10.2.2 and functions deployment started working as before.
Update:
Firebase team confirmed there is an issue with 10.3.0 firebase-tools. They are working on a fix:
https://github.com/firebase/firebase-tools/issues/4307
Solution 01:
I think the main problem lies inside whether you are using a valid way to use .env file. Since I just recently have this kind of error. If you do use the .env file inside the structure of your folder, then
You need to declare the path to .env file in all files which uses process.env.VARIABLE_NAME. Example way is like this:
require("dotenv").config({path: "../.env"});
After that try to delete all existing Cloud Functions in Google Cloud Console
Try deploy again: firebase deploy --only functions
I added a path to the .env file, since I checked Firebase Cloud Function Logs and it gave me error for all Cloud Functions which uses process.env.VARIABLE_NAME. This .env file must be located inside the root project and placed inside folder functions/. Give it a try. Hope it works.
Solution no. 2:
You should check if there is a variable or function which doesn't give return value, like example below:
const key = async () => {
const response = await pk.accessSecret("PRIVATEKEY");
return response;
};
I forgot to add a return value from the variable const key. Therefore I get so many errors like you do in all of my functions. And that errors cause firebase cli to state"
Changing from a callable function to an HTTPS function is not allowed.
Please delete your function and create a new one instead.
this happened to me as well. npm i -g firebase-tools#latest did the trick.

Firebase cloud function process.env variables can be set from GUI but not from CLI?

I can create process.env variables from https://console.cloud.google.com/functions
but I've to create this variables for each and every firebase cloud functions which is inefficient.
How can I create these process.env variables from code? while deploying cloud functions from my machine? If I can set it up from GUI then there must be a way to set it up from CLI/code.
Firebase does not directly support setting process environment variables. Currently, the only way to provide configuration at the time of deployment is using a different kind of environment configuration, which is not really the same as process env vars that you're showing in the screenshot.
If you want to set process environment variable for functions deployed with the Firebase CLI, you will have to use the method that you've already discovered. If you want to set them at the time of deployment, you will have to use Google Cloud's command line "gcloud" instead, which means you won't be able to take advantage of the firebase-functions API or the Firebase CLI at all. You just have to choose between the two.
Update: Feb 16, 2022
This can now be done from the code using .env, see answer.

structure firebase functions into multiple runtimes

I've been using firebase ecosystem for over 2 years but as it google lacks decent documentation i often come here to ask very basic things that we should learn right after "hello world".
When using firebase functions i try to modularize it to keep it readable and easy to maintain. the way i managed to do it was by having an "index" file and multiple subfiles which contains the logic for complex functions...
although it works very well, my index file is getting super long since i'm having more and more functions and it also needs to deal with some configuration for each of those specific functions...
i was messing around firebase dashboard https://console.cloud.google.com/functions/list? and i found that is possible to create a new function over this online form... when doing it the firebase backeend somehow create a new "runtime" for this function. I mean each function created by this form has its own "index.js" "package.json"
how can i do this without need to create every function from this form?
how can i simple code a new function ecosystem, deploy it using firebase cli and have this separeted structure for it?
All Cloud Functions are logically isolated from each other at all times at runtime. While they might share some common code at deployment, they don't share anything else.
The Firebase CLI requires that all your functions be defined in a single entrypoint, which is your index.js. That is just how it works. If you don't like that, you can deploy functions individually using gcloud, but you will not be able to use the firebase-functions module to declare and implement your function. gcloud uses different conventions.
If you want to continue to deploy with the Firebase CLI, you can add the new function to your index.js. It can be deployed separately from your other functions using the --only argument. For example, if your new function is called "fn":
firebase deploy --only functions:fn
This will deploy just fn and none of the other functions defined in your index. You can read about this and more options in the Firebase CLI documentation for deploying functions.
If you abosolutely do not want to have all your functions in a single index.js, you can split the definitions among multiple files, and require or import them into the main index.js. It's up to you how you want to organize your source file, using the facilities provided by nodejs and JavaScript.

How to use aria2c on cloud function?

In the same way that a cloud function can run the ffmpeg, is possible download and run aria2c? If yes, how?
PS. Cloud Run isn't an option right now.
Edit: Something like this https://blog.qbatch.com/aws-lambda-custom-binaries-support-available-for-rescue-239aab820d60
Executing custom binaries like aria2c in the runtime are not supported in Cloud Functions.
You can find a hacky solution here: Can you call out to FFMPEG in a Firebase Cloud Function This involves having a statically linked binary (so you might need to recompile aria2c as I'm assuming it won't be statically linked by default and it'll rely on more system packages like libc, libxxxx...) and bundling this library to your function deployment fackage.
You should really consider using Cloud Run for this use case. Cloud Run gives you the flexibility of creating your own container image that can include the binaries and libraries you want.
You can find a tutorial that bundles custom binaries on Cloud Run here: https://cloud.google.com/run/docs/tutorials/system-packages

How to handle environment variables on a deployed SF4 application

Symfony introduced a new Dotenv component since Symfony 3 which allows us to handle environment variables as application parameters. This looks really nice and it's the best practice to follow according to 12factor app manifesto.
Now, regarding Symfony 4 they went further by pushing forward this practice and that's why I started using environment variables via the .env file.
And then I wanted to deploy and I realized that the .env file must not be persisted on the server as it would be the same as having a parameters.yml file.
So I've been digging into the documentation a bit and I found this article which explains that we can directly create environment variables via some webserver directives. That's great for code being executed via FPM but it does not tell us how to handle environment variables when running a command via the CLI for instance.
How can I achieve this ?
Should there be an equivalent of a .env file stored somewhere? But then parameters would be duplicated ?
I'm welcoming any help ;)
Finally had the time to check the link Neodan posted and everything is in there!
So for those of you wondering what to do, simply edit the /etc/environment file and add your variables. Then reboot your server and all your processes will have access to these variables.
I guess that's the simplest solution. The only drawback of this method is that these variables are available by any process / users but that's ok as far as I'm concerned.
If you want a more secure solution I suppose that you could, as I stated before, configure your webserver to add environment variables and export them via your .bash_profile or .bashrc file but be careful about how you start your shell (when deploying your application for instance). It's more complicated to maintain and prone to errors I'd say.
N.B.: You also might want to be careful about how you name your variables to prevent collisions.

Resources