@Test public void getVulnerabilitiesDistributionTest() throws Exception { String response = "{\"aggregations\":{\"apps\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0," + "\"buckets\":[{\"key\":\"ag\",\"doc_count\":90,\"envs\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0," + "\"buckets\":[{\"key\":\"Production::prd\",\"doc_count\":32,\"vulns\":{\"doc_count\":2002," + "\"NAME\":{\"buckets\":{\"S3\":{\"doc_count\":197},\"S4\":{\"doc_count\":164},\"S5\":{\"doc_count\":30}}}}}]}}]}}}"; mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(response); ReflectionTestUtils.setField(vulnerabilityRepository, "esUrl", "dummyEsURL"); assertTrue(vulnerabilityRepository.getVulnerabilitiesDistribution("ag", "parent").size() == 1); }
public List<Map<String, Object>> getVulnerabilitiesDistribution(String assetGroup, String parentType) throws Exception { List<Map<String, Object>> vulnDistributions = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(parentType).append("/_search"); String requestBody = "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}}]}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":1000}," + "\"aggs\":{\"envs\":{\"terms\":{\"field\":\"tags.Environment.keyword\",\"size\":1000},\"aggs\":{\"vulns\":{\"children\":{\"type\":\"vulninfo\"}," + "\"aggs\":{\"NAME\":{\"filters\":{\"filters\":{\"S3\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":3}},{\"match\":{\"latest\":true}}]}},\"S4\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":4}},{\"match\":{\"latest\":true}}]}},\"S5\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":5}},{\"match\":{\"latest\":true}}]}}}}}}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitiesDistribution 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++) { Map<String, Object> applist = new HashMap<>(); String appName = outerBuckets.get(i).getAsJsonObject().get("key").getAsString(); JsonArray envs = outerBuckets.get(i).getAsJsonObject().getAsJsonObject("envs").getAsJsonArray(BUCKETS); List<Map<String, Object>> envDetails = new ArrayList<>(); if (envs.size() > 0) { for (int j = 0; j < envs.size(); j++) { String envName = envs.get(j).getAsJsonObject().get("key").getAsString(); List<Map<String, Object>> severityInfo = getSeverityInfo(envs.get(j).getAsJsonObject() .getAsJsonObject(VULN).getAsJsonObject("NAME").getAsJsonObject(BUCKETS), null); Map<String, Object> envSeverity = new HashMap<>(); envSeverity.put("environment", envName); envSeverity.put(SEVERITY_INFO, severityInfo); envSeverity.put(VULNEREBILITIES, Integer.valueOf(severityInfo.get(0).get(COUNT).toString()) + Integer.valueOf(severityInfo.get(ONE).get(COUNT).toString()) + Integer.valueOf(severityInfo.get(TWO).get(COUNT).toString())); envDetails.add(envSeverity); } } applist.put(APPS, appName); applist.put("applicationInfo", envDetails); vulnDistributions.add(applist); } } return vulnDistributions; }
VulnerabilityRepository implements Constants { public List<Map<String, Object>> getVulnerabilitiesDistribution(String assetGroup, String parentType) throws Exception { List<Map<String, Object>> vulnDistributions = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(parentType).append("/_search"); String requestBody = "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}}]}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":1000}," + "\"aggs\":{\"envs\":{\"terms\":{\"field\":\"tags.Environment.keyword\",\"size\":1000},\"aggs\":{\"vulns\":{\"children\":{\"type\":\"vulninfo\"}," + "\"aggs\":{\"NAME\":{\"filters\":{\"filters\":{\"S3\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":3}},{\"match\":{\"latest\":true}}]}},\"S4\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":4}},{\"match\":{\"latest\":true}}]}},\"S5\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":5}},{\"match\":{\"latest\":true}}]}}}}}}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitiesDistribution 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++) { Map<String, Object> applist = new HashMap<>(); String appName = outerBuckets.get(i).getAsJsonObject().get("key").getAsString(); JsonArray envs = outerBuckets.get(i).getAsJsonObject().getAsJsonObject("envs").getAsJsonArray(BUCKETS); List<Map<String, Object>> envDetails = new ArrayList<>(); if (envs.size() > 0) { for (int j = 0; j < envs.size(); j++) { String envName = envs.get(j).getAsJsonObject().get("key").getAsString(); List<Map<String, Object>> severityInfo = getSeverityInfo(envs.get(j).getAsJsonObject() .getAsJsonObject(VULN).getAsJsonObject("NAME").getAsJsonObject(BUCKETS), null); Map<String, Object> envSeverity = new HashMap<>(); envSeverity.put("environment", envName); envSeverity.put(SEVERITY_INFO, severityInfo); envSeverity.put(VULNEREBILITIES, Integer.valueOf(severityInfo.get(0).get(COUNT).toString()) + Integer.valueOf(severityInfo.get(ONE).get(COUNT).toString()) + Integer.valueOf(severityInfo.get(TWO).get(COUNT).toString())); envDetails.add(envSeverity); } } applist.put(APPS, appName); applist.put("applicationInfo", envDetails); vulnDistributions.add(applist); } } return vulnDistributions; } }
VulnerabilityRepository implements Constants { public List<Map<String, Object>> getVulnerabilitiesDistribution(String assetGroup, String parentType) throws Exception { List<Map<String, Object>> vulnDistributions = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(parentType).append("/_search"); String requestBody = "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}}]}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":1000}," + "\"aggs\":{\"envs\":{\"terms\":{\"field\":\"tags.Environment.keyword\",\"size\":1000},\"aggs\":{\"vulns\":{\"children\":{\"type\":\"vulninfo\"}," + "\"aggs\":{\"NAME\":{\"filters\":{\"filters\":{\"S3\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":3}},{\"match\":{\"latest\":true}}]}},\"S4\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":4}},{\"match\":{\"latest\":true}}]}},\"S5\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":5}},{\"match\":{\"latest\":true}}]}}}}}}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitiesDistribution 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++) { Map<String, Object> applist = new HashMap<>(); String appName = outerBuckets.get(i).getAsJsonObject().get("key").getAsString(); JsonArray envs = outerBuckets.get(i).getAsJsonObject().getAsJsonObject("envs").getAsJsonArray(BUCKETS); List<Map<String, Object>> envDetails = new ArrayList<>(); if (envs.size() > 0) { for (int j = 0; j < envs.size(); j++) { String envName = envs.get(j).getAsJsonObject().get("key").getAsString(); List<Map<String, Object>> severityInfo = getSeverityInfo(envs.get(j).getAsJsonObject() .getAsJsonObject(VULN).getAsJsonObject("NAME").getAsJsonObject(BUCKETS), null); Map<String, Object> envSeverity = new HashMap<>(); envSeverity.put("environment", envName); envSeverity.put(SEVERITY_INFO, severityInfo); envSeverity.put(VULNEREBILITIES, Integer.valueOf(severityInfo.get(0).get(COUNT).toString()) + Integer.valueOf(severityInfo.get(ONE).get(COUNT).toString()) + Integer.valueOf(severityInfo.get(TWO).get(COUNT).toString())); envDetails.add(envSeverity); } } applist.put(APPS, appName); applist.put("applicationInfo", envDetails); vulnDistributions.add(applist); } } return vulnDistributions; }  }
VulnerabilityRepository implements Constants { public List<Map<String, Object>> getVulnerabilitiesDistribution(String assetGroup, String parentType) throws Exception { List<Map<String, Object>> vulnDistributions = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(parentType).append("/_search"); String requestBody = "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}}]}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":1000}," + "\"aggs\":{\"envs\":{\"terms\":{\"field\":\"tags.Environment.keyword\",\"size\":1000},\"aggs\":{\"vulns\":{\"children\":{\"type\":\"vulninfo\"}," + "\"aggs\":{\"NAME\":{\"filters\":{\"filters\":{\"S3\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":3}},{\"match\":{\"latest\":true}}]}},\"S4\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":4}},{\"match\":{\"latest\":true}}]}},\"S5\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":5}},{\"match\":{\"latest\":true}}]}}}}}}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitiesDistribution 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++) { Map<String, Object> applist = new HashMap<>(); String appName = outerBuckets.get(i).getAsJsonObject().get("key").getAsString(); JsonArray envs = outerBuckets.get(i).getAsJsonObject().getAsJsonObject("envs").getAsJsonArray(BUCKETS); List<Map<String, Object>> envDetails = new ArrayList<>(); if (envs.size() > 0) { for (int j = 0; j < envs.size(); j++) { String envName = envs.get(j).getAsJsonObject().get("key").getAsString(); List<Map<String, Object>> severityInfo = getSeverityInfo(envs.get(j).getAsJsonObject() .getAsJsonObject(VULN).getAsJsonObject("NAME").getAsJsonObject(BUCKETS), null); Map<String, Object> envSeverity = new HashMap<>(); envSeverity.put("environment", envName); envSeverity.put(SEVERITY_INFO, severityInfo); envSeverity.put(VULNEREBILITIES, Integer.valueOf(severityInfo.get(0).get(COUNT).toString()) + Integer.valueOf(severityInfo.get(ONE).get(COUNT).toString()) + Integer.valueOf(severityInfo.get(TWO).get(COUNT).toString())); envDetails.add(envSeverity); } } applist.put(APPS, appName); applist.put("applicationInfo", envDetails); vulnDistributions.add(applist); } } return vulnDistributions; }  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>> getVulnerabilitiesDistribution(String assetGroup, String parentType) throws Exception { List<Map<String, Object>> vulnDistributions = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(parentType).append("/_search"); String requestBody = "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}}]}},\"aggs\":{\"apps\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":1000}," + "\"aggs\":{\"envs\":{\"terms\":{\"field\":\"tags.Environment.keyword\",\"size\":1000},\"aggs\":{\"vulns\":{\"children\":{\"type\":\"vulninfo\"}," + "\"aggs\":{\"NAME\":{\"filters\":{\"filters\":{\"S3\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":3}},{\"match\":{\"latest\":true}}]}},\"S4\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":4}},{\"match\":{\"latest\":true}}]}},\"S5\":{\"bool\":{\"must\":[{\"term\":{\"severitylevel.keyword\":5}},{\"match\":{\"latest\":true}}]}}}}}}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { LOGGER.error("Error in getVulnerabilitiesDistribution 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++) { Map<String, Object> applist = new HashMap<>(); String appName = outerBuckets.get(i).getAsJsonObject().get("key").getAsString(); JsonArray envs = outerBuckets.get(i).getAsJsonObject().getAsJsonObject("envs").getAsJsonArray(BUCKETS); List<Map<String, Object>> envDetails = new ArrayList<>(); if (envs.size() > 0) { for (int j = 0; j < envs.size(); j++) { String envName = envs.get(j).getAsJsonObject().get("key").getAsString(); List<Map<String, Object>> severityInfo = getSeverityInfo(envs.get(j).getAsJsonObject() .getAsJsonObject(VULN).getAsJsonObject("NAME").getAsJsonObject(BUCKETS), null); Map<String, Object> envSeverity = new HashMap<>(); envSeverity.put("environment", envName); envSeverity.put(SEVERITY_INFO, severityInfo); envSeverity.put(VULNEREBILITIES, Integer.valueOf(severityInfo.get(0).get(COUNT).toString()) + Integer.valueOf(severityInfo.get(ONE).get(COUNT).toString()) + Integer.valueOf(severityInfo.get(TWO).get(COUNT).toString())); envDetails.add(envSeverity); } } applist.put(APPS, appName); applist.put("applicationInfo", envDetails); vulnDistributions.add(applist); } } return vulnDistributions; }  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);  }