@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.getEBSSnapshotWithCheckId(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getMapString("r_123 ")); assertThat(ebsSnapshotWithPublicAccess.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.getEBSSnapshotWithCheckId(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getEmptyMapString()); assertThat(ebsSnapshotWithPublicAccess.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.getEBSSnapshotWithCheckId(anyString(),anyString(),anyString(),anyString(),anyString())).thenThrow(new Exception()); assertThatThrownBy( () -> ebsSnapshotWithPublicAccess.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(RuleExecutionFailedExeption.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> ebsSnapshotWithPublicAccess.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); }
@Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========EbsSnapshotWithPublicAccess started========="); Annotation annotation = null; String resourceId = null; String region = null; String accountId = null; String checkId = StringUtils.trim(ruleParam .get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isEmpty(formattedUrl)){ serviceEsURL = 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(checkId, severity, category, serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { region = resourceAttributes.get(PacmanRuleConstants.REGION); accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); resourceId = StringUtils.trim(resourceAttributes .get("_resourceid")); Map<String, String> ebsSnapshotMap = new HashMap<>(); try { ebsSnapshotMap = PacmanUtils.getEBSSnapshotWithCheckId(checkId, resourceId, serviceEsURL, region, accountId); } catch (Exception e) { logger.error("unable to determine", e); throw new RuleExecutionFailedExeption("unable to determine" + e); } if (!ebsSnapshotMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Amazon EBS snapshot with public access found !!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put("status", ebsSnapshotMap.get("status")); annotation.put("volumeId", ebsSnapshotMap.get("volumeId")); annotation .put("description", ebsSnapshotMap.get("description")); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Amazon EBS snapshot with public access found"); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========EbsSnapshotWithPublicAccess ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========EbsSnapshotWithPublicAccess ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }
EbsSnapshotWithPublicAccess extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========EbsSnapshotWithPublicAccess started========="); Annotation annotation = null; String resourceId = null; String region = null; String accountId = null; String checkId = StringUtils.trim(ruleParam .get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isEmpty(formattedUrl)){ serviceEsURL = 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(checkId, severity, category, serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { region = resourceAttributes.get(PacmanRuleConstants.REGION); accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); resourceId = StringUtils.trim(resourceAttributes .get("_resourceid")); Map<String, String> ebsSnapshotMap = new HashMap<>(); try { ebsSnapshotMap = PacmanUtils.getEBSSnapshotWithCheckId(checkId, resourceId, serviceEsURL, region, accountId); } catch (Exception e) { logger.error("unable to determine", e); throw new RuleExecutionFailedExeption("unable to determine" + e); } if (!ebsSnapshotMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Amazon EBS snapshot with public access found !!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put("status", ebsSnapshotMap.get("status")); annotation.put("volumeId", ebsSnapshotMap.get("volumeId")); annotation .put("description", ebsSnapshotMap.get("description")); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Amazon EBS snapshot with public access found"); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========EbsSnapshotWithPublicAccess ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========EbsSnapshotWithPublicAccess ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } }
EbsSnapshotWithPublicAccess extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========EbsSnapshotWithPublicAccess started========="); Annotation annotation = null; String resourceId = null; String region = null; String accountId = null; String checkId = StringUtils.trim(ruleParam .get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isEmpty(formattedUrl)){ serviceEsURL = 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(checkId, severity, category, serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { region = resourceAttributes.get(PacmanRuleConstants.REGION); accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); resourceId = StringUtils.trim(resourceAttributes .get("_resourceid")); Map<String, String> ebsSnapshotMap = new HashMap<>(); try { ebsSnapshotMap = PacmanUtils.getEBSSnapshotWithCheckId(checkId, resourceId, serviceEsURL, region, accountId); } catch (Exception e) { logger.error("unable to determine", e); throw new RuleExecutionFailedExeption("unable to determine" + e); } if (!ebsSnapshotMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Amazon EBS snapshot with public access found !!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put("status", ebsSnapshotMap.get("status")); annotation.put("volumeId", ebsSnapshotMap.get("volumeId")); annotation .put("description", ebsSnapshotMap.get("description")); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Amazon EBS snapshot with public access found"); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========EbsSnapshotWithPublicAccess ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========EbsSnapshotWithPublicAccess ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  }
EbsSnapshotWithPublicAccess extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========EbsSnapshotWithPublicAccess started========="); Annotation annotation = null; String resourceId = null; String region = null; String accountId = null; String checkId = StringUtils.trim(ruleParam .get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isEmpty(formattedUrl)){ serviceEsURL = 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(checkId, severity, category, serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { region = resourceAttributes.get(PacmanRuleConstants.REGION); accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); resourceId = StringUtils.trim(resourceAttributes .get("_resourceid")); Map<String, String> ebsSnapshotMap = new HashMap<>(); try { ebsSnapshotMap = PacmanUtils.getEBSSnapshotWithCheckId(checkId, resourceId, serviceEsURL, region, accountId); } catch (Exception e) { logger.error("unable to determine", e); throw new RuleExecutionFailedExeption("unable to determine" + e); } if (!ebsSnapshotMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Amazon EBS snapshot with public access found !!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put("status", ebsSnapshotMap.get("status")); annotation.put("volumeId", ebsSnapshotMap.get("volumeId")); annotation .put("description", ebsSnapshotMap.get("description")); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Amazon EBS snapshot with public access found"); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========EbsSnapshotWithPublicAccess ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========EbsSnapshotWithPublicAccess ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText(); }
EbsSnapshotWithPublicAccess extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========EbsSnapshotWithPublicAccess started========="); Annotation annotation = null; String resourceId = null; String region = null; String accountId = null; String checkId = StringUtils.trim(ruleParam .get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isEmpty(formattedUrl)){ serviceEsURL = 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(checkId, severity, category, serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { region = resourceAttributes.get(PacmanRuleConstants.REGION); accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); resourceId = StringUtils.trim(resourceAttributes .get("_resourceid")); Map<String, String> ebsSnapshotMap = new HashMap<>(); try { ebsSnapshotMap = PacmanUtils.getEBSSnapshotWithCheckId(checkId, resourceId, serviceEsURL, region, accountId); } catch (Exception e) { logger.error("unable to determine", e); throw new RuleExecutionFailedExeption("unable to determine" + e); } if (!ebsSnapshotMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Amazon EBS snapshot with public access found !!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put("status", ebsSnapshotMap.get("status")); annotation.put("volumeId", ebsSnapshotMap.get("volumeId")); annotation .put("description", ebsSnapshotMap.get("description")); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Amazon EBS snapshot with public access found"); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========EbsSnapshotWithPublicAccess ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========EbsSnapshotWithPublicAccess ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText(); static final Logger logger; }