Socket
Socket
Sign inDemoInstall

12factor-env

Package Overview
Dependencies
12
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    12factor-env

12factor env vars and secrets


Version published
Maintainers
1
Install size
0.965 MB
Created

Readme

Source

12factor

Secrets meant for usage with docker-based applications.

Uses envalid under the hood, but considers secrets for true integration of 12factor apps.

default secret path

defaults to /run/secrets/<secret_name>

You must set process.env.ENV_SECRETS_PATH to change this, for example,

process.env.ENV_SECRETS_PATH='/var/run/your/secrets/folder/';

or

ENV_SECRETS_PATH='/var/run/your/secrets/folder/' node yourapp.js

Using _FILE convention, include SECRET_NAME_FILE as a config var

const myEnv = env(
  process.env
  {
    // put all secrets here
    SECRET_NAME: str()
  },
  {
    // all config vars here
    PORT: port({ default: 10101 })
  }
);

If you haven't specified the value, you can enter it inside of a secret() call

const myEnv = env(
  process.env
  {
    // put all secrets here
    SECRET_NAME: secret('secret.txt') // will look in /run/secrets/secret.txt
  },
  {
    // all config vars here
    PORT: port({ default: 10101 })
  }
);

Examples

basic usage with envalid

if you use the _FILE standard:

  const myEnv = cleanEnv(
  process.env,
  {
    PORT: port({ default: 10101 }),
    GITHUB_TOKEN: secret(process.env.GITHUB_TOKEN_FILE)
  });

or you can specify the name of the secret file as it is stored

  const myEnv = cleanEnv(
  process.env,
  {
    PORT: port({ default: 10101 }),
    GITHUB_TOKEN: secret('github_token.txt')
  });

Better yet, just ensure that you use the env shortcut and it handles it for you

const myEnv = env(
  {
    GITHUB_TOKEN_FILE: process.env.GITHUB_TOKEN_FILE
  },
  {
    GITHUB_TOKEN: str()
  },
  {
    PORT: port({ default: 10101 })
  }
);

const { GITHUB_TOKEN, PORT } = myEnv;

env shortcut

Here env() expects 2 args, secrets and env vars.

In this example, it will look for /var/run/secrets/MAILGUN_KEY, and populate the final env with everything in one object.

  const myEnv = env(
    process.env,
    { MAILGUN_KEY: str() },
    { PORT: port({ default: 10101 }) }
  );

secret field

The secret object let's you specify the secret name as it is saved in the /var/run/secrets folder.

  const myEnv = env(
    process.env,
    { MAILGUN_KEY: secret('MAILGUN_KEY') },
    { PORT: port({ default: 10101 }) }
  );

FAQs

Last updated on 23 Nov 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc