@Test public void test()throws Exception{ Collection<String> li = new ArrayList<>(); li.add("123"); ListDetectorsResult detectorsResult = new ListDetectorsResult(); detectorsResult.setDetectorIds(li); Collection<String> emptyList = new ArrayList<>(); ListDetectorsResult emptyDetectorsResult = new ListDetectorsResult(); emptyDetectorsResult.setDetectorIds(emptyList); mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( true); Map<String,Object>map=new HashMap<String, Object>(); map.put("client", dutyClient); CheckGuardDutyForAllAccountsRule spy = Mockito.spy(new CheckGuardDutyForAllAccountsRule ()); Mockito.doReturn(map).when((BaseRule)spy).getClientFor(anyObject(), anyString(), anyObject()); when(dutyClient.listDetectors(anyObject())).thenReturn(detectorsResult); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(dutyClient.listDetectors(anyObject())).thenReturn(emptyDetectorsResult); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(dutyClient.listDetectors(anyObject())).thenThrow(new RuleExecutionFailedExeption()); assertThatThrownBy( () -> checkGuardDutyForAllAccountsRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> checkGuardDutyForAllAccountsRule.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("========CheckGuardDutyForAllAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonGuardDutyClient dutyClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName= resourceAttributes.get(PacmanRuleConstants.ACCOUNT_NAME); 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)); Gson gson = new Gson(); List<LinkedHashMap<String,Object>>issueList = new ArrayList(); LinkedHashMap<String,Object>issue = new LinkedHashMap(); Map<String,Object> failedType =new HashMap(); if (!PacmanUtils.doesAllHaveValue(severity, category,roleIdentifyingString)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { map = getClientFor(AWSService.GUARD_DUTY, roleIdentifyingString, temp); dutyClient = (AmazonGuardDutyClient) map.get(PacmanSdkConstants.CLIENT); ListDetectorsRequest detectorsRequest = new ListDetectorsRequest(); ListDetectorsResult detectorsResult = dutyClient.listDetectors(detectorsRequest); boolean isDisabled = true; if (!detectorsResult.getDetectorIds().isEmpty()) { isDisabled = false; } if (isDisabled) { failedType.put("detectors", "Not found"); annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS Guard Duty service is not enabled to " + accountName + " account"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS Guard Duty service is not enabled to " + accountName + " account"); issue.put("failed_reason", gson.toJson(failedType) ); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========CheckGuardDutyForAllAccountsRule ended with annotaion {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } logger.debug("========CheckGuardDutyForAllAccountsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
CheckGuardDutyForAllAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckGuardDutyForAllAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonGuardDutyClient dutyClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName= resourceAttributes.get(PacmanRuleConstants.ACCOUNT_NAME); 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)); Gson gson = new Gson(); List<LinkedHashMap<String,Object>>issueList = new ArrayList(); LinkedHashMap<String,Object>issue = new LinkedHashMap(); Map<String,Object> failedType =new HashMap(); if (!PacmanUtils.doesAllHaveValue(severity, category,roleIdentifyingString)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { map = getClientFor(AWSService.GUARD_DUTY, roleIdentifyingString, temp); dutyClient = (AmazonGuardDutyClient) map.get(PacmanSdkConstants.CLIENT); ListDetectorsRequest detectorsRequest = new ListDetectorsRequest(); ListDetectorsResult detectorsResult = dutyClient.listDetectors(detectorsRequest); boolean isDisabled = true; if (!detectorsResult.getDetectorIds().isEmpty()) { isDisabled = false; } if (isDisabled) { failedType.put("detectors", "Not found"); annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS Guard Duty service is not enabled to " + accountName + " account"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS Guard Duty service is not enabled to " + accountName + " account"); issue.put("failed_reason", gson.toJson(failedType) ); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========CheckGuardDutyForAllAccountsRule ended with annotaion {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } logger.debug("========CheckGuardDutyForAllAccountsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
CheckGuardDutyForAllAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckGuardDutyForAllAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonGuardDutyClient dutyClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName= resourceAttributes.get(PacmanRuleConstants.ACCOUNT_NAME); 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)); Gson gson = new Gson(); List<LinkedHashMap<String,Object>>issueList = new ArrayList(); LinkedHashMap<String,Object>issue = new LinkedHashMap(); Map<String,Object> failedType =new HashMap(); if (!PacmanUtils.doesAllHaveValue(severity, category,roleIdentifyingString)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { map = getClientFor(AWSService.GUARD_DUTY, roleIdentifyingString, temp); dutyClient = (AmazonGuardDutyClient) map.get(PacmanSdkConstants.CLIENT); ListDetectorsRequest detectorsRequest = new ListDetectorsRequest(); ListDetectorsResult detectorsResult = dutyClient.listDetectors(detectorsRequest); boolean isDisabled = true; if (!detectorsResult.getDetectorIds().isEmpty()) { isDisabled = false; } if (isDisabled) { failedType.put("detectors", "Not found"); annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS Guard Duty service is not enabled to " + accountName + " account"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS Guard Duty service is not enabled to " + accountName + " account"); issue.put("failed_reason", gson.toJson(failedType) ); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========CheckGuardDutyForAllAccountsRule ended with annotaion {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } logger.debug("========CheckGuardDutyForAllAccountsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
CheckGuardDutyForAllAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckGuardDutyForAllAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonGuardDutyClient dutyClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName= resourceAttributes.get(PacmanRuleConstants.ACCOUNT_NAME); 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)); Gson gson = new Gson(); List<LinkedHashMap<String,Object>>issueList = new ArrayList(); LinkedHashMap<String,Object>issue = new LinkedHashMap(); Map<String,Object> failedType =new HashMap(); if (!PacmanUtils.doesAllHaveValue(severity, category,roleIdentifyingString)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { map = getClientFor(AWSService.GUARD_DUTY, roleIdentifyingString, temp); dutyClient = (AmazonGuardDutyClient) map.get(PacmanSdkConstants.CLIENT); ListDetectorsRequest detectorsRequest = new ListDetectorsRequest(); ListDetectorsResult detectorsResult = dutyClient.listDetectors(detectorsRequest); boolean isDisabled = true; if (!detectorsResult.getDetectorIds().isEmpty()) { isDisabled = false; } if (isDisabled) { failedType.put("detectors", "Not found"); annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS Guard Duty service is not enabled to " + accountName + " account"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS Guard Duty service is not enabled to " + accountName + " account"); issue.put("failed_reason", gson.toJson(failedType) ); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========CheckGuardDutyForAllAccountsRule ended with annotaion {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } logger.debug("========CheckGuardDutyForAllAccountsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes); @Override String getHelpText(); }
CheckGuardDutyForAllAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckGuardDutyForAllAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonGuardDutyClient dutyClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName= resourceAttributes.get(PacmanRuleConstants.ACCOUNT_NAME); 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)); Gson gson = new Gson(); List<LinkedHashMap<String,Object>>issueList = new ArrayList(); LinkedHashMap<String,Object>issue = new LinkedHashMap(); Map<String,Object> failedType =new HashMap(); if (!PacmanUtils.doesAllHaveValue(severity, category,roleIdentifyingString)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { map = getClientFor(AWSService.GUARD_DUTY, roleIdentifyingString, temp); dutyClient = (AmazonGuardDutyClient) map.get(PacmanSdkConstants.CLIENT); ListDetectorsRequest detectorsRequest = new ListDetectorsRequest(); ListDetectorsResult detectorsResult = dutyClient.listDetectors(detectorsRequest); boolean isDisabled = true; if (!detectorsResult.getDetectorIds().isEmpty()) { isDisabled = false; } if (isDisabled) { failedType.put("detectors", "Not found"); annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS Guard Duty service is not enabled to " + accountName + " account"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS Guard Duty service is not enabled to " + accountName + " account"); issue.put("failed_reason", gson.toJson(failedType) ); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========CheckGuardDutyForAllAccountsRule ended with annotaion {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } logger.debug("========CheckGuardDutyForAllAccountsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes); @Override String getHelpText();  }