Lease lifecycle and backends
Fnox leases turn a configured source of authority into short-lived credentials for a command or a direct secret lookup. The lease system creates or reuses credentials, records enough metadata to find and revoke them, and treats expiry as a reason to stop reuse rather than as a background renewal event. This page extends Lease management with the lifecycle shared by all backends.
Lease model
A lease is a set of environment-ready credential values paired with a lease ID and an optional expiry time. Each configured backend maps its provider-specific result into this common shape, so the rest of fnox can handle creation, caching, display, and cleanup without knowing the credential service details.
| Lease property | Role in the system |
|---|---|
| Credentials | Values injected into a command or returned by fnox get |
| Lease ID | Stable handle used for listing and revocation |
| Expiry | Point after which the lease is not active or reusable |
| Backend name | Link from the local record to the configured revocation method |
| Configuration identity | Prevents cached credentials from surviving a security-relevant backend change |
Lifecycle
The lifecycle moves from configuration and source credentials to a backend-issued result, then to local tracking and eventual reuse, revocation, or expiry cleanup. The same resolution path serves command execution and lease-backed secret reads.
This diagram shows how a lease moves from source authority to a usable result and then to local cleanup.
flowchart LR
A[Lease configuration] --> B[Resolve source credentials]
B --> C{Reusable cached lease?}
C -->|Yes| D[Use cached credentials]
C -->|No| E[Create lease through backend]
E --> F[Optionally cache credentials]
F --> G[Record lease in project ledger]
G --> H[Inject or return credentials]
H --> I{User revoke or expiry}
I -->|Revoke| J[Best-effort backend revoke]
I -->|Expire| K[Cleanup path]
J --> L[Mark local record revoked]
K --> L
Creation and cache reuse
Lease creation starts after fnox has resolved the source credentials needed by the selected backend. The backend receives a duration and label, returns credentials with provider-specific expiry data, and fnox records the result after it has attempted to protect the cached values. fnox lease create can target one backend or all configured backends. In the all-backend mode, fnox resolves shared source secrets once and reports the failures together after it attempts each backend.
When a cached record exists, fnox reuses it only when it is not revoked, has cached credentials, has the same security-relevant backend configuration, and is more than five minutes from expiry. A failed decrypt or a changed configuration causes a fresh backend request. The duration and command timeout do not change the cache identity, because they do not change the authority represented by already-issued credentials.
References: crates/…/lease.rs• L645-740 crates/…/mod.rs• L17-50 src/…/get.rs• L115-240 src/…/exec.rs• L91-151 src/…/lease.rs• L106-214
Ledger persistence
The lease ledger is a per-project local record stored below the fnox state directory. A project path selects the ledger, which keeps leases from unrelated projects separate. Commands that change the ledger use a separate lock file, and writes replace the data file as one operation so concurrent commands do not observe a partial record.
The record can hold cached credential values in plain form when no encryption provider is configured. When the configured default provider supports encryption, fnox stores encrypted values and remembers which provider must decrypt them. If encryption is configured but unavailable, fnox skips credential caching instead of writing unusable cached data.
The ledger also removes revoked or expired records after 24 hours. A record without an expiry uses its creation time for this staleness limit.
References: crates/…/lease.rs• L17-33 crates/…/lease.rs• L48-183 crates/…/lease.rs• L375-409 crates/…/lease.rs• L500-541
Listing and status
fnox lease list reads only the local ledger. It reports each record as active, expired, or revoked; --active and --expired filter those states without contacting the remote service. An absent expiry is shown as never, which covers backends that issue credentials without a usable TTL.
References: src/…/lease.rs• L375-421 crates/…/lease.rs• L203-219
Revocation and cleanup
Explicit revocation first tries to call the configured backend. It decrypts cached credentials when a backend needs the credential value itself, as with a GitHub App installation token. Fnox then marks the local record revoked and clears its cached values even when the remote call fails, so local state does not claim that the lease remains usable.
Cleanup handles every non-revoked expired record. It makes a best-effort backend revoke call, passes no credentials because the token has already expired, and marks the record revoked even when the backend is unavailable or the remote call fails. The later ledger save applies the 24-hour retention rule to these records.
References: src/…/lease.rs• L425-517 src/…/lease.rs• L520-579 crates/…/lease.rs• L122-151 crates/…/lease.rs• L190-201
Backend contracts
Every backend implements the same create, revoke, and maximum-duration contract. The important difference is where the short-lived authority comes from and whether the remote service supports early revocation.
| Backend | Credential source | Expiry behavior | Early revocation |
|---|---|---|---|
| AWS STS | Assumed IAM role | AWS returns the expiry | No remote call; STS TTL ends access |
| Azure token | Azure identity token for a scope | Azure controls about one hour | No remote call; token expires |
| GCP IAM | Impersonated service-account token | IAM returns an expiry | No remote call; token expires |
| GitHub App | Installation access token | GitHub limits it to one hour | Deletes the installation token |
| GitHub OAuth | User access token from device authorization | GitHub supplies token lifetime | No remote call; token expires |
| Vault | Dynamic secret response | Vault lease duration or no expiry | Calls Vault lease revoke |
| Cloudflare | Child API token made from a parent token | Fnox sends the requested expiry | Deletes the child token |
| Command | Script-defined credentials | Script may return an expiry or omit it | Optional revoke script |
AWS, Azure, and GCP identity backends
The cloud identity backends exchange existing caller authority for a narrower or shorter-lived token. AWS STS assumes a role and returns the three AWS session variables. Azure obtains a bearer token for one configured scope and places it under the configured environment variable. GCP uses caller credentials to impersonate a service account and returns an access token under a configurable environment variable.
These services control important parts of the lifetime. AWS allows up to 12 hours, while GCP allows one hour under the backend limit. Azure uses its own token lifetime of about one hour and can ignore a shorter requested duration. None of these backends needs an early revoke operation because the remote service enforces the token TTL.
References: crates/…/aws_sts.rs• L98-188 crates/…/azure_token.rs• L84-131 crates/…/gcp_iam.rs• L52-183
GitHub App and OAuth backends
The GitHub App backend creates an installation token from an app identity. Its optional permission and repository limits make the result suitable for automation. GitHub App tokens expire after one hour, and explicit revocation deletes the token by using the token value from the cached credentials.
The GitHub OAuth backend creates a user access token through device authorization. It keeps access and refresh tokens in the operating-system keyring when enabled, refreshes an aging token when possible, and falls back to interactive device authorization when it cannot refresh. The project ledger still provides the common lease cache, while the keyring preserves the OAuth sign-in across projects.
References: crates/…/github_app.rs• L135-170 crates/…/github_app.rs• L237-314 crates/…/github_oauth.rs• L224-267 crates/…/github_oauth.rs• L332-374
Vault and Cloudflare backends
The Vault backend asks a dynamic secret engine for values and maps response fields to environment variable names. Vault may return a real lease ID and duration, or it may return a static-style response with no expiry. Fnox warns when the service returns a TTL that differs from the requested value, because the Vault role can control the actual lifetime. Revocation sends the Vault lease ID to the server.
The Cloudflare backend uses a parent API token to create a child token. The child can inherit the parent policies or use explicit resource and permission policies. The returned Cloudflare token ID becomes the local lease ID, which lets explicit revocation delete the child token.
References: crates/…/vault.rs• L198-343 crates/…/vault.rs• L346-405 crates/…/cloudflare.rs• L328-445 crates/…/cloudflare.rs• L448-498
Command backend
The command backend is the adapter for an external credential system that has no built-in backend. Fnox supplies the requested duration and label to the create command, then expects JSON that contains credential values and may contain an RFC3339 expiry and lease ID. A configured revoke command receives the lease ID during explicit revocation or cleanup.
The command controls the truth of its expiry data. Missing or invalid expiry is treated as no automatic expiry, so such credentials remain reusable until explicit revocation or ledger staleness removes them. Command execution also has a timeout and must return valid JSON with at least one string credential.
References: crates/…/command.rs• L24-79 crates/…/command.rs• L83-189
Credential flow
Lease backends need source credentials to create new leases, but those source credentials should not become the credentials used by the target command. Fnox resolves only the environment values a backend may consume, places them in the process environment for the backend operation, and removes the temporary values before the child process starts.
Source credentials and output credentials
The input side can come from configured secrets, interactive prompts, cloud CLI sessions, local credential files, an OS keyring, or a backend-specific credential command. The output side is always a map of environment variable names to short-lived values. When a lease output key matches a normal secret key, the lease value wins so a long-lived source credential does not overwrite it in the target process.
References: src/…/lease.rs• L106-163 src/…/lease.rs• L216-312 src/…/exec.rs• L77-105 src/…/exec.rs• L154-216 crates/…/mod.rs• L252-301
Secret lookup and command execution
fnox get detects when a requested key is produced by a lease backend and returns that lease value instead of reading the key as an ordinary secret. fnox exec resolves configured leases before it starts the target command and injects the resulting values into that command. Both paths use the same cache and creation rules, which keeps direct lookup and process execution aligned.
References: src/…/get.rs• L20-47 src/…/get.rs• L124-162 src/…/get.rs• L228-257 src/…/exec.rs• L95-151
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the common
Leasecontract aligned with every backend's credential keys, expiry meaning, and lease ID needs. - Keep
produces_env_varandconsumed_env_varsaligned with backend output and source credential names, orgetandexeccan select or prepare the wrong values. - Keep the backend configuration hash limited to security-relevant fields so authority changes invalidate caches while lifetime-only changes do not.
- Preserve the distinction between an expired remote credential and a revoked local record. Cleanup must remain able to mark local state even when remote cleanup cannot run.
- Keep all-backend creation's shared secret resolution and per-backend error aggregation aligned with the create path. A partial success must not hide the failed backend.
- If a backend changes its expiry response, update the no-expiry and reuse behavior as well as the create path.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Backend configuration changes after a lease is cached | Old credentials could be used for a new role, scope, policy, or target | The cache compares a security-relevant configuration hash before reuse |
| Cached lease is within five minutes of expiry | A token may expire during the command | The lease is treated as not reusable and a fresh lease is requested |
| Ledger save fails after remote creation | The remote lease exists but local commands cannot track or revoke it | Fnox warns that the lease is untracked and requires manual revocation |
| Remote revoke fails during explicit revoke | Local state says revoked while remote cleanup may still be pending | Fnox reports a warning and always clears the local cache |
| Expiry is missing or cannot be parsed | The credential is treated as non-expiring until ledger staleness cleanup | Backend output must provide a valid expiry when the remote credential has a TTL |
| A required source secret fails to resolve during lease creation | The backend is not called, even if other optional secrets use a warn policy | Lease creation hard-fails when a backend-required value is missing |
One backend fails during fnox lease create --all | Other leases may be created, but the command returns an aggregate failure | The all-backend path reports each failed backend after its attempt |
| GitHub OAuth is used in a non-interactive session without a valid cached or refresh token | Device authorization cannot complete | Pre-authorize interactively or provide a still-valid keyring cache |
| Command output is invalid or the command exceeds its timeout | No lease is recorded | The create command must return valid JSON and complete within its configured timeout |
Key files
| File | Why to read it |
|---|---|
| crates/…/lease.rs | Defines the shared lease record, cache rules, ledger persistence, encryption choice, and resolution flow |
| crates/…/mod.rs | Defines the backend contract and the supported backend configuration model |
| src/…/lease.rs | Connects create, list, revoke, and cleanup commands to the ledger and backends |
| src/…/exec.rs | Shows how leases join normal secret resolution and command environment construction |
| src/…/get.rs | Shows how a lease-backed key is selected for direct lookup |
| crates/…/aws_sts.rs | Illustrates a cloud role exchange with native expiry and no early revoke |
| crates/…/github_app.rs | Illustrates scoped token creation and credential-dependent remote revoke |
| crates/…/vault.rs | Illustrates dynamic secret mapping, service-controlled TTL, and lease revoke |
| crates/…/command.rs | Illustrates the external command contract for unsupported credential systems |