Bug 1356524 - Only add Authorization header when sending requests to the tooltool url. r=gps
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 14 Apr 2017 10:28:42 +0900
changeset 563294 be3413a130d8a0c42c936b8a2bd6e3d119ad6ede
parent 563293 6370e9cae2485dd9231dd47d15088ff171acbbcf
child 563295 98c08360e61b83fe1eeefd42c93a9099ebf86806
push id54258
push usercpeterson@mozilla.com
push dateSun, 16 Apr 2017 05:52:14 +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):