From a977666ee60899b0302a19de9ada82fc10f6906d Mon Sep 17 00:00:00 2001 From: Evan Senter Date: Mon, 9 Nov 2026 15:40:40 +0000 Subject: [PATCH 1/2] fix(bootstrap): write gcloud access token to ~/.npmrc for Artifact Registry 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. --- bootstrap.sh | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index 750902b..12597e9 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1336,18 +1336,34 @@ configure_npm_registry() { if settings=$(gcloud artifacts print-settings npm --project=artifact-foundry-prod --repository=ah-3p-staging-npm --location=us 2>/dev/null); then echo "Configuring NPM registry credentials via gcloud..." - # Remove old/expired artifact-foundry-prod lines if present - if [[ -f "$HOME/.npmrc" ]]; then - local tmp_npmrc - tmp_npmrc=$(mktemp) - grep -v "artifact-foundry-prod" "$HOME/.npmrc" > "$tmp_npmrc" || true - mv "$tmp_npmrc" "$HOME/.npmrc" + # print-settings only emits the registry URL (plus a deprecated + # always-auth line) — it carries no credential. Without a token, npm + # hits the registry anonymously and fails with E401. Mint an OAuth access + # token (what `npx google-artifactregistry-auth` does). It expires after + # ~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="" + if [[ -z "$registry" || -z "$token" ]]; then + echo "Could not obtain NPM registry URL or gcloud access token; leaving ~/.npmrc unchanged." + return 0 fi - # Strip the deprecated always-auth line. Prepend a newline so the first - # appended line can't concatenate onto an existing ~/.npmrc entry. - { printf '\n'; echo "$settings" | { grep -v "always-auth" || true; }; } >> "$HOME/.npmrc" - echo "NPM registry credentials configured in ~/.npmrc" + # Remove old/expired artifact-foundry-prod lines and the snippet comment + # gcloud prints (earlier versions of this function accumulated it), then + # squeeze runs of blank lines. + 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 + fi + { + echo "registry=${registry}" + echo "${registry#https:}:_authToken=${token}" + } >>"$tmp_npmrc" + mv "$tmp_npmrc" "$HOME/.npmrc" + chmod 600 "$HOME/.npmrc" + echo "NPM registry credentials configured in ~/.npmrc (token valid ~1h)" fi } From 4527dce05771954b362d6070687a2ba1b556cd69 Mon Sep 17 00:00:00 2001 From: Evan Senter Date: Mon, 9 Nov 2026 16:17:49 +0000 Subject: [PATCH 2/2] refactor(bootstrap): move npm registry auth into npm-auth helper 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. --- bootstrap.sh | 51 ++-------------- home/.aliases | 3 + home/.bin/npm-auth | 88 ++++++++++++++++++++++++++++ tests/test-bootstrap.sh | 127 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 224 insertions(+), 45 deletions(-) create mode 100755 home/.bin/npm-auth diff --git a/bootstrap.sh b/bootstrap.sh index 12597e9..47d3c79 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1320,51 +1320,12 @@ install_agy_mcp_servers() { } configure_npm_registry() { - # Require gcloud and npm to be installed - if ! command -v gcloud >/dev/null 2>&1 || ! command -v npm >/dev/null 2>&1; then - return 0 - fi - - # Check if user has an active gcloud account - local active_account - active_account=$(gcloud config get-value account 2>/dev/null) || return 0 - if [[ -z "$active_account" ]]; then - return 0 - fi - - local settings - if settings=$(gcloud artifacts print-settings npm --project=artifact-foundry-prod --repository=ah-3p-staging-npm --location=us 2>/dev/null); then - echo "Configuring NPM registry credentials via gcloud..." - - # print-settings only emits the registry URL (plus a deprecated - # always-auth line) — it carries no credential. Without a token, npm - # hits the registry anonymously and fails with E401. Mint an OAuth access - # token (what `npx google-artifactregistry-auth` does). It expires after - # ~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="" - if [[ -z "$registry" || -z "$token" ]]; then - echo "Could not obtain NPM registry URL or gcloud access token; leaving ~/.npmrc unchanged." - return 0 - fi - - # Remove old/expired artifact-foundry-prod lines and the snippet comment - # gcloud prints (earlier versions of this function accumulated it), then - # squeeze runs of blank lines. - 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 - fi - { - echo "registry=${registry}" - echo "${registry#https:}:_authToken=${token}" - } >>"$tmp_npmrc" - mv "$tmp_npmrc" "$HOME/.npmrc" - chmod 600 "$HOME/.npmrc" - echo "NPM registry credentials configured in ~/.npmrc (token valid ~1h)" - fi + # Logic lives in home/.bin/npm-auth so the ~1h access token can be refreshed + # on its own (`npm-auth`) without a full sync. --if-available makes it a + # quiet no-op on machines without gcloud/npm or Artifact Registry access. + local dotfiles_dir + dotfiles_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" + "$dotfiles_dir/home/.bin/npm-auth" --if-available || echo " Warning: npm-auth failed; npm may return E401" } sync_dotfiles() { diff --git a/home/.aliases b/home/.aliases index dd1cfc7..f0eb597 100644 --- a/home/.aliases +++ b/home/.aliases @@ -44,6 +44,9 @@ if ! command -v tailscale &>/dev/null && [[ -x /Applications/Tailscale.app/Conte alias tailscale="/Applications/Tailscale.app/Contents/MacOS/Tailscale" fi +# Refresh the ~1h Artifact Registry token in ~/.npmrc (run when npm hits E401) +alias npm-auth="$HOME/.bin/npm-auth" + # ============================================================================== # Git # ============================================================================== diff --git a/home/.bin/npm-auth b/home/.bin/npm-auth new file mode 100755 index 0000000..f18cd86 --- /dev/null +++ b/home/.bin/npm-auth @@ -0,0 +1,88 @@ +#!/bin/bash +# Refresh the Artifact Registry npm credential in ~/.npmrc. +# +# `gcloud artifacts print-settings npm` only emits the registry URL (plus a +# deprecated always-auth line) — it carries no credential, so npm would hit the +# registry anonymously and fail with E401. This mints an OAuth access token +# (what `npx google-artifactregistry-auth` does) and writes it as _authToken. +# +# The token expires after ~1h: run `npm-auth` whenever npm starts returning +# E401. bootstrap.sh calls this with --if-available during every sync. +# +# Usage: npm-auth [--if-available] +# --if-available Exit 0 quietly when gcloud/npm/an active account/registry +# access is missing (non-corp machines). Without it, those +# cases are errors. +set -euo pipefail + +PROJECT="artifact-foundry-prod" +REPOSITORY="ah-3p-staging-npm" +LOCATION="us" +NPMRC="$HOME/.npmrc" + +if_available=false +case "${1:-}" in + --if-available) if_available=true ;; + "") ;; + -h | --help) + sed -n '2,/^set -euo/{/^set -euo/d;s/^# \{0,1\}//;p;}' "$0" + exit 0 + ;; + *) + echo "npm-auth: unknown argument: $1" >&2 + exit 2 + ;; +esac + +# Prerequisite missing: silent no-op under --if-available, error otherwise. +unavailable() { + if [[ "$if_available" == true ]]; then + exit 0 + fi + echo "npm-auth: $1" >&2 + exit 1 +} + +command -v gcloud >/dev/null 2>&1 || unavailable "gcloud not installed" +command -v npm >/dev/null 2>&1 || unavailable "npm not installed" + +account=$(gcloud config get-value account 2>/dev/null) || account="" +[[ -n "$account" ]] || unavailable "no active gcloud account (run: gcloud auth login)" + +settings=$(gcloud artifacts print-settings npm \ + --project="$PROJECT" --repository="$REPOSITORY" --location="$LOCATION" 2>/dev/null) || + unavailable "cannot read npm settings for $PROJECT/$REPOSITORY" + +registry=$(sed -n 's/^registry=//p' <<<"$settings" | head -n1) +token=$(gcloud auth print-access-token 2>/dev/null) || token="" +if [[ -z "$registry" || -z "$token" ]]; then + # Reached only once we know this is a machine that should have access, so + # always say so — but don't fail a bootstrap sync over it. + echo "npm-auth: could not obtain registry URL or gcloud access token; leaving ~/.npmrc unchanged." >&2 + [[ "$if_available" == true ]] && exit 0 + exit 1 +fi + +# Rewrite via a temp file in the same directory (atomic mv; mktemp is 0600, so +# the token is never world-readable). Drop our previous entries and the snippet +# comment gcloud prints (older bootstrap versions accumulated it), then trim the +# blank lines those versions left at the end of the file. Everything else in +# ~/.npmrc — including its interior blank lines — is left exactly as it was. +tmp=$(mktemp "${NPMRC}.XXXXXX") +trap 'rm -f "$tmp"' EXIT +if [[ -f "$NPMRC" ]]; then + awk -v project="$PROJECT" ' + index($0, project) || /^# Insert the following snippet/ { next } + { lines[n++] = $0 } + END { + while (n > 0 && lines[n - 1] ~ /^[[:space:]]*$/) n-- + for (i = 0; i < n; i++) print lines[i] + } + ' "$NPMRC" >"$tmp" +fi +printf 'registry=%s\n%s:_authToken=%s\n' "$registry" "${registry#https:}" "$token" >>"$tmp" +chmod 600 "$tmp" +mv "$tmp" "$NPMRC" +trap - EXIT + +echo "npm registry credentials refreshed in ~/.npmrc (token valid ~1h)" diff --git a/tests/test-bootstrap.sh b/tests/test-bootstrap.sh index a11e63a..7fe84a9 100755 --- a/tests/test-bootstrap.sh +++ b/tests/test-bootstrap.sh @@ -970,6 +970,124 @@ test_source_only_ignored_when_executed() { # Run all tests # ============================================================================ +# ============================================================================ +# npm-auth (~/.npmrc Artifact Registry token) +# ============================================================================ +# Run home/.bin/npm-auth against a throwaway HOME with stubbed gcloud/npm. +# Stubs go first on PATH so a real gcloud (dev machine, CI image) never runs. +# NPM_AUTH_ACCOUNT / NPM_AUTH_TOKEN control the stub (empty = failure). + +NPM_AUTH="$SCRIPT_DIR/../home/.bin/npm-auth" +NPM_AUTH_REGISTRY="https://us-npm.pkg.dev/artifact-foundry-prod/ah-3p-staging-npm/" + +_npm_auth_sandbox() { + local dir; dir=$(mktemp -d) || return 1 + mkdir -p "$dir/home" "$dir/stubs" + cat > "$dir/stubs/gcloud" < "$dir/stubs/npm" + chmod +x "$dir/stubs/gcloud" "$dir/stubs/npm" + echo "$dir" +} + +# Usage: _npm_auth [args...] +_npm_auth() { + local dir="$1"; shift + HOME="$dir/home" PATH="$dir/stubs:$PATH" "$NPM_AUTH" "$@" +} + +_file_mode() { + stat -c '%a' "$1" 2>/dev/null || stat -f '%Lp' "$1" +} + +test_npm_auth_rewrites_stale_npmrc() { + # Reviewer's fixture, shaped like what older versions left behind: each run + # appended a blank line + the gcloud snippet comment + settings, so stale + # artifact-foundry-prod lines and blank lines pile up at the end, with no + # final newline. Unrelated config (and its interior blank line) must survive. + local dir; dir=$(_npm_auth_sandbox) || return 1 + printf 'color=false\n\nsave-exact=true\n\n# Insert the following snippet into your .npmrc\n\nregistry=https://us-npm.pkg.dev/artifact-foundry-prod/old/\n//us-npm.pkg.dev/artifact-foundry-prod/old/:_authToken=expired\n\n# Insert the following snippet into your .npmrc\n\nregistry=https://us-npm.pkg.dev/artifact-foundry-prod/old/' \ + > "$dir/home/.npmrc" + _npm_auth "$dir" >/dev/null || { rm -rf "$dir"; return 1; } + local rc=0 expected + expected=$(printf 'color=false\n\nsave-exact=true\nregistry=%s\n%s:_authToken=tok' \ + "$NPM_AUTH_REGISTRY" "${NPM_AUTH_REGISTRY#https:}") + [[ "$(cat "$dir/home/.npmrc")" == "$expected" ]] || rc=1 + [[ "$(_file_mode "$dir/home/.npmrc")" == "600" ]] || rc=1 + rm -rf "$dir" + return $rc +} + +test_npm_auth_is_idempotent() { + # Refreshing must replace the credential, not stack another copy of it. + local dir; dir=$(_npm_auth_sandbox) || return 1 + printf 'color=false\n' > "$dir/home/.npmrc" + if ! { _npm_auth "$dir" && NPM_AUTH_TOKEN=tok2 _npm_auth "$dir"; } >/dev/null; then rm -rf "$dir"; return 1; fi + local rc=0 + [[ $(grep -c '^registry=' "$dir/home/.npmrc") -eq 1 ]] || rc=1 + [[ $(grep -c ':_authToken=' "$dir/home/.npmrc") -eq 1 ]] || rc=1 + grep -q ':_authToken=tok2$' "$dir/home/.npmrc" || rc=1 + grep -q '^color=false$' "$dir/home/.npmrc" || rc=1 + rm -rf "$dir" + return $rc +} + +test_npm_auth_creates_missing_npmrc() { + local dir; dir=$(_npm_auth_sandbox) || return 1 + _npm_auth "$dir" >/dev/null || { rm -rf "$dir"; return 1; } + local rc=0 + [[ $(wc -l < "$dir/home/.npmrc") -eq 2 ]] || rc=1 + [[ "$(_file_mode "$dir/home/.npmrc")" == "600" ]] || rc=1 + rm -rf "$dir" + return $rc +} + +test_npm_auth_token_failure_leaves_npmrc_unchanged() { + # No token → don't strip the old credential and leave npm with nothing. + local dir; dir=$(_npm_auth_sandbox) || return 1 + printf 'registry=%s\nkeep=me\n' "$NPM_AUTH_REGISTRY" > "$dir/home/.npmrc" + local before; before=$(cat "$dir/home/.npmrc") + local rc=0 + NPM_AUTH_TOKEN='' _npm_auth "$dir" >/dev/null 2>&1 && rc=1 # manual: fails + NPM_AUTH_TOKEN='' _npm_auth "$dir" --if-available >/dev/null 2>&1 || rc=1 # bootstrap: doesn't + [[ "$(cat "$dir/home/.npmrc")" == "$before" ]] || rc=1 + # No temp files left behind next to ~/.npmrc + [[ $(find "$dir/home" -name '.npmrc.*' | wc -l) -eq 0 ]] || rc=1 + rm -rf "$dir" + return $rc +} + +test_npm_auth_if_available_skips_quietly() { + # Non-corp machine (no active account): bootstrap mode is a silent no-op, + # manual mode is an error that says why. + local dir; dir=$(_npm_auth_sandbox) || return 1 + local rc=0 out + out=$(NPM_AUTH_ACCOUNT='' _npm_auth "$dir" --if-available 2>&1) || rc=1 + [[ -z "$out" ]] || rc=1 + [[ ! -e "$dir/home/.npmrc" ]] || rc=1 + out=$(NPM_AUTH_ACCOUNT='' _npm_auth "$dir" 2>&1) && rc=1 + grep -q 'no active gcloud account' <<< "$out" || rc=1 + rm -rf "$dir" + return $rc +} + +test_bootstrap_delegates_to_npm_auth() { + # One implementation: bootstrap must call the helper, not carry a copy. + grep -q 'home/.bin/npm-auth" --if-available' "$BOOTSTRAP" && + ! grep -q 'gcloud auth print-access-token' "$BOOTSTRAP" +} + main() { echo "Running bootstrap tests..." echo "" @@ -1054,6 +1172,15 @@ main() { run_test "INSTALL_AI and INSTALL_TAILSCALE are independent" "test_install_ai_and_tailscale_are_independent" echo "" + echo "=== npm-auth ===" + run_test "rewrites stale ~/.npmrc fixture" "test_npm_auth_rewrites_stale_npmrc" + run_test "refresh is idempotent" "test_npm_auth_is_idempotent" + run_test "creates missing ~/.npmrc (mode 600)" "test_npm_auth_creates_missing_npmrc" + run_test "token failure leaves ~/.npmrc unchanged" "test_npm_auth_token_failure_leaves_npmrc_unchanged" + run_test "--if-available skips quietly" "test_npm_auth_if_available_skips_quietly" + run_test "bootstrap delegates to npm-auth" "test_bootstrap_delegates_to_npm_auth" + echo "" + echo "=== self-update re-exec ===" run_test "re-execs when pull rewrites bootstrap.sh" "test_pull_latest_reexecs_when_pull_rewrites_script" run_test "no re-exec when script unchanged" "test_pull_latest_no_reexec_when_script_unchanged"