diff mbox series

[2/3] fetch2/npmsw: Add basic auth credential support for npmsw fetcher

Message ID 20250127142918.163-3-eric.meyers@arthrex.com
State New
Headers show
Series NPM/NPM Shrinkwrap Basic Authentication Credential Support: | expand

Commit Message

Eric Meyers Jan. 27, 2025, 2:29 p.m. UTC
From: Eric Meyers <eric.meyers@arthrex.com>

Signed-off-by: Eric Meyers <eric.meyers@arthrex.com>
Cc: Geoff Parker <geoffrey.parker@arthrex.com>
---
 lib/bb/fetch2/npmsw.py | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
diff mbox series

Patch

diff --git a/lib/bb/fetch2/npmsw.py b/lib/bb/fetch2/npmsw.py
index 2f9599ee9..1c894c561 100644
--- a/lib/bb/fetch2/npmsw.py
+++ b/lib/bb/fetch2/npmsw.py
@@ -15,12 +15,24 @@  Supported SRC_URI options are:
 
 - destsuffix
     Specifies the directory to use to unpack the dependencies (default: ${S}).
+
+- username
+    Specifies the basic auth username to use when accessing an npm registry specified in the shrinkwrap.
+
+- password
+    Specifies the basic auth password (or API key) to use when accessing the npm registry specified in the shrinkwrap.
+
+- basic_auth_registry
+    Specifies the registry within the npm shrinkwrap to use basic authentication with.
+    NOTE: This parameter is optional - if nothing is passed in but a username/password IS supplied, then
+    fetcher will assume to use basic auth credentials for all registries within the shrinkwrap file.
 """
 
 import json
 import os
 import re
 import bb
+import urllib.parse
 from bb.fetch2 import Fetch
 from bb.fetch2 import FetchMethod
 from bb.fetch2 import ParameterError
@@ -67,10 +79,26 @@  class NpmShrinkWrap(FetchMethod):
 
     def urldata_init(self, ud, d):
         """Init npmsw specific variables within url data"""
+        ud.shrinkwrap_file = None
+        ud.dev = None
+        ud.deps = None
+        ud.basic_auth_registry = None
+        ud.username = None
+        ud.password = None
 
         # Get the 'shrinkwrap' parameter
         ud.shrinkwrap_file = re.sub(r"^npmsw://", "", ud.url.split(";")[0])
 
+        if "basic_auth_registry" in ud.parm:
+            ud.basic_auth_registry = ud.parm.get("basic_auth_registry")
+
+        if "username" in ud.parm:
+            ud.username = ud.parm.get("username")
+
+        if "password" in ud.parm:
+            ud.password = ud.parm.get("password")
+
+
         # Get the 'dev' parameter
         ud.dev = bb.utils.to_boolean(ud.parm.get("dev"), False)
 
@@ -107,6 +135,14 @@  class NpmShrinkWrap(FetchMethod):
                 checksum_name, checksum_expected = npm_integrity(integrity)
                 uri.params[checksum_name] = checksum_expected
 
+                # Check if the registry we are using matches the registry passed in
+                # that the user wants to use for basic authentication
+                if ud.basic_auth_registry is not None:
+                    use_basic_auth = urllib.parse.urlparse(resolved).hostname in ud.basic_auth_registry
+                    if (ud.username is not None and ud.password is not None and use_basic_auth):
+                        uri.params["user"] = ud.username
+                        uri.params["pswd"] = ud.password
+
                 url = str(uri)
 
                 localpath = os.path.join(d.getVar("DL_DIR"), localfile)