@Test public void test()throws Exception{ HostedZone hostedZone = new HostedZone(); hostedZone.setId("123"); Collection<HostedZone> hostedZones = new ArrayList<>(); hostedZones.add(hostedZone); ListHostedZonesResult result = new ListHostedZonesResult(); result.setHostedZones(hostedZones); Collection<HostedZone> emptyList = new ArrayList<>(); ListHostedZonesResult emptyDetectorsResult = new ListHostedZonesResult(); emptyDetectorsResult.setHostedZones(emptyList); mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString())).thenReturn( true); when(PacmanUtils.splitStringToAList(anyString(),anyString())).thenReturn(CommonTestUtils.getListString()); Map<String,Object>map=new HashMap<String, Object>(); map.put("client", route53Client); CheckAwsRoute53DNSForAccountsRule spy = Mockito.spy(new CheckAwsRoute53DNSForAccountsRule()); Mockito.doReturn(map).when((BaseRule)spy).getClientFor(anyObject(), anyString(), anyObject()); when(route53Client.listHostedZones()).thenReturn(result); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(route53Client.listHostedZones()).thenReturn(emptyDetectorsResult); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(route53Client.listHostedZones()).thenThrow(new InvalidInputException()); assertThatThrownBy( () -> checkAwsRoute53DNSForAccountsRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> checkAwsRoute53DNSForAccountsRule.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("========CheckAwsRoute53DNSForAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonRoute53Client route53Client = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_NAME); String accNames = ruleParam.get(PacmanRuleConstants.ACCOUNT_NAMES); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); 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, accNames, tagsSplitter)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<String> accountNames = PacmanUtils.splitStringToAList(accNames,tagsSplitter); Boolean isAccountExists = false; for (String account : accountNames) { if (accountName.equalsIgnoreCase(account)) { isAccountExists = true; } } if(!isAccountExists) { try { map = getClientFor(AWSService.ROUTE53, roleIdentifyingString, temp); route53Client = (AmazonRoute53Client) map.get(PacmanSdkConstants.CLIENT); ListHostedZonesResult result = route53Client.listHostedZones(); if (!result.getHostedZones().isEmpty()) { failedType.put("hostedZones", "Found"); annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS route53 DNS for "+accountName+" account has found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS route53 DNS for "+accountName+" account has found!!"); issue.put("failed_reason", gson.toJson(failedType) ); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); 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()); } } return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
CheckAwsRoute53DNSForAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAwsRoute53DNSForAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonRoute53Client route53Client = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_NAME); String accNames = ruleParam.get(PacmanRuleConstants.ACCOUNT_NAMES); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); 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, accNames, tagsSplitter)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<String> accountNames = PacmanUtils.splitStringToAList(accNames,tagsSplitter); Boolean isAccountExists = false; for (String account : accountNames) { if (accountName.equalsIgnoreCase(account)) { isAccountExists = true; } } if(!isAccountExists) { try { map = getClientFor(AWSService.ROUTE53, roleIdentifyingString, temp); route53Client = (AmazonRoute53Client) map.get(PacmanSdkConstants.CLIENT); ListHostedZonesResult result = route53Client.listHostedZones(); if (!result.getHostedZones().isEmpty()) { failedType.put("hostedZones", "Found"); annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS route53 DNS for "+accountName+" account has found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS route53 DNS for "+accountName+" account has found!!"); issue.put("failed_reason", gson.toJson(failedType) ); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); 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()); } } return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
CheckAwsRoute53DNSForAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAwsRoute53DNSForAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonRoute53Client route53Client = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_NAME); String accNames = ruleParam.get(PacmanRuleConstants.ACCOUNT_NAMES); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); 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, accNames, tagsSplitter)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<String> accountNames = PacmanUtils.splitStringToAList(accNames,tagsSplitter); Boolean isAccountExists = false; for (String account : accountNames) { if (accountName.equalsIgnoreCase(account)) { isAccountExists = true; } } if(!isAccountExists) { try { map = getClientFor(AWSService.ROUTE53, roleIdentifyingString, temp); route53Client = (AmazonRoute53Client) map.get(PacmanSdkConstants.CLIENT); ListHostedZonesResult result = route53Client.listHostedZones(); if (!result.getHostedZones().isEmpty()) { failedType.put("hostedZones", "Found"); annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS route53 DNS for "+accountName+" account has found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS route53 DNS for "+accountName+" account has found!!"); issue.put("failed_reason", gson.toJson(failedType) ); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); 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()); } } return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
CheckAwsRoute53DNSForAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAwsRoute53DNSForAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonRoute53Client route53Client = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_NAME); String accNames = ruleParam.get(PacmanRuleConstants.ACCOUNT_NAMES); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); 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, accNames, tagsSplitter)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<String> accountNames = PacmanUtils.splitStringToAList(accNames,tagsSplitter); Boolean isAccountExists = false; for (String account : accountNames) { if (accountName.equalsIgnoreCase(account)) { isAccountExists = true; } } if(!isAccountExists) { try { map = getClientFor(AWSService.ROUTE53, roleIdentifyingString, temp); route53Client = (AmazonRoute53Client) map.get(PacmanSdkConstants.CLIENT); ListHostedZonesResult result = route53Client.listHostedZones(); if (!result.getHostedZones().isEmpty()) { failedType.put("hostedZones", "Found"); annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS route53 DNS for "+accountName+" account has found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS route53 DNS for "+accountName+" account has found!!"); issue.put("failed_reason", gson.toJson(failedType) ); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); 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()); } } return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes); @Override String getHelpText(); }
CheckAwsRoute53DNSForAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAwsRoute53DNSForAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonRoute53Client route53Client = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_NAME); String accNames = ruleParam.get(PacmanRuleConstants.ACCOUNT_NAMES); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); 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, accNames, tagsSplitter)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<String> accountNames = PacmanUtils.splitStringToAList(accNames,tagsSplitter); Boolean isAccountExists = false; for (String account : accountNames) { if (accountName.equalsIgnoreCase(account)) { isAccountExists = true; } } if(!isAccountExists) { try { map = getClientFor(AWSService.ROUTE53, roleIdentifyingString, temp); route53Client = (AmazonRoute53Client) map.get(PacmanSdkConstants.CLIENT); ListHostedZonesResult result = route53Client.listHostedZones(); if (!result.getHostedZones().isEmpty()) { failedType.put("hostedZones", "Found"); annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"AWS route53 DNS for "+accountName+" account has found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "AWS route53 DNS for "+accountName+" account has found!!"); issue.put("failed_reason", gson.toJson(failedType) ); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); 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()); } } return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes); @Override String getHelpText();  }