Skip to content

fix(secrets): five follow-ups of the secret store review (#16) - #43

Open
Gordex2014 wants to merge 3 commits into
HeliosSoftware:mainfrom
Gordex2014:fix/secret-store-follow-ups
Open

Gordex2014 wants to merge 3 commits into
HeliosSoftware:mainfrom
Gordex2014:fix/secret-store-follow-ups

Conversation

@Gordex2014

@Gordex2014 Gordex2014 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #18: the five follow-ups from the review of #16.

What changes

Item 1: a credential registration holds the store's connection for 2 s at most, not 10 s.

  • secret_tx (a source's registration for its credential, and a credential's delete) caps SQLite's wait for another connection's write lock at 1 s per attempt, or the connection's own wait when that is shorter. retry_busy still tries twice and still answers Busy.
  • The cap is set and the connection's own wait given back under the store's mutex, after COMMIT or ROLLBACK, on every path, so no other caller ever runs with the shorter wait.
  • PostgreSQL is unchanged. Releasing registrations and storing or replacing a credential are single statements no runner pass makes; they keep the connection's own wait (stated in the amendment).
  • Not spawn_blocking: it would free the tokio worker but leave the mutex held, and could let a registration finish after an aborted runner's hand-over.

Item 2: a definition lookup no longer parses every file, and a stray file no longer keeps every registration.

  • find_source reads each file's id with the same decoding a definition uses (id: 123 is "123"), once per content: a cache keyed by path and a hash of the bytes. Only files with the wanted id are parsed as a definition and resolved. A hand edit is seen at the next lookup.
  • The cleanup of a deleted or replaced source's registrations passes over a file with another id, and a file with no id a definition could have (not UTF-8, not YAML, no text id), with a warn naming it once per content. config::load refuses to start with such a file, so no start could run it as the source. A file with the source's id that does not parse still keeps the registrations, as before.
  • A transform save stays strict: any file it cannot read fails the save, as before, and now before any source restarts (the new code is already written, as before).

Item 3: listing the credentials is one statement. GET /api/secrets reads every credential with its registered sources in one LEFT JOIN, deduplicated across instances, names sorted in Rust as before (so both stores order them the same).

Item 4: a source's lock leaves the manager's map once nothing holds or waits for it. source_lock returns a handle; the last handle to drop takes the entry out. Handles are made and dropped only under the map's mutex, so the count is exact and two callers never get two locks for one source.

Item 5: the definition alert of a skipped file clears once its file is no longer skipped.

  • alerts gains raised_by, added in place on both stores (nullable). A skipped file's alert records the instance that raised it: its stable id, HIE_INSTANCE_ID or sources/.hie-instance-id. The helper that keeps that id now builds without a secret backend.
  • One alert per source and instance. At start, an instance clears its own open alerts of the sources it no longer skips (the file fixed, removed, or loaded by a build with a store), with the reason <id>: its definition is no longer skipped and one webhook cleared. Another instance's alerts are left to it.
  • An alert an earlier version raised names no instance, so no start clears it: the instance it describes may still skip the file. Retention now purges it by age, as other open alerts without a rule. An instance that still skips the file raises its own.
  • An instance without a stable id (no HIE_INSTANCE_ID, a sources directory it cannot write) behaves as before: one alert naming no instance per source, raised only when none is open.
flowchart TD
    S[start of an instance] --> K{skips a file of source X?}
    K -- yes --> O{its own open alert for X?}
    O -- no --> R[raise one, raised_by = this instance]
    O -- yes --> N[nothing]
    K -- no --> C{its own open alert for X?}
    C -- yes --> CL[clear it: one cleared event]
    C -- no --> N
    L[alert with raised_by null: an earlier version's] --> P[left open by every start; purged by age]
Loading

Docs: an amendment to decision 37 (docs/decisions.md); docs/running.md (the skipped file's alert, HIE_INSTANCE_ID); docs/api.md (which open alerts retention keeps).

In plain words

Storing a credential's use no longer freezes the engine for up to ten seconds when the database is busy. Deleting a source frees its credential even when an unrelated file sits in the sources folder. Listing credentials and tidying up after deleted sources do less work. And the alert that says "this file was skipped" goes away once the file is fixed or removed, without one engine clearing another engine's alert.

Side effects checked

  • Callers. secret_tx: secret_hold (the runner's ensure_credential, the manager's create and replace) and secret_delete_unless_held (DELETE /api/secrets/{id}). find_source: only the manager (cleanup_refs, lenient; save_transform, strict). NativeStore::list: GET /api/secrets; find and reveal keep their per-secret query. source_lock: every change of a source and the cleanup task. insert_alert_with_webhook: the alerter (and two tests).
  • API. No route, body or event changes. GET /api/alerts does not show raised_by. A definition alert now gets a cleared webhook event when it clears, the documented shape. An API client that lists credentials sees the same JSON in the same order.
  • Upgrade. alerts.raised_by is added in place (ADD COLUMN) on SQLite and PostgreSQL. Open definition alerts from before name no instance: they stay open until retention purges them by age (retention.history, 30 days by default), where before they stayed for good.
  • Several instances and an earlier version on one store. Two instances that skip one source each keep their own alert, and only the one that stops skipping clears its own. An earlier version raises none for a source while another instance's alert for it is open, so that source shows none from when that alert clears to the earlier version's next start (stated in the amendment). Registrations are untouched: secret_hold still locks the secret's row first.
  • Without a secret backend. getrandom is no longer optional (the instance id needs it); cargo clippy and the tests run without default features too. Such a build now makes sources/.hie-instance-id when it skips a file, or finds open an alert some instance raised.
  • Not changed. The PostgreSQL lock waits; the waits of single-statement credential writes; config::load (a start still refuses any file it cannot parse).
  • Public library types. config::find_source takes the id cache and a strict flag; config::SourceIds is new; Db::insert_alert_with_webhook takes raised_by; Db::secret_rows_with_users, Db::open_rule_less_alerts and Db::open_rule_less_alerts_of are new.

How to verify

Before and after: PR #43 fix(secrets): five follow-ups of the secret store review (#16)

Temporary review evidence expires 2026-11-30 22:35 UTC.

Each item has a test that fails on the base and passes on the head (the same test, inputs and command on both):

Item Test Base (3a58bfe) Head
1 cargo test --lib a_registration_on_a_locked_sqlite_store_holds_the_database_briefly Busy after 10.04 s Busy after 2.01 s; the 5 s wait is back
2 cargo test --lib a_stray_file_keeps_registrations_only_when_it_could_be_the_definition registration kept with a stray id: other file released; kept only for a file with the source's id
3 cargo test --lib listing_credentials_reads_the_store_once 4 statements for 3 credentials 1 statement
4 cargo test --lib a_deleted_source_leaves_no_lock_behind the entry stays after 5 s removed
5 cargo test --no-default-features --test no_backend_startup the alert stays open after the file is fixed cleared, one cleared event

Also new: a_source_is_found_reading_each_unchanged_file_once (ids read once per content, id: 123, edits seen, not UTF-8), a_transform_save_restarts_nothing_while_a_definition_cannot_be_read (fails without the strict lookup: the save answered Ok), overlapping_changes_of_a_source_leave_no_lock_behind, and in tests/no_backend_startup.rs the still-skipped, two-instances, another-instance-and-earlier-version and no-stable-id cases; tests/events.rs retention now keeps an instance's definition alert and purges an unowned one.

Run on the head, with CI=true and HIE_TEST_PYTHON=python3:

Command Result
cargo fmt --all -- --check pass
cargo clippy --all-targets -- -D warnings pass
cargo clippy --all-targets --no-default-features -- -D warnings pass
python3 -m unittest tests/test_lab_results.py tests/test_hl7.py tests/test_terminology.py tests/test_worker.py pass
cargo test pass
cargo test --no-default-features --lib --test secrets --test no_backend_startup --test check --test s3_credentials pass
git diff --check origin/main...HEAD pass
cargo test --lib and --test secrets, secret_refs, secret_store, alerts, alert_webhook, events, twice more on SQLite pass
the same twice on PostgreSQL 16 (HIE_TEST_DATABASE_URL=postgres://...) pass, except two tests that fail on the base too (below)
docker build -t hie:local ., docker run --rm hie:local check, docker run --rm --entrypoint python3 hie:local --version pass (built locally: the image job is skipped for a pull request from a fork)

Stores. The new unit tests of items 2 to 4 and the changed suites ran on SQLite and PostgreSQL. Item 1's test is SQLite's (the busy wait is SQLite's); its PostgreSQL row-lock branch (a_lock_wait_that_runs_out_twice_is_busy_not_a_store_error) passes. Item 5's restart tests start engines that must keep their store across restarts, so they run on SQLite even under HIE_TEST_DATABASE_URL; on PostgreSQL the raised_by column, its migration and the retention rule are covered by tests/events.rs and tests/alert_webhook.rs.

Known, not from this change. On PostgreSQL, secrets::key::tests::a_key_file_removed_after_start_asks_for_a_restart and secrets::key::tests::a_socket_at_the_key_path_is_refused fail on the base 3a58bfe as well (2 of 2 runs there, 2 of 2 on the head).

Links

Closes #18
PR #16 (the review these follow up)

- A credential registration or delete waits at most 1 s per attempt on
  SQLite for another connection's write lock, instead of the 5 s busy
  timeout twice, while it holds the store's only connection. The
  connection gets its own wait back before the lock goes.
- Listing the credentials reads them with their sources in one statement.
- A lookup of a saved definition parses only the files with the source's
  id, and reads each other file's id once per content. A YAML file with
  another id no longer keeps every registration of a deleted source; a
  file that is not YAML still does. A transform save looks up every
  definition it restarts before restarting any.
- The manager's lock of a source leaves its map once nothing holds or
  waits for it.
- The definition alert of a skipped source file records the instance that
  raised it, and that instance's first start that skips no file of the
  source clears it. Another instance on the store leaves it open; one an
  earlier version raised is taken by an instance that still skips a file
  of the source. The instance id no longer needs a secret backend.
- Two instances that skip a file of one source each raise an alert of
  their own; the one that stops skipping it clears only its own. An
  alert an earlier version raised is taken only by an instance with no
  alert of its own for the source.
- A source file that is not YAML is passed over by a definition lookup,
  with a warning, as no start could run it: it no longer keeps the
  registrations of a deleted source.
- The id cache drops gone files through a set, not a list.
- The restart tests run their engines on SQLite and without the
  developer's HIE_INSTANCE_ID; docs say when the instance id file is
  made and which credential writes keep the connection's own wait.
- An instance no longer takes a definition alert that names no
  instance: the earlier version that raised it may still skip the file.
  It raises its own, and retention purges the unowned one by age, as
  other open alerts without a rule.
- An instance without a stable id raises such an alert only for a
  source with none open, naming no instance, as earlier versions did,
  instead of one more at every start.
- A definition lookup reads files as bytes: a file that is not UTF-8 is
  passed over by the cleanup like one that is not YAML.
- A transform save still fails on any source file it cannot read, and
  restarts nothing then.
@Gordex2014
Gordex2014 marked this pull request as ready for review November 25, 2026 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Secret store follow-ups from the #16 review

1 participant