@Test public void getUniqueHostTest() throws Exception { String response = "{\"hits\":{\"total\":30056},\"aggregations\":{\"vulninfo\":{\"sev-filter\":{\"severity\":{\"buckets\":[" + "{\"key\":4,\"unique-host\":{\"value\":25071}},{\"key\":3,\"unique-host\":{\"value\":23776}}," + "{\"key\":5,\"unique-host\":{\"value\":19250}}]}}}}}"; mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(response); ReflectionTestUtils.setField(vulnerabilityRepository, "esUrl", "dummyEsURL"); assertTrue(vulnerabilityRepository.getUniqueHost("ag", "3,4,5").size() == 4); }
public Map<String, Object> getUniqueHost(String assetGroup, String severity) { Map<String, Object> uniqueHost = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[" + severity + "]}}]}}}}]}},\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"}," + "\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[" + severity + "]}}]}}," + "\"aggs\":{\"severity\":{\"terms\":{\"field\":\"severitylevel.keyword\",\"size\":5}," + "\"aggs\":{\"unique-host\":{\"cardinality\":{\"field\":\"_resourceid.keyword\",\"precision_threshold\":40000}}}}}}}}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error(Constants.ERROR_UNIQUEHOST, e); } JsonParser jsonParser = new JsonParser(); if (StringUtils.isNotEmpty(responseJson)) { JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(HITS).toString()); long total = hitsJson.get(TOTAL).getAsLong(); uniqueHost.put(TOTAL, total); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); JsonArray buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter") .getAsJsonObject(SEVERITY).getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { uniqueHost.put(buckets.get(i).getAsJsonObject().get("key").getAsString(), buckets.get(i) .getAsJsonObject().get("unique-host").getAsJsonObject().get(VALUE).getAsLong()); } } } return uniqueHost; }
VulnerabilityRepository implements Constants { public Map<String, Object> getUniqueHost(String assetGroup, String severity) { Map<String, Object> uniqueHost = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[" + severity + "]}}]}}}}]}},\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"}," + "\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[" + severity + "]}}]}}," + "\"aggs\":{\"severity\":{\"terms\":{\"field\":\"severitylevel.keyword\",\"size\":5}," + "\"aggs\":{\"unique-host\":{\"cardinality\":{\"field\":\"_resourceid.keyword\",\"precision_threshold\":40000}}}}}}}}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error(Constants.ERROR_UNIQUEHOST, e); } JsonParser jsonParser = new JsonParser(); if (StringUtils.isNotEmpty(responseJson)) { JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(HITS).toString()); long total = hitsJson.get(TOTAL).getAsLong(); uniqueHost.put(TOTAL, total); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); JsonArray buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter") .getAsJsonObject(SEVERITY).getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { uniqueHost.put(buckets.get(i).getAsJsonObject().get("key").getAsString(), buckets.get(i) .getAsJsonObject().get("unique-host").getAsJsonObject().get(VALUE).getAsLong()); } } } return uniqueHost; } }
VulnerabilityRepository implements Constants { public Map<String, Object> getUniqueHost(String assetGroup, String severity) { Map<String, Object> uniqueHost = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[" + severity + "]}}]}}}}]}},\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"}," + "\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[" + severity + "]}}]}}," + "\"aggs\":{\"severity\":{\"terms\":{\"field\":\"severitylevel.keyword\",\"size\":5}," + "\"aggs\":{\"unique-host\":{\"cardinality\":{\"field\":\"_resourceid.keyword\",\"precision_threshold\":40000}}}}}}}}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error(Constants.ERROR_UNIQUEHOST, e); } JsonParser jsonParser = new JsonParser(); if (StringUtils.isNotEmpty(responseJson)) { JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(HITS).toString()); long total = hitsJson.get(TOTAL).getAsLong(); uniqueHost.put(TOTAL, total); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); JsonArray buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter") .getAsJsonObject(SEVERITY).getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { uniqueHost.put(buckets.get(i).getAsJsonObject().get("key").getAsString(), buckets.get(i) .getAsJsonObject().get("unique-host").getAsJsonObject().get(VALUE).getAsLong()); } } } return uniqueHost; }  }
VulnerabilityRepository implements Constants { public Map<String, Object> getUniqueHost(String assetGroup, String severity) { Map<String, Object> uniqueHost = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[" + severity + "]}}]}}}}]}},\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"}," + "\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[" + severity + "]}}]}}," + "\"aggs\":{\"severity\":{\"terms\":{\"field\":\"severitylevel.keyword\",\"size\":5}," + "\"aggs\":{\"unique-host\":{\"cardinality\":{\"field\":\"_resourceid.keyword\",\"precision_threshold\":40000}}}}}}}}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error(Constants.ERROR_UNIQUEHOST, e); } JsonParser jsonParser = new JsonParser(); if (StringUtils.isNotEmpty(responseJson)) { JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(HITS).toString()); long total = hitsJson.get(TOTAL).getAsLong(); uniqueHost.put(TOTAL, total); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); JsonArray buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter") .getAsJsonObject(SEVERITY).getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { uniqueHost.put(buckets.get(i).getAsJsonObject().get("key").getAsString(), buckets.get(i) .getAsJsonObject().get("unique-host").getAsJsonObject().get(VALUE).getAsLong()); } } } return uniqueHost; }  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 Map<String, Object> getUniqueHost(String assetGroup, String severity) { Map<String, Object> uniqueHost = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"has_child\":{\"type\":\"vulninfo\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[" + severity + "]}}]}}}}]}},\"aggs\":{\"vulninfo\":{\"children\":{\"type\":\"vulninfo\"}," + "\"aggs\":{\"sev-filter\":{\"filter\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"terms\":{\"severitylevel.keyword\":[" + severity + "]}}]}}," + "\"aggs\":{\"severity\":{\"terms\":{\"field\":\"severitylevel.keyword\",\"size\":5}," + "\"aggs\":{\"unique-host\":{\"cardinality\":{\"field\":\"_resourceid.keyword\",\"precision_threshold\":40000}}}}}}}}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error(Constants.ERROR_UNIQUEHOST, e); } JsonParser jsonParser = new JsonParser(); if (StringUtils.isNotEmpty(responseJson)) { JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(HITS).toString()); long total = hitsJson.get(TOTAL).getAsLong(); uniqueHost.put(TOTAL, total); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); JsonArray buckets = aggsJson.getAsJsonObject(VULN_INFO).getAsJsonObject("sev-filter") .getAsJsonObject(SEVERITY).getAsJsonArray(BUCKETS); if (buckets.size() > 0) { for (int i = 0; i < buckets.size(); i++) { uniqueHost.put(buckets.get(i).getAsJsonObject().get("key").getAsString(), buckets.get(i) .getAsJsonObject().get("unique-host").getAsJsonObject().get(VALUE).getAsLong()); } } } return uniqueHost; }  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);  }