External Secrets¶
External secrets are secrets that are stored in an external secret management service and fetched during a deployment by Doco-CD. This allows you to keep your secrets out of your Git repository and manage them in a secure way.
Supported Secret Provider¶
| Provider | More Information |
|---|---|
| AWS Secrets Manager | https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html |
| Azure Key Vault | https://azure.microsoft.com/en-us/products/key-vault |
| Bitwarden Secrets Manager | https://bitwarden.com/products/secrets-manager/ |
| Bitwarden Vault / Vaultwarden | https://bitwarden.com/help/vault-management-api/ and https://github.com/dani-garcia/vaultwarden |
| 1Password | https://1password.com |
| Infisical | https://infisical.com/ |
| OpenBao | https://openbao.org/ |
| Webhook | Fetch secrets from any remote service via HTTP requests with a flexible configuration |
Tip
Additional external secret providers may be supported in the future. If you have a specific provider in mind, please open a feature request or submit a pull request if you are able to implement the provider yourself.
Setting up an External Secret Provider¶
To use an external secret provider, configure the environment variables for your provider and then set external_secrets in your .doco-cd.yml. See the provider-specific pages for details.
Using External Secrets in Deployments¶
Doco-CD uses variable interpolation to replace variables in your Compose files with the values fetched from the external secret provider, see the Compose file reference for more information and examples.
For example with Bitwarden Secrets Manager, if you want to use secrets named DB_PASSWORD and LABEL_SECRET in your Compose file, you can reference it like this:
name: myapp
external_secrets:
DB_PASSWORD: a8f1e4eb-d76d-47b4-aa3c-103733e77fce
LABEL_SECRET: cfd0c4a9-16d4-44c8-9a80-c6143a7c7b71
Then you can use the variable in your Compose file like this:
Warning
External secrets have a higher priority than variables set in a .env file or in the environment. If a variable is set in both an external secret and in a .env file, the value from the external secret will be used.
services:
app:
image: myapp:latest
environment:
DATABASE_HOST: db
DATABASE_USER: ${$DB_USER:-postgres} # You can also set a default value if the secret is missing
DATABASE_PASSWORD: $DB_PASSWORD
labels:
- "traefik.enable=true"
- "traefik.http.routers.myapp.rule=Host(`myapp.${DOMAIN}`)" # Note that DOMAIN is set in a local .env file and not fetched from the secret provider
db:
image: postgres:latest
environment:
POSTGRES_USER: ${$DB_USER:-postgres}
POSTGRES_PASSWORD: ${DB_PASSWORD}
This will result in the following docker-compose configuration being used during deployment:
services:
app:
image: myapp:latest
environment:
DATABASE_HOST: db
DATABASE_USER: postgres
DATABASE_PASSWORD: supersecretpassword123 # Value of external secret fetched from Bitwarden Secrets Manager
labels:
- "traefik.enable=true"
- "traefik.http.routers.myapp.rule=Host(`myapp.example.com`)" # Value from .env file
db:
image: postgres:latest
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: supersecretpassword123 # Value of external secret fetched from Bitwarden Secrets Manager
As Compose Secrets and Configs¶
You can also pass external secrets to a service as a file with the top-level Compose secrets or configs sections. Use the environment attribute to read the value from the external secret.
services:
app:
image: myapp:latest
secrets:
- db_password
secrets:
db_password:
environment: DB_PASSWORD # Name of the external secret in .doco-cd.yml
Compose writes the value into the container only when it creates the container. For this reason, doco-cd adds the cd.doco.deployment.content.sha label to each service that uses an environment-backed secret or a config with an environment or content source. When the value changes, doco-cd redeploys the project and Compose recreates the services that use it. This change alone does not recreate unrelated services.
Note
- The label holds a SHA-256 hash of the consumed values. Everyone with access to
docker inspectcan read the hash. Use secret values that are hard to guess, because a short or common value can be found from its hash. - Secrets and configs that
cd.doco.deployment.recreate.ignorematches are not included in the label. Changes to their values do not recreate the service. - For
environment-backed secrets and configs that use apki-role:certificate, key, or full chain, doco-cd uses the reference in the digest. Reissuing the certificate does not change the label. Automatic certificate rotation recreates these services. - This label applies to Docker (Standalone) deployments only. Docker Swarm deployments do not get this label. Swarm creates the secret or config with a hash of the value in its name, and rotates it when the value changes. See Values from variables and inline content.
- After an upgrade to a doco-cd version with this label, doco-cd redeploys affected projects one time and recreates the services that consume such secrets or configs.
With Interpolation¶
External-secret references can optionally support Compose-style interpolation using environment variables set on the doco-cd deployment itself. Set INTERPOLATE_EXTERNAL_SECRETS=true to enable this feature. It is disabled by default:
With INTERPOLATE_EXTERNAL_SECRETS=true and PROJECT_STAGE=lab set in the doco-cd container, the provider receives kv:db-lab. If PROJECT_STAGE is not set, the default prod is used.
Only legacy string references are interpolated
Structured webhook references are preserved unchanged.
Only use with trusted external secret references
Enable this option only when the external secret references are trusted, because referenced process environment variables are included in provider requests.
Defining External Secrets in a File¶
Instead of (or in addition to) declaring external_secrets inline in your .doco-cd.yml, you can set external_secrets_files to a list of YAML files, each holding a map of env var name to external secret reference using the same shape as external_secrets:
DB_PASSWORD: a8f1e4eb-d76d-47b4-aa3c-103733e77fce
LABEL_SECRET: cfd0c4a9-16d4-44c8-9a80-c6143a7c7b71
File paths are resolved relative to the same directory as dotenv files and are not confined to the repository (see the trust model). Like env_files, you can use the remote:<filepath> syntax to load a file from the remote repository when repository_url is also specified:
name: myapp
repository_url: https://github.com/example/other-repo.git
external_secrets_files:
- remote:secrets.yaml
Files can also be SOPS-encrypted, the same way encrypted dotenv files are supported.
Inline entries take precedence
external_secrets_files are merged first, then external_secrets entries are applied on top. If the same name is defined in both, the inline value from external_secrets wins.
Fetching Secrets in Bulk¶
If a stack needs many secrets, you do not have to list each one in external_secrets. Set external_secrets_paths to a list of provider paths instead. Doco-CD fetches every secret below each path and makes it available as a variable:
name: myapp
external_secrets_paths:
- path: 0db45926-c97c-40d4-a3aa-fefd5d5fb492:prod:/myapp/ # Provider specific path, see the provider pages
prefix: MYAPP_ # Optional, added in front of every variable name
recursive: true # Optional, also fetch secrets in sub folders
| Key | Description | Default |
|---|---|---|
path | Provider specific path. Required, see the provider pages for the format. | |
prefix | Text added in front of every variable name. | "" |
recursive | Also fetch secrets in sub folders. Ignored by providers without folders. | false |
Variable names are built from the secret names. All letters become upper case and every other character, such as -, . or /, becomes an underscore. A name that starts with a digit gets an underscore in front. With the example above, a secret named db-password is available as $MYAPP_DB_PASSWORD in your Compose file.
Supported providers
Bulk fetching is available for Infisical, OpenBao (KV v2), AWS Secrets Manager, Azure Key Vault and Bitwarden Secrets Manager. The 1Password and Webhook providers do not support it. The deployment fails with an error if you use external_secrets_paths with them.
Precedence
Secrets from external_secrets_paths have the lowest priority. The order is: external_secrets_paths < external_secrets_files < external_secrets. If a name is defined more than once, the entry with the higher priority wins.
Name collisions fail the deployment
If two secrets in external_secrets_paths result in the same variable name (for example db-password and DB_PASSWORD), the deployment fails. Use a different prefix for each path to avoid this. The error message names the colliding secrets but never contains secret values.
Things to know
- The variables are only available for Compose interpolation. A secret reaches a container only if your Compose file references it.
- A path without any secrets does not fail the deployment. Doco-CD logs a warning instead.
- Changed secret values trigger a redeployment in the same way as values from
external_secrets. INTERPOLATE_EXTERNAL_SECRETSalso applies topath.INTERPOLATE_RESOLVED_SECRETSapplies to the fetched values.- Fetching everything below a path needs list permissions in the provider. Grant the access token only the paths it needs.
- A nested
.doco-cd.ymlwithexternal_secrets_pathsreplaces the list of the base config.
Referencing Other Resolved Secrets¶
A resolved secret's value can itself reference another external secret by name, letting you compose one secret from others without duplicating values in your Compose file. Set INTERPOLATE_RESOLVED_SECRETS=true to enable this feature. It is disabled by default:
name: myapp
external_secrets:
DB_PASSWORD: <secret-ref-a>
DB_HOST: <secret-ref-b>
DB_URL: <secret-ref-c> # resolves to: postgres://user:${DB_PASSWORD}@${DB_HOST}/mydb
With INTERPOLATE_RESOLVED_SECRETS=true, if the secret provider returns postgres://user:${DB_PASSWORD}@${DB_HOST}/mydb for DB_URL, doco-cd substitutes ${DB_PASSWORD} and ${DB_HOST} with the values of the other resolved external secrets before the value is made available to the Compose file. References can be chained across multiple secrets.
Only other external secrets are used
Unlike reference interpolation, this only substitutes names that match another entry in external_secrets. The doco-cd process environment is never consulted, so an unrelated ${VAR} left in a secret's value (e.g. matching an OS environment variable) is left untouched.
Circular references fail the deployment
If secret A's value references secret B, and B's value references A, doco-cd returns an error instead of interpolating.
Literal $ characters must be escaped as $$
While enabled, secret values are parsed for Compose-style variables, so a literal $ must be written as $$. A value of pa$$word is delivered as pa$word, and a value of $DB_HOST is replaced when another external secret is named DB_HOST. If any of your secret values contain literal $ characters that you cannot escape at the source, leave this option disabled.
OpenBao pki-role certificates are issued on every check
Without this setting, Doco-CD issues OpenBao pki-role certificates only when a deployment runs. With this setting, Doco-CD needs the certificate values before it checks for changes. Then every poll or webhook issues new certificates, also when nothing is deployed.