@Test public void getRuleDetailsByApplicationFromESTest() throws Exception { String response = "{\"took\":469,\"timed_out\":false,\"_shards\":{\"total\":176,\"successful\":176,\"failed\":0},\"hits\":{\"total\":115456,\"max_score\":0,\"hits\":[]},\"aggregations\":{\"NAME\":{\"doc_count_error_upper_bound\":691,\"sum_other_doc_count\":34323,\"buckets\":[" + "{\"key\":\"key\",\"doc_count\":6258},{\"key\":\"key\",\"doc_count\":3339}]}}}"; ReflectionTestUtils.setField(complianceRepositoryImpl, "esUrl", "dummyEsURL"); ReflectionTestUtils.setField(complianceRepositoryImpl, "mandatoryTags", "Application,Environment"); mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(response); complianceRepositoryImpl.getRuleDetailsByApplicationFromES("aws-all", "tagging-rule", "app1"); complianceRepositoryImpl.getRuleDetailsByApplicationFromES("aws-all", "tagging-rule", null); complianceRepositoryImpl.getRuleDetailsByApplicationFromES("aws-all", TAGGING_POLICY, null); complianceRepositoryImpl.getRuleDetailsByApplicationFromES("aws-all", EC2_KERNEL_COMPLIANCE_RULE, null); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenThrow(new RuntimeException()); assertThatThrownBy( () -> complianceRepositoryImpl.getRuleDetailsByApplicationFromES("aws-all", "tagging-rule", "app1")) .isInstanceOf(DataException.class); }
public JsonArray getRuleDetailsByApplicationFromES(String assetGroup, String ruleId, String searchText) throws DataException { String responseJson = null; JsonParser jsonParser; JsonObject resultJson; StringBuilder requestBody = null; StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append(assetGroup).append("/") .append(SEARCH); requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"ruleId.keyword\":{\"value\":\"" + ruleId + "\"}}},{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}"); if (!StringUtils.isEmpty(searchText)) { requestBody.append(",{\"match_phrase_prefix\":{\"_all\":\"" + searchText + "\"}}"); } if (EC2_KERNEL_COMPLIANCE_RULE.equalsIgnoreCase(ruleId)) { requestBody .append(",{\"has_parent\":{\"parent_type\":\"ec2\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"match\":{\"statename\":\"running\"}}],\"must_not\":[{\"match\":{\"platform\":\"windows\"}}]}}}}"); } requestBody.append("]"); if (ruleId.contains(TAGGING_POLICY)) { List<String> tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); if (!tagsList.isEmpty()) { requestBody = requestBody.append(",\"should\":["); for (String tag : tagsList) { requestBody = requestBody.append("{\"match_phrase_prefix\":{\"missingTags\":\"" + tag + "\"}},"); } requestBody.setLength(requestBody.length() - 1); requestBody.append("]"); requestBody.append(",\"minimum_should_match\":1"); } } requestBody .append("}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":1000}}}}"); try { responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch (Exception e) { logger.error(ERROR_IN_US, e); throw new DataException(e); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); return aggsJson.getAsJsonObject("NAME").getAsJsonArray(BUCKETS); }
ComplianceRepositoryImpl implements ComplianceRepository, Constants { public JsonArray getRuleDetailsByApplicationFromES(String assetGroup, String ruleId, String searchText) throws DataException { String responseJson = null; JsonParser jsonParser; JsonObject resultJson; StringBuilder requestBody = null; StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append(assetGroup).append("/") .append(SEARCH); requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"ruleId.keyword\":{\"value\":\"" + ruleId + "\"}}},{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}"); if (!StringUtils.isEmpty(searchText)) { requestBody.append(",{\"match_phrase_prefix\":{\"_all\":\"" + searchText + "\"}}"); } if (EC2_KERNEL_COMPLIANCE_RULE.equalsIgnoreCase(ruleId)) { requestBody .append(",{\"has_parent\":{\"parent_type\":\"ec2\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"match\":{\"statename\":\"running\"}}],\"must_not\":[{\"match\":{\"platform\":\"windows\"}}]}}}}"); } requestBody.append("]"); if (ruleId.contains(TAGGING_POLICY)) { List<String> tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); if (!tagsList.isEmpty()) { requestBody = requestBody.append(",\"should\":["); for (String tag : tagsList) { requestBody = requestBody.append("{\"match_phrase_prefix\":{\"missingTags\":\"" + tag + "\"}},"); } requestBody.setLength(requestBody.length() - 1); requestBody.append("]"); requestBody.append(",\"minimum_should_match\":1"); } } requestBody .append("}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":1000}}}}"); try { responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch (Exception e) { logger.error(ERROR_IN_US, e); throw new DataException(e); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); return aggsJson.getAsJsonObject("NAME").getAsJsonArray(BUCKETS); } }
ComplianceRepositoryImpl implements ComplianceRepository, Constants { public JsonArray getRuleDetailsByApplicationFromES(String assetGroup, String ruleId, String searchText) throws DataException { String responseJson = null; JsonParser jsonParser; JsonObject resultJson; StringBuilder requestBody = null; StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append(assetGroup).append("/") .append(SEARCH); requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"ruleId.keyword\":{\"value\":\"" + ruleId + "\"}}},{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}"); if (!StringUtils.isEmpty(searchText)) { requestBody.append(",{\"match_phrase_prefix\":{\"_all\":\"" + searchText + "\"}}"); } if (EC2_KERNEL_COMPLIANCE_RULE.equalsIgnoreCase(ruleId)) { requestBody .append(",{\"has_parent\":{\"parent_type\":\"ec2\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"match\":{\"statename\":\"running\"}}],\"must_not\":[{\"match\":{\"platform\":\"windows\"}}]}}}}"); } requestBody.append("]"); if (ruleId.contains(TAGGING_POLICY)) { List<String> tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); if (!tagsList.isEmpty()) { requestBody = requestBody.append(",\"should\":["); for (String tag : tagsList) { requestBody = requestBody.append("{\"match_phrase_prefix\":{\"missingTags\":\"" + tag + "\"}},"); } requestBody.setLength(requestBody.length() - 1); requestBody.append("]"); requestBody.append(",\"minimum_should_match\":1"); } } requestBody .append("}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":1000}}}}"); try { responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch (Exception e) { logger.error(ERROR_IN_US, e); throw new DataException(e); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); return aggsJson.getAsJsonObject("NAME").getAsJsonArray(BUCKETS); }  }
ComplianceRepositoryImpl implements ComplianceRepository, Constants { public JsonArray getRuleDetailsByApplicationFromES(String assetGroup, String ruleId, String searchText) throws DataException { String responseJson = null; JsonParser jsonParser; JsonObject resultJson; StringBuilder requestBody = null; StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append(assetGroup).append("/") .append(SEARCH); requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"ruleId.keyword\":{\"value\":\"" + ruleId + "\"}}},{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}"); if (!StringUtils.isEmpty(searchText)) { requestBody.append(",{\"match_phrase_prefix\":{\"_all\":\"" + searchText + "\"}}"); } if (EC2_KERNEL_COMPLIANCE_RULE.equalsIgnoreCase(ruleId)) { requestBody .append(",{\"has_parent\":{\"parent_type\":\"ec2\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"match\":{\"statename\":\"running\"}}],\"must_not\":[{\"match\":{\"platform\":\"windows\"}}]}}}}"); } requestBody.append("]"); if (ruleId.contains(TAGGING_POLICY)) { List<String> tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); if (!tagsList.isEmpty()) { requestBody = requestBody.append(",\"should\":["); for (String tag : tagsList) { requestBody = requestBody.append("{\"match_phrase_prefix\":{\"missingTags\":\"" + tag + "\"}},"); } requestBody.setLength(requestBody.length() - 1); requestBody.append("]"); requestBody.append(",\"minimum_should_match\":1"); } } requestBody .append("}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":1000}}}}"); try { responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch (Exception e) { logger.error(ERROR_IN_US, e); throw new DataException(e); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); return aggsJson.getAsJsonObject("NAME").getAsJsonArray(BUCKETS); }  long getIssuesCount(String assetGroup, String ruleId, String domain); @SuppressWarnings("rawtypes") ResponseWithOrder getIssuesFromES(Request request); @SuppressWarnings("rawtypes") Map<String, Long> getTagging(String assetGroup, String targetType); Map<String, Long> getCertificates(String assetGroup); @SuppressWarnings("rawtypes") List<Map<String, Object>> getRecommendations(String assetGroup, String targetType); Long getTotalAssetCountForAnytargetType(String assetGroup, String targetType); List<Map<String, Object>> getResourceDetailsFromES(String assetGroup, String resourceId); List<LinkedHashMap<String, Object>> getIssueAuditLog(String annotationId, String targetType, int from,
            int size, String searchText); Long getIssueAuditLogCount(String annotationId, String targetType); Map<String, Long> getRulesFromES(String assetGroup); @Override Boolean exemptAndUpdateIssueDetails(IssueResponse issueException); @Override Boolean closeIssuesByRule(RuleDetails ruleDetails); @Override Boolean revokeAndUpdateIssueDetails(String issueId); @Override List<Map<String, Object>> getExemptedIssueDetails(String issueId); @Override List<Map<String, Object>> getOpenIssueDetails(final String issueId); List<Map<String, Object>> getRuleIdWithDisplayNameQuery(String targetType); List<Map<String, Object>> getRuleIDsForTargetType(String targetType); List<Map<String, Object>> getRuleIdDetails(String ruleId); @Override List<Object> getRuleIds(String targetType); @SuppressWarnings("rawtypes") Map<String, Long> getNonCompliancePolicyByEsWithAssetGroup(String assetGroup, String searchText,
            Map<String, String> filters, int from, int size, String targetTypes); String getTargetTypeForAG(String assetGroup, String domain); List<Map<String, Object>> getRulesLastScanDate(); String getScanDate(String ruleId, Map<String, String> rulidwithScanDate); String getRuleCategory(Object ruleId, Map<String, String> ruleIdwithruleParamsMap); JsonArray getRuleDetailsByApplicationFromES(String assetGroup, String ruleId, String searchText); Map<String, Long> getAllApplicationsAssetCountForTargetType(String assetGroup, String targetType); List<Map<String, Object>> getTargetTypeByRuleId(String ruleId); Long getTotalAssetCountForEnvironment(String assetGroup, String application, String environment,
            String targetType); JsonArray getRuleDetailsByEnvironmentFromES(String assetGroup, String ruleId, String application,
            String searchText,String targetType); JsonArray getEventsProcessedFromEs(); List<Map<String, Object>> getRuleDescriptionFromDb(String ruleId); List<Map<String, Object>> getKernelComplianceByInstanceIdFromDb(String instanceId); String fetchSystemConfiguration(final String keyname); @SuppressWarnings("unchecked") @Override Map<String, Object> updateKernelVersion(final KernelVersion kernelVersion); Map<String, Object> getRuleCategoryWeightagefromDB(String domain); @SuppressWarnings("rawtypes") Map<String, Object> getTaggingByAG(String assetGroup, String targetTypes, String application); @SuppressWarnings("rawtypes") Map<String, Object> getPolicyViolationDetailsByIssueId(String assetGroup, String issueId); Map<String, Long> getPatchableAssetsByApplication(String assetGroup, String application, String resourceType); List<Map<String, Object>> getRuleIdWithDisplayNameWithRuleCategoryQuery(String targetType,
            String ruleCategory); Long getPatchabeAssetsCount(String assetGroup, String targetType); Long getUnpatchedAssetsCount(String assetGroup, String targetType, String application); Map<String, Object> formatUnpatchedMustFilter(String targetType, String ruleId); Map<String, Long> getRulesDistribution(String assetGroup, String domain, List<Object> rules,
            String aggsFiltername); Map<String, Object> getRuleCategoryPercentage(Map<String, Long> ruleCategoryDistribution, Long totalIssues); Long getInstanceCountForQualys(String assetGroup, String apiType, String application, String environment,
			String resourceType); Map<String, Long> getInstanceCountForQualysByAppsOrEnv(String assetGroup, String apiType, String application,
			String environment, String resourceType); @Override IssueExceptionResponse exemptAndUpdateMultipleIssueDetails(IssuesException issuesException); @Override IssueExceptionResponse revokeAndUpdateMultipleIssueDetails(List<String> issueIds); String createESDoc(Map<String, ?> doc); Response invokeAPI(String method, String endpoint, String payLoad); Map<String,Long> getTotalAssetCountByEnvironment(String assetGroup, String application,String targetType); List<Map<String, String>> getDataSourceForTargetTypeForAG(String assetGroup, String domain,
			String targetType); Map<String, Long> getTotalAssetCount(String assetGroup, String domain, String application, String type); @Override Map<String, Integer> getExemptedAssetsCountByRule(String assetGroup, String application, String type); Long getPatchabeAssetsCount(String assetGroup, String targetType, String application, String environment,
			String searchText); }
ComplianceRepositoryImpl implements ComplianceRepository, Constants { public JsonArray getRuleDetailsByApplicationFromES(String assetGroup, String ruleId, String searchText) throws DataException { String responseJson = null; JsonParser jsonParser; JsonObject resultJson; StringBuilder requestBody = null; StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append(assetGroup).append("/") .append(SEARCH); requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"term\":{\"type.keyword\":{\"value\":\"issue\"}}},{\"term\":{\"ruleId.keyword\":{\"value\":\"" + ruleId + "\"}}},{\"term\":{\"issueStatus.keyword\":{\"value\":\"open\"}}}"); if (!StringUtils.isEmpty(searchText)) { requestBody.append(",{\"match_phrase_prefix\":{\"_all\":\"" + searchText + "\"}}"); } if (EC2_KERNEL_COMPLIANCE_RULE.equalsIgnoreCase(ruleId)) { requestBody .append(",{\"has_parent\":{\"parent_type\":\"ec2\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"match\":{\"statename\":\"running\"}}],\"must_not\":[{\"match\":{\"platform\":\"windows\"}}]}}}}"); } requestBody.append("]"); if (ruleId.contains(TAGGING_POLICY)) { List<String> tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); if (!tagsList.isEmpty()) { requestBody = requestBody.append(",\"should\":["); for (String tag : tagsList) { requestBody = requestBody.append("{\"match_phrase_prefix\":{\"missingTags\":\"" + tag + "\"}},"); } requestBody.setLength(requestBody.length() - 1); requestBody.append("]"); requestBody.append(",\"minimum_should_match\":1"); } } requestBody .append("}},\"aggs\":{\"NAME\":{\"terms\":{\"field\":\"tags.Application.keyword\",\"size\":1000}}}}"); try { responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch (Exception e) { logger.error(ERROR_IN_US, e); throw new DataException(e); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get(AGGREGATIONS).toString()); return aggsJson.getAsJsonObject("NAME").getAsJsonArray(BUCKETS); }  long getIssuesCount(String assetGroup, String ruleId, String domain); @SuppressWarnings("rawtypes") ResponseWithOrder getIssuesFromES(Request request); @SuppressWarnings("rawtypes") Map<String, Long> getTagging(String assetGroup, String targetType); Map<String, Long> getCertificates(String assetGroup); @SuppressWarnings("rawtypes") List<Map<String, Object>> getRecommendations(String assetGroup, String targetType); Long getTotalAssetCountForAnytargetType(String assetGroup, String targetType); List<Map<String, Object>> getResourceDetailsFromES(String assetGroup, String resourceId); List<LinkedHashMap<String, Object>> getIssueAuditLog(String annotationId, String targetType, int from,
            int size, String searchText); Long getIssueAuditLogCount(String annotationId, String targetType); Map<String, Long> getRulesFromES(String assetGroup); @Override Boolean exemptAndUpdateIssueDetails(IssueResponse issueException); @Override Boolean closeIssuesByRule(RuleDetails ruleDetails); @Override Boolean revokeAndUpdateIssueDetails(String issueId); @Override List<Map<String, Object>> getExemptedIssueDetails(String issueId); @Override List<Map<String, Object>> getOpenIssueDetails(final String issueId); List<Map<String, Object>> getRuleIdWithDisplayNameQuery(String targetType); List<Map<String, Object>> getRuleIDsForTargetType(String targetType); List<Map<String, Object>> getRuleIdDetails(String ruleId); @Override List<Object> getRuleIds(String targetType); @SuppressWarnings("rawtypes") Map<String, Long> getNonCompliancePolicyByEsWithAssetGroup(String assetGroup, String searchText,
            Map<String, String> filters, int from, int size, String targetTypes); String getTargetTypeForAG(String assetGroup, String domain); List<Map<String, Object>> getRulesLastScanDate(); String getScanDate(String ruleId, Map<String, String> rulidwithScanDate); String getRuleCategory(Object ruleId, Map<String, String> ruleIdwithruleParamsMap); JsonArray getRuleDetailsByApplicationFromES(String assetGroup, String ruleId, String searchText); Map<String, Long> getAllApplicationsAssetCountForTargetType(String assetGroup, String targetType); List<Map<String, Object>> getTargetTypeByRuleId(String ruleId); Long getTotalAssetCountForEnvironment(String assetGroup, String application, String environment,
            String targetType); JsonArray getRuleDetailsByEnvironmentFromES(String assetGroup, String ruleId, String application,
            String searchText,String targetType); JsonArray getEventsProcessedFromEs(); List<Map<String, Object>> getRuleDescriptionFromDb(String ruleId); List<Map<String, Object>> getKernelComplianceByInstanceIdFromDb(String instanceId); String fetchSystemConfiguration(final String keyname); @SuppressWarnings("unchecked") @Override Map<String, Object> updateKernelVersion(final KernelVersion kernelVersion); Map<String, Object> getRuleCategoryWeightagefromDB(String domain); @SuppressWarnings("rawtypes") Map<String, Object> getTaggingByAG(String assetGroup, String targetTypes, String application); @SuppressWarnings("rawtypes") Map<String, Object> getPolicyViolationDetailsByIssueId(String assetGroup, String issueId); Map<String, Long> getPatchableAssetsByApplication(String assetGroup, String application, String resourceType); List<Map<String, Object>> getRuleIdWithDisplayNameWithRuleCategoryQuery(String targetType,
            String ruleCategory); Long getPatchabeAssetsCount(String assetGroup, String targetType); Long getUnpatchedAssetsCount(String assetGroup, String targetType, String application); Map<String, Object> formatUnpatchedMustFilter(String targetType, String ruleId); Map<String, Long> getRulesDistribution(String assetGroup, String domain, List<Object> rules,
            String aggsFiltername); Map<String, Object> getRuleCategoryPercentage(Map<String, Long> ruleCategoryDistribution, Long totalIssues); Long getInstanceCountForQualys(String assetGroup, String apiType, String application, String environment,
			String resourceType); Map<String, Long> getInstanceCountForQualysByAppsOrEnv(String assetGroup, String apiType, String application,
			String environment, String resourceType); @Override IssueExceptionResponse exemptAndUpdateMultipleIssueDetails(IssuesException issuesException); @Override IssueExceptionResponse revokeAndUpdateMultipleIssueDetails(List<String> issueIds); String createESDoc(Map<String, ?> doc); Response invokeAPI(String method, String endpoint, String payLoad); Map<String,Long> getTotalAssetCountByEnvironment(String assetGroup, String application,String targetType); List<Map<String, String>> getDataSourceForTargetTypeForAG(String assetGroup, String domain,
			String targetType); Map<String, Long> getTotalAssetCount(String assetGroup, String domain, String application, String type); @Override Map<String, Integer> getExemptedAssetsCountByRule(String assetGroup, String application, String type); Long getPatchabeAssetsCount(String assetGroup, String targetType, String application, String environment,
			String searchText);  }