def vulnerability_analysis_post(): """Handle the POST REST API call. Component Analyses Batch is 3 Step Process: 1. Gather and clean Request. 2. Query GraphDB. 3. Build Stack Recommendation """ input_json: Dict = request.get_json() ecosystem: str = input_json.get('ecosystem') try: # Step1: Gather and clean Request packages_list = validate_input(input_json, ecosystem) # Step2: Get aggregated CA data from Query GraphDB, graph_response = get_vulnerability_data(ecosystem, packages_list) # Step3: Build Unknown packages and Generates Stack Recommendation. stack_recommendation = get_known_pkgs(graph_response, packages_list) except BadRequest as br: logger.error(br) raise HTTPError(400, str(br)) from br except Exception as e: msg = "Internal Server Exception. Please contact us if problem persists." logger.error(e) raise HTTPError(500, msg) from e return jsonify(stack_recommendation), 200
def test_get_known_pkgs_no_cve(self): """Test Known Pkgs, No Cve.""" input_pkgs = [{"name": "markdown2", "version": "2.3.2"}] gremlin_batch_data_no_cve = {"result": {"data": []}} stack_recommendation = get_known_pkgs(gremlin_batch_data_no_cve, input_pkgs) ideal_output = [{ 'name': 'markdown2', 'version': '2.3.2', 'vulnerabilities': [] }] self.assertListEqual(stack_recommendation, ideal_output)
def test_get_known_pkgs_with_cve(self): """Test Known Pkgs with Cve(VA).""" input_pkgs = [{"name": "st", "version": "0.2.5"}] batch_data_cve = os.path.join('tests/data/gremlin/va.json') with open(batch_data_cve) as f: gremlin_batch_data_cve = json.load(f) stack_recommendation = get_known_pkgs(gremlin_batch_data_cve, input_pkgs) ideal_output = [{ 'name': 'st', 'version': '0.2.5', 'vulnerabilities': [{ "fixed_in": ["1.2.2", "1.2.3", "1.2.4"], "id": "SNYK-JS-ST-10820", "severity": "medium", "title": "Open Redirect", "url": "https://snyk.io/vuln/SNYK-JS-ST-10820" }] }] self.assertListEqual(stack_recommendation, ideal_output)