Repository navigation
fix(sbx): confirm step 7's cleanup instead of believing it - #669
Conversation
deploy/sbx/smoke.sh's step-7 (egress credentials) cleanup reported success for three things it had not actually done. Each is the one case the cleanup exists to catch, so a leak was reported as cleaned up. drop_branch accepted 404 and 422 as "the branch is gone". GitHub sends "Reference does not exist" as *both* (cli/cli#11187), 422 is also its answer for the default branch and for a spammed endpoint, and a PAT that can no longer reach the repository gets 404 for every ref. Now anything but a 204 is confirmed: the ref reads as absent AND the repository still answers with this PAT. The trap deleted every recorded credential name. cred_add records a name *before* the add -- deliberately, so an add whose answer was lost is still cleaned up -- but that also records names no add ever stored. Deleting those printed a cleanup failure for a credential that never existed and masked the real error. The record-first contract stays; the trap now deletes only the recorded names `credentials list` still shows (stored_creds). The credential names were fixed (smoke-gh, smoke-gh-mcp) while the sandboxes and the branch were already namespaced per run. Two runs as the same user -- a retry over a still-running one, two CI jobs -- shared one credential and each deleted the other's mid-check. Both names now carry $RUN and fit mocactl's 40-character CREDENTIAL_NAME. Six new assertions in deploy/sbx/tests/smoke.test.sh, each verified to fail without the corresponding fix. The fakes gained MOCK_BRANCH_DELETE_CODE, MOCK_BRANCH_GONE, MOCK_REPO_GONE, MOCK_CRED_ADD_LOST and MOCK_SBX_DELBRANCH_FAIL (drop_branch is only reachable when the in-sandbox delete fails), and `credentials list --json`. Part of rossoctl#653 (the last unchecked item under "Test gaps that hide real checks"). 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.
The branch confirmation and per-run credential names look right. Names fit CREDENTIAL_NAME (at most 31 characters), the credentials --json shape matches cmdCredentials, the branch probe fails safe, and each fix has a test.
One must-fix: the new stored_creds fails open. If listing fails at cleanup, a stored PAT credential is left with no message and rc 0, which is the "leak reported as cleaned up" case this PR sets out to remove. One suggestion on the ref-probe endpoint.
| sys.exit(0) | ||
| for c in doc.get("credentials") or []: | ||
| if c.get("name"): | ||
| print(c["name"])' 2>>"$LOGS/cleanup.log")" || listed='' |
There was a problem hiding this comment.
must-fix: stored_creds fails open. If as a credentials --json fails at cleanup time (connection refused, session expired, a proxy's HTML error page), the pipeline's non-zero exit sets listed=''. The except ValueError: sys.exit(0) path does the same. Then no recorded name matches, left_cred never runs, and the trap prints nothing and keeps rc at 0. A credential that was stored (and holds the PAT) is left on the service, and the run gives no sign of it. That's the same class as the 404/422 bug this PR fixes. Before this PR, the trap at least tried the delete and printed could not delete credential ….
Proposed fix: tell "listed, and not there" apart from "could not list". When the listing fails or isn't JSON, fall back to deleting every name in $CRED_NAMES, as before. A spurious "could not delete" is the safe side here. Or print # cleanup: could not list credentials of user A; check <names> by hand and set rc=1. A mock knob such as MOCK_CRED_LIST_FAIL=1, combined with MOCK_CRED_ADD_LOST=1, would pin this case.
There was a problem hiding this comment.
Fixed in ef39395.
stored_creds now tells "listed, and not there" from "could not list". The listing's failure (and the non-JSON path, now sys.exit(1) rather than 0) returns 1 after printing every recorded name, so the caller falls back to trying all of $CRED_NAMES, as before this PR. The trap also prints # cleanup: could not list user A's credentials; trying every recorded name (<names>).
One deliberate difference from your second option: the listing failure alone does not force rc=1. If the fallback's deletes all succeed, nothing leaked, so rc 0 is the honest answer; rc goes to 1 as soon as any of those deletes fails. The run still says it could not list either way. Happy to make the listing failure itself fatal if you'd rather have the harder signal.
Added the mock knob you suggested (MOCK_CRED_LIST_FAIL=1) and two assertions in deploy/sbx/tests/smoke.test.sh:
MOCK_CRED_ADD_LOST=1 MOCK_CRED_LIST_FAIL=1— exactly your case: the credential is on the service, the listing is broken. Asserts exit 1, the could-not-list message, the credential is still deleted, and none is left live.MOCK_FAIL=add MOCK_CRED_LIST_FAIL=1— the safe side: with the listing broken the trap cannot rule out a name no add ever stored, so it tries the delete and reports the failure. Reached only when listing failed, so the "no spurious delete failure for a refused add" assertion still holds.
I verified this by reverting only the stored_creds change (keeping the ref-path change) and re-running: FAIL: a failed listing was not reported.
| local code ref | ||
| code="$(pat_curl -X DELETE -o /dev/null -w '%{http_code}' "https://api.github.com/repos/$CRED_REPO/git/refs/heads/$BRANCH" 2>>"$LOGS/cleanup.log")" || code='' | ||
| if [[ "$code" != 204 ]]; then | ||
| ref="$(api_code "https://api.github.com/repos/$CRED_REPO/git/refs/heads/$BRANCH")" |
There was a problem hiding this comment.
suggestion: GET /repos/{o}/{r}/git/refs/heads/<branch> is the legacy plural form. When no ref matches exactly, it can return 200 with an array of refs that start with that name. Exact-match "get a reference" is the singular GET /repos/{o}/{r}/git/ref/heads/<branch>, which returns 404 when the ref is absent. With sbx-smoke-<ts> names a prefix collision is unlikely, and the result fails safe (a false "could not delete"). Still, the singular path makes the probe mean exactly "this ref". The mock's */git/refs/heads/* pattern would need the same change.
There was a problem hiding this comment.
Taken, in ef39395. The probe is now the singular exact-match get:
ref="$(api_code "https://api.github.com/repos/$CRED_REPO/git/ref/heads/$BRANCH")"
The DELETE stays on the documented plural git/refs/heads/<branch>. The mock's pattern moved to */git/ref/heads/* to match, and the DELETE branch there is reached before the case on the URL, so it is unaffected.
Agreed the prefix collision is unlikely with sbx-smoke-<ts> names and that it failed safe — but the probe is the thing that decides whether to trust a 404, so it should mean exactly that ref rather than "a ref starting with it".
Verified the direction: with the plural path restored the suite fails at FAIL: a 404 whose ref is confirmed absent was not accepted as deleted.
Review of rossoctl#669: stored_creds failed open. If `credentials --json` itself failed at cleanup time (a refused connection, an expired session, a proxy's error page), pipefail set listed='' and the except-ValueError path did the same, so no recorded name matched, the trap printed nothing and kept rc 0 -- a stored PAT credential left on the service with no sign of it. The same leak-reported-as-cleaned-up class the PR sets out to remove. stored_creds now tells "listed, and not there" from "could not list": the listing's failure (or its not being JSON) exits 1, and the caller falls back to trying every recorded name, as before this PR. A spurious "could not delete" is the safe side, and the run says it could not list. The ref probe also moved to the singular git/ref/heads/<branch>, the exact-match get; the plural git/refs form can answer 200 with refs that merely start with the name. The DELETE stays on the documented plural form. The mock curl's pattern follows. Two new assertions in deploy/sbx/tests/smoke.test.sh (MOCK_CRED_LIST_FAIL, combined with MOCK_CRED_ADD_LOST and with MOCK_FAIL=add), each verified to fail without the fix. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: cwiklik <[email protected]>
|
@ingpaolodettori-dev ready for another look — both items are addressed in Must-fix: One deliberate difference from a strict reading of the request, so it's visible rather than buried: a failed listing does not by itself force Suggestion: ref-probe endpoint. The probe moved to the singular Tests. Added a |
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Follow-up at ef39395: both 85aac6a items are fixed, and I verified them against the code.
stored_credsno longer fails open. Withpipefailon in the trap, a failed or non-JSONcredentials --jsonreturns 1 after echoing every recorded name, so the trap reports that it could not list and still tries every delete. Any failure sets rc=1. Leaving rc at 0 when every fallback delete succeeds is fine with me. The two newMOCK_CRED_LIST_FAILtests pin both sides.- The ref probe now uses the exact-match singular
git/ref/heads/<branch>, and the mock was updated to match.
CI is green. Approving.
Assisted-By: Claude Code
What
The last unchecked item under #653's "Test gaps that hide real checks (do first)": step 7's
(egress credentials) deferred cleanup follow-ups in
deploy/sbx/smoke.sh.Three things the step-7 cleanup reported as done but had not done. Each is the case the cleanup
exists to catch, so a leak was reported as cleaned up.
1.
drop_branchbelieved GitHub's ambiguous "gone" codesIt accepted
204 | 404 | 422as success. But GitHub sendsReference does not existas both422 and 404 (cli/cli#11187), 422 is also its answer for the default branch and for a spammed
endpoint, and a PAT that can no longer reach the repository gets 404 for every ref. So a
revoked PAT left the branch and the smoke said it had deleted it.
Tightening the accepted-code list cannot fix this — the codes are genuinely ambiguous. Anything
but a 204 is now confirmed: the ref reads as absent and the repository still answers with
this PAT (the second probe is what separates "gone" from "I can't see it").
2. The trap deleted credential names no add had ever stored
cred_addrecords a name before the add. That is deliberate: an add whose answer was lost didstore the credential server-side, so it must still be cleaned up. But it also records names no add
ever stored — a refused add, a connection that never opened. Deleting those printed a cleanup
failure for a credential that never existed, and the run exited on that instead of on the real
error.
The record-first contract stays. The trap now deletes only the recorded names that
credentials liststill shows (newstored_creds).3. The credential names were shared between runs
The sandboxes and the branch were already namespaced per run; the credentials were not
(
smoke-gh,smoke-gh-mcp). Credential names are a shared namespace per user, so two runs as thesame user — a retry over a still-running one, two CI jobs — stored one credential and each deleted
the other's mid-check. Both names now carry
$RUNand fit mocactl's 40-characterCREDENTIAL_NAME.Tests
Six new assertions in
deploy/sbx/tests/smoke.test.sh, each verified to fail without its fix(
smoke.shreverted to HEAD, tests kept: 15 failures, covering all six plus the existingname-dependent ones).
New fake knobs:
MOCK_BRANCH_DELETE_CODE,MOCK_BRANCH_GONE,MOCK_REPO_GONE,MOCK_CRED_ADD_LOST,MOCK_SBX_DELBRANCH_FAIL, andcredentials list --jsonin the mockmocactl.MOCK_SBX_DELBRANCH_FAILis worth calling out:drop_branchis only reachable when thein-sandbox branch delete fails. On a clean step 7 the sandbox deletes its own branch and clears
BRANCH, so the trap has nothing to do — without that knob the branch tests set their codes andnever reached the code under test.
The existing fakes keyed on the literal name
cred-smoke-gh;have(), the fakeghand the fakegit pushnow match the preset suffix, so they test the production logic rather than a hardcodedname.
Verification
bash deploy/sbx/tests/smoke.test.shsmoke.sh)shellcheck -x -S warning(CI severity) on both filesmake test-deploy-bash32make test-deploymake test-deploydoes fail ondeploy/microvm/tests/build-snapshot.test.sh. That ispre-existing and unrelated — it exits 2 identically with these changes stashed, and it is two
SC2016 infos in
deploy/microvm/build-snapshot.sh, which this PR does not touch.Docs
deploy/sbx/RUNBOOK.md§step-7 andsmoke.sh's own header both named the old fixed credentials;updated to the per-run form.
Part of #653.
Assisted-By: Claude Code