Skip to content

Commit 70a0a59

Browse files
committed
Add dynamic .npmrc authentication
Fixes static authentication token caching issue where tokens from .npmrc were read once during module extension evaluation and cached, causing 401 errors when short-lived credentials expired (e.g., AWS CodeArtifact 12-hour tokens). Changes: - Add _read_npmrc_auth() function to read .npmrc files dynamically on each download instead of caching tokens statically - Add _get_auth_from_url() helper to extract auth for specific registry URLs - Add npmrc and use_home_npmrc attributes to npm_import_rule - Modify _download_and_extract_archive() to read auth dynamically first, with fallback to static auth attributes for backward compatibility - Pass npmrc/use_home_npmrc from npm_translate_lock extension instead of static npm_auth* attributes Testing: - Add unit tests for _get_auth_from_url() helper function - Add e2e integration test that verifies tokens are read dynamically: * Fetches succeed with valid tokens * Fetches fail with 401 when tokens are broken (proving fresh reads) * Fetches succeed when tokens are restored - Test uses --repository_cache= to force fresh downloads Fixes: #2547
1 parent 409b866 commit 70a0a59

16 files changed

Lines changed: 468 additions & 38 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
try-import %workspace%/../../.aspect/workflows/bazelrc
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../.bazelversion
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
hoist=false
2+
3+
# This will be replaced by test script with dynamic token
4+
_authToken=${ASPECT_NPM_AUTH_TOKEN}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
2+
load("@npm//:defs.bzl", "npm_link_all_packages")
3+
4+
npm_link_all_packages(name = "node_modules")
5+
6+
build_test(
7+
name = "test",
8+
targets = [
9+
":node_modules",
10+
],
11+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
bazel_dep(name = "aspect_rules_js", version = "0.0.0", dev_dependency = True)
2+
local_path_override(
3+
module_name = "aspect_rules_js",
4+
path = "../..",
5+
)
6+
7+
bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True)
8+
9+
pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")
10+
pnpm.pnpm(
11+
name = "pnpm",
12+
)
13+
use_repo(pnpm, "pnpm", "pnpm__links")
14+
15+
npm = use_extension(
16+
"@aspect_rules_js//npm:extensions.bzl",
17+
"npm",
18+
dev_dependency = True,
19+
)
20+
npm.npm_translate_lock(
21+
name = "npm",
22+
data = ["//:package.json"],
23+
pnpm_lock = "//:pnpm-lock.yaml",
24+
use_home_npmrc = True,
25+
verify_node_modules_ignored = "//:.bazelignore",
26+
)
27+
use_repo(npm, "npm")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Dynamic .npmrc authentication integration test
2+
3+
Tests that authentication tokens are read dynamically from `.npmrc` on each download,
4+
not cached statically. Critical for short-lived credentials like AWS CodeArtifact tokens.
5+
6+
Auth token with permission to pull packages from `@aspect-test` scope must be set in
7+
`ASPECT_NPM_AUTH_TOKEN` environment variable for this e2e test to pass.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Marker file for Bazel
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This file marks the root of the Bazel workspace.
2+
# See MODULE.bazel for external dependencies setup with bzlmod.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "npm-translate-lock-dynamic-auth-test",
3+
"version": "0.0.0",
4+
"dependencies": {
5+
"@aspect-test/a": "5.0.0"
6+
}
7+
}

0 commit comments

Comments
 (0)