Add encrypted secret cache to survive provider outages#15
Merged
Conversation
Adds a single-file encrypted cache that lets psi serve resolve secrets
from memory when the upstream provider is unreachable. Setup eagerly
populates the cache from Infisical's list_secrets responses; serve
decrypts the file once at startup and holds the dict in RAM for the
lifetime of the process.
Two backends are supported and configured per host:
- tpm: AES-256-GCM with a key sealed by systemd-creds and delivered via
LoadCredentialEncrypted=psi-cache-key on the serve unit.
- hsm: hybrid RSA-OAEP + AES-256-GCM using the existing Nitrokey HSM
envelope format (psi/providers/nitrokeyhsm/crypto.py).
The envelope carries a magic, version, and backend tag so the format is
self-describing and forward-compatible. If the backend cannot be opened
or the cache file is unreadable, serve logs a warning and falls back to
live provider lookups — a missing cache key never takes down the fleet.
New commands: psi cache init --backend {tpm,hsm}, refresh, invalidate,
status. The native serve unit gains StateDirectory=psi and, with the
tpm backend, LoadCredentialEncrypted=psi-cache-key.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
state_dir/cache.encsopsi servecan resolve secrets from memory when the upstream provider is unreachable.tpm(AES-256-GCM key sealed bysystemd-credsand delivered viaLoadCredentialEncrypted=psi-cache-key) andhsm(reuses the existing Nitrokey hybrid envelope inpsi/providers/nitrokeyhsm/crypto.py).psi-infisical-setupnow eagerly populates the cache fromlist_secretsresponses; serve decrypts once at startup and holds the dict in RAM. Cache misses fall through to the provider and are persisted.Why
On 2026-04-08, an Infisical outage on homelab.inf7.dev took down every PSI-backed container that needed to restart, because PSI's lookup path was a live HTTP proxy with no caching. The cache closes that single point of failure while keeping the zero-plaintext-on-disk invariant: the file carries a magic + version + backend tag and the payload is encrypted end-to-end.
Graceful degradation: if the backend cannot be opened, or the file is corrupt or written by a different backend, serve logs a warning and falls back to live provider lookups. A missing cache key never takes down the fleet.
New commands
psi cache init --backend {tpm,hsm}— provisions the key (tpm:systemd-creds encrypt --tpm2-pcrs=7) and writes an empty cache file.psi cache refresh— re-runs setup to pull fresh values from providers.psi cache invalidate <id>— drops a single entry and persists.psi cache status— shows backend, file size, last-write timestamp, entry count. Never prints plaintext.Systemd changes
Native
psi-secrets.servicegainsStateDirectory=psiand, whencache.backend == tpm,LoadCredentialEncrypted=psi-cache-key:<config_dir>/cache.key.Test plan
uv run ruff check psi/ tests/— cleanuv run ruff format --check psi/ tests/— cleanuv run ty check— cleanuv run pytest -q— 278 passed (23 new: 20 cache unit tests + 3 offline serve integration tests)psi cache init --backend tpm, verifypsi cache statusshows entries after nextpsi-infisical-setuprun