Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 6 additions & 29 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1320,35 +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..."

# 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"
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"
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() {
Expand Down
3 changes: 3 additions & 0 deletions home/.aliases
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ==============================================================================
Expand Down
88 changes: 88 additions & 0 deletions home/.bin/npm-auth
Original file line number Diff line number Diff line change
@@ -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)"
127 changes: 127 additions & 0 deletions tests/test-bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" <<STUB
#!/bin/bash
case "\$*" in
"config get-value account") echo "\${NPM_AUTH_ACCOUNT-user@example.com}" ;;
"auth print-access-token") [[ -n "\${NPM_AUTH_TOKEN-tok}" ]] || exit 1; echo "\${NPM_AUTH_TOKEN-tok}" ;;
artifacts\ print-settings\ npm*)
echo "# Insert the following snippet into your .npmrc"
echo ""
echo "registry=$NPM_AUTH_REGISTRY"
echo "${NPM_AUTH_REGISTRY#https:}:always-auth=true" ;;
*) exit 1 ;;
esac
STUB
printf '#!/bin/bash\nexit 0\n' > "$dir/stubs/npm"
chmod +x "$dir/stubs/gcloud" "$dir/stubs/npm"
echo "$dir"
}

# Usage: _npm_auth <sandbox> [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 ""
Expand Down Expand Up @@ -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"
Expand Down
Loading