Skip to content

fix(bootstrap): write gcloud access token to ~/.npmrc for Artifact Registry - #359

Open
evansenter wants to merge 2 commits into
mainfrom
fix/npm-registry-auth-token
Open

evansenter wants to merge 2 commits into
mainfrom
fix/npm-registry-auth-token

Conversation

@evansenter

@evansenter evansenter commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

Problem

gcloud artifacts print-settings npm only emits the registry URL (plus a deprecated always-auth line) — no credential. configure_npm_registry appended that to ~/.npmrc, so npm hit Artifact Registry anonymously and failed with E401.

Fix

  • New home/.bin/npm-auth helper (aliased as npm-auth): mints an OAuth access token via gcloud auth print-access-token and writes registry= + //…:_authToken= to ~/.npmrc.
  • configure_npm_registry in bootstrap now just calls npm-auth --if-available (quiet no-op without gcloud/npm/an active account/registry access).
  • Leaves ~/.npmrc untouched if the registry URL or token can't be obtained.
  • Strips stale artifact-foundry-prod lines and accumulated gcloud snippet comments, trims only trailing blank lines (no whole-file reformatting).
  • Writes via a same-dir 0600 mktemp + mv; EXIT trap cleans up on failure.

Token refresh

The token expires after ~1h. Run npm-auth when npm returns E401 — no full ./bootstrap.sh -f needed.

Testing

  • make check passes (bootstrap 73/73, hooks 133/133).
  • New === 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-available skip, bootstrap delegates to helper.
  • Live-verified on macOS: npm-auth refreshed ~/.npmrc (mode 600, no temp files left), then npm ping → PONG and npm view typescript version succeeded against Artifact Registry.

…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.
Comment thread bootstrap.sh Outdated
# ~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=""

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] 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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread bootstrap.sh Outdated
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

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] 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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

claude[bot]
claude Bot previously approved these changes Nov 9, 2026

@claude claude 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.

Prompt: evansenter/dotfiles/.../claude-review.md

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 ~/.npmrc untouched when the URL or token is missing.
  • Rewriting via a 0600 mktemp file and then mv means the token is never world-readable on disk.
  • configure_npm_registry runs in the sync phase, so the documented ./bootstrap.sh -f refresh path works.
  • Verified: ${registry#https:} yields the correct //.../ auth key.
  • Verified: GNU grep newline-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.

@claude claude 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.

Prompt: evansenter/dotfiles/.../claude-review.md

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.
  • awk reads a last line that has no trailing newline, and only trailing blank lines are trimmed, so the rest of the user's ~/.npmrc is left alone.
  • The file is written to a 0600 mktemp in 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-available keeps bootstrap quiet on machines without gcloud. The || echo in bootstrap means a helper failure can't abort a sync under set -e.
  • ~/.bin is 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

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.

1 participant