From 6ed1702b01d56e9fb0cd0ae49ff8a5f0a787b6b1 Mon Sep 17 00:00:00 2001 From: Yusuf Zainee Date: Wed, 19 Jun 2019 16:44:27 +0530 Subject: [PATCH 1/3] non cve latest version --- bayesian/utils.py | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/bayesian/utils.py b/bayesian/utils.py index 3ace1792..4a2c3e9d 100644 --- a/bayesian/utils.py +++ b/bayesian/utils.py @@ -349,35 +349,40 @@ def get_analyses_from_graph(ecosystem, package, version): if graph_req is not None: resp = graph_req.json() + result_data = resp['result'].get('data') - if not (resp['result'].get('data') and len(resp['result'].get('data')) > 0): + if not (result_data and len(result_data) > 0): # trigger unknown component flow in API for missing package return None clubbed_data.append({ - "epv": resp['result']['data'] + "epv": result_data }) - if "cve" in resp['result'].get('data')[0]: - script2 = """\ -g.V().has('pecosystem', ecosystem).has('pname', name).has('version', version)\ -.in('has_version').out('has_version').not(out('has_cve')).values('version').dedup();\ -""" - payload = { - 'gremlin': script2, - 'bindings': { - 'ecosystem': ecosystem, - 'name': package, - 'version': version - } - } - - graph_req2 = post(gremlin_url, data=json.dumps(payload)) - if graph_req2 is not None: - resp2 = graph_req2.json() + if "cve" in result_data[0]: + if "latest_non_cve_version" in result_data[0]['package']: clubbed_data.append({ - "recommended_versions": resp2['result']['data'] + "recommended_versions": result_data[0]['package']['latest_non_cve_version'] }) + else: + script2 = "g.V().has('pecosystem', ecosystem).has('pname', name)" \ + ".has('version', version).in('has_version')" \ + ".out('has_version').not(out('has_cve')).values('version').dedup();" + payload = { + 'gremlin': script2, + 'bindings': { + 'ecosystem': ecosystem, + 'name': package, + 'version': version + } + } + + graph_req2 = post(gremlin_url, data=json.dumps(payload)) + if graph_req2 is not None: + resp2 = graph_req2.json() + clubbed_data.append({ + "recommended_versions": resp2['result']['data'] + }) else: clubbed_data.append({ "recommended_versions": [] From ccc57d8bdd4f7436b722792f34952e5ac27617e8 Mon Sep 17 00:00:00 2001 From: Yusuf Zainee Date: Wed, 19 Jun 2019 18:22:30 +0530 Subject: [PATCH 2/3] test covg increased --- bayesian/utils.py | 9 ++++++--- tests/test_utils.py | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/bayesian/utils.py b/bayesian/utils.py index 4a2c3e9d..f702b429 100644 --- a/bayesian/utils.py +++ b/bayesian/utils.py @@ -12,6 +12,7 @@ import shutil import hashlib import zipfile +import logging from io import BytesIO from functools import lru_cache @@ -38,6 +39,8 @@ from github import Github, BadCredentialsException, GithubException, RateLimitExceededException from git import Repo, Actor +logger = logging.getLogger(__file__) + # TODO remove hardcoded gremlin_url when moving to Production This is just # a stop-gap measure for demo @@ -388,13 +391,13 @@ def get_analyses_from_graph(ecosystem, package, version): "recommended_versions": [] }) except Exception as e: - current_app.logger.debug(' '.join([type(e), ':', str(e)])) + logger.debug(' '.join([type(e), ':', str(e)])) return None finally: elapsed_seconds = (datetime.datetime.now() - start).total_seconds() epv = "{e}/{p}/{v}".format(e=ecosystem, p=package, v=version) - current_app.logger.debug("Gremlin request {p} took {t} seconds.".format(p=epv, - t=elapsed_seconds)) + logger.debug("Gremlin request {p} took {t} seconds.".format(p=epv, + t=elapsed_seconds)) resp = generate_recommendation(clubbed_data, package, version) return resp diff --git a/tests/test_utils.py b/tests/test_utils.py index 09c25bdf..a26bc87a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -21,7 +21,8 @@ CveByDateEcosystemUtils, resolved_files_exist, get_ecosystem_from_manifest, - check_for_accepted_ecosystem + check_for_accepted_ecosystem, + get_analyses_from_graph ) from f8a_worker.enums import EcosystemBackend from f8a_worker.models import Analysis, Ecosystem, Package, Version, WorkerResult @@ -61,6 +62,33 @@ } } +non_cve_input = { + "result": { + "data": [ + { + "cve": { + "ecosystem": ["npm"], + "cve_id": ["CVE-2018-0001"], + "cvss_v2": [10.0], + "nvd_status": ["Awaiting Analyses"], + "description": ["Some description here updated just now."], + "modified_date": ["20180911"] + }, + "version": { + "pname": ["lodash"], + "version": ["4.17.4"], + "pecosystem": ["npm"] + }, + "package": { + "name": ["lodash"], + "latest_non_cve_version": ["4.17.11"], + "pecosystem": ["npm"] + } + } + ] + } +} + @pytest.fixture def analyses(app): @@ -445,3 +473,13 @@ def test_check_for_accepted_ecosystem(): resp = check_for_accepted_ecosystem("abcd") assert not resp + + +@patch("bayesian.utils.post") +def test_get_analyses_from_graph(mocker): + """Test the get_analyses_from_graph function.""" + mocker.return_value = mock_response = Mock() + mock_response.json.return_value = non_cve_input + resp = get_analyses_from_graph("npm", "lodash", "4.17.4") + print(resp) + assert resp['result']['recommendation']['change_to'] == "4.17.11" From 58b56f88fab1fb8d0d4715f984047cea54cfb264 Mon Sep 17 00:00:00 2001 From: Yusuf Zainee Date: Wed, 19 Jun 2019 18:24:39 +0530 Subject: [PATCH 3/3] print st removed --- tests/test_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index a26bc87a..74deccb5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -481,5 +481,4 @@ def test_get_analyses_from_graph(mocker): mocker.return_value = mock_response = Mock() mock_response.json.return_value = non_cve_input resp = get_analyses_from_graph("npm", "lodash", "4.17.4") - print(resp) assert resp['result']['recommendation']['change_to'] == "4.17.11"