@Test public void test()throws Exception{ FlowLog flow = new FlowLog(); flow.setResourceId("123"); List<FlowLog> flowLogs = new ArrayList<>(); flowLogs.add(flow); List<FlowLog> emptyList = new ArrayList<>(); mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( true); Map<String,Object>map=new HashMap<String, Object>(); map.put("client", ec2Client); VpcFlowLogsEnabled spy = Mockito.spy(new VpcFlowLogsEnabled()); Mockito.doReturn(map).when((BaseRule)spy).getClientFor(anyObject(), anyString(), anyObject()); mockStatic(PacmanEc2Utils.class); when(PacmanEc2Utils.getFlowLogs(anyObject(),anyObject())).thenReturn(flowLogs); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(PacmanEc2Utils.getFlowLogs(anyObject(),anyObject())).thenReturn(emptyList); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(PacmanEc2Utils.getFlowLogs(anyObject(),anyObject())).thenThrow(new InvalidInputException()); assertThatThrownBy( () -> flowLogsEnabled.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> flowLogsEnabled.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("========VpcFlowLogsEnabled started========="); Map<String, Object> map = null; AmazonEC2 ec2Client = null; String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String entityId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); 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.EC2, roleIdentifyingString, ruleParam); ec2Client = (AmazonEC2) map.get(PacmanSdkConstants.CLIENT); } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } DescribeFlowLogsRequest describeFlowLogsRequest = new DescribeFlowLogsRequest(); Filter filter = new Filter(); filter.setName(PacmanRuleConstants.RESOURCE_NAME); filter.setValues(Arrays.asList(entityId)); describeFlowLogsRequest.setFilter(Arrays.asList(filter)); List<FlowLog> flowLogs = PacmanEc2Utils.getFlowLogs(ec2Client, describeFlowLogsRequest); if (CollectionUtils.isNullOrEmpty(flowLogs)) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "VPC Flow log not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "VPC Flow log not enabled"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========VpcFlowLogsEnabled ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } else { logger.info(entityId,"Flowlog enabled for the VPC"); } logger.debug("========VpcFlowLogsEnabled ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }
VpcFlowLogsEnabled extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========VpcFlowLogsEnabled started========="); Map<String, Object> map = null; AmazonEC2 ec2Client = null; String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String entityId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); 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.EC2, roleIdentifyingString, ruleParam); ec2Client = (AmazonEC2) map.get(PacmanSdkConstants.CLIENT); } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } DescribeFlowLogsRequest describeFlowLogsRequest = new DescribeFlowLogsRequest(); Filter filter = new Filter(); filter.setName(PacmanRuleConstants.RESOURCE_NAME); filter.setValues(Arrays.asList(entityId)); describeFlowLogsRequest.setFilter(Arrays.asList(filter)); List<FlowLog> flowLogs = PacmanEc2Utils.getFlowLogs(ec2Client, describeFlowLogsRequest); if (CollectionUtils.isNullOrEmpty(flowLogs)) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "VPC Flow log not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "VPC Flow log not enabled"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========VpcFlowLogsEnabled ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } else { logger.info(entityId,"Flowlog enabled for the VPC"); } logger.debug("========VpcFlowLogsEnabled ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } }
VpcFlowLogsEnabled extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========VpcFlowLogsEnabled started========="); Map<String, Object> map = null; AmazonEC2 ec2Client = null; String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String entityId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); 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.EC2, roleIdentifyingString, ruleParam); ec2Client = (AmazonEC2) map.get(PacmanSdkConstants.CLIENT); } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } DescribeFlowLogsRequest describeFlowLogsRequest = new DescribeFlowLogsRequest(); Filter filter = new Filter(); filter.setName(PacmanRuleConstants.RESOURCE_NAME); filter.setValues(Arrays.asList(entityId)); describeFlowLogsRequest.setFilter(Arrays.asList(filter)); List<FlowLog> flowLogs = PacmanEc2Utils.getFlowLogs(ec2Client, describeFlowLogsRequest); if (CollectionUtils.isNullOrEmpty(flowLogs)) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "VPC Flow log not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "VPC Flow log not enabled"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========VpcFlowLogsEnabled ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } else { logger.info(entityId,"Flowlog enabled for the VPC"); } logger.debug("========VpcFlowLogsEnabled ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  }
VpcFlowLogsEnabled extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========VpcFlowLogsEnabled started========="); Map<String, Object> map = null; AmazonEC2 ec2Client = null; String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String entityId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); 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.EC2, roleIdentifyingString, ruleParam); ec2Client = (AmazonEC2) map.get(PacmanSdkConstants.CLIENT); } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } DescribeFlowLogsRequest describeFlowLogsRequest = new DescribeFlowLogsRequest(); Filter filter = new Filter(); filter.setName(PacmanRuleConstants.RESOURCE_NAME); filter.setValues(Arrays.asList(entityId)); describeFlowLogsRequest.setFilter(Arrays.asList(filter)); List<FlowLog> flowLogs = PacmanEc2Utils.getFlowLogs(ec2Client, describeFlowLogsRequest); if (CollectionUtils.isNullOrEmpty(flowLogs)) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "VPC Flow log not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "VPC Flow log not enabled"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========VpcFlowLogsEnabled ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } else { logger.info(entityId,"Flowlog enabled for the VPC"); } logger.debug("========VpcFlowLogsEnabled ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); String getHelpText(); }
VpcFlowLogsEnabled extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========VpcFlowLogsEnabled started========="); Map<String, Object> map = null; AmazonEC2 ec2Client = null; String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String entityId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); 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.EC2, roleIdentifyingString, ruleParam); ec2Client = (AmazonEC2) map.get(PacmanSdkConstants.CLIENT); } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } DescribeFlowLogsRequest describeFlowLogsRequest = new DescribeFlowLogsRequest(); Filter filter = new Filter(); filter.setName(PacmanRuleConstants.RESOURCE_NAME); filter.setValues(Arrays.asList(entityId)); describeFlowLogsRequest.setFilter(Arrays.asList(filter)); List<FlowLog> flowLogs = PacmanEc2Utils.getFlowLogs(ec2Client, describeFlowLogsRequest); if (CollectionUtils.isNullOrEmpty(flowLogs)) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "VPC Flow log not enabled"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "VPC Flow log not enabled"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========VpcFlowLogsEnabled ended with annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } else { logger.info(entityId,"Flowlog enabled for the VPC"); } logger.debug("========VpcFlowLogsEnabled ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); String getHelpText();  }