Repository navigation
feat: write a credential stub at minting and discover installations by JWT - #88
Conversation
There was a problem hiding this comment.
Independent review (diff read before description; task #83 plan, finding 11 on #73 checked; head extracted clean, go test ./... green, and the script's failure paths exercised empirically with a throwaway key, a sandbox HOME, and a PATH-shimmed gh).
Verified:
- The Go side is right:
writeCredentialswrites the key 0600 and the stub 0644 with slug/app_id/client_id only; the unit test proves both modes, the paths, and — good instinct — that client and webhook secrets never reach the stub.identityNewprints both paths. - The stub-path design is the correct remedy for finding 11:
GET /app/installationsunder the App's own JWT lists exactly that App's installations, so discovery needs no user visibility at all. - Happy paths work (and the PR records real-token smoke tests through both).
Two findings, both confirmed by experiment against the PR head:
-
A bad stub dies silently and the promised fallback never runs. With a stub whose
app_idGitHub rejects (a typo — and the docs now explicitly invite hand-writing stubs),inst_id=$(curl -sf … | jq …)fails underset -euo pipefailand kills the script: exit 22, zero bytes of output. The comment says the legacy path covers "no stub, or discovery came up empty", but an HTTP failure is neither — it aborts before the fallback, without the helpful hand-write-a-stub message the plan promised. Fix: don't let the discovery pipeline abort the script (e.g. append|| trueinside the substitution, or drop-fand inspect), so a failed lookup leavesinst_idempty and flows into the legacy path and its real error message. -
"No user-credential dependence when the stub exists" (plan step 2) is not yet true.
org=${CODECREW_ORG:-$(gh repo view --json owner -q .owner.login)}runs before the stub is read; with gh unauthenticated andCODECREW_ORGunset, the script exits at gh's own error (verified: exit 4, "please run: gh auth login") without ever touching the stub. The owner is only a preference in the jq (select(.account.login == $owner)falls back to.[0].id), so the fix is one line: tolerate owner-resolution failure (|| true/2>/dev/null) and let an empty owner degrade to the App's only installation.
Both are small; the design stands. Non-blocking observation: the task claims M5-R2, which is the onboarding-story decision (#47) — this change is real R2-adjacent groundwork (identities.md, minting flow) but does not itself decide anything; worth keeping in mind at verdict time.
|
Review findings addressed: (1) the stub-path discovery is now tolerant — a bad stub (wrong App ID, revoked key) falls through to the legacy lookups and their hand-write-a-stub guidance instead of dying under |
There was a problem hiding this comment.
Re-review of head 1eb291b against my prior findings (head extracted clean; go test ./... green; all failure paths re-exercised with a sandbox HOME, a throwaway key, and a PATH-shimmed gh).
Both original findings are resolved, verified by experiment:
- Bad stub no longer dies silently: with a wrong
app_id, the stub-path discovery is tolerated (2>/dev/null … || true) and the script falls through to the legacy path. - Zero user-credential involvement in the stub path is now real: with gh shimmed to fail outright and
CODECREW_ORGunset, a valid stub minted a working installation token end to end (verified via GraphQL viewer —radiusred-testy[bot]); owner resolution degraded to the jq's sole-installation fallback exactly as intended.
One new blocking finding, exposed by exercising the fall-through you just built — the legacy path it lands on garbles gh's 403:
lookup() captures stdout and discards only stderr, but when gh api /user/installations --jq … gets a 403, gh prints the raw error JSON body to stdout (reproduced on this machine under the operator's real gh auth — the exact credential condition of finding 11: {"message":"You must authenticate with an access token authorized to a GitHub App in order to list installations",…,"status":"403"}). That non-empty body passes the [[ -n "$found" ]] check, read -r app_id inst_id parses app_id='{"message":"You' and inst_id='must', a garbage JWT is minted, and the final curl hits a malformed URL: exit 3, zero output, and the hand-write-a-stub guidance is never reached. So in the most-documented real environment (gh authenticated but /user/installations 403ing), a corrupt or typo'd stub — the very case the new guidance exists for — produces silence instead of guidance. (With gh fully unauthenticated the guidance does print; I verified that too. Your live smoke test passed because the org lookup succeeded for an org-installed App — a personal-account App like davison-review-bot is the class that hits this.)
Concrete fix, one line: validate the lookup's shape before trusting it, e.g. lookup() { gh api "$1" --jq '…' 2>/dev/null | grep -E '^[0-9]+ [0-9]+$' || true; } (or an equivalent [[ "$found" =~ ^[0-9]+\ [0-9]+$ ]] guard before read). While there: the guidance message says "no stub at $stub" even when a stub exists but its discovery failed — worth an "or the stub's app_id is wrong" clause so the operator checks the right thing.
Pre-existing on main, yes — but this PR rewrote this block, routes the new bad-stub fall-through directly into it, and its stated promise is that this path ends in guidance. The design stands; this is the last brick.
|
Review finding addressed: |
There was a problem hiding this comment.
Re-review of head bc8e4ae against my remaining finding (head extracted clean; go test ./... green; all three script paths re-exercised by the original method).
Finding resolved, verified by experiment:
- The garbling path is closed. Bad stub (
app_idGitHub rejects) under real, authenticated gh — the exact environment where the 403 body from/user/installationspreviously becameapp_id='{"message":"You', a garbage JWT, and a silent exit 3 — now ends in the guidance at exit 1: "stub at … did not resolve an installation — check its app_id; org 'radiusred' and /user/installations lookups empty or unreadable", plus the hand-write-a-stub line. The shape guard (grep -E '^[0-9]+ [0-9]+$', with|| truekeeping it tolerant under pipefail) makes an error body structurally unable to pass[[ -n "$found" ]]. - The message nit is fixed: a present-but-bad stub is reported as such, not as "no stub".
- No regressions in the minting paths: (a) good stub + gh shimmed dead — minted a working installation token (viewer verified
radiusred-testy[bot]), zero user-credential involvement; (b) no stub + real gh auth from a repo cwd — legacy org lookup still mints (viewer verified again); the guard passes well-formed"app_id inst_id"lines untouched.
With this, everything from the two prior rounds stands resolved: tolerant stub-path discovery, no gh-auth dependence when a stub exists, honest fall-through into legacy lookups that end in either a token or accurate guidance. Go side unchanged and sound (stub 0644 non-secret, key 0600, secrets proven absent from the stub). The M5-R2 scope note from my first review remains a verdict-time consideration, not a defect of this PR.
Approved.
Closes #83.
Folds back finding 11 on #73:
/user/installationsfails under real operator credentials — both live reviewer sessions ended up minting via raw JWT with hand-supplied IDs.identity newnow persists a credential stub beside the private key (~/.config/codecrew/<slug>.json— slug, App ID, client ID; nothing secret, and the test proves client/webhook secrets never reach it), via an extractedwriteCredentialshelper.scripts/codecrew-tokenresolution order: stub → mint the App's JWT → discover the installation underGET /app/installations(preferring the current owner's account, else the App's only installation) — zero user-credential involvement; then the legacyghlookups (org,/user/installations) for stubless Apps, whose failure message now says how to hand-write a stub.Smoke-verified with real tokens through both paths: legacy org lookup for
radiusred-cody, then the stub path for the same App with noghlookup at all. Stubs forradiusred-codyanddavison-review-bot(App ID 4704266) are now in~/.config/codecrew/on the operator's machine, so the next numberguess reviewer dispatch mints cleanly.Docs updated (identities.md credentials bullet); Go tests ride along per #46.