From 174d3ce428fb477520c318257bcb09ddb76b29e4 Mon Sep 17 00:00:00 2001 From: Mathieu Pillard Date: Tue, 1 Mar 2016 14:42:24 +0100 Subject: [PATCH 1/2] Avoid 500 error when IARC returns invalid JSON, regardless of the reason --- lib/iarc_v2/client.py | 6 +++++- lib/iarc_v2/tests/test_client.py | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/iarc_v2/client.py b/lib/iarc_v2/client.py index 6b45fe29f8d..e55a84012af 100644 --- a/lib/iarc_v2/client.py +++ b/lib/iarc_v2/client.py @@ -36,7 +36,11 @@ def _iarc_request(endpoint_name, data): url = urljoin(settings.IARC_V2_SERVICE_ENDPOINT, endpoint_name) headers = _iarc_headers() response = requests.post(url, headers=headers, json=data) - return response.json() + try: + value = response.json() + except ValueError: + value = {} + return value def _iarc_app_data(app): diff --git a/lib/iarc_v2/tests/test_client.py b/lib/iarc_v2/tests/test_client.py index 8d695ce5897..231321e40db 100644 --- a/lib/iarc_v2/tests/test_client.py +++ b/lib/iarc_v2/tests/test_client.py @@ -197,6 +197,12 @@ def test_iarc_app_data_not_public(self): 'Publish': False, 'ProductName': unicode(self.app.name)}) + @responses.activate + def test_search_cert_error(self): + setup_mock_response('SearchCerts', data='error') + serializer = search_cert(mock.Mock(), unicode(uuid4())) + eq_(serializer.is_valid(), False) + @responses.activate def test_search_cert_uuid_hex_string_w_separators(self): setup_mock_response('SearchCerts') From 624215d59081d58af0d3ac6e0d89a67dc165ac4f Mon Sep 17 00:00:00 2001 From: Mathieu Pillard Date: Tue, 1 Mar 2016 16:50:55 +0100 Subject: [PATCH 2/2] Add some additional logging around iarc calls --- lib/iarc_v2/client.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/iarc_v2/client.py b/lib/iarc_v2/client.py index e55a84012af..c2443a59602 100644 --- a/lib/iarc_v2/client.py +++ b/lib/iarc_v2/client.py @@ -80,6 +80,8 @@ def get_rating_changes(date=None): start_date = date or datetime.datetime.utcnow() end_date = start_date - datetime.timedelta(days=1) + log.info('Calling IARC GetRatingChanges from %s to %s', + start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')) data = _iarc_request('GetRatingChanges', { 'StartDate': start_date.strftime('%Y-%m-%d'), 'EndDate': end_date.strftime('%Y-%m-%d'), @@ -118,6 +120,8 @@ def search_and_attach_cert(app, cert_id): def search_cert(app, cert_id): """Ask IARC for information about a cert.""" + log.info('Calling IARC SearchCert for app %s, cert %s', + app.pk, unicode(UUID(cert_id))) data = _iarc_request('SearchCerts', {'CertID': unicode(UUID(cert_id))}) # We don't care about MatchFound, serializer won't find the right fields # if no match is found. @@ -129,6 +133,8 @@ def _attach_to_cert(app, cert_id): """Tell IARC to attach a cert to an app.""" data = _iarc_app_data(app) data['CertID'] = unicode(UUID(cert_id)) + log.info('Calling IARC AttachToCert for app %s, cert %s', + app.pk, data['CertID']) return _iarc_request('AttachToCert', data) @@ -143,7 +149,7 @@ def publish(app_id): return try: cert_id = app.iarc_cert.cert_id - data = _update_certs(cert_id, 'Publish') + data = _update_certs(app_id, cert_id, 'Publish') except IARCCert.DoesNotExist: data = None return data @@ -160,7 +166,7 @@ def unpublish(app_id): return try: cert_id = app.iarc_cert.cert_id - data = _update_certs(cert_id, 'RemoveProduct') + data = _update_certs(app_id, cert_id, 'RemoveProduct') except IARCCert.DoesNotExist: data = None return data @@ -176,7 +182,7 @@ def refresh(app): # changes. -def _update_certs(cert_id, action): +def _update_certs(app_id, cert_id, action): """ UpdateCerts to tell IARC when we publish or unpublish a product. Endpoint can handle batch updates, but we only need one at a time. @@ -198,4 +204,6 @@ def _update_certs(cert_id, action): 'Action': action, }] } + log.info('Calling IARC UpdateCert %s for app %s, cert %s', + action, app_id, data['UpdateList'][0]['CertID']) return _iarc_request('UpdateCerts', data)