Skip to main content

Run a command with dynamically-sourced environment variables.

Project description

envrun is a CLI tool that runs a command with dynamically-sourced env variables.

Sick of scrubbing secrets from .bash_history? Do your fingers hurt from hitting ctrl+c and ctrl+v while copying the API token - for 154th time? This is a tool for you.

When working on modern projects it is often difficult to manage application secrets in a sane way. Sure, there are soleutions out there. Some of them even work just fine!

So... why a new tool? I wanted a tool that would fit into many different workflows. When locally running commands that need to have access to secrets, managing deployments with multiple differing sets of variables, injecting secrets into config files,...

In envrun a variable can be sourced from a file, output of an arbitrary coommand, from the environment, or from any compatible backend.

This tool is still under heavy development and its API might change at any time. Use with caution.

Installation

To install using pip, run:

pip install envrun

Other installation options are not yet available.

Usage

Usage: envrun [OPTIONS] COMMAND...

Execute COMMAND with env variables from .envrun

If COMMAND uses flags, prepend it with " -- ".

Options:
--non-interactive  Don't prompt for missing variable values.
--isolated         Don't pass the variables from the outer environment.
--help             Show this message and exit.

For debugging, invoke the env command, which should be available on most Unix-like systems:

envrun env

If invoking a COMMAND which uses flags, prepend it with " -- ":

envrun -- ls -al

If wanting to execute multiple commands within the same environment, COMMAND can be passed as a quoted string:

envrun "ls -a | sort"

envrun.toml file

Envrun starts searching for a envrun.toml file in the current working directory, checking parent directories if no envrun.toml file is found.

Variables are namespaced using the following convention:

vars.<backend>.<var_name> = <var>

The following lines are equivalent:

vars.<backend>.<var_name> = { key="x }
vars.<backend>.<var_name> = "x"

The "key" has a backend-specific meaning. Generally, it represents the most commonly-used setting for a particular backend.

See https://toml.io/ for file format specifics.

Example

# Variable LS will be set to the output of `ls -al`
vars.shell.LS = "ls -al"

# MY_PATH will be set to the value of $PATH.
vars.env.MY_PATH = "PATH"

[vars.const]
    # Hardcoded vars can be placed here.
    ENV = "development"
    TWO = "2"

[vars.file.SSH_PUBKEY]
    # SSH_PUBKEY will be set to the contents of id_rsa.pub.
    key = "~/.ssh/id_rsa.pub"

Use cases

12-Factor apps

According to the Twelve-Factor App methodology, app secrets should be configured from the environment. envrun can neatly support this workflow by keeping the .envrun.toml config files in version control and sourcing the config values from storage backends.

Infrastructure as code

When running configuration managament and Infrastructure as Code tools, there is often a need to inject secrets into the tool. Different tools have different ways of handling configuration and secret managament, and they can rarely work together.

Environment variables provide a common ground, as they are supported by the majority of popular tools.

This is where envrun comes into play - it provides a concise and extensible way of defining and passing the variables to tools like terraform and ansible.

Generating config files

Combined with the excelent envsubst, envrun can be used as a rudimentary templating engine.

envrun envsubst < nginx.conf.tmpl > nginx.conf

This will generate nginx.conf from nginx.conf.tmpl and replace all strings like $VAR or ${VAR} with their values - as provided by envrun.

Backends

const

Used for setting static env variables.

[vars.const]
PATH = "/usr/bin"

file

Used for setting a variable to contents of a file.

[vars.const]
PUBKEY = "~/.ssh/id_rsa.pub"

env

Generally useful only when used with --isolated. Used for passing or renaming specific env vars.

[vars.env]
RENAMED_PATH = "PATH"

shell

Used for setting static env variables.

[vars.shell]
GIT_REF = "git show-ref | head -1"

keyring

The keyring backend currently supports key stores implementing freedesktop.org secret service protocol; most notably Gnome keyring and KSecretsService. MacOS and Windows is still a work in progress.

Additionally, from version 2.5 KeepassXC should also work on systems that are D-Bus enabled (Linux). See See https://avaldes.co/2020/01/28/secret-service-keepassxc.html for details.

[vars.keyring.MY_SECRET]
key = "my-secret"

Extensions

Contributing

Create a virtualenv

python3 -m venv venv
. venv/bin/activate

Install the package:

pip install -e ".[dev]"

Make your changes.

Run code formatter:

black envrun

And finally: create a pull request.

Changes

0.0.5 (Unreleased)

Nothing yet

0.0.4

  • Use Toml instead of ini files for config
  • Remove --prefix; namespacing will be implemented differently
  • Update config file format
  • Add support for 3rd party storage backends
  • Make interactive mode the default
  • Pass env vars from the outer environment, unless --isolated is used
  • Temporarily remove --config flag
  • Use parent directories to search for config files
  • envrun now executes the supplied command in a subshell

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envrun-0.0.4.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

envrun-0.0.4-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file envrun-0.0.4.tar.gz.

File metadata

  • Download URL: envrun-0.0.4.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for envrun-0.0.4.tar.gz
Algorithm Hash digest
SHA256 5eb2be75805c6038623cf6675eeb1b99f2f4f6469734d7f09b10502f73b5272c
MD5 3152c5091f8f24f67dbf31b56a84954d
BLAKE2b-256 4ec32100355115e8d6c49b8ebf5a6e09b61d1c15d55c3fb1a8abfee8883e55b5

See more details on using hashes here.

File details

Details for the file envrun-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: envrun-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for envrun-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6486c866bf976feb4bddab5b1b5b74172963aeeeaea67e03dae6482967323586
MD5 4a218375082cb99bcdc94e5c77704642
BLAKE2b-256 3151158ed30473b87cc93e4d875543dcb5f63660217a0de4e3dcd5dc88813e2b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page