@Test public void getDistributionSummaryByVulnTypeTest_Exception() throws Exception { mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenThrow(new Exception()); ReflectionTestUtils.setField(vulnerabilityRepository, "esUrl", "dummyEsURL"); assertThatThrownBy(() -> vulnerabilityRepository.getDistributionSummaryByVulnType("ag", "3")) .isInstanceOf(DataException.class); }
public List<Map<String, Object>> getDistributionSummaryByVulnType(String assetGroup, String severity) throws DataException { List<Map<String, Object>> distributionList = new ArrayList<>(); long totalVulnCount = 0; Map<String, Object> infoOS = new HashMap<>(); infoOS.put("category", "OS"); infoOS.put(TOTAL_VULN_ASSETS, 0); infoOS.put(VULNEREBILITIES, 0); infoOS.put(UNIQUE_VULN_COUNT, 0); Map<String, Object> infoApp = new HashMap<>(); infoApp.put("category", "Application"); infoApp.put(TOTAL_VULN_ASSETS, 0); infoApp.put(VULNEREBILITIES, 0); infoApp.put(UNIQUE_VULN_COUNT, 0); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},").append( "{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}}}}]}},") .append("\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"},\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}},") .append("\"aggs\":{\"classification\":{\"terms\":{\"field\":\"classification.keyword\",\"size\":10},\"aggs\":{\"resources\":{\"cardinality\":{\"field\":\"_resourceid.keyword\",\"precision_threshold\":40000}}}}}}}}}}"); String requestJson = String.format(requestBody.toString(), severity, severity); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestJson); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitySummaryByClassification from ES", e); throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); JsonArray buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter") .getAsJsonObject("classification").getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { totalVulnCount += buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong(); if (buckets.get(i).getAsJsonObject().get("key").toString().replace("\"", "").equals("OS")) { infoOS.put(TOTAL_VULN_ASSETS, buckets.get(i).getAsJsonObject().get("resources").getAsJsonObject().get(VALUE).getAsLong()); infoOS.put(VULNEREBILITIES, buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong()); } else { infoApp.put(TOTAL_VULN_ASSETS, buckets.get(i).getAsJsonObject().get("resources").getAsJsonObject().get(VALUE).getAsLong()); infoApp.put(VULNEREBILITIES, buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong()); } } } requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},") .append("{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}}}}]}},") .append("\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"},\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}},") .append("\"aggs\":{\"classification\":{\"terms\":{\"field\":\"classification.keyword\",\"size\":10},\"aggs\":{\"unique-qid\":{\"cardinality\":{\"script\":\"doc['qid.keyword'].toString().replace('.0','')\",\"precision_threshold\":40000}}}}}}}}}}"); requestJson = String.format(requestBody.toString(), severity, severity); responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestJson); } catch (Exception e) { LOGGER.error(Constants.ERROR_UNIQUEHOST, e); throw new DataException(e); } resultJson = (JsonObject) jsonParser.parse(responseJson); aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter").getAsJsonObject("classification") .getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { if (buckets.get(i).getAsJsonObject().get("key").toString().replace("\"", "").equals("OS")) { infoOS.put(UNIQUE_VULN_COUNT, buckets.get(i).getAsJsonObject().get(UNIQUE_QID).getAsJsonObject().get(VALUE).getAsLong()); } else { infoApp.put(UNIQUE_VULN_COUNT, buckets.get(i).getAsJsonObject().get(UNIQUE_QID).getAsJsonObject().get(VALUE).getAsLong()); } } } if (totalVulnCount > 0) { distributionList.add(infoOS); distributionList.add(infoApp); } double contribution = HUNDRED; for (int i = 0; i < distributionList.size(); i++) { Map<String, Object> info = distributionList.get(i); if (totalVulnCount > 0) { double contributionPercent = Math .floor((Double.valueOf(info.get(VULNEREBILITIES).toString()) / totalVulnCount) * HUNDRED); if (i == distributionList.size() - 1) { info.put("contribution", contribution); } else { info.put("contribution", contributionPercent); contribution = contribution - contributionPercent; } } } return distributionList; }
VulnerabilityRepository implements Constants { public List<Map<String, Object>> getDistributionSummaryByVulnType(String assetGroup, String severity) throws DataException { List<Map<String, Object>> distributionList = new ArrayList<>(); long totalVulnCount = 0; Map<String, Object> infoOS = new HashMap<>(); infoOS.put("category", "OS"); infoOS.put(TOTAL_VULN_ASSETS, 0); infoOS.put(VULNEREBILITIES, 0); infoOS.put(UNIQUE_VULN_COUNT, 0); Map<String, Object> infoApp = new HashMap<>(); infoApp.put("category", "Application"); infoApp.put(TOTAL_VULN_ASSETS, 0); infoApp.put(VULNEREBILITIES, 0); infoApp.put(UNIQUE_VULN_COUNT, 0); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},").append( "{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}}}}]}},") .append("\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"},\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}},") .append("\"aggs\":{\"classification\":{\"terms\":{\"field\":\"classification.keyword\",\"size\":10},\"aggs\":{\"resources\":{\"cardinality\":{\"field\":\"_resourceid.keyword\",\"precision_threshold\":40000}}}}}}}}}}"); String requestJson = String.format(requestBody.toString(), severity, severity); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestJson); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitySummaryByClassification from ES", e); throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); JsonArray buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter") .getAsJsonObject("classification").getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { totalVulnCount += buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong(); if (buckets.get(i).getAsJsonObject().get("key").toString().replace("\"", "").equals("OS")) { infoOS.put(TOTAL_VULN_ASSETS, buckets.get(i).getAsJsonObject().get("resources").getAsJsonObject().get(VALUE).getAsLong()); infoOS.put(VULNEREBILITIES, buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong()); } else { infoApp.put(TOTAL_VULN_ASSETS, buckets.get(i).getAsJsonObject().get("resources").getAsJsonObject().get(VALUE).getAsLong()); infoApp.put(VULNEREBILITIES, buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong()); } } } requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},") .append("{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}}}}]}},") .append("\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"},\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}},") .append("\"aggs\":{\"classification\":{\"terms\":{\"field\":\"classification.keyword\",\"size\":10},\"aggs\":{\"unique-qid\":{\"cardinality\":{\"script\":\"doc['qid.keyword'].toString().replace('.0','')\",\"precision_threshold\":40000}}}}}}}}}}"); requestJson = String.format(requestBody.toString(), severity, severity); responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestJson); } catch (Exception e) { LOGGER.error(Constants.ERROR_UNIQUEHOST, e); throw new DataException(e); } resultJson = (JsonObject) jsonParser.parse(responseJson); aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter").getAsJsonObject("classification") .getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { if (buckets.get(i).getAsJsonObject().get("key").toString().replace("\"", "").equals("OS")) { infoOS.put(UNIQUE_VULN_COUNT, buckets.get(i).getAsJsonObject().get(UNIQUE_QID).getAsJsonObject().get(VALUE).getAsLong()); } else { infoApp.put(UNIQUE_VULN_COUNT, buckets.get(i).getAsJsonObject().get(UNIQUE_QID).getAsJsonObject().get(VALUE).getAsLong()); } } } if (totalVulnCount > 0) { distributionList.add(infoOS); distributionList.add(infoApp); } double contribution = HUNDRED; for (int i = 0; i < distributionList.size(); i++) { Map<String, Object> info = distributionList.get(i); if (totalVulnCount > 0) { double contributionPercent = Math .floor((Double.valueOf(info.get(VULNEREBILITIES).toString()) / totalVulnCount) * HUNDRED); if (i == distributionList.size() - 1) { info.put("contribution", contribution); } else { info.put("contribution", contributionPercent); contribution = contribution - contributionPercent; } } } return distributionList; } }
VulnerabilityRepository implements Constants { public List<Map<String, Object>> getDistributionSummaryByVulnType(String assetGroup, String severity) throws DataException { List<Map<String, Object>> distributionList = new ArrayList<>(); long totalVulnCount = 0; Map<String, Object> infoOS = new HashMap<>(); infoOS.put("category", "OS"); infoOS.put(TOTAL_VULN_ASSETS, 0); infoOS.put(VULNEREBILITIES, 0); infoOS.put(UNIQUE_VULN_COUNT, 0); Map<String, Object> infoApp = new HashMap<>(); infoApp.put("category", "Application"); infoApp.put(TOTAL_VULN_ASSETS, 0); infoApp.put(VULNEREBILITIES, 0); infoApp.put(UNIQUE_VULN_COUNT, 0); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},").append( "{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}}}}]}},") .append("\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"},\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}},") .append("\"aggs\":{\"classification\":{\"terms\":{\"field\":\"classification.keyword\",\"size\":10},\"aggs\":{\"resources\":{\"cardinality\":{\"field\":\"_resourceid.keyword\",\"precision_threshold\":40000}}}}}}}}}}"); String requestJson = String.format(requestBody.toString(), severity, severity); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestJson); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitySummaryByClassification from ES", e); throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); JsonArray buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter") .getAsJsonObject("classification").getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { totalVulnCount += buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong(); if (buckets.get(i).getAsJsonObject().get("key").toString().replace("\"", "").equals("OS")) { infoOS.put(TOTAL_VULN_ASSETS, buckets.get(i).getAsJsonObject().get("resources").getAsJsonObject().get(VALUE).getAsLong()); infoOS.put(VULNEREBILITIES, buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong()); } else { infoApp.put(TOTAL_VULN_ASSETS, buckets.get(i).getAsJsonObject().get("resources").getAsJsonObject().get(VALUE).getAsLong()); infoApp.put(VULNEREBILITIES, buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong()); } } } requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},") .append("{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}}}}]}},") .append("\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"},\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}},") .append("\"aggs\":{\"classification\":{\"terms\":{\"field\":\"classification.keyword\",\"size\":10},\"aggs\":{\"unique-qid\":{\"cardinality\":{\"script\":\"doc['qid.keyword'].toString().replace('.0','')\",\"precision_threshold\":40000}}}}}}}}}}"); requestJson = String.format(requestBody.toString(), severity, severity); responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestJson); } catch (Exception e) { LOGGER.error(Constants.ERROR_UNIQUEHOST, e); throw new DataException(e); } resultJson = (JsonObject) jsonParser.parse(responseJson); aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter").getAsJsonObject("classification") .getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { if (buckets.get(i).getAsJsonObject().get("key").toString().replace("\"", "").equals("OS")) { infoOS.put(UNIQUE_VULN_COUNT, buckets.get(i).getAsJsonObject().get(UNIQUE_QID).getAsJsonObject().get(VALUE).getAsLong()); } else { infoApp.put(UNIQUE_VULN_COUNT, buckets.get(i).getAsJsonObject().get(UNIQUE_QID).getAsJsonObject().get(VALUE).getAsLong()); } } } if (totalVulnCount > 0) { distributionList.add(infoOS); distributionList.add(infoApp); } double contribution = HUNDRED; for (int i = 0; i < distributionList.size(); i++) { Map<String, Object> info = distributionList.get(i); if (totalVulnCount > 0) { double contributionPercent = Math .floor((Double.valueOf(info.get(VULNEREBILITIES).toString()) / totalVulnCount) * HUNDRED); if (i == distributionList.size() - 1) { info.put("contribution", contribution); } else { info.put("contribution", contributionPercent); contribution = contribution - contributionPercent; } } } return distributionList; }  }
VulnerabilityRepository implements Constants { public List<Map<String, Object>> getDistributionSummaryByVulnType(String assetGroup, String severity) throws DataException { List<Map<String, Object>> distributionList = new ArrayList<>(); long totalVulnCount = 0; Map<String, Object> infoOS = new HashMap<>(); infoOS.put("category", "OS"); infoOS.put(TOTAL_VULN_ASSETS, 0); infoOS.put(VULNEREBILITIES, 0); infoOS.put(UNIQUE_VULN_COUNT, 0); Map<String, Object> infoApp = new HashMap<>(); infoApp.put("category", "Application"); infoApp.put(TOTAL_VULN_ASSETS, 0); infoApp.put(VULNEREBILITIES, 0); infoApp.put(UNIQUE_VULN_COUNT, 0); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},").append( "{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}}}}]}},") .append("\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"},\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}},") .append("\"aggs\":{\"classification\":{\"terms\":{\"field\":\"classification.keyword\",\"size\":10},\"aggs\":{\"resources\":{\"cardinality\":{\"field\":\"_resourceid.keyword\",\"precision_threshold\":40000}}}}}}}}}}"); String requestJson = String.format(requestBody.toString(), severity, severity); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestJson); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitySummaryByClassification from ES", e); throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); JsonArray buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter") .getAsJsonObject("classification").getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { totalVulnCount += buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong(); if (buckets.get(i).getAsJsonObject().get("key").toString().replace("\"", "").equals("OS")) { infoOS.put(TOTAL_VULN_ASSETS, buckets.get(i).getAsJsonObject().get("resources").getAsJsonObject().get(VALUE).getAsLong()); infoOS.put(VULNEREBILITIES, buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong()); } else { infoApp.put(TOTAL_VULN_ASSETS, buckets.get(i).getAsJsonObject().get("resources").getAsJsonObject().get(VALUE).getAsLong()); infoApp.put(VULNEREBILITIES, buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong()); } } } requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},") .append("{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}}}}]}},") .append("\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"},\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}},") .append("\"aggs\":{\"classification\":{\"terms\":{\"field\":\"classification.keyword\",\"size\":10},\"aggs\":{\"unique-qid\":{\"cardinality\":{\"script\":\"doc['qid.keyword'].toString().replace('.0','')\",\"precision_threshold\":40000}}}}}}}}}}"); requestJson = String.format(requestBody.toString(), severity, severity); responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestJson); } catch (Exception e) { LOGGER.error(Constants.ERROR_UNIQUEHOST, e); throw new DataException(e); } resultJson = (JsonObject) jsonParser.parse(responseJson); aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter").getAsJsonObject("classification") .getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { if (buckets.get(i).getAsJsonObject().get("key").toString().replace("\"", "").equals("OS")) { infoOS.put(UNIQUE_VULN_COUNT, buckets.get(i).getAsJsonObject().get(UNIQUE_QID).getAsJsonObject().get(VALUE).getAsLong()); } else { infoApp.put(UNIQUE_VULN_COUNT, buckets.get(i).getAsJsonObject().get(UNIQUE_QID).getAsJsonObject().get(VALUE).getAsLong()); } } } if (totalVulnCount > 0) { distributionList.add(infoOS); distributionList.add(infoApp); } double contribution = HUNDRED; for (int i = 0; i < distributionList.size(); i++) { Map<String, Object> info = distributionList.get(i); if (totalVulnCount > 0) { double contributionPercent = Math .floor((Double.valueOf(info.get(VULNEREBILITIES).toString()) / totalVulnCount) * HUNDRED); if (i == distributionList.size() - 1) { info.put("contribution", contribution); } else { info.put("contribution", contributionPercent); contribution = contribution - contributionPercent; } } } return distributionList; }  List<Map<String, Object>> getAllVulnerabilities(List<String> vulnAssetsAffectedQids); List<Map<String, Object>> getAllVulnerabilitiesByAssetGroup(String assetGroup, String targetType,
			Map<String, Object> mustFilter, List<String> occurrenceFieldList, int from, int size,
			Map<String, Object> mustTermsFilter); List<Map<String, Object>> getDetailsByResourceId(String assetGroup, Map<String, Object> mustFilter,
			List<String> resourceFieldList, Map<String, Object> mustTermFilter); Map<String, Long> getAssetsAffectedCount(String assetGroup, Map<String, String> filter, String parentType); List<Map<String, Object>> getVulnerabilyAcrossAppAndEnv(String assetGroup, String filter, String application,
			String parentType, String severity); List<Map<String, Object>> getVulnerabilityTrend(String assetGroup, Map<String, String> filter, Date from,
			Date to); List<Map<String, Object>> getVulnerabilitiesDistribution(String assetGroup, String parentType); Map<String, Object> getVulnerabilitysummaryByResourceId(String resourceId); List<Map<String, Object>> getVulnerabilityDetailsByResourceId(String resourceId); @SuppressWarnings("deprecation") List<Map<String, Object>> fetchExecDirectorApps(); Map<String, Object> getUniqueHost(String assetGroup, String severity); Map<String, Object> getVulnInfo(String assetGroup, String severity); Map<String, Object> getUniqueApp(String assetGroup); List<Map<String, Object>> getAgingSummary(String assetGroup); long getTotalQualysHostCount(String index, String vulnType); Map<String, Object> getVulnerabilityByQid(String qid); Map<String, Object> getDistributionSummaryByInfraType(String assetGroup, String severitylevel,
			String parentType); Map<String, Long> getProdInfoByEnv(String assetGroup, String severitylevel); Map<String, Long> getNonProdInfoByEnv(String assetGroup, String severitylevel); List<Map<String, Object>> getDistributionSummaryByVulnType(String assetGroup, String severity); Map<String, Object> getAllQidByAG(String assetGroup, String severity); Map<String, Long> getAppsBySeverity(String assetGroup, String parentType, String severity); boolean createTrendAnnotation(TrendNote request); void createIndex(String indexName); List<Map<String, Object>> getTrendAnnotations(String ag, Date from); boolean deleteTrendAnnotation(String noteId); List<Map<String, Object>> getDataFromPacmanRDS(String query); long getRunningInstancesCount(String index, String vulnType); long getExemptedByRuleCount(String index, String vulnType); Map<String, Object> getUniqueHostBySeverity(String index, String severity); Map<String, Object> getCompliantHostsBySeverity(String assetGroup); List<String> getNonCompliantResourceIds(String index); @SuppressWarnings("deprecation") List<Map<String, Object>> fetchOrgInfoForApps(); List<Map<String, Object>> getTrendProgress(String assetGroup, String ruleId, LocalDate startDate,
			LocalDate endDate, String trendCategory); int vulnerabilityAssetsCount(String assetGroup, String targetType, Map<String, Object> mustFilter, int from,
			int size, Map<String, Object> mustTermsFilter); }
VulnerabilityRepository implements Constants { public List<Map<String, Object>> getDistributionSummaryByVulnType(String assetGroup, String severity) throws DataException { List<Map<String, Object>> distributionList = new ArrayList<>(); long totalVulnCount = 0; Map<String, Object> infoOS = new HashMap<>(); infoOS.put("category", "OS"); infoOS.put(TOTAL_VULN_ASSETS, 0); infoOS.put(VULNEREBILITIES, 0); infoOS.put(UNIQUE_VULN_COUNT, 0); Map<String, Object> infoApp = new HashMap<>(); infoApp.put("category", "Application"); infoApp.put(TOTAL_VULN_ASSETS, 0); infoApp.put(VULNEREBILITIES, 0); infoApp.put(UNIQUE_VULN_COUNT, 0); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},").append( "{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}}}}]}},") .append("\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"},\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}},") .append("\"aggs\":{\"classification\":{\"terms\":{\"field\":\"classification.keyword\",\"size\":10},\"aggs\":{\"resources\":{\"cardinality\":{\"field\":\"_resourceid.keyword\",\"precision_threshold\":40000}}}}}}}}}}"); String requestJson = String.format(requestBody.toString(), severity, severity); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestJson); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitySummaryByClassification from ES", e); throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); JsonArray buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter") .getAsJsonObject("classification").getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { totalVulnCount += buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong(); if (buckets.get(i).getAsJsonObject().get("key").toString().replace("\"", "").equals("OS")) { infoOS.put(TOTAL_VULN_ASSETS, buckets.get(i).getAsJsonObject().get("resources").getAsJsonObject().get(VALUE).getAsLong()); infoOS.put(VULNEREBILITIES, buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong()); } else { infoApp.put(TOTAL_VULN_ASSETS, buckets.get(i).getAsJsonObject().get("resources").getAsJsonObject().get(VALUE).getAsLong()); infoApp.put(VULNEREBILITIES, buckets.get(i).getAsJsonObject().get(DOC_COUNT).getAsLong()); } } } requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},") .append("{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}}}}]}},") .append("\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"},\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[%s]}}]}},") .append("\"aggs\":{\"classification\":{\"terms\":{\"field\":\"classification.keyword\",\"size\":10},\"aggs\":{\"unique-qid\":{\"cardinality\":{\"script\":\"doc['qid.keyword'].toString().replace('.0','')\",\"precision_threshold\":40000}}}}}}}}}}"); requestJson = String.format(requestBody.toString(), severity, severity); responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestJson); } catch (Exception e) { LOGGER.error(Constants.ERROR_UNIQUEHOST, e); throw new DataException(e); } resultJson = (JsonObject) jsonParser.parse(responseJson); aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter").getAsJsonObject("classification") .getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { if (buckets.get(i).getAsJsonObject().get("key").toString().replace("\"", "").equals("OS")) { infoOS.put(UNIQUE_VULN_COUNT, buckets.get(i).getAsJsonObject().get(UNIQUE_QID).getAsJsonObject().get(VALUE).getAsLong()); } else { infoApp.put(UNIQUE_VULN_COUNT, buckets.get(i).getAsJsonObject().get(UNIQUE_QID).getAsJsonObject().get(VALUE).getAsLong()); } } } if (totalVulnCount > 0) { distributionList.add(infoOS); distributionList.add(infoApp); } double contribution = HUNDRED; for (int i = 0; i < distributionList.size(); i++) { Map<String, Object> info = distributionList.get(i); if (totalVulnCount > 0) { double contributionPercent = Math .floor((Double.valueOf(info.get(VULNEREBILITIES).toString()) / totalVulnCount) * HUNDRED); if (i == distributionList.size() - 1) { info.put("contribution", contribution); } else { info.put("contribution", contributionPercent); contribution = contribution - contributionPercent; } } } return distributionList; }  List<Map<String, Object>> getAllVulnerabilities(List<String> vulnAssetsAffectedQids); List<Map<String, Object>> getAllVulnerabilitiesByAssetGroup(String assetGroup, String targetType,
			Map<String, Object> mustFilter, List<String> occurrenceFieldList, int from, int size,
			Map<String, Object> mustTermsFilter); List<Map<String, Object>> getDetailsByResourceId(String assetGroup, Map<String, Object> mustFilter,
			List<String> resourceFieldList, Map<String, Object> mustTermFilter); Map<String, Long> getAssetsAffectedCount(String assetGroup, Map<String, String> filter, String parentType); List<Map<String, Object>> getVulnerabilyAcrossAppAndEnv(String assetGroup, String filter, String application,
			String parentType, String severity); List<Map<String, Object>> getVulnerabilityTrend(String assetGroup, Map<String, String> filter, Date from,
			Date to); List<Map<String, Object>> getVulnerabilitiesDistribution(String assetGroup, String parentType); Map<String, Object> getVulnerabilitysummaryByResourceId(String resourceId); List<Map<String, Object>> getVulnerabilityDetailsByResourceId(String resourceId); @SuppressWarnings("deprecation") List<Map<String, Object>> fetchExecDirectorApps(); Map<String, Object> getUniqueHost(String assetGroup, String severity); Map<String, Object> getVulnInfo(String assetGroup, String severity); Map<String, Object> getUniqueApp(String assetGroup); List<Map<String, Object>> getAgingSummary(String assetGroup); long getTotalQualysHostCount(String index, String vulnType); Map<String, Object> getVulnerabilityByQid(String qid); Map<String, Object> getDistributionSummaryByInfraType(String assetGroup, String severitylevel,
			String parentType); Map<String, Long> getProdInfoByEnv(String assetGroup, String severitylevel); Map<String, Long> getNonProdInfoByEnv(String assetGroup, String severitylevel); List<Map<String, Object>> getDistributionSummaryByVulnType(String assetGroup, String severity); Map<String, Object> getAllQidByAG(String assetGroup, String severity); Map<String, Long> getAppsBySeverity(String assetGroup, String parentType, String severity); boolean createTrendAnnotation(TrendNote request); void createIndex(String indexName); List<Map<String, Object>> getTrendAnnotations(String ag, Date from); boolean deleteTrendAnnotation(String noteId); List<Map<String, Object>> getDataFromPacmanRDS(String query); long getRunningInstancesCount(String index, String vulnType); long getExemptedByRuleCount(String index, String vulnType); Map<String, Object> getUniqueHostBySeverity(String index, String severity); Map<String, Object> getCompliantHostsBySeverity(String assetGroup); List<String> getNonCompliantResourceIds(String index); @SuppressWarnings("deprecation") List<Map<String, Object>> fetchOrgInfoForApps(); List<Map<String, Object>> getTrendProgress(String assetGroup, String ruleId, LocalDate startDate,
			LocalDate endDate, String trendCategory); int vulnerabilityAssetsCount(String assetGroup, String targetType, Map<String, Object> mustFilter, int from,
			int size, Map<String, Object> mustTermsFilter);  }