File secret management
File secret management lets fnox store whole file contents as named secrets and later restore them to the path they belong at. It also lets an ordinary resolved secret cross a process boundary as a private file path instead of as its raw value. These are two related but distinct uses: [files] entries are persistent file records, while as_file = true changes how a resolved [secrets] value reaches a consumer.
File secret model
The file secret model treats the deployment path as the name of the stored item. A file uses the same provider and profile resolution rules as other secrets, but it is not an environment variable and can only be declared in fnox's global configuration.
| Concept | Role |
|---|---|
[files] entry | Stores whole file content for later deployment. |
| Path key | Identifies both the file record and its destination. |
| Provider | Encrypts the content or stores it at a remote secret service. |
source | Keeps the provider value in a separate file inside the config directory. |
sync cache | Provides a local encrypted copy that can satisfy reads without the original provider. |
The active profile set supplies the file entries. Later profiles replace an earlier entry for the same normalized path, and --no-defaults removes top-level entries from that active set. This makes a profile selection choose both which content is used and which path definition owns it. References: crates/…/files.rs• L1-6, crates/…/files.rs• L268-330, crates/…/config.rs• L214-225
File path handling
Path handling turns user-friendly path spellings into one absolute path before fnox compares or writes them. A path may be absolute or start with ~/; dot and dot-dot parts are cleaned lexically, and a path under the home directory is displayed again with ~/ when possible.
The path key is the deployment target, not a provider item name. A remote provider gets a separate item name derived from the path unless the caller supplies one. Relative source paths are resolved from the directory of the config that declares the entry and must stay inside that directory.
| Path rule | Result |
|---|---|
~/.kube/config | Resolves from the current user's home directory. |
/home/me/.kube/config | Resolves to the same target when it is that home path. |
./config or ../config as a deployment key | Rejected because the target must be absolute or home-relative. |
source = "files/key.age" | Reads or writes below the declaring config directory. |
source with an absolute path or .. | Rejected to keep stored material inside the config directory. |
Only the global config may declare file entries. This prevents a project config from selecting an arbitrary path on a user's machine. A source file may be a deliberate symlink, but fnox refuses source paths that would overwrite the config or another file's stored material. References: crates/…/files.rs• L73-142, crates/…/files.rs• L158-228, crates/…/config.rs• L230-263, crates/…/files.rs• L607-656
File content lifecycle
The file lifecycle moves content between a local path, a provider-backed record, and the destination path. The write command resolves every selected file before it writes any of them, which avoids a partial deployment when one provider lookup fails.
flowchart LR
A[Local file content] -->|set --file| B[Provider or source record]
B -->|get --file or files write| C[Resolved file content]
C -->|files write| D[Destination path]
B -->|sync cache| E[Encrypted local cache]
E --> C
F[Active profiles] --> B
F --> C
When content is stored, fnox sends it to an encryption-capable provider or a remote storage provider. The plain provider is not valid for file storage because it would leave the content readable in configuration. get --file prints the content without adding a newline. edit --file uses the entry's provider and profile. Removing an entry removes only its declaration; it does not remove the deployed file, source file, or remote item.
When files write deploys content, it creates missing parent directories and writes with owner-only mode 0600 on systems that support Unix modes. It leaves matching regular files in place, replaces changed files through a temporary file, and replaces a symlink at the destination instead of writing through it. References: src/…/files.rs• L10-18, src/…/files.rs• L82-159, src/…/files.rs• L223-386, crates/…/files.rs• L340-368, crates/…/files.rs• L572-605
File listing and content commands
The file commands separate discovery from content access. Listing shows path, profile, and provider without resolving or decrypting content; content commands resolve only the selected entry.
| Command | Purpose | File lifetime or effect |
|---|---|---|
fnox files | List active file entries. | Does not read content. |
fnox set --file PATH | Store the content currently at PATH. | Updates the provider-backed record. |
fnox set --file PATH --from-file INPUT | Store content from another UTF-8 file. | Preserves the input content, including its final newline. |
fnox get --file PATH | Print stored content. | Does not add a newline. |
fnox edit --file PATH | Edit and store content. | Keeps the entry's provider and declaring profile. |
fnox files write [PATH...] | Restore one or all active files. | Writes persistent destination files. |
fnox remove --file PATH | Remove the file entry. | Leaves existing content and provider items in place. |
The --source option is for the stored provider value, not the deployed file. It keeps large ciphertext or another provider value outside the TOML while retaining the same path-to-file relationship. References: src/…/files.rs• L50-79, src/…/files.rs• L82-160, src/…/files.rs• L367-471, src/…/set.rs• L182-229
Resolved secrets as files
An ordinary secret becomes a file-delivered secret when its configuration sets as_file = true. Resolution still produces the secret's value through the normal provider, default, environment, and cache rules; the file boundary is applied only after that value is resolved.
The consumer receives a path in the environment or command output. The file contains the resolved value as-is, so a service that expects a certificate, key, or JSON credentials document can read it without exposing that content as the variable value.
sequenceDiagram
participant C as Consumer command
participant F as fnox
participant R as Secret resolver
participant T as Private temp file
C->>F: Request secret or environment
F->>R: Resolve named secret
R-->>F: Resolved content
F->>T: Write content
F-->>C: Return temp-file path
C->>T: Read content through path
F->>T: Remove when its lifetime ends
fnox exec uses an ephemeral file and removes it when the command ends. get and export use persistent temporary files so their printed or exported paths remain usable after fnox returns. Shell integration uses session-owned persistent files, replaces them when values change, and removes them on deactivation or shell exit. On Unix these files use mode 0600.
The file flag does not change the resolved configuration value itself. For example, list --values displays resolved values, while list marks the configured source as [file]; get and environment-producing consumers are the boundaries that turn the value into a path. References: crates/…/config.rs• L306-365, src/…/get.rs• L9-15, src/…/get.rs• L122-149, src/…/list.rs• L167-193, crates/…/temp_file_secrets.rs• L10-57, src/…/hook_env.rs• L231-362
Exported file paths
Export carries the same resolved secret set into another format. A selected as_file secret is written to a persistent temporary file and its path is placed in the exported map; a normal secret keeps its value in the map.
This means exported data describes how a consumer should access the secret, not always the secret bytes. The export can be dotenv, shell, JSON, YAML, or TOML, and structured output can be read back by fnox import; the temporary file is separate from that formatted output.
Export filters the active profile before resolution unless --all or explicit key names widen the selection. Therefore a file secret can be absent because it is outside the export selection, or its path can be absent because resolution returned no value. If temporary-file creation fails, export warns and falls back to placing the raw value in the output.
References: src/…/export.rs• L29-45 src/…/export.rs• L114-191 crates/…/temp_file_secrets.rs• L60-121
Relationship to resolved configuration
File material and resolved configuration meet at different points in the system. A [files] entry is itself resolved through a small secret-like request, while as_file is metadata on a normal secret that changes only its delivery form.
| Question | [files] entry | as_file secret |
|---|---|---|
| What is named? | The destination path. | A secret name such as an environment variable. |
| Where is it declared? | [files] or [profiles.NAME.files] in global config. | [secrets] or a profile's secrets table. |
| What does resolution return? | Whole file content for deployment or printing. | A secret value before delivery conversion. |
| Does it enter the environment? | No. | Yes, as a temporary file path when the consumer allows it. |
| What selects it? | Active profiles and optional path arguments. | Secret filters and the consumer's env mode. |
Both paths use the central resolver's provider and cache behavior. A missing provider value can use a configured default or follow if_missing; post-processing such as JSON or line extraction applies to ordinary secrets before as_file writes the result. File entries force a value to be present when they are deployed and do not use environment injection. References: crates/…/files.rs• L340-368, crates/…/secret_resolver.rs• L595-615, crates/…/secret_resolver.rs• L693-747, docs/…/configuration.md• L222-230
For the wider model, read configuration and secret resolution, configuration discovery and profiles, secret references and resolution, encryption and local secret storage, and template evaluation.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep path normalization, display keys, CLI path lookup, and profile override matching aligned. A new path spelling rule can make one entry appear to be two paths.
- Keep the global-config restriction aligned with every file-loading path. Allowing project declarations would change the safety boundary for
files write. - Keep
sourcevalidation and source-target checks aligned. A change that permits a path outside the config directory can expose or overwrite unrelated files. - Keep
as_filehandling aligned acrossexec,get,export, and shell hooks. They need different lifetimes but must all write the resolved value and expose the path. - Keep persistent temporary-file cleanup aligned with the session data that owns each path, especially when
TMPDIRchanges or a nested shell exits. - Keep provider capability checks aligned for both direct storage and sync caches. The plain provider must not become a file-storage or encryption-cache path.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A file path is relative or empty | The file entry cannot be validated or selected. | Require an absolute path or ~/ and normalize it before comparison. |
A project config declares [files] | Configuration loading fails instead of allowing the project to choose a write target. | Accept file tables only in the global config area. |
source points outside its config directory or conflicts with another stored item | The write is rejected before it can overwrite config or ciphertext. | Validate relative paths and check resolved source targets. |
| A file provider cannot resolve one selected file | No selected file is written. | Resolve all files before starting deployment. |
| A temporary file cannot be created during export | Export can expose the raw value instead of a path. | Treat the warning as a protection loss and inspect export output. |
| A shell hook refreshes file secrets without cleaning its old session | Old private files remain on disk. | Track paths in the session and clean them on refresh, deactivation, or shell exit. |
as_file is used with process replacement | fnox cannot reliably clean up the file after the child replaces it. | Reject replacement when file secrets need cleanup. |
Key files
| File | Why to read it |
|---|---|
| crates/…/files.rs | Defines the path model, provider-backed file resolution, storage, validation, and safe deployment. |
| src/…/files.rs | Shows the user-facing list, write, store, read, edit, and remove flows for named file entries. |
| src/…/set.rs | Shows how --file, --from-file, profiles, and provider selection enter the write flow. |
| crates/…/config.rs | Defines file and secret configuration fields and the global-config boundary. |
| crates/…/secret_resolver.rs | Defines the resolved value that file delivery consumes. |
| src/…/get.rs | Shows the difference between returning a raw secret and returning a persistent file path. |
| src/…/export.rs | Shows how file-backed secrets become paths in exported formats. |
| src/…/hook_env.rs | Shows session-scoped file creation and cleanup for shell integration. |
| crates/…/temp_file_secrets.rs | Defines private temporary-file creation and lifetime primitives. |
| docs/…/files.md | Gives the user-level model for storing, editing, caching, and deploying whole files. |