Generalize /vulnerabilities/coordinates endpoint for C & Python
@@ -16,7 +16,6 @@ import java.io.IOException;
import java.util.List;
@RestController
-@RequestMapping("/mvn")
public class VulnerabilityApi {
private final Logger logger = LoggerFactory.getLogger(getClass());
@@ -80,9 +79,10 @@ public class VulnerabilityApi {
@PostMapping(value = "/vulnerabilities/coordinates", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<String> getCoordinatesVulnerabilities(@RequestBody List<String> mavenCoordinates,
@RequestParam(required = false, defaultValue = "") List<String> attributes) {
-
- logger.info("Received a list of Maven coordinates");
- var jsonArray = new JSONArray();
+ JSONArray jsonArray = new JSONArray();
+ logger.info("Received a list of coordinates");
+ switch (KnowledgeBaseConnector.forge) {
+ case "maven": {
mavenCoordinates.stream().map(c -> {
var package_name = c.split(Constants.mvnCoordinateSeparator)[0]
+ Constants.mvnCoordinateSeparator
@@ -106,6 +106,33 @@ public class VulnerabilityApi {
json.put(c, vulnerabilities);
return json;
}).forEach(jsonArray::put);
+ break;
+ }
+ default : {
+ mavenCoordinates.stream().map(c -> {
+ var package_name = c.split(Constants.mvnCoordinateSeparator)[0];
+ var package_version = c.split(Constants.mvnCoordinateSeparator)[1];
+ var vulnerabilities = "";
+ if (!KnowledgeBaseConnector.kbDao.assertPackageExistence(package_name, package_version)) {
+ try {
+ LazyIngestionProvider.ingestArtifactWithDependencies(package_name, package_version);
+ logger.info("Coordinate " + c + " not found, but should be processed soon. Try again later");
+ } catch (Exception e) {
+ logger.info("Cannot find coordinate " + c + ": " +e.getMessage());
+ }
+ } else {
+ var statements = KnowledgeBaseConnector.kbDao
+ .getPackageVersionVulnerabilities(package_name, package_version, false);
+ JSONArray vul = new JSONArray(statements);
+ vulnerabilities = getStatementsAttributes(vul, attributes).toString();
+ }
+ var json = new JSONObject();
+ json.put(c, vulnerabilities);
+ return json;
+ }).forEach(jsonArray::put);
+ break;
+ }
+ }
var result = jsonArray.toString();
result = result.replace("\\/", "/");
return new ResponseEntity<>(result, HttpStatus.OK);
