@Test public void getPolicyViolationDetailsByIssueIdTest() throws Exception { String response = "{\"took\":391,\"timed_out\":false,\"_shards\":{\"total\":176,\"successful\":176,\"failed\":0},\"hits\":{\"total\":5427,\"max_score\":4.344331,\"hits\":[{\"_index\":\"_index\",\"_type\":\"_type\",\"_id\":\"_id\",\"_score\":4.344331,\"_routing\":\"_routing\",\"_parent\":\"_parent\",\"_source\":{\"severity\":\"high\",\"_resourceid\":\"_resourceid\",\"ruleCategory\":\"security\",\"_docid\":\"_docid\",\"targetType\":\"ec2\",\"type\":\"issue\",\"issueDetails\":\"[{violationReason=Default target kernel criteria not maintained}]\",\"accountid\":\"123456789\",\"executionId\":\"executionId\",\"issueStatus\":\"open\",\"createdDate\":\"2018-07-03T01:00:50.820Z\",\"policyId\":\"cloud-kernel-compliance_version-1\",\"accountname\":\"account1\",\"tags.Environment\":\"env\",\"tags.Application\":\"appl\",\"pac_ds\":\"aws\",\"modifiedDate\":\"2018-07-09T23:00:50.788Z\",\"ruleId\":\"kernelcomplianceRule\",\"region\":\"region\",\"desc\":\"Target Kernerl Criteria not maintained\"}}]}}"; ReflectionTestUtils.setField(complianceRepositoryImpl, "esUrl", "dummyEsURL"); mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(response); complianceRepositoryImpl.getPolicyViolationDetailsByIssueId("test", "1234"); }
@SuppressWarnings("rawtypes") public Map<String, Object> getPolicyViolationDetailsByIssueId(String assetGroup, String issueId) throws DataException { Map<String, Object> issueDetails = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder(); requestBody.append("{\"query\":{\"bool\":{\"must\":[{\"match\":{\"type\":\"issue\"}},{\"match\":{\"_id\":\"") .append(issueId).append("\"}}]}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { logger.error(e.getMessage()); throw new DataException("No response found!!" + e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); if (resultJson != null) { JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(HITS).toString()); JsonArray hitsArray = hitsJson.getAsJsonArray(HITS); for (int i = 0; i < hitsArray.size(); i++) { JsonObject source = hitsArray.get(i).getAsJsonObject().get("_source").getAsJsonObject(); issueDetails.put(RULEID, source.get(RULEID).getAsString()); issueDetails.put(RESOURCEID, source.get(RESOURCEID).getAsString()); issueDetails.put(TARGET_TYPE, source.get(TARGET_TYPE).getAsString()); issueDetails.put(ISSUE_STATUS, source.get(ISSUE_STATUS).getAsString()); issueDetails.put(RULE_CATEGORY, source.get(RULE_CATEGORY).getAsString()); issueDetails.put(SEVERITY, source.get(SEVERITY).getAsString()); issueDetails.put(POLICYID, source.get(POLICYID).getAsString()); issueDetails.put(CREATED_DATE, source.get(CREATED_DATE).getAsString()); issueDetails.put(MODIFIED_DATE, source.get(MODIFIED_DATE).getAsString()); if(source.has("desc")){ issueDetails.put(ISSUE_REASON, source.get("desc").getAsString()); }else{ JsonObject message = (JsonObject) jsonParser.parse(source.get("message").getAsString()); issueDetails.put(ISSUE_REASON, message.get("desc").getAsString()); } if (null != source.get(ISSUE_DETAILS)) { issueDetails.put(ISSUE_DETAILS, source.get(ISSUE_DETAILS).getAsString()); } } } return issueDetails; }
ComplianceRepositoryImpl implements ComplianceRepository, Constants { @SuppressWarnings("rawtypes") public Map<String, Object> getPolicyViolationDetailsByIssueId(String assetGroup, String issueId) throws DataException { Map<String, Object> issueDetails = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder(); requestBody.append("{\"query\":{\"bool\":{\"must\":[{\"match\":{\"type\":\"issue\"}},{\"match\":{\"_id\":\"") .append(issueId).append("\"}}]}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { logger.error(e.getMessage()); throw new DataException("No response found!!" + e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); if (resultJson != null) { JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(HITS).toString()); JsonArray hitsArray = hitsJson.getAsJsonArray(HITS); for (int i = 0; i < hitsArray.size(); i++) { JsonObject source = hitsArray.get(i).getAsJsonObject().get("_source").getAsJsonObject(); issueDetails.put(RULEID, source.get(RULEID).getAsString()); issueDetails.put(RESOURCEID, source.get(RESOURCEID).getAsString()); issueDetails.put(TARGET_TYPE, source.get(TARGET_TYPE).getAsString()); issueDetails.put(ISSUE_STATUS, source.get(ISSUE_STATUS).getAsString()); issueDetails.put(RULE_CATEGORY, source.get(RULE_CATEGORY).getAsString()); issueDetails.put(SEVERITY, source.get(SEVERITY).getAsString()); issueDetails.put(POLICYID, source.get(POLICYID).getAsString()); issueDetails.put(CREATED_DATE, source.get(CREATED_DATE).getAsString()); issueDetails.put(MODIFIED_DATE, source.get(MODIFIED_DATE).getAsString()); if(source.has("desc")){ issueDetails.put(ISSUE_REASON, source.get("desc").getAsString()); }else{ JsonObject message = (JsonObject) jsonParser.parse(source.get("message").getAsString()); issueDetails.put(ISSUE_REASON, message.get("desc").getAsString()); } if (null != source.get(ISSUE_DETAILS)) { issueDetails.put(ISSUE_DETAILS, source.get(ISSUE_DETAILS).getAsString()); } } } return issueDetails; } }
ComplianceRepositoryImpl implements ComplianceRepository, Constants { @SuppressWarnings("rawtypes") public Map<String, Object> getPolicyViolationDetailsByIssueId(String assetGroup, String issueId) throws DataException { Map<String, Object> issueDetails = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder(); requestBody.append("{\"query\":{\"bool\":{\"must\":[{\"match\":{\"type\":\"issue\"}},{\"match\":{\"_id\":\"") .append(issueId).append("\"}}]}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { logger.error(e.getMessage()); throw new DataException("No response found!!" + e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); if (resultJson != null) { JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(HITS).toString()); JsonArray hitsArray = hitsJson.getAsJsonArray(HITS); for (int i = 0; i < hitsArray.size(); i++) { JsonObject source = hitsArray.get(i).getAsJsonObject().get("_source").getAsJsonObject(); issueDetails.put(RULEID, source.get(RULEID).getAsString()); issueDetails.put(RESOURCEID, source.get(RESOURCEID).getAsString()); issueDetails.put(TARGET_TYPE, source.get(TARGET_TYPE).getAsString()); issueDetails.put(ISSUE_STATUS, source.get(ISSUE_STATUS).getAsString()); issueDetails.put(RULE_CATEGORY, source.get(RULE_CATEGORY).getAsString()); issueDetails.put(SEVERITY, source.get(SEVERITY).getAsString()); issueDetails.put(POLICYID, source.get(POLICYID).getAsString()); issueDetails.put(CREATED_DATE, source.get(CREATED_DATE).getAsString()); issueDetails.put(MODIFIED_DATE, source.get(MODIFIED_DATE).getAsString()); if(source.has("desc")){ issueDetails.put(ISSUE_REASON, source.get("desc").getAsString()); }else{ JsonObject message = (JsonObject) jsonParser.parse(source.get("message").getAsString()); issueDetails.put(ISSUE_REASON, message.get("desc").getAsString()); } if (null != source.get(ISSUE_DETAILS)) { issueDetails.put(ISSUE_DETAILS, source.get(ISSUE_DETAILS).getAsString()); } } } return issueDetails; }  }
ComplianceRepositoryImpl implements ComplianceRepository, Constants { @SuppressWarnings("rawtypes") public Map<String, Object> getPolicyViolationDetailsByIssueId(String assetGroup, String issueId) throws DataException { Map<String, Object> issueDetails = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder(); requestBody.append("{\"query\":{\"bool\":{\"must\":[{\"match\":{\"type\":\"issue\"}},{\"match\":{\"_id\":\"") .append(issueId).append("\"}}]}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { logger.error(e.getMessage()); throw new DataException("No response found!!" + e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); if (resultJson != null) { JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(HITS).toString()); JsonArray hitsArray = hitsJson.getAsJsonArray(HITS); for (int i = 0; i < hitsArray.size(); i++) { JsonObject source = hitsArray.get(i).getAsJsonObject().get("_source").getAsJsonObject(); issueDetails.put(RULEID, source.get(RULEID).getAsString()); issueDetails.put(RESOURCEID, source.get(RESOURCEID).getAsString()); issueDetails.put(TARGET_TYPE, source.get(TARGET_TYPE).getAsString()); issueDetails.put(ISSUE_STATUS, source.get(ISSUE_STATUS).getAsString()); issueDetails.put(RULE_CATEGORY, source.get(RULE_CATEGORY).getAsString()); issueDetails.put(SEVERITY, source.get(SEVERITY).getAsString()); issueDetails.put(POLICYID, source.get(POLICYID).getAsString()); issueDetails.put(CREATED_DATE, source.get(CREATED_DATE).getAsString()); issueDetails.put(MODIFIED_DATE, source.get(MODIFIED_DATE).getAsString()); if(source.has("desc")){ issueDetails.put(ISSUE_REASON, source.get("desc").getAsString()); }else{ JsonObject message = (JsonObject) jsonParser.parse(source.get("message").getAsString()); issueDetails.put(ISSUE_REASON, message.get("desc").getAsString()); } if (null != source.get(ISSUE_DETAILS)) { issueDetails.put(ISSUE_DETAILS, source.get(ISSUE_DETAILS).getAsString()); } } } return issueDetails; }  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 { @SuppressWarnings("rawtypes") public Map<String, Object> getPolicyViolationDetailsByIssueId(String assetGroup, String issueId) throws DataException { Map<String, Object> issueDetails = new HashMap<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup); urlToQuery.append("/").append(SEARCH); StringBuilder requestBody = new StringBuilder(); requestBody.append("{\"query\":{\"bool\":{\"must\":[{\"match\":{\"type\":\"issue\"}},{\"match\":{\"_id\":\"") .append(issueId).append("\"}}]}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { logger.error(e.getMessage()); throw new DataException("No response found!!" + e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); if (resultJson != null) { JsonObject hitsJson = (JsonObject) jsonParser.parse(resultJson.get(HITS).toString()); JsonArray hitsArray = hitsJson.getAsJsonArray(HITS); for (int i = 0; i < hitsArray.size(); i++) { JsonObject source = hitsArray.get(i).getAsJsonObject().get("_source").getAsJsonObject(); issueDetails.put(RULEID, source.get(RULEID).getAsString()); issueDetails.put(RESOURCEID, source.get(RESOURCEID).getAsString()); issueDetails.put(TARGET_TYPE, source.get(TARGET_TYPE).getAsString()); issueDetails.put(ISSUE_STATUS, source.get(ISSUE_STATUS).getAsString()); issueDetails.put(RULE_CATEGORY, source.get(RULE_CATEGORY).getAsString()); issueDetails.put(SEVERITY, source.get(SEVERITY).getAsString()); issueDetails.put(POLICYID, source.get(POLICYID).getAsString()); issueDetails.put(CREATED_DATE, source.get(CREATED_DATE).getAsString()); issueDetails.put(MODIFIED_DATE, source.get(MODIFIED_DATE).getAsString()); if(source.has("desc")){ issueDetails.put(ISSUE_REASON, source.get("desc").getAsString()); }else{ JsonObject message = (JsonObject) jsonParser.parse(source.get("message").getAsString()); issueDetails.put(ISSUE_REASON, message.get("desc").getAsString()); } if (null != source.get(ISSUE_DETAILS)) { issueDetails.put(ISSUE_DETAILS, source.get(ISSUE_DETAILS).getAsString()); } } } return issueDetails; }  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);  }