Repository navigation
feat(plugins): key store hashes with a secret - #589
Conversation
Add the optional setting plugins.defaults.store.key_secret, with the environment variable FOCA_PLUGINS_STORE_KEY_SECRET as fallback. With a secret, Store.key_for() returns HMAC-SHA256 keys prefixed hk:, which never collide with the plain SHA-256 keys (h:). Without one, keys stay plain and the plugin host logs a warning at startup if plugins keep their entries in the database. The secret is a SecretStr and not part of the repr of the store settings. Changing it makes earlier entries unreachable until they expire; documented in the configuration reference, the plugin author guide and the 0.20.0 migration guide. The Helm library chart value is left to a separate change. Refs #565 Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Reviewer's GuideIntroduces optional HMAC-SHA256 keying for database-backed plugin store entries, securely resolves and hides the secret, warns when unkeyed persistence remains, and provisions the secret through Helm while documenting migration and rotation behavior. Sequence diagram for resolving the plugin store key secretsequenceDiagram
participant Config
participant Engine
participant Env as Environment
participant StoreSettings
Config->>Engine: _store_settings()
Engine->>Config: read key_secret
alt configured key_secret is non-empty
Engine->>StoreSettings: set key_secret bytes
else key_secret unset or empty
Engine->>Env: read FOCA_PLUGINS_STORE_KEY_SECRET
Env-->>Engine: secret or empty
Engine->>StoreSettings: set key_secret bytes
end
Sequence diagram for deriving keyed plugin store keyssequenceDiagram
participant Plugin
participant Store
participant StoreSettings
Plugin->>Store: key_for(parts)
Store->>StoreSettings: read key_secret
alt secret configured
Store-->>Plugin: hk: HMAC-SHA256(message)
else no secret
Store-->>Plugin: h: SHA-256(message)
end
Flow diagram for protecting the plugin store secret in Helmflowchart TD
Overlay[Config overlay ConfigMap]
Check{key_secret is empty or unset?}
Render[Render deployment and ConfigMap]
Fail[Fail rendering without showing the secret]
Overlay --> Check
Check -->|yes| Render
Check -->|no| Fail
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #589 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 56 56
Lines 7068 7090 +22
=========================================
+ Hits 7068 7090 +22
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Add the value plugins.existingSecret to foca-common: the name of an existing Secret whose key store-key-secret the app and the worker get as FOCA_PLUGINS_STORE_KEY_SECRET. Rendering fails if the config overlay, a ConfigMap, sets plugins.defaults.store.key_secret. Bump the chart to 0.4.0 and the petstore example's dependency. Closes #565 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Describe plugins.existingSecret in the chart's deployment notes and values, the configuration reference (also the environment variable), the deployment guide and the migration guide to 0.20.0. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Pydantic showed the raw input of a failed check (input_value), which could contain plugins.defaults.store.key_secret before it is masked, e.g., when another plugin setting is invalid. Hide the input in validation errors of the configuration (Config, whose setting applies to nested models, and the plugin models validated on their own). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Also refuse booleans, zero, maps and lists in the overlay's key secret in the tests, and explain the helper's walk of the overlay. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
CodeQL flagged the startup warning as clear-text logging of a secret, as it interpolated the constant with the variable's name. Write the name into the message instead; the test checks it matches the constant. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/plugins/engine.py" line_range="676-680" />
<code_context>
+ `False` only if some plugin keeps its entries in the database
+ (mode `shared`) and the operator set no key secret.
+ """
+ return all(
+ settings.key_secret
+ for settings in self._stores.values()
+ if settings.mode == "shared"
+ )
+
def setup_store(self) -> None:
</code_context>
<issue_to_address>
**Unused stores trigger warning**
When a gateway loads a plugin with no declared `data_stored` whose store is enabled and the database backend is shared, `_key_secret_set()` checks only whether a plugin's store is in shared mode, so a loaded plugin that declares no stored data still triggers the startup warning even though it does not keep entries in the database; operators see a misleading warning to configure a secret.
Apply the warning check only to shared stores of plugins that declare stored data.
</issue_to_address>Linked issues
#565 feat(plugins): key plugin store hashes with a secret · 5 of 5 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Add the optional store key secret, HMAC keys with an hk: prefix, plain SHA-256 fallback, and a startup warning when database-backed entries have no secret. |
✅ Met | The secret resolves from configuration or the environment; the warning is emitted for shared stores without a secret. |
| 2 | Keep the secret out of logs, the plugin listing, and error messages. | ✅ Met | The secret uses SecretStr; configuration validation hides inputs, and the plugin listing exposes plugin metadata rather than admin settings. |
| 3 | Test both key modes, prefixes, the warning, and secret rotation. | ✅ Met | Tests cover key generation, warning conditions, and entries becoming unreachable across secret changes. |
| 4 | Document configuration, plugin-author use of key_for(), operator secret handling, rotation and replicas, and upgrade notes. |
✅ Met | The configuration reference, plugin author guide, deployment guide, and 0.20.0 migration guide cover these topics. |
| 5 | Add the Helm library chart secret reference and documentation. | ✅ Met | plugins.existingSecret passes the existing Secret to the app and worker; overlay secrets are rejected and the chart docs describe setup. |
Sourcery assessment
Needs a human reviewer. With a configured secret, key_for() writes entries under HMAC-derived keys instead of the previous SHA-256 keys; inconsistent secrets or a faulty derivation can make stored entries unreachable and reset plugin state such as per-user budgets. The entries remain in the store and are bounded by max_ttl, so reverting or restoring the matching secret can recover access.
Warn about a missing key secret only if plugins that declare stored data (data_stored) keep it in the database, like the warning about stores in memory (Sourcery review). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG
Summary
Implements the maintainer's decision in #565 (optional HMAC secret):
plugins.defaults.store.key_secret(SecretStr; applies to all plugins); if unset or empty, the environment variableFOCA_PLUGINS_STORE_KEY_SECRETis used, so it can come from an env var or a mounted secret and is never committed.key_for()returns HMAC-SHA256 keys prefixedhk:; without, plain SHA-256h:as before.repr()of config or store settings, logs, dumps, the plugin listing or validation error messages: the top-levelConfigand the plugin config models now sethide_input_in_errors, so config validation errors no longer showinput_value(noted in the migration guide).plugins.existingSecret, the name of an existing Secret whose keystore-key-secretthe app and the worker get asFOCA_PLUGINS_STORE_KEY_SECRET(same pattern asbroker.existingSecret: fixed kebab-case key, no value inlined). Rendering fails if the config overlay (a ConfigMap) sets a non-emptyconfig.plugins.defaults.store.key_secret; the error never shows the value. Schema (library and petstore copy), petstore values, dependency andChart.lockupdated. The chart version is bumped in this PR because CI requires it for every chart change sincechart-v0.3.0(as feat(chart): add app replicas and a PDB #585 did for 0.3.0); no tag.Closes #565
Verification
coverage run -m pytest(full suite, local MongoDB 7.0 on port 12345): 3293 passed, 1 skipped; coverage 100% (rerun after every fix)pytest charts/foca-common/testswith Helm 3.22.1 (CI's version): 248 passed; snapshots unchangedhelm lint --strictof the library chart and the petstore chart with allci/*-values.yaml: cleanruff check,ruff format --check,mypy,ast-grep scan/ast-grep test,semgrep scan --config .semgrep/semgrep --test,mkdocs build --strict: cleanCI passed, CodeQL, PR title, Chart, Chart install, tests, integration tests, image scans, Codecov, Read the DocsReview
Regular lane, two rounds.
input_valuein config validation errors (model-level check failing elsewhere inplugins): fixed withhide_input_in_errorsplus regression test. Low, blankplugins.existingSecret: declined (below).dig, rewrapped docs.errors()/json()still carry input): documented in the_Modeldocstring.data_storedplugins count, as for the memory warning), with test and docs. Small logic fix after the two reviewer rounds (the maximum), covered by the tests.Declined review findings
plugins.existingSecretrenders and is rejected by the API server at apply time: same behaviour asbroker.existingSecretandmongodb.auth.existingSecret; kept consistent rather than special-casing one value.ValidationError.errors()/.json()still contain the raw input (pydantic keeps it there regardless ofhide_input_in_errors); FOCA never logs or serialises them for config errors. Documented in the_Modeldocstring.engine.pyinto a helper: readability only, behaviour covered by tests.plugins.existingSecretinci/gateway-values.yaml: the kind install would need an extra Secret; template tests cover the rendering.Open questions
None.
🤖 Generated with Claude Code
https://claude.ai/code/session_011FSoqMUX3EU35poH13xMgG