Skip to content

fix(vm): remove the minted subject's record from the credential store (#418) - #671

Merged
cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/418-research-smoke-subject-record
Nov 2, 2026
Merged

cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/418-research-smoke-subject-record

Conversation

@cwiklik

@cwiklik cwiklik commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #418

The leak

research-smoke.sh deletes its credential on exit, but that does not remove the subject. The file credential store's delete rewrites 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:

  • reads SH_CREDENTIAL_DIR from control-plane.env — no line, or a vault store, means there is nothing here to remove
  • recomputes the store's own name for the subject: sha256(subject)[:16], per packages/control-plane/src/subject-document.ts
  • removes that one file

Deliberately narrow:

Guard Why
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 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 curl now mirrors the real store: PUT writes the record, DELETE empties it without unlinking. A MOCK_CRED_DELETE_NOOP=1 knob simulates a delete that does not land, and a PLANT=<file> argument drops another subject's record next to this run's.

12 new assertions (75 ok total, was 63):

  • a passing run leaves no record
  • a failing run leaves none
  • the operator-fallback path leaves none, and warns about nothing
  • another subject's record survives both a fallback run and a storing run — and is still that subject's afterwards
  • KEEP=1 keeps the record, deletes no credential, and warns about nothing
  • no SH_CREDENTIAL_DIR configured: exit 0, warns about nothing
  • a record that still holds a credential is kept, and the run says still holds a credential

Six 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

Gate Result
deploy/vm/tests/research-smoke.test.sh 75 ok, 0 FAIL
Same with MOCA_REQUIRE_WORKSPACE=1 (CI mode) 75 ok, exit 0
deploy/vm/tests/setup-vm.test.sh (sibling) 101 ok, exit 0
shellcheck -x -S warning over deploy/ scripts/ 0 findings
prettier --check deploy/vm/README.md clean

make test-deploy reports 2 failures in deploy/microvm/tests/build-snapshot.test.sh. Both reproduce on main with this change stashed — pre-existing and unrelated; worth its own issue.

make test-deploy-bash32 creates no new obligation here: its explicit list excludes research-smoke.test.sh, which the Makefile comment notes uses bash 4 constructs.

Assisted-By: Claude

…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]>
@cwiklik
cwiklik requested a review from pdettori as a code owner November 2, 2026 14:49
@cwiklik cwiklik added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 2, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-)"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

@cwiklik
cwiklik merged commit d1a5b0a into rossoctl:main Nov 2, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

deploy/vm: research-smoke.sh leaves its minted subject's empty record in the credential store

2 participants