op run to provision an app with the secrets it requires.
Many applications that follow the popular 12-Factor App guidelines source their secrets from the environment.
Requirements
Before you can load secrets into the environment, you’ll need to:- Sign up for 1Password.
- Install 1Password CLI.
- Store the secrets you want to provision in your 1Password account.
Step 1: Get secret references
You can get secret references in several ways:- With the 1Password desktop app: Copy secret references from the app.
- With 1Password for VSCode: Insert secret references from 1Password as you edit code.
- With 1Password CLI: Get secret references for one or multiple fields with
op item get. - Use the secret reference syntax rules to write secret references manually.
Step 2: Pass the secrets to the application
To provision an app with the secrets it needs, map the secret references to the appropriate environment variables, then useop run to pass the secrets to the application.
There are two ways to do this:
Export environment variables
You can individually export environment variables as secret references from the command line.-
Export the necessary environment variables:
- Bash, Zsh, sh
- fish
- PowerShell
-
Run
op run --with your command for starting the app:
Use environment (.env) files
Environment files allow you to define multiple (secret) environment variables with KEY=VALUE statements separated by a newline. These files have the .env extension.
You can check environment files into source control and use the same environment everywhere. For example, if you use environment files for your test secrets, you can run your tests both locally and in CI without having to provision the secrets separately.
prod.env
op run, specify the path to the environment file with the flag --env-file:
Environment file syntax rules
Environment file syntax rules
The
.env file parsing engine follows the following rules:-
Environment variables are defined as
KEY=VALUEstatements separated by a newline. -
Variables can span multiple lines if they are enclosed in either
'or": - Empty lines are skipped.
-
Lines beginning with
#are treated as comments. Comments can also be placed inline afterKEY=VALUEstatements. -
Empty values become empty strings. For example,
EMPTY=will set the environment variableEMPTYto the empty string. -
If a value is surrounded by single or double quotes, these quotes do not end up in the evaluated value. So
KEY="VALUE"andKEY='VALUE'both evaluate toKEYandVALUE. -
Occurrences of
$VAR_NAMEor${VAR_NAME}are replaced with their respective value from the environment. -
A variable defined in a .env file can be referred to later in the same file:
-
Special characters can be escaped with
\. For example,MY_VAR = "\$SOME_VAR that is not actually replaced."results in the following value for MY_VAR:$SOME_VAR that is not actually replaced.. -
Inner quotes are maintained, so
JSON={"foo":"bar"}evaluates toJSONand{"foo":"bar"}. -
Variables do not get replaced in values that are enclosed in single quotes. So
KEY='$SOME_VAR'evaluates toKEYand$SOME_VAR. -
Template syntax can be used in the
VALUEto inject secrets. TheKEYcan only contain template variables. -
Template parsing is performed after
.envfile parsing, so you cannot use the former to construct the latter. -
Leading and trailing whitespace of both
KEYandVALUEsegments are ignored, soKEY = VALUEis parsed the same asKEY=VALUE. -
Single and double quoted values maintain both leading and trailing whitespace, so
KEY=" some value "evaluates toKEYandsome value. - These files should use UTF-8 character encoding.
Expand variables in a subshell
When you reference a variable like$MY_VAR in the same command where you call op run, your shell expands $MY_VAR before op run can substitute the secret reference. For example, a command like the following will not resolve the secret from 1Password:
op run substitutes the secret before the variable expands, run the command to expand the variable in a subshell:
Step 3: Differentiate between environments
If you have different sets of secrets for different environments, you can use a single environment file to pass the appropriate secrets to the application. To do this, make sure you structure your 1Password secrets in the same way across all your environments. For example:dev/mysql/password and prod/mysql/password.
Then, include an externally set variable ($VARIABLE_NAME) in place of the vault name for each secret in your environment file. This will allow you to specify which set of secrets you use with each environment.
For example, in the following .env file, $APP_ENV is the externally set environment variable:
app.env
dev vault to deploy an app in the development environment:
- Bash, Zsh, sh, fish
- PowerShell