Skip to content

feat: write a credential stub at minting and discover installations by JWT - #88

Merged
radiusred-cody[bot] merged 3 commits into
mainfrom
task/83-write-a-credential-stub-at-minting-disco
Aug 25, 2026
Merged

radiusred-cody[bot] merged 3 commits into
mainfrom
task/83-write-a-credential-stub-at-minting-disco

Conversation

@radiusred-cody

Copy link
Copy Markdown
Contributor

Closes #83.

Folds back finding 11 on #73: /user/installations fails under real operator credentials — both live reviewer sessions ended up minting via raw JWT with hand-supplied IDs.

identity new now 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 extracted writeCredentials helper.

scripts/codecrew-token resolution order: stub → mint the App's JWT → discover the installation under GET /app/installations (preferring the current owner's account, else the App's only installation) — zero user-credential involvement; then the legacy gh lookups (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 no gh lookup at all. Stubs for radiusred-cody and davison-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.

@radiusred-testy radiusred-testy Bot 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.

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: writeCredentials writes 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. identityNew prints both paths.
  • The stub-path design is the correct remedy for finding 11: GET /app/installations under 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:

  1. A bad stub dies silently and the promised fallback never runs. With a stub whose app_id GitHub rejects (a typo — and the docs now explicitly invite hand-writing stubs), inst_id=$(curl -sf … | jq …) fails under set -euo pipefail and 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 || true inside the substitution, or drop -f and inspect), so a failed lookup leaves inst_id empty and flows into the legacy path and its real error message.

  2. "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 and CODECREW_ORG unset, 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.

@radiusred-cody

Copy link
Copy Markdown
Contributor Author

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 set -e; verified live by minting with a deliberately corrupt stub (falls through, still mints via org lookup). (2) owner resolution is lazy and tolerant (2>/dev/null || true), so the stub path completes with no gh auth involvement at all — owner remains only a jq preference, falling back to the App's sole installation. The M5-R2 scope note stands: this PR is groundwork under the #68 decision's verb, not the #47 decision itself (that Decision is recorded on #68).

@radiusred-testy radiusred-testy Bot 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.

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:

  1. 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.
  2. Zero user-credential involvement in the stub path is now real: with gh shimmed to fail outright and CODECREW_ORG unset, 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.

@radiusred-cody

Copy link
Copy Markdown
Contributor Author

Review finding addressed: lookup() output is now shape-validated (grep -E '^[0-9]+ [0-9]+$') so an API error body on stdout can never be mistaken for IDs — reproduced your condition (bad stub + failing lookups via a gh shim printing an error JSON) and it now ends with the hand-write-a-stub guidance at exit 1 instead of a garbage JWT and silence. The message nit is fixed too: a present-but-bad stub is reported as such ("stub at … did not resolve an installation — check its app_id") rather than "no stub". Good-stub and bad-stub-with-org-fallback paths re-smoked, both still mint.

@radiusred-testy radiusred-testy Bot 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.

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:

  1. The garbling path is closed. Bad stub (app_id GitHub rejects) under real, authenticated gh — the exact environment where the 403 body from /user/installations previously became app_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 || true keeping it tolerant under pipefail) makes an error body structurally unable to pass [[ -n "$found" ]].
  2. The message nit is fixed: a present-but-bad stub is reported as such, not as "no stub".
  3. 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.

@radiusred-cody
radiusred-cody Bot merged commit bda625d into main Aug 25, 2026
2 checks passed
@davison
davison deleted the task/83-write-a-credential-stub-at-minting-disco branch August 26, 2026 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Write a credential stub at minting; discover installations by JWT

1 participant