@Test public void test()throws Exception{ Trail dt = new Trail(); dt.setName("test"); dt.setIsMultiRegionTrail(true); Collection<Trail> li = new ArrayList<>(); li.add(dt); DescribeTrailsResult result = new DescribeTrailsResult(); result.setTrailList(li); Collection<Trail> emptyList = new ArrayList<>(); DescribeTrailsResult emptyDetectorsResult = new DescribeTrailsResult(); emptyDetectorsResult.setTrailList(emptyList); mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( true); Map<String,Object>map=new HashMap<String, Object>(); map.put("client", cloudTrailClient); CheckAWSCloudTrailConfig spy = Mockito.spy(new CheckAWSCloudTrailConfig()); Mockito.doReturn(map).when((BaseRule)spy).getClientFor(anyObject(), anyString(), anyObject()); when(cloudTrailClient.describeTrails()).thenReturn(result); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(cloudTrailClient.describeTrails()).thenReturn(emptyDetectorsResult); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(cloudTrailClient.describeTrails()).thenThrow(new InvalidInputException()); assertThatThrownBy( () -> checkAWSCloudTrailConfig.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> checkAWSCloudTrailConfig.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("========CheckAWSCloudTrailConfig started========="); Map<String, Object> map = null; AWSCloudTrailClient cloudTrailClient = null; Annotation annotation = null; String cloudtrailFlg = 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); } try { map = getClientFor(AWSService.CLOUDTRL, roleIdentifyingString, ruleParam); cloudTrailClient = (AWSCloudTrailClient) map .get(PacmanSdkConstants.CLIENT); } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } DescribeTrailsResult result = cloudTrailClient.describeTrails(); List<Trail> trailList = result.getTrailList(); for (Trail trail : trailList) { if (trail.getIsMultiRegionTrail()) { cloudtrailFlg = "X"; break; } } if (cloudtrailFlg == null) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "AWS CloudTrail Config not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS CloudTrail Config not enabled"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckAWSCloudTrailConfig ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } logger.debug("========CheckAWSCloudTrailConfig ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }
CheckAWSCloudTrailConfig extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAWSCloudTrailConfig started========="); Map<String, Object> map = null; AWSCloudTrailClient cloudTrailClient = null; Annotation annotation = null; String cloudtrailFlg = 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); } try { map = getClientFor(AWSService.CLOUDTRL, roleIdentifyingString, ruleParam); cloudTrailClient = (AWSCloudTrailClient) map .get(PacmanSdkConstants.CLIENT); } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } DescribeTrailsResult result = cloudTrailClient.describeTrails(); List<Trail> trailList = result.getTrailList(); for (Trail trail : trailList) { if (trail.getIsMultiRegionTrail()) { cloudtrailFlg = "X"; break; } } if (cloudtrailFlg == null) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "AWS CloudTrail Config not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS CloudTrail Config not enabled"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckAWSCloudTrailConfig ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } logger.debug("========CheckAWSCloudTrailConfig ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } }
CheckAWSCloudTrailConfig extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAWSCloudTrailConfig started========="); Map<String, Object> map = null; AWSCloudTrailClient cloudTrailClient = null; Annotation annotation = null; String cloudtrailFlg = 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); } try { map = getClientFor(AWSService.CLOUDTRL, roleIdentifyingString, ruleParam); cloudTrailClient = (AWSCloudTrailClient) map .get(PacmanSdkConstants.CLIENT); } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } DescribeTrailsResult result = cloudTrailClient.describeTrails(); List<Trail> trailList = result.getTrailList(); for (Trail trail : trailList) { if (trail.getIsMultiRegionTrail()) { cloudtrailFlg = "X"; break; } } if (cloudtrailFlg == null) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "AWS CloudTrail Config not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS CloudTrail Config not enabled"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckAWSCloudTrailConfig ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } logger.debug("========CheckAWSCloudTrailConfig ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  }
CheckAWSCloudTrailConfig extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAWSCloudTrailConfig started========="); Map<String, Object> map = null; AWSCloudTrailClient cloudTrailClient = null; Annotation annotation = null; String cloudtrailFlg = 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); } try { map = getClientFor(AWSService.CLOUDTRL, roleIdentifyingString, ruleParam); cloudTrailClient = (AWSCloudTrailClient) map .get(PacmanSdkConstants.CLIENT); } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } DescribeTrailsResult result = cloudTrailClient.describeTrails(); List<Trail> trailList = result.getTrailList(); for (Trail trail : trailList) { if (trail.getIsMultiRegionTrail()) { cloudtrailFlg = "X"; break; } } if (cloudtrailFlg == null) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "AWS CloudTrail Config not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS CloudTrail Config not enabled"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckAWSCloudTrailConfig ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } logger.debug("========CheckAWSCloudTrailConfig ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText(); }
CheckAWSCloudTrailConfig extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAWSCloudTrailConfig started========="); Map<String, Object> map = null; AWSCloudTrailClient cloudTrailClient = null; Annotation annotation = null; String cloudtrailFlg = 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); } try { map = getClientFor(AWSService.CLOUDTRL, roleIdentifyingString, ruleParam); cloudTrailClient = (AWSCloudTrailClient) map .get(PacmanSdkConstants.CLIENT); } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } DescribeTrailsResult result = cloudTrailClient.describeTrails(); List<Trail> trailList = result.getTrailList(); for (Trail trail : trailList) { if (trail.getIsMultiRegionTrail()) { cloudtrailFlg = "X"; break; } } if (cloudtrailFlg == null) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "AWS CloudTrail Config not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS CloudTrail Config not enabled"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckAWSCloudTrailConfig ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } logger.debug("========CheckAWSCloudTrailConfig ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText();  }