Repository navigation
fix(secrets): five follow-ups of the secret store review (#16) - #43
Open
Gordex2014 wants to merge 3 commits into
Open
Gordex2014 wants to merge 3 commits into
Gordex2014 wants to merge 3 commits into
Conversation
- 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
marked this pull request as ready for review
November 25, 2026 22:35
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.
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_busystill tries twice and still answersBusy.COMMITorROLLBACK, on every path, so no other caller ever runs with the shorter wait.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_sourcereads each file'sidwith the same decoding a definition uses (id: 123is"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.id), with awarnnaming it once per content.config::loadrefuses 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.Item 3: listing the credentials is one statement.
GET /api/secretsreads every credential with its registered sources in oneLEFT 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_lockreturns 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
definitionalert of a skipped file clears once its file is no longer skipped.alertsgainsraised_by, added in place on both stores (nullable). A skipped file's alert records the instance that raised it: its stable id,HIE_INSTANCE_IDorsources/.hie-instance-id. The helper that keeps that id now builds without a secret backend.<id>: its definition is no longer skippedand one webhookcleared. Another instance's alerts are left to it.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]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
secret_tx:secret_hold(the runner'sensure_credential, the manager's create and replace) andsecret_delete_unless_held(DELETE /api/secrets/{id}).find_source: only the manager (cleanup_refs, lenient;save_transform, strict).NativeStore::list:GET /api/secrets;findandrevealkeep their per-secret query.source_lock: every change of a source and the cleanup task.insert_alert_with_webhook: the alerter (and two tests).GET /api/alertsdoes not showraised_by. Adefinitionalert now gets aclearedwebhook event when it clears, the documented shape. An API client that lists credentials sees the same JSON in the same order.alerts.raised_byis added in place (ADD COLUMN) on SQLite and PostgreSQL. Opendefinitionalerts 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.secret_holdstill locks the secret's row first.getrandomis no longer optional (the instance id needs it);cargo clippyand the tests run without default features too. Such a build now makessources/.hie-instance-idwhen it skips a file, or finds open an alert some instance raised.config::load(a start still refuses any file it cannot parse).config::find_sourcetakes the id cache and astrictflag;config::SourceIdsis new;Db::insert_alert_with_webhooktakesraised_by;Db::secret_rows_with_users,Db::open_rule_less_alertsandDb::open_rule_less_alerts_ofare 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):
3a58bfe)cargo test --lib a_registration_on_a_locked_sqlite_store_holds_the_database_brieflyBusyafter 10.04 sBusyafter 2.01 s; the 5 s wait is backcargo test --lib a_stray_file_keeps_registrations_only_when_it_could_be_the_definitionid: otherfilecargo test --lib listing_credentials_reads_the_store_oncecargo test --lib a_deleted_source_leaves_no_lock_behindcargo test --no-default-features --test no_backend_startupclearedeventAlso 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 answeredOk),overlapping_changes_of_a_source_leave_no_lock_behind, and intests/no_backend_startup.rsthe still-skipped, two-instances, another-instance-and-earlier-version and no-stable-id cases;tests/events.rsretention now keeps an instance'sdefinitionalert and purges an unowned one.Run on the head, with
CI=trueandHIE_TEST_PYTHON=python3:cargo fmt --all -- --checkcargo clippy --all-targets -- -D warningscargo clippy --all-targets --no-default-features -- -D warningspython3 -m unittest tests/test_lab_results.py tests/test_hl7.py tests/test_terminology.py tests/test_worker.pycargo testcargo test --no-default-features --lib --test secrets --test no_backend_startup --test check --test s3_credentialsgit diff --check origin/main...HEADcargo test --liband--test secrets,secret_refs,secret_store,alerts,alert_webhook,events, twice more on SQLiteHIE_TEST_DATABASE_URL=postgres://...)docker build -t hie:local .,docker run --rm hie:local check,docker run --rm --entrypoint python3 hie:local --versionStores. 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 underHIE_TEST_DATABASE_URL; on PostgreSQL theraised_bycolumn, its migration and the retention rule are covered bytests/events.rsandtests/alert_webhook.rs.Known, not from this change. On PostgreSQL,
secrets::key::tests::a_key_file_removed_after_start_asks_for_a_restartandsecrets::key::tests::a_socket_at_the_key_path_is_refusedfail on the base3a58bfeas well (2 of 2 runs there, 2 of 2 on the head).Links
Closes #18
PR #16 (the review these follow up)