Repository navigation
fix(bootstrap): write gcloud access token to ~/.npmrc for Artifact Registry - #359
evansenter wants to merge 2 commits into
Conversation
…gistry gcloud artifacts print-settings npm only emits the registry URL (plus a deprecated always-auth line) and no credential, so npm hit the registry anonymously and failed with E401. Mint an OAuth access token with gcloud auth print-access-token and write it as _authToken. The token expires after ~1h; re-run ./bootstrap.sh -f to refresh it. Also strip the gcloud snippet comment that earlier versions accumulated, squeeze blank lines, and chmod 600 ~/.npmrc.
| # ~1h; re-run `./bootstrap.sh -f` to refresh it. | ||
| local registry token | ||
| registry=$(echo "$settings" | sed -n 's/^registry=//p' | head -n1) | ||
| token=$(gcloud auth print-access-token 2>/dev/null) || token="" |
There was a problem hiding this comment.
[Suggestion] Because the token expires after ~1h, ~/.npmrc will go back to E401 between bootstrap runs, which is the same symptom this PR fixes. Worth considering as a follow-up: a small helper or alias (e.g. npm-auth) that refreshes only this line, or google-artifactregistry-auth as a credential helper. Then a refresh does not need a full bootstrap.sh -f sync.
There was a problem hiding this comment.
Addressed in 4527dce: extracted the logic into home/.bin/npm-auth (aliased npm-auth), so a refresh no longer needs a full sync. Bootstrap delegates via --if-available.
| local tmp_npmrc | ||
| tmp_npmrc=$(mktemp) | ||
| if [[ -f "$HOME/.npmrc" ]]; then | ||
| grep -v -e "artifact-foundry-prod" -e "^# Insert the following snippet" "$HOME/.npmrc" | cat -s >"$tmp_npmrc" || true |
There was a problem hiding this comment.
[Suggestion] cat -s squeezes blank lines across the whole ~/.npmrc, not just the area this function manages. It is harmless for npmrc semantics, but it does mean bootstrap rewrites formatting it does not own. Also, make test-bootstrap has no coverage of this rewrite. A small fixture test would catch regressions in the strip/append logic. Suggested fixture: an npmrc with a stale artifact-foundry-prod line, the gcloud snippet comment, and no trailing newline. Expected output: exactly one registry= line and one _authToken line, file mode 600.
There was a problem hiding this comment.
Addressed in 4527dce: replaced the whole-file cat -s with trimming only trailing blank lines, and added the suggested fixture tests (stale line + snippet comment + no final newline → exactly one registry= and one _authToken, mode 600), plus idempotency / token-failure / skip cases.
There was a problem hiding this comment.
Code Review — Round 1
Summary
Correctly fixes the E401: print-settings npm carries no credential, so the function now mints a gcloud access token and writes registry= + //host/path/:_authToken= to ~/.npmrc.
- The bail-out leaves
~/.npmrcuntouched when the URL or token is missing. - Rewriting via a 0600
mktempfile and thenmvmeans the token is never world-readable on disk. configure_npm_registryruns in the sync phase, so the documented./bootstrap.sh -frefresh path works.- Verified:
${registry#https:}yields the correct//.../auth key. - Verified: GNU
grepnewline-terminates the last line, so appended entries cannot concatenate onto a file lacking a trailing newline.
Verdict
APPROVE - No blocking findings. Two suggestions posted inline (token refresh ergonomics, fixture test / cat -s scope).
Automated review by Claude Code
Address review on #359: - Extract the logic into home/.bin/npm-auth (aliased as npm-auth) so the ~1h access token can be refreshed without a full bootstrap sync. bootstrap's configure_npm_registry now delegates with --if-available, which keeps it a quiet no-op on machines without gcloud/npm/access. - Replace the whole-file cat -s with trimming only trailing blank lines, so bootstrap no longer reformats parts of ~/.npmrc it doesn't own. - Write via a same-dir mktemp file (0600) with an EXIT trap so a failed run leaves no temp file behind. - Add fixture tests: stale lines + snippet comments + no final newline, idempotent refresh, missing file, token failure leaves file untouched, --if-available skip, and bootstrap delegating to the helper.
There was a problem hiding this comment.
Code Review — Round 2
Summary
Moves the token logic into home/.bin/npm-auth so the ~1h token can be refreshed on its own, and configure_npm_registry now just calls the helper. Both round-1 suggestions are handled: there is a standalone refresh path, and stubbed fixture tests now cover the rewrite.
Things I checked:
- Running it twice gives the same file: the new
//…/artifact-foundry-prod/…:_authToken=line contains the project name, so the next run strips it before writing a fresh one. awkreads a last line that has no trailing newline, and only trailing blank lines are trimmed, so the rest of the user's~/.npmrcis left alone.- The file is written to a 0600
mktempin the same directory and then moved into place, and the EXIT trap removes the temp file if something fails. The token is never world-readable. --if-availablekeeps bootstrap quiet on machines without gcloud. The|| echoin bootstrap means a helper failure can't abort a sync underset -e.~/.binis not on PATH (home/.exports), so the alias is needed.
Minor, non-blocking: if ~/.npmrc were ever a symlink, the mv would replace it with a regular file. It isn't tracked in home/, so this doesn't apply today.
Verdict
APPROVE - No blocking findings.
Automated review by Claude Code
Problem
gcloud artifacts print-settings npmonly emits the registry URL (plus a deprecatedalways-authline) — no credential.configure_npm_registryappended that to~/.npmrc, so npm hit Artifact Registry anonymously and failed with E401.Fix
home/.bin/npm-authhelper (aliased asnpm-auth): mints an OAuth access token viagcloud auth print-access-tokenand writesregistry=+//…:_authToken=to~/.npmrc.configure_npm_registryin bootstrap now just callsnpm-auth --if-available(quiet no-op without gcloud/npm/an active account/registry access).~/.npmrcuntouched if the registry URL or token can't be obtained.artifact-foundry-prodlines and accumulated gcloud snippet comments, trims only trailing blank lines (no whole-file reformatting).mktemp+mv; EXIT trap cleans up on failure.Token refresh
The token expires after ~1h. Run
npm-authwhen npm returns E401 — no full./bootstrap.sh -fneeded.Testing
make checkpasses (bootstrap 73/73, hooks 133/133).=== npm-auth ===tests (stubbed gcloud/npm, throwaway HOME): stale-fixture rewrite incl. no final newline + mode 600, idempotent refresh, missing file, token failure leaves file unchanged,--if-availableskip, bootstrap delegates to helper.npm-authrefreshed~/.npmrc(mode 600, no temp files left), thennpm ping→ PONG andnpm view typescript versionsucceeded against Artifact Registry.