Environment and secrets resolution
Kamal turns deployment configuration and secret sources into the environment passed to application and accessory containers. It keeps ordinary values separate from secret values, adds host-specific values through environment tags, and loads secrets from local dotenv files or supported provider CLIs. For the wider configuration flow, see Deployment configuration system, Application topology configuration, and Service, proxy, and builder configuration.
Environment values
Clear environment values
Clear values are safe-to-show container settings such as URLs or feature flags. A simple env map is treated as clear data; the structured form uses clear when the same environment block also has secrets or tags. Clear data becomes direct container environment arguments.
References: lib/…/env.rb• L7-20 lib/…/env.rb• L4-23
Secret environment values
Secret entries name values that Kamal must resolve before it starts a container. A secret can use NAME:SOURCE_NAME to expose the value under one environment name while reading it under another name. The resolved values go into a separate environment file rather than into the normal clear-value arguments.
References: lib/…/env.rb• L19-41 lib/…/role.rb• L93-111
Environment tags
An environment tag is a named set of clear or secret values that can be attached to selected hosts. A role combines the root environment, its role-specific environment, and the tags assigned to that host. If several tags set the same clear key, the later tag wins.
The tag definitions live under the root env.tags map. Host or role topology selects the tag names, so tag expansion connects environment policy to the host placement described by the topology configuration.
References: lib/…/configuration.rb• L284-294 lib/…/role.rb• L34-40 lib/…/role.rb• L93-111 lib/…/env.rb• L33-52
Environment precedence
Environment resolution uses a layered merge. Root values provide the base, role values specialize it, and host tags apply the final host-specific layer. Secret names from each layer are combined, while clear values from a later layer replace earlier values with the same key.
The resulting clear arguments and secret file are supplied together when the application or accessory starts. This keeps the container interface consistent even when only one layer changes.
References: lib/…/env.rb• L23-36 lib/…/role.rb• L93-111 lib/…/boot.rb• L46-53 lib/…/accessory.rb• L27-31
Secret file sources
Dotenv file loading
The local secret store is a pair of dotenv files. Kamal reads the shared file <secrets-path>-common first, then reads the destination file <secrets-path>.<destination> or the base <secrets-path> when no destination is set. A later file replaces a value from an earlier file.
The default path is .kamal/secrets, and configuration can change it. Files are read when needed and then cached for the life of the configuration. A missing requested key produces a configuration error that names the files Kamal checked.
References: lib/…/configuration.rb• L54-62 lib/…/configuration.rb• L272-274 lib/…/secrets.rb• L12-18 lib/…/secrets.rb• L22-45
Dotenv substitutions
Dotenv parsing resolves values in relation to both the current process environment and other values in the secret files. A file value takes precedence over a process environment value with the same name.
Supported substitutions include:
$NAMEand${NAME}variable references.$(command)command substitution, with nested or escaped parentheses handled by the parser.$(kamal secrets ...)substitution, which calls the Kamal secrets command and inserts its result.- A backslash before the command form, which keeps the command text instead of running it.
Command substitution can therefore bridge a provider-backed fetch into a dotenv value without writing the provider result to a separate local file.
References: lib/…/secrets.rb• L1-4 lib/…/inline_command_substitution.rb• L1-45 test/secrets_test.rb• L34-58
Secret file construction
Kamal builds the runtime secret file from the resolved secret map. It writes one KEY=value line per entry, escapes values for Docker env-file rules, keeps non-ASCII text intact, and emits a newline even when the map is empty.
The application and accessory boot flows upload this file to the host with restrictive file permissions. Containers then receive the file through their environment-file setting while clear values remain separate command arguments.
References: lib/…/env_file.rb• L1-20 lib/…/env_file.rb• L24-43 lib/…/boot.rb• L46-53 lib/…/accessory.rb• L27-31
Secret provider integrations
Provider adapter flow
The kamal secrets fetch command is the common boundary for external secret providers. It selects an adapter by name, checks the provider CLI, performs any required login or unlock step, fetches the requested values, and returns a name-to-value map. The dotenv command substitution path uses the same command boundary when it needs a provider value during file parsing.
The provider name may include these aliases:
| Adapter family | Accepted alias or input shape |
|---|---|
| 1Password | 1password |
| LastPass | lastpass |
| Google Secret Manager | gcp |
| Bitwarden Secrets Manager | bitwarden-sm |
| AWS Secrets Manager | Provider names and optional --from prefixes identify secrets |
| Doppler | Project and config can be supplied through --from |
| Bitwarden | Items can name a field after / |
| Passbolt | Folder paths can prefix a secret name |
| Enpass | A vault path is supplied through --from |
Provider-specific names and prefixes select a vault, project, folder, item, field, or version. The adapter converts those names to the common map used by environment resolution.
References: lib/…/secrets.rb• L1-28 lib/…/adapters.rb• L2-15 lib/…/base.rb• L1-32
Provider credentials
Provider authentication stays with the provider CLI. Some adapters need an account value, while others use the existing CLI session or credentials in the process environment. If the CLI is absent, login fails, or a requested item is missing, the fetch stops instead of producing a partial secret set.
The main supported integrations are:
- AWS Secrets Manager through
aws. - Google Secret Manager through
gcloud, with optional project and version selection. - Doppler through
doppler. - 1Password through
op. - Bitwarden through
bw. - Bitwarden Secrets Manager through
bws. - LastPass through
lpass. - Passbolt through
passbolt. - Enpass through
enpass-cli.
References: lib/…/aws_secrets_manager.rb• L11-58 lib/…/gcp_secret_manager.rb• L29-107 lib/…/doppler.rb• L19-57 lib/…/one_password.rb• L5-23 lib/…/bitwarden.rb• L1-19 lib/…/bitwarden_secrets_manager.rb• L12-65 lib/…/last_pass.rb• L3-39 lib/…/passbolt.rb• L8-16 lib/…/enpass.rb• L17-36
Sensitive values
Secret redaction
Sensitive values carry their real value for command use and a redacted value for display. The redaction wrapper integrates with SSHKit output and YAML serialization, which prevents secret text from appearing in normal logs or serialized diagnostic data.
Environment files still contain the real values because containers must read them. Kamal uploads runtime secret files with mode 0600; this protects the file on the target host but does not change the value inside the running container.
References: lib/…/utils.rb• L39-56 lib/…/sensitive.rb• L4-19 lib/…/boot.rb• L50-52 lib/…/accessory.rb• L29-31
Deployment metadata tags
Deployment metadata tags are a separate tag set from host environment tags. Kamal builds them from deployment facts such as destination, version, service, performer, and time, then exposes them as KAMAL_* variables for hook commands and uses them in operational output.
This distinction matters when reading configuration: env.tags selects container environment layers, while deployment metadata tags describe the operation that is running.
References: lib/…/tags.rb• L6-35 lib/…/base.rb• L99-101 lib/…/hook.rb• L6-10
Resolution flow
The following flow shows how configuration reaches a running application or accessory.
flowchart LR
C[Deployment configuration] --> L[Root and role environment]
H[Host or role tags] --> M[Layered environment]
L --> M
S[Common and destination secret files] --> R[Resolved secrets]
P[Provider CLI fetch] --> R
D[Dotenv substitutions] --> R
M --> E[Clear values plus secret names]
R --> F[Docker env file]
E --> B[Application or accessory boot]
F --> B
The same resolved secret map can also serve build-time secret inputs and hook environments when those consumers request it. The boot path is the point where the map becomes a host file and the clear values become container arguments.
References: lib/…/configuration.rb• L61-75 lib/…/builder.rb• L68-74 lib/…/hook.rb• L6-10 lib/…/role.rb• L93-111
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the merge order aligned between root, role, and host-tag environments. A change can silently alter which value wins.
- Keep secret aliases compatible with the names expected by the application and with the keys returned by provider adapters.
- Keep the common and destination file naming rules aligned with custom
secrets_pathhandling. - When changing provider output shapes or adapter aliases, check dotenv inline fetches as well as direct
kamal secrets fetchcalls. - When changing secret file encoding, preserve Docker env-file escaping and the
0600upload contract. - Keep deployment metadata variables distinct from
env.tagsso hook consumers do not receive the wrong tag set.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A requested secret is absent from all selected files | Environment file construction raises a configuration error and boot does not proceed | Check the common, destination, or custom secret path and the exact secret key or alias |
| A destination-specific file overrides a shared value unexpectedly | The destination receives a different secret than the shared file defines | Review the file load order and keep destination-only overrides deliberate |
| A provider CLI is missing or cannot authenticate | The provider fetch fails before a partial secret map is returned | Install and authenticate the provider CLI before use |
| A provider item, field, folder, project, or version is wrong | Adapter fetch fails or reports the missing item | Match the adapter's name and --from format to the provider |
| A secret value contains Docker-sensitive characters and is not encoded correctly | The container reads a changed or invalid value | Use the shared env-file construction path rather than writing a file by hand |
| A secret is printed through a non-redacted path | Secret text can appear in logs or diagnostics | Pass sensitive values through the redaction wrapper and avoid debug printing of resolved maps |
Key files
| File | Why to read it |
|---|---|
| lib/…/env.rb | Defines clear values, secret names, aliases, and environment merging. |
| lib/…/role.rb | Shows how root, role, and host-tag layers become a role environment. |
| lib/…/secrets.rb | Defines local secret file selection, dotenv parsing, caching, and missing-key errors. |
| lib/…/inline_command_substitution.rb | Defines variable, shell-command, and inline Kamal secret substitutions. |
| lib/…/env_file.rb | Defines the Docker-compatible runtime secret file. |
| lib/…/base.rb | Defines the shared contract for external secret providers. |
| lib/…/adapters.rb | Maps provider names and aliases to adapters. |
| lib/…/secrets.rb | Defines the user-facing fetch and extract boundary for provider-backed secrets. |
| lib/…/sensitive.rb | Defines redaction behavior for sensitive values. |
| lib/…/tags.rb | Defines deployment metadata variables used by hooks and operational output. |