Bug 1356524 - Only add Authorization header when sending requests to the tooltool url. r?gps draft
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 14 Apr 2017 10:28:42 +0900
changeset 563082 28c0c3af9c1569cf9cfa6fab6d5c748e2cdf4396
parent 563081 76b21e5cbafe5a679975fcd33728c3e2c9eb9ee8
child 563083 915a11d499fa708895f2a3940746488b52a91cf3
push id54196
push userbmo:mh+mozilla@glandium.org
push dateFri, 14 Apr 2017 22:06:59 +0000
reviewersgps
bugs1356524
milestone55.0a1
Bug 1356524 - Only add Authorization header when sending requests to the tooltool url. r?gps We're going to potentially use the same download manager for tooltool and taskcluster artifacts, and we don't want to send the tooltool authentication header to the taskcluster requests.
python/mozbuild/mozbuild/mach_commands.py
--- a/python/mozbuild/mozbuild/mach_commands.py
+++ b/python/mozbuild/mozbuild/mach_commands.py
@@ -1560,16 +1560,17 @@ class PackageFrontend(MachCommandBase):
         '''Download, cache and install pre-built toolchains.
         '''
         from mozbuild.artifacts import ArtifactCache
         from mozbuild.action.tooltool import (
             FileRecord,
             open_manifest,
             unpack_file,
         )
+        from requests.adapters import HTTPAdapter
         import redo
         import requests
         import shutil
 
         self._set_log_level(verbose)
         # Normally, we'd use self.log_manager.enable_unstructured(),
         # but that enables all logging, while we only really want tooltool's
         # and it also makes structured log output twice.
@@ -1589,18 +1590,26 @@ class PackageFrontend(MachCommandBase):
                         'https://api.pub.build.mozilla.org/tooltool').rstrip('/')
 
         cache = ArtifactCache(cache_dir=cache_dir, log=self.log,
                               skip_cache=skip_cache)
 
         if authentication_file:
             with open(authentication_file, 'rb') as f:
                 token = f.read().strip()
-            cache._download_manager.session.headers['Authorization'] = \
-                'Bearer {}'.format(token)
+
+            class TooltoolAuthenticator(HTTPAdapter):
+                def send(self, request, *args, **kwargs):
+                    request.headers['Authorization'] = \
+                        'Bearer {}'.format(token)
+                    return super(TooltoolAuthenticator, self).send(
+                        request, *args, **kwargs)
+
+            cache._download_manager.session.mount(
+                tooltool_url, TooltoolAuthenticator())
 
         manifest = open_manifest(tooltool_manifest)
         downloaded_files = {}
 
         for record in manifest.file_records:
             if files and not any(record.filename == f or
                                       record.filename.startswith('%s.' % f)
                                       for f in files):