-
-
Notifications
You must be signed in to change notification settings - Fork 156
Add dynamic .npmrc authentication #2550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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: aspect-build#2547
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
rules_js/npm/private/npm_import.bzl
Lines 1366 to 1370 in 70a0a59
| generate_bzl_library_targets = kwargs.pop("generate_bzl_library_targets", None) | |
| extract_full_archive = kwargs.pop("extract_full_archive", None) | |
| if len(kwargs): | |
| msg = "Invalid npm_import parameter '{}'".format(kwargs.keys()[0]) | |
| fail(msg) |
Although the repository rule now declares npmrc and use_home_npmrc, the npm_import macro still pops only generate_bzl_library_targets/extract_full_archive and fails on any remaining kwargs, so callers cannot pass the new auth-related parameters and the repository rule never sees them (dynamic .npmrc auth remains disabled and passing npmrc will raise "Invalid npm_import parameter"). The macro should accept and forward the new attributes to npm_import_rule.
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| def _get_auth_from_url(url, npm_auth_dict): | ||
| for registry, auth_info in npm_auth_dict.items(): | ||
| if registry in url: | ||
| if "bearer" in auth_info: | ||
| return { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer registry-specific auth over first .npmrc entry
The new _get_auth_from_url helper returns the first registry entry whose key is a substring of the URL, so a global _authToken entry (key "") in ~/.npmrc will always match and short‑circuit even when a scoped registry token is present. In that common setup, downloads for scoped registries will use the global token and get 401s instead of the registry‑specific credentials, which regresses the longest‑prefix selection previously used when static auth was passed to the rule. Consider skipping the empty registry until no specific match exists or applying the same longest‑match logic as _select_npm_auth.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems correct and an easy test+fix?
6042fa5 to
f0f606e
Compare
|
Don't we need to change |
|
I think your e2e test needs to be added to CI still? |
| npm_auth_basic = "", | ||
| npm_auth_username = "", | ||
| npm_auth_password = "", | ||
| npmrc = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see these being specified anywhere?
|
I'm also curious if #2554 solves this? Can you try that? That is for |



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:
Changes are visible to end-users: no
Test plan
Fixes: #2547