How to run firebase (functions) emulator on https instead of http?
Asked Answered
K

3

12

Would anyone know if there is a way to initialize a Firebase function (using emulator to debug locally) with an https address instead of the default http? I'm trying to debug a Telegram bot-related script and Telegram only allows https webhooks.

Shell output is the following:

PS C:\Users\<user>\Desktop\tmp_node\functions> firebase emulators:start --only functions
i  emulators: Starting emulators: functions
+  hub: emulator hub started at http://localhost:4400
+  functions: Using node@10 from host.
+  functions: functions emulator started at http://localhost:8443
i  functions: Watching "C:\Users\<user>\Desktop\tmp_node\functions" for Cloud Functions...
+  functions[<function name>]: http function initialized (http://localhost:8443/<endpoint>).
+  emulators: All emulators started, it is now safe to connect.

I need it to start on https://localhost:8443/ instead.

Thank you very much in advance!

Koopman answered 5/5, 2020 at 13:39 Comment(4)
This sounds like a feature request that should be directed to the Firebase CLI GitHub. github.com/firebase/firebase-toolsPostexilian
hey @Koopman were you able to solve this?Stempien
Hi @Stempien sorry, no, I wasn't.Koopman
@Koopman I got it to work using ngrokStempien
I
3

Like @bermick mentioned in the comments, ngrok is a good way to get around this until the Firebase guys give us a way to enable https on the local emulator. Here are the steps:

  1. Head over to https://ngrok.com and sign up for a free account.
  2. Download the ngrok binary file (it's the normal thing they have you download).
  3. Open the console/terminal and navigate to the folder that the ngrok binary is in.
  4. Set up your auth token by running this command with your auth token dropped in: ./ngrok authtoken [[your_token]] (the current page at https://dashboard.ngrok.com/get-started/setup will have the whole command including the token so you can just copy and paste it into the console).
  5. Start up the Firebase emulator suite if it's not already running and make a note of the port that cloud functions are running on (I assume you know how to do this if you're googling how to use https with the emulator).
  6. Back in the console, make sure that you're in the directory containing the ngrok binary and run this command (replacing 5001 with your machine's firebase functions emulator port) ./ngrok http 5001
  7. Ngrok will open a UI that shows you the forwarding https url you can hit, which will forward to your local machine. E.g. Forwarding https://xxxxxxxxx.ngrok.io -> http://localhost:5001
  8. Test out the cloud function by using the ngrok https forwarding url (remember to append the appropriate cloud function path). E.g. https://xxxxxxxxx.ngrok.io/my-project/us-central1/cloudFunctionName
Idealize answered 21/5, 2021 at 16:37 Comment(1)
Solution is for google to start giving f*** about developers not us using complicated setups just to workaround their incompetenceDementia
C
2

Answer is that google implements https in the emulator like upvote if you agree

Communistic answered 15/7 at 11:25 Comment(0)
B
0

You can achieve a similar setup with NGinx as a reverse proxy plus TLS termination.

I wrapped it with Docker as I don't like installing things, but you can skip that layer if you install NGinx directly.

You may need a proper certificate to avoid the hassle of overriding security at the client end. (e.g. I don't know if Telegram webhooks can have their trust level reduced to accept self-signed certs, or to not check the domain etc). Calling Firebase Functions from a browser leads to endless issues when using temporary security overrides etc, so I always use a fully signed certificate with a dev CA installed in the OS certificate root. You could also use Lets Encrypt to generate them. This aspect is the hardest part of hosting secure servers locally.

The basic NGinx proxy and TLS termination is:

server {
    server_name app-local.mydomain.com;

    listen 8081 ssl;

    ssl_certificate     /etc/nginx/tls/mydomain.local.crt;
    ssl_certificate_key /etc/nginx/tls/mydomain.local.key;

    location / {
            proxy_pass http://app-local.mydomain.com:5001;
            include proxy_params;
        }
}

If you use Docker, set proxy_pass to use host.docker.internal.

I always use a fake domain in local development, instead of localhost or 127 IP directly, as it avoids any special treatment/restrictions they get and they work differently in Docker anyway. So in /etc/hosts e.g.

127.0.0.1   app-local.mydomain.com

Your Firebase Functions would be served via app-local.mydomain.com:8081 over TLS.

For me I had to do this because Safari simply doesn't accept using HTTP within an HTTPS page context - so I cannot develop locally. In Chrome you can set each site to "Insecure Content" = "Allow" which turns the errors into warnings, but it's still annoying.

Bieber answered 7/9 at 2:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.