Skip to content

Commit

Permalink
Merge pull request #322 from KanchanaAradhya/VulnLatestTrueCheck
Browse files Browse the repository at this point in the history
Added latest true check
  • Loading branch information
santhoshigorle committed Sep 24, 2019
2 parents f1c3e97 + 87d1c95 commit 44cb4e7
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 642 deletions.
Expand Up @@ -475,47 +475,6 @@ public ResponseEntity<Object> getVulnerabilityDetailsByResourceId(
return ResponseUtils.buildSucessResponse(response);
}

/**
* Gets the vulnerability distribution summary.
*
* @param assetGroup
* the asset group
* @param severity
* the severity
* @return ResponseEntity<Object>
*/
@RequestMapping(path = "/v1/vulnerabilities/distributionsummary", method = RequestMethod.GET)
public ResponseEntity<Object> getVulnerabilityDistributionSummary(@RequestParam("ag") String assetGroup,
@RequestParam(name = "severity", required = false) String severity) {

try {
return ResponseUtils.buildSucessResponse(
vulnerabilityService.getVulnerabilityDistributionSummary(assetGroup, severity));
} catch (Exception e) {
return ResponseUtils.buildFailureResponse(e);
}
}

/**
* Gets the aging distribution summary.
*
* @param assetGroup
* the asset group
* @param severity
* the severity
* @return ResponseEntity<Object>
*/
@RequestMapping(path = "/v1/vulnerabilities/aging/distributionsummary", method = RequestMethod.GET)
public ResponseEntity<Object> getAgingDistributionSummary(@RequestParam("ag") String assetGroup,
@RequestParam(name = "severity", required = false) String severity) {
try {
return ResponseUtils
.buildSucessResponse(vulnerabilityService.getAgingDistributionSummary(assetGroup, severity));
} catch (Exception e) {
return ResponseUtils.buildFailureResponse(e);
}
}

/**
* Gets the aging summary.
*
Expand Down
Expand Up @@ -813,133 +813,6 @@ public List<Map<String, Object>> getAgingSummary(String assetGroup) {
return agingSummary;
}

/**
* Gets the aging by application.
*
* @param assetGroup
* the asset group
* @param parentType
* the parent type
* @param severity
* the severity
* @return the aging by application
* @throws Exception
* the exception
*/
public List<Map<String, Object>> getAgingByApplication(String assetGroup, String parentType, String severity)
throws Exception {

List<Map<String, Object>> vulnApplications = new ArrayList<>();
StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup);
urlToQuery.append("/").append(parentType);
urlToQuery.append("/").append(SEARCH);

StringBuilder requestBody = new StringBuilder(
"{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}}]}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":10000},"
+ "\"aggs\":{\"vulns\":{\"children\":{\"type\":\"vulninfo\"},\"aggs\":{\"NAME\":{\"filters\":{\"filters\":{\"");
if (StringUtils.isNotEmpty(severity)) {
requestBody.append("S").append(severity);
requestBody.append("\":{\"bool\":{\"must\":[ {\"match\":{\"latest\":true}},{\"term\":{\"severitylevel\":")
.append(severity).append("}}]}}");
} else {
requestBody.append(
"S3\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel\":3}},{\"match\":{\"latest\":true}}]}},\"S4\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel\":4}},{\"match\":{\"latest\":true}}]}},\"S5\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel\":5}},{\"match\":{\"latest\":true}}]}}");
}
requestBody.append("}},\"aggs\":{\"aging\":{\"sum\":{\"field\":\"_vulnage\"}}}}}}}}}}");
String responseJson = "";
try {
responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString());
} catch (Exception e) {
LOGGER.error("Error in getAgingByApplication from ES", e);
throw e;
}
JsonParser jsonParser = new JsonParser();
JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson);
JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString());
JsonArray outerBuckets = aggsJson.getAsJsonObject("apps").getAsJsonArray(BUCKETS);
if (outerBuckets.size() > 0) {
for (int i = 0; i < outerBuckets.size(); i++) {
String appName = outerBuckets.get(i).getAsJsonObject().get("key").getAsString();
List<Map<String, Object>> agingInfo = getAgingInfo(outerBuckets.get(i).getAsJsonObject()
.getAsJsonObject(VULN).getAsJsonObject("NAME").getAsJsonObject(BUCKETS), severity);
Map<String, Object> applicationInfo = new HashMap<>();
applicationInfo.put(APPS, appName);
applicationInfo.put("severityinfo", agingInfo);
vulnApplications.add(applicationInfo);
}
}
return vulnApplications;
}

/**
* Gets the aging info.
*
* @param countBucket
* the count bucket
* @param severity
* the severity
* @return the aging info
* @throws DataException
* the data exception
*/
private List<Map<String, Object>> getAgingInfo(JsonObject countBucket, String severity) throws DataException {

List<Map<String, Object>> severityInfo = new ArrayList<>();
if (StringUtils.isEmpty(severity)) {
Map<String, Object> severity3 = new HashMap<>();
severity3.put(SEVEITY_LEVEL, 3);
severity3.put(SEVERITY, "S3");
if (countBucket.getAsJsonObject("S3").get(DOC_COUNT).toString().equals(ZERO)) {
severity3.put("days", 0);
severity3.put(COUNT, 0);
} else {
severity3.put(COUNT, countBucket.getAsJsonObject("S3").get(DOC_COUNT).getAsDouble());
severity3.put("days", Math.floor(
countBucket.getAsJsonObject("S3").get(AGING).getAsJsonObject().get(VALUE).getAsDouble()));
}
Map<String, Object> severity4 = new HashMap<>();
severity4.put(SEVEITY_LEVEL, 4);
severity4.put(SEVERITY, "S4");
if (countBucket.getAsJsonObject("S4").get(DOC_COUNT).toString().equals(ZERO)) {
severity4.put("days", 0);
severity4.put(COUNT, 0);
} else {
severity4.put(COUNT, countBucket.getAsJsonObject("S4").get(DOC_COUNT).getAsDouble());
severity4.put("days",
countBucket.getAsJsonObject("S4").get(AGING).getAsJsonObject().get(VALUE).getAsDouble());
}
Map<String, Object> severity5 = new HashMap<>();
severity5.put(SEVEITY_LEVEL, 5);
severity5.put(SEVERITY, "S5");
if (countBucket.getAsJsonObject("S5").get(DOC_COUNT).toString().equals(ZERO)) {
severity5.put(COUNT, 0);
severity5.put("days", 0);
} else {
severity5.put(COUNT, countBucket.getAsJsonObject("S5").get(DOC_COUNT).getAsDouble());
severity5.put("days",
countBucket.getAsJsonObject("S5").get(AGING).getAsJsonObject().get(VALUE).getAsDouble());
}
severityInfo.add(severity3);
severityInfo.add(severity4);
severityInfo.add(severity5);
} else {
Map<String, Object> severityMap = new HashMap<>();
severityMap.put(SEVEITY_LEVEL, Integer.valueOf(severity));
severityMap.put(SEVERITY, "S" + severity);
if (countBucket.getAsJsonObject("S" + severity).get(DOC_COUNT).toString().equals(ZERO)) {
severityMap.put("days", 0);
severityMap.put(COUNT, 0);
} else {
severityMap.put(COUNT, countBucket.getAsJsonObject("S" + severity).get(DOC_COUNT).getAsDouble());
severityMap.put("days", countBucket.getAsJsonObject("S" + severity).get(AGING).getAsJsonObject()
.get(VALUE).getAsDouble());
}
severityInfo.add(severityMap);
}

return severityInfo;
}

/**
* Gets the total qualys host count.
*
Expand Down

0 comments on commit 44cb4e7

Please sign in to comment.