Skip to contentSkip to conversation

NPM Fetcher Private Registry Authentication Support:

PATCH Superseded Eric Meyers 3 participants 7 messages Feb 20, 2025 – Feb 25, 2025

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover NPM Fetcher Private Registry Authentication Support:

Changes in this commit include:
- npm fetcher changes:
- Adding support to detect .npmrc file within user's home directory, copy it over to the tmp npm environment, then append on any user configs passed in. This models similar behavior within the wget fetcher (which uses a netrc file for auth).

Eric Meyers (1):
fetch2/npm: Adding npmrc support for private registry authentication

 lib/bb/fetch2/npm.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
#1 lore raw

PATCH v2 1/1 fetch2/npm: Adding npmrc support for private registry authentication

Cc Geoff Parker <[email protected]>
Cc Alex Kanavin <[email protected]>
Cc Chuck Wolber <[email protected]>
Cc Stefan Herbrechtsmeier <[email protected]>
---
 lib/bb/fetch2/npm.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
lib/bb/fetch2/npm.py +15 -0
index c09f05044..23fb5029d 100644
@@ -107,6 +107,21 @@ class NpmEnvironment(object):
         """Run npm command in a controlled environment"""
         with tempfile.TemporaryDirectory() as tmpdir:
             d = bb.data.createCopy(self.d)
+
+            # If an .npmrc file exists in the HOME directory, then use it in the NPM environment
+            # (along with the user provided configs). Otherwise, leave it blank.
+            home_npmrc_file = os.path.join(os.environ.get("HOME"), ".npmrc")
+            tmp_npmrc_file = os.path.join(tmpdir, ".npmrc")
+            if os.path.exists(home_npmrc_file):
+                bb.utils.copyfile(home_npmrc_file, tmp_npmrc_file)
+                # Still need to append the contents of the user config created in __init__
+                with open(tmp_npmrc_file, 'a') as t:
+                    with open(self.user_config.name, 'r') as u:
+                        t.write(u.read())
+                        t.flush()
+
+                self.user_config.name = tmp_npmrc_file
+
             d.setVar("PATH", d.getVar("PATH"))  # PATH might contain $HOME - evaluate it before patching
             d.setVar("HOME", tmpdir)
 
#2 lore raw

Re: [PATCH v2 1/1] fetch2/npm: Adding npmrc support for private registry authentication

I'm broadly ok with this (except the question below), but it needs to be lgtm'd by real npm fetcher users.

Does npmrc contain secrets? Would copying it to some tmpdir put the secrets at risk?

Alex

Quoted 36 lines · Eric Meyers
Cc Geoff Parker <[email protected]>
Cc Alex Kanavin <[email protected]>
Cc Chuck Wolber <[email protected]>
Cc Stefan Herbrechtsmeier <[email protected]>
---
 lib/bb/fetch2/npm.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
lib/bb/fetch2/npm.py +15 -0
index c09f05044..23fb5029d 100644
@@ -107,6 +107,21 @@ class NpmEnvironment(object):
         """Run npm command in a controlled environment"""
         with tempfile.TemporaryDirectory() as tmpdir:
             d = bb.data.createCopy(self.d)
+
+            # If an .npmrc file exists in the HOME directory, then use it in the NPM environment
+            # (along with the user provided configs). Otherwise, leave it blank.
+            home_npmrc_file = os.path.join(os.environ.get("HOME"), ".npmrc")
+            tmp_npmrc_file = os.path.join(tmpdir, ".npmrc")
+            if os.path.exists(home_npmrc_file):
+                bb.utils.copyfile(home_npmrc_file, tmp_npmrc_file)
+                # Still need to append the contents of the user config created in __init__
+                with open(tmp_npmrc_file, 'a') as t:
+                    with open(self.user_config.name, 'r') as u:
+                        t.write(u.read())
+                        t.flush()
+
+                self.user_config.name = tmp_npmrc_file
+
             d.setVar("PATH", d.getVar("PATH"))  # PATH might contain $HOME - evaluate it before patching
             d.setVar("HOME", tmpdir)
 
#3 lore raw

Re: [PATCH v2 1/1] fetch2/npm: Adding npmrc support for private registry authentication

Quoted 9 lines · Alexander Kanavin

I'm broadly ok with this (except the question below), but it needs to be lgtm'd by real npm fetcher users.

Does npmrc contain secrets? Would copying it to some tmpdir put the secrets at risk?

Alex

It *could* have authentication credentials in plaintext, but ultimately this is up to the user if they provide them in their .npmrc file within their home directory. See:

https://docs.npmjs.com/cli/v9/configuring-npm/npmrc#auth-related-configuration

The way I'm using it now requires me to create a base64 encrypted string based on the input string of "<username>:<API_KEY>" and insert this as the "_auth" value in the .npmrc file, but there are multiple ways to provide credentials.

The only reason I copied the .npmrc file into the tmp directory was to avoid losing any user-provided configs passed into the environment (I could have taken the opposite approach and modified the user's .npmrc file instead, but didn't think that was desirable) - an example of this is within the create_npm.py function upstream in the poky repo:

https://github.com/yoctoproject/poky/blob/ca289e5d853c5ef980ad070eab0d20f973fcdb7c/scripts/lib/recipetool/create_npm.py#L98

FWIW, the wget fetcher relies on a netrc file in a user's home directory in a similar fashion, which uses username/password in plaintext to insert a basic auth header to a URL:

https://github.com/yoctoproject/poky/blob/ca289e5d853c5ef980ad070eab0d20f973fcdb7c/bitbake/lib/bb/fetch2/wget.py#L399

#4 lore raw

Re: [PATCH v2 1/1] fetch2/npm: Adding npmrc support for private registry authentication

Quoted 9 lines · Eric Meyers
Quoted 6 lines · Alexander Kanavin

I'm broadly ok with this (except the question below), but it needs to be lgtm'd by real npm fetcher users.

Does npmrc contain secrets? Would copying it to some tmpdir put the secrets at risk?

If the secrets are already in $HOME, copying them into a build is not going to
expose things any more than they already are.

That being said, as a general rule I would prefer that we be more explicit when elements found in $HOME are being used in a build.

I say this from a build consistency standpoint more than anything else. As someone
who is responsible for consistent results across a wide range of developers and
platforms, environment creep is a huge time-sink.

Can we disable this behavior by default and allow it to be enabled in the distro
or local config with a flag?

Quoted 3 lines

The only reason I copied the .npmrc file into the tmp directory was to avoid losing any user-provided configs passed into the environment

I think this is very problematic from a build consistency standpoint. As I noted above, I see no problem with reusing credentials. But I have significant
concerns with reusing anything else.

I believe it is possible to configure an .npmrc in a way that can trigger behavior variations that affect build results. Build variation should be controlled by the distro level configs and the recipe. All other sources of variation should be disallowed or, at the very least, disabled by default with the option to enable if you are willing to accept the consequences.

Quoted 3 lines

(I could have taken the opposite approach and modified the user's .npmrc file instead, but didn't think that was desirable)

Correct, it is definitely undesirable. Can you simply grab the credentials from a
user's npmrc if they happen to be available? Or does that fall into the "easier
said than done" bucket?

Quoted 4 lines

FWIW, the wget fetcher relies on a netrc file in a user's home directory in a similar fashion, which uses username/password in plaintext to insert a basic auth header to a URL

I am unaware of any netrc behavior that could introduce uncontrolled build variation. But I learn something new every day, so maybe it is possible?

..Ch:W..

Signature · 3 lines
*  Opinions expressed are my own. | --------------------------------|------
"Perfection must be reached by degrees              she requires the slow
hand of time."                                 |       -Voltaire*
#5 lore raw

Re: [PATCH v2 1/1] fetch2/npm: Adding npmrc support for private registry authentication

Quoted 5 lines · Chuck Wolber

Can we disable this behavior by default and allow it to be enabled in the distro
or local config with a flag?

That seems like a reasonable request - although in the past, I've seen some folks push back on adding variables to bitbake, but in this scenario it seems like it's the easiest/cleanest approach.

Quoted 6 lines

Correct, it is definitely undesirable. Can you simply grab the credentials from a
user's npmrc if they happen to be available? Or does that fall into the "easier
said than done" bucket?

After digging into it more, it looks like this is exactly what the netrc authenticator does in the wget fetcher (i.e. it extracts the credentials from ~/.netrc):

- wget fetcher:
https://github.com/openembedded/bitbake/blob/71fba3b193711104c83c15c961c15da346da6d4f/lib/bb/fetch2/wget.py#L399
- netrc authenticator:
https://github.com/python/cpython/blob/69426fcee7fcecbe34be66d2c5b58b6d0ffe2809/Lib/netrc.py#L70

And while theoretically I can create an extractor that will pull out various fields from the npmrc file, I think the nature of scoped packages coupled with variations in how to specify authentication strings in npm will make this easier said than done.

I will submit a v3 patch within the coming days that adds support for a bitbake variable (defaults to off and allows the user to turn this functionality on/off), along with a bb.warn log to let the user know they're using this functionality. Thanks for the insights Chuck!

-Eric

#6 lore raw

Re: [PATCH v2 1/1] fetch2/npm: Adding npmrc support for private registry authentication

On Mon, Feb 24, 2025 at 4:29 AM Eric Meyers <[email protected]> wrote:

$< Snip $<

Quoted 5 lines

And while theoretically I can create an extractor that will pull out various fields from the npmrc file, I think the nature of scoped packages coupled with variations in how to specify authentication strings in npm will make this easier said than done.

That makes sense. Until we find a clean way to extract the authentication elements, locking it up behind a variable should avoid surprises.

I will submit a v3 patch within the coming days that adds support for a

Quoted 4 lines

bitbake variable (defaults to off and allows the user to turn this functionality on/off), along with a bb.warn log to let the user know they're using this functionality. Thanks for the insights Chuck!

Thanks, that seems like a reasonable middle ground.

..Ch:W..

Signature · 3 lines
*  Opinions expressed are my own. | --------------------------------|------
"Perfection must be reached by degrees              she requires the slow
hand of time."                                 |       -Voltaire*