Firebase Functions

Synchronize Onboardbase secrets directly with your Firebase functions

📘

Requirements

📘

TIP

You can authenticate the CLI with Environment variables.
Supported environment variables are:

  • ONBOARDBASE_TOKEN - A service token
  • ONBOARDBASE_PROJECT - an onboardbase project name
  • ONBOARDBASE_ENVIRONMENT - an environment in the specified project

Environment Variables in Firebase

Updating function environment variables

Use the below command to update the environment configuration. It does complete mutation of the environment variables.

firebase functions:config:set env='{"KEY": "VALUE"}'

Read function environment variables

You can read the configuration of a function and get just the environment variables

firebase functions:config:get env

Authenticate Onboardbase CLI

If your CLI is not authenticated or newly installed, get a service token from the Onboardbase account settings (or from your account's administrator if you are not an admin of your Onboardbase account) and run the below command

onboardbasae config:set-token $SERVICE_TOKEN scope /

Fetching Secrets

Below is a package JSON for a firebase function.

Notice the clear_env which removes all the variables from the function, log_env which logs the environment variable, and update_config which updates the function's environment variable.

Notice the upload_env script we are using Onboardbase secrets command to pull secrets from Onboardbase.

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log",
    "clear_env": "firebase functions:config:unset env",
    "log_env": "firebase functions:config:get",
    "upload_env": "firebase functions:config:set env=\"$(onboardbase secrets -p='firebase' -e='development' --json)\"",
    "update_config": "npm run clear_env && npm run clear_env && npm run upload_env"
  },
  "engines": {
    "node": "16"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.22.0",
    "firebase-functions-test": "^0.2.0",
    "typescript": "^3.8.0"
  },
  "private": true
}

Uploading Secrets

To finalize the setup run:

firebase deploy

Using uploaded environment variables

Create a file named config.ts and add the below code into it.

The code exports all the env from the firebase function config.

import * as functions from "firebase-functions";

let config = process.env;
// use firebase config when deployed to firebase
const deployedToFirebase = process.env.NODE_ENV === "production";
if (deployedToFirebase) {
  config = functions.config().env;
}

export default config;

And in your index.ts use the above config file like the below:

import * as functions from "firebase-functions";
import config from "./config";
// Start writing Firebase Functions
// https://firebase.google.com/docs/functions/typescript

export const helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {
    tructuredData: true,
  });
  response.send("Hello from Firebase! \n" + JSON.stringify(config));
});

A sample firebase functions repo

Onboardbase Firebase Integration