@Test public void test()throws Exception{ ConfigurationRecorder dt = new ConfigurationRecorder(); dt.setName("test"); Collection<ConfigurationRecorder> li = new ArrayList<>(); li.add(dt); DescribeConfigurationRecordersResult describeConfigurationRecordersResult = new DescribeConfigurationRecordersResult(); describeConfigurationRecordersResult.setConfigurationRecorders(li); Collection<ConfigurationRecorder> emptyList = new ArrayList<>(); DescribeConfigurationRecordersResult emptyDetectorsResult = new DescribeConfigurationRecordersResult(); emptyDetectorsResult.setConfigurationRecorders(emptyList); mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( true); Map<String,Object>map=new HashMap<String, Object>(); map.put("client", awsConfigClient); CheckAWSConfigEnabled spy = Mockito.spy(new CheckAWSConfigEnabled()); Mockito.doReturn(map).when((BaseRule)spy).getClientFor(anyObject(), anyString(), anyObject()); when(awsConfigClient.describeConfigurationRecorders()).thenReturn(describeConfigurationRecordersResult); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(awsConfigClient.describeConfigurationRecorders()).thenReturn(emptyDetectorsResult); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(awsConfigClient.describeConfigurationRecorders()).thenThrow(new InvalidInputException()); assertThatThrownBy( () -> configEnabled.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> configEnabled.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); }
public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckAWSConfigEnabled started========="); Map<String, Object> map = null; AmazonConfigClient awsConfigClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); 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(severity,category,roleIdentifyingString)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } Annotation annotation = null; try { map = getClientFor(AWSService.CONFIG, roleIdentifyingString, ruleParam); awsConfigClient = (AmazonConfigClient) map.get(PacmanSdkConstants.CLIENT); DescribeConfigurationRecordersResult describeConfigurationRecordersResult = awsConfigClient.describeConfigurationRecorders(); List<ConfigurationRecorder> configurationRecorders = describeConfigurationRecordersResult.getConfigurationRecorders(); if (CollectionUtils.isNullOrEmpty(configurationRecorders)) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS Config not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS Config not enabled"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========CheckAWSConfigEnabled ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } else { logger.info("AWS Config enabled"); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.getMessage()); } logger.debug("========CheckAWSConfigEnabled ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
CheckAWSConfigEnabled extends BaseRule { public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckAWSConfigEnabled started========="); Map<String, Object> map = null; AmazonConfigClient awsConfigClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); 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(severity,category,roleIdentifyingString)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } Annotation annotation = null; try { map = getClientFor(AWSService.CONFIG, roleIdentifyingString, ruleParam); awsConfigClient = (AmazonConfigClient) map.get(PacmanSdkConstants.CLIENT); DescribeConfigurationRecordersResult describeConfigurationRecordersResult = awsConfigClient.describeConfigurationRecorders(); List<ConfigurationRecorder> configurationRecorders = describeConfigurationRecordersResult.getConfigurationRecorders(); if (CollectionUtils.isNullOrEmpty(configurationRecorders)) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS Config not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS Config not enabled"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========CheckAWSConfigEnabled ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } else { logger.info("AWS Config enabled"); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.getMessage()); } logger.debug("========CheckAWSConfigEnabled ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
CheckAWSConfigEnabled extends BaseRule { public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckAWSConfigEnabled started========="); Map<String, Object> map = null; AmazonConfigClient awsConfigClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); 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(severity,category,roleIdentifyingString)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } Annotation annotation = null; try { map = getClientFor(AWSService.CONFIG, roleIdentifyingString, ruleParam); awsConfigClient = (AmazonConfigClient) map.get(PacmanSdkConstants.CLIENT); DescribeConfigurationRecordersResult describeConfigurationRecordersResult = awsConfigClient.describeConfigurationRecorders(); List<ConfigurationRecorder> configurationRecorders = describeConfigurationRecordersResult.getConfigurationRecorders(); if (CollectionUtils.isNullOrEmpty(configurationRecorders)) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS Config not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS Config not enabled"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========CheckAWSConfigEnabled ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } else { logger.info("AWS Config enabled"); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.getMessage()); } logger.debug("========CheckAWSConfigEnabled ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
CheckAWSConfigEnabled extends BaseRule { public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckAWSConfigEnabled started========="); Map<String, Object> map = null; AmazonConfigClient awsConfigClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); 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(severity,category,roleIdentifyingString)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } Annotation annotation = null; try { map = getClientFor(AWSService.CONFIG, roleIdentifyingString, ruleParam); awsConfigClient = (AmazonConfigClient) map.get(PacmanSdkConstants.CLIENT); DescribeConfigurationRecordersResult describeConfigurationRecordersResult = awsConfigClient.describeConfigurationRecorders(); List<ConfigurationRecorder> configurationRecorders = describeConfigurationRecordersResult.getConfigurationRecorders(); if (CollectionUtils.isNullOrEmpty(configurationRecorders)) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS Config not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS Config not enabled"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========CheckAWSConfigEnabled ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } else { logger.info("AWS Config enabled"); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.getMessage()); } logger.debug("========CheckAWSConfigEnabled ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes); String getHelpText(); }
CheckAWSConfigEnabled extends BaseRule { public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckAWSConfigEnabled started========="); Map<String, Object> map = null; AmazonConfigClient awsConfigClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); 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(severity,category,roleIdentifyingString)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } Annotation annotation = null; try { map = getClientFor(AWSService.CONFIG, roleIdentifyingString, ruleParam); awsConfigClient = (AmazonConfigClient) map.get(PacmanSdkConstants.CLIENT); DescribeConfigurationRecordersResult describeConfigurationRecordersResult = awsConfigClient.describeConfigurationRecorders(); List<ConfigurationRecorder> configurationRecorders = describeConfigurationRecordersResult.getConfigurationRecorders(); if (CollectionUtils.isNullOrEmpty(configurationRecorders)) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS Config not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS Config not enabled"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========CheckAWSConfigEnabled ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } else { logger.info("AWS Config enabled"); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.getMessage()); } logger.debug("========CheckAWSConfigEnabled ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes); String getHelpText();  }