From c9ba46c272efd11571cd26c0204b4c92ddcdf080 Mon Sep 17 00:00:00 2001 From: William Durand Date: Tue, 17 Sep 2019 16:02:37 +0200 Subject: [PATCH] Send download_url to the scanners (#12317) --- docker-compose.private.yml | 16 +++++++++++++++- src/olympia/scanners/tasks.py | 23 ++++++++++++++--------- src/olympia/scanners/tests/test_tasks.py | 6 ++++-- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/docker-compose.private.yml b/docker-compose.private.yml index 516c83f4c8..a096f244bc 100644 --- a/docker-compose.private.yml +++ b/docker-compose.private.yml @@ -1,7 +1,7 @@ version: "2.3" services: - web: + worker: depends_on: - customs - wat @@ -9,7 +9,21 @@ services: customs: build: context: ./private/addons-customs-scanner + environment: + - ALLOWED_ORIGIN=http://olympia.test + - PORT=10101 + links: + - "nginx:olympia.test" + depends_on: + - web wat: build: context: ./private/addons-wat + environment: + - ALLOWED_ORIGIN=http://olympia.test + - PORT=10102 + links: + - "nginx:olympia.test" + depends_on: + - web diff --git a/src/olympia/scanners/tasks.py b/src/olympia/scanners/tasks.py index 8074ac7982..321e7ed9d7 100644 --- a/src/olympia/scanners/tasks.py +++ b/src/olympia/scanners/tasks.py @@ -33,22 +33,27 @@ def run_scanner(upload_pk, scanner, api_url, api_key): try: if not os.path.exists(upload.path): - raise ValueError('Path "{}" is not a file or directory or does ' - 'not exist.' .format(upload.path)) + raise ValueError('File "{}" does not exist.' .format(upload.path)) result = ScannersResult() result.upload = upload result.scanner = scanner with statsd.timer('devhub.{}'.format(scanner_name)): - headers = {'Authorization': 'Bearer {}'.format(api_key)} - with open(upload.path, 'rb') as xpi: - response = requests.post(url=api_url, - files={'xpi': xpi}, - headers=headers, - timeout=settings.SCANNER_TIMEOUT) + json_payload = { + 'api_key': api_key, + 'download_url': upload.get_authenticated_download_url(), + } + response = requests.post(url=api_url, + json=json_payload, + timeout=settings.SCANNER_TIMEOUT) + + try: + results = response.json() + except ValueError: + # Log the response body when JSON decoding has failed. + raise ValueError(response.text) - results = response.json() if 'error' in results: raise ValueError(results) diff --git a/src/olympia/scanners/tests/test_tasks.py b/src/olympia/scanners/tests/test_tasks.py index 869209d057..43c71e23eb 100644 --- a/src/olympia/scanners/tests/test_tasks.py +++ b/src/olympia/scanners/tests/test_tasks.py @@ -52,8 +52,10 @@ class TestRunScanner(UploadTest, TestCase): assert requests_mock.called requests_mock.assert_called_with( url=self.API_URL, - files={'xpi': mock.ANY}, - headers={'Authorization': 'Bearer {}'.format(self.API_KEY)}, + json={ + 'api_key': self.API_KEY, + 'download_url': upload.get_authenticated_download_url(), + }, timeout=123 ) result = ScannersResult.objects.all()[0]