@Test public void executeTest() throws Exception { mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString())).thenReturn( true); when(PacmanUtils.formatUrl(anyObject(),anyString())).thenReturn("host"); when(PacmanUtils.getSeverityVulnerabilitiesByInstanceId(anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getListString()); assertThat(ec2withSeverityVulnerabilityRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); assertThat(ec2withSeverityVulnerabilityRule.execute(CommonTestUtils.getMapString("r_123 "),null), is(notNullValue())); when(PacmanUtils.getSeverityVulnerabilitiesByInstanceId(anyString(),anyString(),anyString())).thenThrow(new Exception()); assertThatThrownBy( () -> ec2withSeverityVulnerabilityRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(RuleExecutionFailedExeption.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> ec2withSeverityVulnerabilityRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); }
public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ResourcewithSeverityVulnerabilityRule started========="); Annotation annotation = null; String instanceId = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String resourceWithVulnInfoForSeverityUrl = null; String severityVulnValue = ruleParam.get(PacmanRuleConstants.SEVERITY_VULN); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_RESOURCE_WITH_VULN_INFO_SEVERITY_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ resourceWithVulnInfoForSeverityUrl = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String,Object>>issueList = new ArrayList<>(); LinkedHashMap<String,Object>issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(resourceWithVulnInfoForSeverityUrl, severityVulnValue,severity,category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { String entityType = resourceAttributes.get(PacmanRuleConstants.ENTITY_TYPE); instanceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.RESOURCE_ID)); try { List<String> severityList = PacmanUtils.getSeverityVulnerabilitiesByInstanceId(instanceId,resourceWithVulnInfoForSeverityUrl, severityVulnValue); if (!severityList.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,""+entityType+" instance with vulnerability "+ severityVulnValue + " found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, ""+entityType+" instance with vulnerability "+ severityVulnValue + " found!!"); issue.put("voilation_title", String.join(",", severityList)); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========ResourcewithSeverityVulnerabilityRule ended with an annotation {} : =========", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (Exception e) { logger.error("error", e); throw new RuleExecutionFailedExeption(e.getMessage()); } } logger.debug("========ResourcewithSeverityVulnerabilityRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
ResourceWithSeverityVulnerabilityRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ResourcewithSeverityVulnerabilityRule started========="); Annotation annotation = null; String instanceId = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String resourceWithVulnInfoForSeverityUrl = null; String severityVulnValue = ruleParam.get(PacmanRuleConstants.SEVERITY_VULN); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_RESOURCE_WITH_VULN_INFO_SEVERITY_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ resourceWithVulnInfoForSeverityUrl = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String,Object>>issueList = new ArrayList<>(); LinkedHashMap<String,Object>issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(resourceWithVulnInfoForSeverityUrl, severityVulnValue,severity,category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { String entityType = resourceAttributes.get(PacmanRuleConstants.ENTITY_TYPE); instanceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.RESOURCE_ID)); try { List<String> severityList = PacmanUtils.getSeverityVulnerabilitiesByInstanceId(instanceId,resourceWithVulnInfoForSeverityUrl, severityVulnValue); if (!severityList.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,""+entityType+" instance with vulnerability "+ severityVulnValue + " found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, ""+entityType+" instance with vulnerability "+ severityVulnValue + " found!!"); issue.put("voilation_title", String.join(",", severityList)); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========ResourcewithSeverityVulnerabilityRule ended with an annotation {} : =========", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (Exception e) { logger.error("error", e); throw new RuleExecutionFailedExeption(e.getMessage()); } } logger.debug("========ResourcewithSeverityVulnerabilityRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
ResourceWithSeverityVulnerabilityRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ResourcewithSeverityVulnerabilityRule started========="); Annotation annotation = null; String instanceId = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String resourceWithVulnInfoForSeverityUrl = null; String severityVulnValue = ruleParam.get(PacmanRuleConstants.SEVERITY_VULN); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_RESOURCE_WITH_VULN_INFO_SEVERITY_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ resourceWithVulnInfoForSeverityUrl = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String,Object>>issueList = new ArrayList<>(); LinkedHashMap<String,Object>issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(resourceWithVulnInfoForSeverityUrl, severityVulnValue,severity,category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { String entityType = resourceAttributes.get(PacmanRuleConstants.ENTITY_TYPE); instanceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.RESOURCE_ID)); try { List<String> severityList = PacmanUtils.getSeverityVulnerabilitiesByInstanceId(instanceId,resourceWithVulnInfoForSeverityUrl, severityVulnValue); if (!severityList.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,""+entityType+" instance with vulnerability "+ severityVulnValue + " found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, ""+entityType+" instance with vulnerability "+ severityVulnValue + " found!!"); issue.put("voilation_title", String.join(",", severityList)); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========ResourcewithSeverityVulnerabilityRule ended with an annotation {} : =========", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (Exception e) { logger.error("error", e); throw new RuleExecutionFailedExeption(e.getMessage()); } } logger.debug("========ResourcewithSeverityVulnerabilityRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
ResourceWithSeverityVulnerabilityRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ResourcewithSeverityVulnerabilityRule started========="); Annotation annotation = null; String instanceId = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String resourceWithVulnInfoForSeverityUrl = null; String severityVulnValue = ruleParam.get(PacmanRuleConstants.SEVERITY_VULN); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_RESOURCE_WITH_VULN_INFO_SEVERITY_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ resourceWithVulnInfoForSeverityUrl = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String,Object>>issueList = new ArrayList<>(); LinkedHashMap<String,Object>issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(resourceWithVulnInfoForSeverityUrl, severityVulnValue,severity,category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { String entityType = resourceAttributes.get(PacmanRuleConstants.ENTITY_TYPE); instanceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.RESOURCE_ID)); try { List<String> severityList = PacmanUtils.getSeverityVulnerabilitiesByInstanceId(instanceId,resourceWithVulnInfoForSeverityUrl, severityVulnValue); if (!severityList.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,""+entityType+" instance with vulnerability "+ severityVulnValue + " found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, ""+entityType+" instance with vulnerability "+ severityVulnValue + " found!!"); issue.put("voilation_title", String.join(",", severityList)); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========ResourcewithSeverityVulnerabilityRule ended with an annotation {} : =========", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (Exception e) { logger.error("error", e); throw new RuleExecutionFailedExeption(e.getMessage()); } } logger.debug("========ResourcewithSeverityVulnerabilityRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes); String getHelpText(); }
ResourceWithSeverityVulnerabilityRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ResourcewithSeverityVulnerabilityRule started========="); Annotation annotation = null; String instanceId = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String resourceWithVulnInfoForSeverityUrl = null; String severityVulnValue = ruleParam.get(PacmanRuleConstants.SEVERITY_VULN); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_RESOURCE_WITH_VULN_INFO_SEVERITY_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ resourceWithVulnInfoForSeverityUrl = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String,Object>>issueList = new ArrayList<>(); LinkedHashMap<String,Object>issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(resourceWithVulnInfoForSeverityUrl, severityVulnValue,severity,category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { String entityType = resourceAttributes.get(PacmanRuleConstants.ENTITY_TYPE); instanceId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.RESOURCE_ID)); try { List<String> severityList = PacmanUtils.getSeverityVulnerabilitiesByInstanceId(instanceId,resourceWithVulnInfoForSeverityUrl, severityVulnValue); if (!severityList.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,""+entityType+" instance with vulnerability "+ severityVulnValue + " found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, ""+entityType+" instance with vulnerability "+ severityVulnValue + " found!!"); issue.put("voilation_title", String.join(",", severityList)); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========ResourcewithSeverityVulnerabilityRule ended with an annotation {} : =========", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (Exception e) { logger.error("error", e); throw new RuleExecutionFailedExeption(e.getMessage()); } } logger.debug("========ResourcewithSeverityVulnerabilityRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes); String getHelpText();  }