Skip to contentSkip to conversation

NPM Fetcher Private Registry Authentication Support:

PATCH Superseded Eric Meyers 2 participants 4 messages Feb 27, 2025 – Feb 28, 2025

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover NPM Fetcher Private Registry Authentication Support:

Changes in this commit include:
- npm fetcher changes:
- Adding support to detect an .npmrc file within user's home directory if the BB_USE_HOME_NPMRC flag is set, then append the contents to the user_config.
- Added entry to bitbake documentation for BB_USE_HOME_NPMRC flag
- This is the fourth revision at adding npmrc support, with the first 3 revisions below:
- v1: https://lists.openembedded.org/g/bitbake-devel/topic/110839321
- v2: https://lists.openembedded.org/g/bitbake-devel/topic/111289667
- v3: https://lists.openembedded.org/g/bitbake-devel/topic/111395041

Eric Meyers (2):
fetch2/npm: Adding npmrc support for private registry authentication
doc: bitbake-user-manual-ref-variables: document BB_USE_HOME_NPMRC

 .../bitbake-user-manual-ref-variables.rst     |  6 ++++++
 lib/bb/fetch2/npm.py                          | 21 +++++++++++++++++++
 2 files changed, 27 insertions(+)
#1 lore raw

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

Cc Geoff Parker <[email protected]>
Cc Chuck Wolber <[email protected]>
---
 lib/bb/fetch2/npm.py | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
lib/bb/fetch2/npm.py +21 -0
index c09f05044..2c7ed5340 100644
@@ -91,6 +91,12 @@ class NpmEnvironment(object):
         self.d = d
 
         self.user_config = tempfile.NamedTemporaryFile(mode="w", buffering=1)
+
+        hn = self._home_npmrc(d)
+        if hn is not None:
+            with open(hn, 'r') as hnf:
+                self.user_config.write(hnf.read())
+
         for key, value in configs:
             self.user_config.write("%s=%s\n" % (key, value))
 
@@ -103,6 +109,21 @@ class NpmEnvironment(object):
         if self.user_config:
             self.user_config.close()
 
+    def _home_npmrc(self, d):
+        """Function to return user's HOME .npmrc file (or None if it doesn't exist)"""
+        home_npmrc_file = None
+        if d.getVar("BB_USE_HOME_NPMRC") == "1":
+            home_npmrc_file = os.path.join(os.environ.get("HOME"), ".npmrc")
+            if not os.path.exists(home_npmrc_file):
+                home_npmrc_file = None
+                bb.warn("BB_USE_HOME_NPMRC flag is set, but NO valid .npmrc was detected - "\
+                        "check that a valid .npmrc file exists in your HOME directory.")
+            else:
+                bb.warn("BB_USE_HOME_NPMRC flag is set and valid .npmrc detected - "\
+                        "npm fetcher will now use the .npmrc file in the HOME directory.")
+
+        return home_npmrc_file
+
     def run(self, cmd, args=None, configs=None, workdir=None):
         """Run npm command in a controlled environment"""
         with tempfile.TemporaryDirectory() as tmpdir:
#2 lore raw

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

Quoted 48 lines · Eric Meyers
Cc Geoff Parker <[email protected]>
Cc Chuck Wolber <[email protected]>
---
 lib/bb/fetch2/npm.py | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
lib/bb/fetch2/npm.py +8 -0
index c09f05044..2c7ed5340 100644
@@ -91,6 +91,12 @@ class NpmEnvironment(object):
         self.d = d
 
         self.user_config = tempfile.NamedTemporaryFile(mode="w",
 buffering=1)
+
+        hn = self._home_npmrc(d)
+        if hn is not None:
+            with open(hn, 'r') as hnf:
+                self.user_config.write(hnf.read())
+
         for key, value in configs:
             self.user_config.write("%s=%s\n" % (key, value))
@@ -103,6 +109,21 @@ class NpmEnvironment(object):
         if self.user_config:
             self.user_config.close()
 
+    def _home_npmrc(self, d):
+        """Function to return user's HOME .npmrc file (or None if it

doesn't exist)"""

lib/bb/fetch2/npm.py +3 -0
+        home_npmrc_file = None
+        if d.getVar("BB_USE_HOME_NPMRC") == "1":
+            home_npmrc_file = os.path.join(os.environ.get("HOME"),

".npmrc")

lib/bb/fetch2/npm.py +3 -0
+            if not os.path.exists(home_npmrc_file):
+                home_npmrc_file = None
+                bb.warn("BB_USE_HOME_NPMRC flag is set, but NO valid

.npmrc was detected - "\
+ "check that a valid .npmrc file exists in your
HOME directory.")

lib/bb/fetch2/npm.py +2 -0
+            else:
+                bb.warn("BB_USE_HOME_NPMRC flag is set and valid .npmrc

detected - "\
+ "npm fetcher will now use the .npmrc file in the
HOME directory.")

lib/bb/fetch2/npm.py class NpmEnvironment(object): · line 129

Quoted 6 lines
lib/bb/fetch2/npm.py +3 -0
+
+        return home_npmrc_file
+
     def run(self, cmd, args=None, configs=None, workdir=None):
         """Run npm command in a controlled environment"""
         with tempfile.TemporaryDirectory() as tmpdir:

Nice work! Only one remaining issue that I can see. I do not think you need to
warn when there is no .npmrc present. That should enable you to simplify your
logic a bit to something like this:

home_npmrc_file = os.path.join(os.environ.get("HOME"), ".npmrc") if d.getVar("BB_USE_HOME_NPMRC") == "1" and os.path.exists(home_npmrc_file):
bb.warn(f"BB_USE_HOME_NPMRC flag set and valid .npmrc detected - "\
"npm fetcher will use {home_npmrc_file}")
return home_npmrc_file
return None

..Ch:W..

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

PATCH v4 2/2 doc: bitbake-user-manual-ref-variables: document BB_USE_HOME_NPMRC

In reply to Eric Meyers
Cc Geoff Parker <[email protected]>
Cc Chuck Wolber <[email protected]>
---
 .../bitbake-user-manual-ref-variables.rst                   | 6 ++++++
 1 file changed, 6 insertions(+)
doc/bitbake-user-manual/bitbake-user-manual-ref-variables.rst +6 -0
index 858de6347..00300d0d8 100644
@@ -692,6 +692,12 @@ overview of their function and contents.
       Within an executing task, this variable holds the hash of the task as
       returned by the currently enabled signature generator.
 
+   :term:`BB_USE_HOME_NPMRC`
+      Controls whether or not BitBake uses the user's .npmrc file within their
+      home directory within the npm fetcher. This can be used for authentication
+      of private NPM registries, among other uses. This is turned off by default
+      and requires the user to explicitly set it to "1" to enable.
+
    :term:`BB_VERBOSE_LOGS`
       Controls how verbose BitBake is during builds. If set, shell scripts
       echo commands and shell script output appears on standard out