Host a static website with Google Cloud Storage using Firebase Cloud Function as a proxy to secure the access with HTTP basic authentication

That is a fucking long title.

I wanted a cheap hosting for a static website. So I though “Hey let’s use Google Cloud Storage”! It can be used easily to host a static website.

But my website has to be protected by username/password authentication. A HTTP basic Authentication is enough for my use case. The thing is: it is super hard to achieve this with Google Cloud Storage… I can restrict the access to my bucket with IAM, that’s for sure. But when my users access my static website hosted on GCS, no one is going to include a nice “Authorization” header or whatever. So it is “Access denied”.

So I wanted to create a Proxy to access my bucket. By creating a service account, I can provide READ access to any server to my bucket. But I still want a cheap solution. So I will definitely not run a VM 24/7 just to provide a proxy to a website rarely used.

Firebase proposes some cheap plan (pay as you go) and allow us to run Cloud Function with basically any NPM module I want, including Express, which provide Proxy middleware.

So I started to build a proxy to access my GCS hosted website. But I never achieve the expected result because of this

fail_google.png

There is a Github issue about a similar error but damn, there is no solution for this; It looks like Google will just not allow Firebase to access GCS via HTTP.

So after a lot of struggles and try-but-failed workaround, I ended up downloading locally the requested file from GCS and stream it as a request.

All is left to do is create a nice CNAME for my firebase project.

See this gist to access the final code I used.

Prettier, Webstorm, NVM and Linux

I installed Webstorm and wanted Prettier.

I simply installed the Prettier extension and added a File Watcher (as stated in the Doc)

But Prettier refused to run with “node not found” something…

I added this line to my .bashrc file to make sure NVM creates a symlink to the current node version:

<br />export NVM_SYMLINK_CURRENT=true

Then I edited my environment sudo nano /etc/environment and added this folder at the end of my PATH /home/tanguy/.nvm/current/bin (the NVM symlink)

Now Webstorm will launch and Node will be in the PATH so Prettier runs fine.

 

Oh and I had to add some command line arguments to the Prettier File Watcher on Webstorm because the plugin was not reading my eslintrc.js configuration file.

VS Code alias auto-completion

All credits go to jgoux ; this post is a post-it to his explanation on Github: https://github.com/tleunen/babel-plugin-module-resolver/issues/92#issuecomment-285755384


1. Teach VS Code’s Intellisense where to look at

So you can jump to the definition and have autocompletion. sparkles

To do that, create a jsconfig.json file at the root of your project :

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "~/*": [
        "src/*"
      ]
    }
  },
  "exclude": [
    "node_modules"
  ]
}

2. Teach VS Code to autocomplete your paths

Install the extension Path Intellisense

This extension allows the autocompletion of the custom paths.

Configure the extension in your project’s settings under .vscode/settings.json :

{
  "path-intellisense.mappings": {
    "~": "${workspaceRoot}/src"
  }
}

3. Teach Babel how to resolve your custom paths

The last step is to install and configure babel-plugin-module-resolver so the code compile!

Here is my .babelrc :

{
  "plugins": [
    [
      "module-resolver",
      {
        "alias": {
          "~": "./src"
        }
      }
    ]
  ]
}

Docker container – Run a container with access to the host’s docker daemon and volumes


docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker/volumes:/var/lib/docker/volumes --mount source=myvol1,target=/vol alpine sh

 

The above command runs a container (alpine image). Inside this container, you have access to the docker daemon of the host as well as the volumes.

Meaning that, from that container, you can manage containers (start, stop, inspect, …)

You can also start new containers with volumes.


apk update && apk add docker

docker run --rm -it --mount source=myvol1,target=/vol alpine sh

 

Netdata in Docker – Monitor my machine

Netdata: https://github.com/netdata/netdata & https://www.netdata.cloud/

is a wonderful tool to monitor a device and trigger alarms

To run it in a second on your local machine, use this docker command

docker run -d --name=netdata -p 19999:19999 -v /proc:/host/proc:ro -v /sys:/host/sys:ro -v /var/run/docker.sock:/var/run/docker.sock:ro --rm --cap-add SYS_PTRACE --security-opt apparmor=unconfined netdata/netdata

Then access the dashboard http://localhost:19999