You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by ak...@apache.org on 2024/04/07 17:15:34 UTC

(buildstream-plugins) branch abderrahim/bearer-auth created (now bfe4921)

This is an automated email from the ASF dual-hosted git repository.

akitouni pushed a change to branch abderrahim/bearer-auth
in repository https://gitbox.apache.org/repos/asf/buildstream-plugins.git


      at bfe4921  cargo: add support for bearer http authentication

This branch includes the following new commits:

     new 5302a1f  cargo: Use the new suffix parameter to translate_url
     new bfe4921  cargo: add support for bearer http authentication

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(buildstream-plugins) 01/02: cargo: Use the new suffix parameter to translate_url

Posted by ak...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

akitouni pushed a commit to branch abderrahim/bearer-auth
in repository https://gitbox.apache.org/repos/asf/buildstream-plugins.git

commit 5302a1fc35a3b0d7072504299882a5c20f7ae3a0
Author: Abderrahim Kitouni <ab...@codethink.co.uk>
AuthorDate: Tue Mar 5 15:05:45 2024 +0100

    cargo: Use the new suffix parameter to translate_url
    
    Depending on the case, we might be unable to know whether the url provided
    has a trailing slash or not (e.g. if it is an alias). To work around that,
    we try to append a trailing slash to the specified URL if necessary, without
    breaking cache keys.
    
    This behaviour can be incompatible with 2.0 in some cases, but is the
    behaviour requested in https://github.com/apache/buildstream-plugins/pull/52
    
    To keep the compatibility with older buildstream versions, we check the
    version and only use suffix with buildstream >= 2.2.
    
    Co-authored by: Jürg Billeter <j...@bitron.ch>
---
 src/buildstream_plugins/sources/cargo.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/buildstream_plugins/sources/cargo.py b/src/buildstream_plugins/sources/cargo.py
index 42fcaa3..e30cd06 100644
--- a/src/buildstream_plugins/sources/cargo.py
+++ b/src/buildstream_plugins/sources/cargo.py
@@ -35,8 +35,8 @@ obtain the crates automatically into %{vendordir}.
    # Specify the cargo source kind
    kind: cargo
 
-   # Url of the crates repository to download from (default: https://static.crates.io/crates)
-   url: https://static.crates.io/crates
+   # Url of the crates repository to download from (default: https://static.crates.io/crates/)
+   url: https://static.crates.io/crates/
 
    # Internal source reference, this is a list of dictionaries
    # which store the crate names and versions.


(buildstream-plugins) 02/02: cargo: add support for bearer http authentication

Posted by ak...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

akitouni pushed a commit to branch abderrahim/bearer-auth
in repository https://gitbox.apache.org/repos/asf/buildstream-plugins.git

commit bfe4921a209b5c45996866ba639ab7b55a2b5e20
Author: Abderrahim Kitouni <ab...@codethink.co.uk>
AuthorDate: Mon Mar 11 15:48:09 2024 +0100

    cargo: add support for bearer http authentication
---
 src/buildstream_plugins/sources/_utils.py | 16 ++++++++++++----
 src/buildstream_plugins/sources/cargo.py  | 22 ++++++++++++++--------
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/src/buildstream_plugins/sources/_utils.py b/src/buildstream_plugins/sources/_utils.py
index 27dfeb3..1ed10ff 100644
--- a/src/buildstream_plugins/sources/_utils.py
+++ b/src/buildstream_plugins/sources/_utils.py
@@ -62,22 +62,30 @@ class _UrlOpenerCreator:
     def __init__(self, netrc_config):
         self.netrc_config = netrc_config
 
-    def get_url_opener(self):
-        if self.netrc_config:
+    def get_url_opener(self, bearer_auth):
+        if self.netrc_config and not bearer_auth:
             netrc_pw_mgr = _NetrcPasswordManager(self.netrc_config)
             http_auth = urllib.request.HTTPBasicAuthHandler(netrc_pw_mgr)
             return urllib.request.build_opener(http_auth)
         return urllib.request.build_opener()
 
 
-def download_file(url, etag, directory):
+def download_file(url, etag, directory, auth_scheme):
     opener_creator = _UrlOpenerCreator(_parse_netrc())
-    opener = opener_creator.get_url_opener()
+    opener = opener_creator.get_url_opener(auth_scheme == "bearer")
     default_name = os.path.basename(url)
     request = urllib.request.Request(url)
     request.add_header("Accept", "*/*")
     request.add_header("User-Agent", "BuildStream/2")
 
+    if opener_creator.netrc_config and auth_scheme == "bearer":
+        parts = urllib.parse.urlsplit(url)
+        entry = opener_creator.netrc_config.authenticators(parts.hostname)
+        if entry:
+            _, _, password = entry
+            auth_header = "Bearer " + password
+            request.add_header("Authorization", auth_header)
+
     if etag is not None:
         request.add_header("If-None-Match", etag)
 
diff --git a/src/buildstream_plugins/sources/cargo.py b/src/buildstream_plugins/sources/cargo.py
index e30cd06..4b9391a 100644
--- a/src/buildstream_plugins/sources/cargo.py
+++ b/src/buildstream_plugins/sources/cargo.py
@@ -121,10 +121,11 @@ class Crate(SourceFetcher):
         if os.path.isfile(self._get_mirror_file()):
             return  # pragma: nocover
 
+        extra_data = {}
         # Download the crate
-        crate_url = self._get_url(alias_override)
+        crate_url, auth_scheme = self._get_url(alias_override)
         with self.cargo.timed_activity("Downloading: {}".format(crate_url), silent_nested=True):
-            sha256 = self._download(crate_url)
+            sha256 = self._download(crate_url, auth_scheme)
             if self.sha is not None and sha256 != self.sha:
                 raise SourceError(
                     "File downloaded from {} has sha256sum '{}', not '{}'!".format(crate_url, sha256, self.sha)
@@ -194,7 +195,7 @@ class Crate(SourceFetcher):
     # Returns:
     #    (str): The sha256 checksum of the downloaded crate
     #
-    def _download(self, url):
+    def _download(self, url, auth_scheme):
         # We do not use etag in case what we have in cache is
         # not matching ref in order to be able to recover from
         # corrupted download.
@@ -204,7 +205,7 @@ class Crate(SourceFetcher):
             etag = None
 
         with self.cargo.tempdir() as td:
-            local_file, etag, error = download_file(url, etag, td)
+            local_file, etag, error = download_file(url, etag, td, auth_scheme)
 
             if error:
                 raise SourceError("{}: Error mirroring {}: {}".format(self, url, error), temporary=True)
@@ -234,10 +235,15 @@ class Crate(SourceFetcher):
     #
     def _get_url(self, alias=None):
         path = "{name}/{name}-{version}.crate".format(name=self.name, version=self.version)
+        extra_data = {}
         if utils.get_bst_version() >= (2, 2):
-            return self.cargo.translate_url(self.cargo.url, suffix=path, alias_override=alias)
+            translated_url = self.cargo.translate_url(
+                self.cargo.url, suffix=path, alias_override=alias, extra_data=extra_data
+            )
         else:
-            return self.cargo.translate_url(self.cargo.url, alias_override=alias) + path
+            translated_url = self.cargo.translate_url(self.cargo.url, alias_override=alias) + path
+
+        return translated_url, extra_data.get("http-auth")
 
     # _get_etag()
     #
@@ -387,9 +393,9 @@ class CargoSource(Source):
 
             crate = Crate(self, crate_obj["name"], crate_obj["version"])
 
-            crate_url = crate._get_url()
+            crate_url, auth_scheme = crate._get_url()
             with self.timed_activity("Downloading: {}".format(crate_url), silent_nested=True):
-                crate_obj["sha"] = crate._download(crate_url)
+                crate_obj["sha"] = crate._download(crate_url, auth_scheme)
 
         return new_ref