Repository navigation
fix(vm): remove the minted subject's record from the credential store (#418) - #671
Conversation
…rossoctl#418) research-smoke.sh deletes its credential on exit, but the file credential store's `delete` rewrites the subject's document without that credential and never unlinks it. The empty record survives -- one file per run, forever, each naming a subject that will never be used again. Add drop_record() to the exit cleanup. It reads SH_CREDENTIAL_DIR from control-plane.env (no line, or a vault store, means nothing to remove), recomputes the store's own name for the subject -- sha256(subject)[:16], per packages/control-plane/src/subject-document.ts -- and removes that one file. It is deliberately narrow: - Only this run's subject. The filename is the hash of a subject this run minted; no other subject hashes to it. - Only an empty record. If the file still holds a credential, the delete above did not land; removing it would destroy the only evidence. It is kept and reported instead. - Only when KEEP is unset. drop_record runs after the KEEP=1 early return, so `KEEP=1` leaves the record alongside the credential, session and files. deploy/microvm/P4-ON-P6.md's smoke does the same thing by hand today. A control-plane route that deleted an empty subject would let both stop reaching into the store; until there is one, both do it this way. Tests: the mock curl now mirrors the store (PUT writes the record, DELETE empties it without unlinking), and 12 assertions cover a passing run, a failing run, the operator fallback, KEEP=1, no store configured, a record that still holds a credential, and -- with another subject's record planted next to it -- that the cleanup never touches a record that is not this run's. Fixes rossoctl#418 Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: cwiklik <[email protected]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Summary
Adds drop_record() to research-smoke.sh's exit cleanup. After the credential DELETE, it removes this run's <sha256(subject)[:16]>.json from the file credential store, but only if the document holds no credentials. That closes the one-record-per-run leak from #418. Verified against main 3ae3378: FileCredentialStore.delete rewrites via withoutCredential and never unlinks (file-store.ts:113-119), and subjectHash is sha256 hex [:16] (subject-document.ts:20). The guards are right: the cleanup touches only this run's subject, keeps a record that still holds a credential and warns, and does nothing under KEEP=1. The tests cover each path, including a planted foreign record. One suggestion inline (quoted SH_CREDENTIAL_DIR).
Author: cwiklik (MEMBER — maintainer)
Areas reviewed: Shell, Docs, tests
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing
| # reaching into the store; until then both do it by hand. | ||
| drop_record() { | ||
| local dir hash f | ||
| dir="$( (grep -oE '^SH_CREDENTIAL_DIR=.*' "$SH_ENV_DIR/control-plane.env" 2>/dev/null || true) | tail -1 | cut -d= -f2-)" |
There was a problem hiding this comment.
suggestion: SH_CREDENTIAL_DIR is read here without unquoting. setup-vm.sh's env_file_value (setup-vm.sh:626) strips surrounding "…"/'…', and systemd's EnvironmentFile= does too. So with a hand-edited SH_CREDENTIAL_DIR="/var/lib/moca-control-plane", the control plane uses the real directory, but here -d fails and drop_record returns 0 silently: the #418 leak comes back with no warning. Could you mirror the same unquote (or share the helper)?
Fixes #418
The leak
research-smoke.shdeletes its credential on exit, but that does not remove the subject. The file credential store'sdeleterewrites the subject's document without that credential and never unlinks it (packages/control-plane/src/file-store.ts), so an empty record survives — one file per run, forever, each naming a subject that will never be used again.The fix
drop_record(), called from the exit cleanup after the credential delete:SH_CREDENTIAL_DIRfromcontrol-plane.env— no line, or a vault store, means there is nothing here to removesha256(subject)[:16], perpackages/control-plane/src/subject-document.tsDeliberately narrow:
KEEPis unsetdrop_recordruns after theKEEP=1early return, soKEEP=1leaves the record alongside the credential, session and files.deploy/microvm/P4-ON-P6.md's smoke already does this by hand (P4-ON-P6.md:472). A control-plane route that deleted an empty subject would let both stop reaching into the store directly; until there is one, both do it this way. Noted in a comment on the function.Tests
The mock
curlnow mirrors the real store:PUTwrites the record,DELETEempties it without unlinking. AMOCK_CRED_DELETE_NOOP=1knob simulates a delete that does not land, and aPLANT=<file>argument drops another subject's record next to this run's.12 new assertions (75 ok total, was 63):
KEEP=1keeps the record, deletes no credential, and warns about nothingSH_CREDENTIAL_DIRconfigured: exit 0, warns about nothingstill holds a credentialSix mutants were applied to the fix; all are caught. Two assertions were strengthened as a result, and one guard line that no mutant could kill was found to be unreachable and removed rather than shipped untested.
Verification
deploy/vm/tests/research-smoke.test.shMOCA_REQUIRE_WORKSPACE=1(CI mode)deploy/vm/tests/setup-vm.test.sh(sibling)shellcheck -x -S warningoverdeploy/ scripts/prettier --check deploy/vm/README.mdmake test-deployreports 2 failures indeploy/microvm/tests/build-snapshot.test.sh. Both reproduce onmainwith this change stashed — pre-existing and unrelated; worth its own issue.make test-deploy-bash32creates no new obligation here: its explicit list excludesresearch-smoke.test.sh, which the Makefile comment notes uses bash 4 constructs.Assisted-By: Claude