@Test public void executeTest() throws Exception { mockStatic(PacmanUtils.class); when(PacmanUtils.formatUrl(anyObject(),anyString())).thenReturn("host"); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( true); mockStatic(RulesElasticSearchRepositoryUtil.class); when(RulesElasticSearchRepositoryUtil.getInvalidRegions(anyString(),anyString())).thenReturn(CommonTestUtils.getListMapOfMap("123")); assertThat(activityInBlacklistedRegionRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(RulesElasticSearchRepositoryUtil.getInvalidRegions(anyString(),anyString())).thenReturn(CommonTestUtils.getEmptyListMapOfMap("123")); assertThat(activityInBlacklistedRegionRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(RulesElasticSearchRepositoryUtil.getInvalidRegions(anyString(),anyString())).thenThrow(new Exception()); assertThatThrownBy( () -> activityInBlacklistedRegionRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(RuleExecutionFailedExeption.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> activityInBlacklistedRegionRule.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("========CheckAwsActivityInBlacklistedRegionRule started========="); Annotation annotation = null; String accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); String url = ruleParam.get(PacmanRuleConstants.URL); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.AWS_SEARCH); if(!StringUtils.isEmpty(formattedUrl)){ url = formattedUrl; } 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<>(); if (!PacmanUtils.doesAllHaveValue(severity,category,url)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } Map<String,Object> regionMap = new HashMap<>(); if (!resourceAttributes.isEmpty()) { try { List<Map<Object,Map<String,Object>>> inValidRegionMap = RulesElasticSearchRepositoryUtil.getInvalidRegions(url, accountId); for(Map<Object,Map<String,Object>> invalidRegion:inValidRegionMap){ if(invalidRegion.containsKey(PacmanRuleConstants.REGION_AND_COUNT) && invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).containsKey("region") && invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).containsKey("issueCount")){ regionMap.put(invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).get("region").toString(),invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).get("issueCount").toString()); } } if (!inValidRegionMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Aws activity in blacklisted region found!!"); annotation.put(PacmanRuleConstants.BLACKLISTED_REGIONS, inValidRegionMap.toString()); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Aws activity in blacklisted region found"); issue.put("invalid_regions", gson.toJson(regionMap) ); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckAwsActivityInBlacklistedRegionRule ended with an annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } } logger.debug("========CheckAwsActivityInBlacklistedRegionRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
CheckAwsActivityInBlacklistedRegionRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAwsActivityInBlacklistedRegionRule started========="); Annotation annotation = null; String accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); String url = ruleParam.get(PacmanRuleConstants.URL); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.AWS_SEARCH); if(!StringUtils.isEmpty(formattedUrl)){ url = formattedUrl; } 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<>(); if (!PacmanUtils.doesAllHaveValue(severity,category,url)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } Map<String,Object> regionMap = new HashMap<>(); if (!resourceAttributes.isEmpty()) { try { List<Map<Object,Map<String,Object>>> inValidRegionMap = RulesElasticSearchRepositoryUtil.getInvalidRegions(url, accountId); for(Map<Object,Map<String,Object>> invalidRegion:inValidRegionMap){ if(invalidRegion.containsKey(PacmanRuleConstants.REGION_AND_COUNT) && invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).containsKey("region") && invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).containsKey("issueCount")){ regionMap.put(invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).get("region").toString(),invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).get("issueCount").toString()); } } if (!inValidRegionMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Aws activity in blacklisted region found!!"); annotation.put(PacmanRuleConstants.BLACKLISTED_REGIONS, inValidRegionMap.toString()); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Aws activity in blacklisted region found"); issue.put("invalid_regions", gson.toJson(regionMap) ); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckAwsActivityInBlacklistedRegionRule ended with an annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } } logger.debug("========CheckAwsActivityInBlacklistedRegionRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
CheckAwsActivityInBlacklistedRegionRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAwsActivityInBlacklistedRegionRule started========="); Annotation annotation = null; String accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); String url = ruleParam.get(PacmanRuleConstants.URL); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.AWS_SEARCH); if(!StringUtils.isEmpty(formattedUrl)){ url = formattedUrl; } 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<>(); if (!PacmanUtils.doesAllHaveValue(severity,category,url)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } Map<String,Object> regionMap = new HashMap<>(); if (!resourceAttributes.isEmpty()) { try { List<Map<Object,Map<String,Object>>> inValidRegionMap = RulesElasticSearchRepositoryUtil.getInvalidRegions(url, accountId); for(Map<Object,Map<String,Object>> invalidRegion:inValidRegionMap){ if(invalidRegion.containsKey(PacmanRuleConstants.REGION_AND_COUNT) && invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).containsKey("region") && invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).containsKey("issueCount")){ regionMap.put(invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).get("region").toString(),invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).get("issueCount").toString()); } } if (!inValidRegionMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Aws activity in blacklisted region found!!"); annotation.put(PacmanRuleConstants.BLACKLISTED_REGIONS, inValidRegionMap.toString()); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Aws activity in blacklisted region found"); issue.put("invalid_regions", gson.toJson(regionMap) ); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckAwsActivityInBlacklistedRegionRule ended with an annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } } logger.debug("========CheckAwsActivityInBlacklistedRegionRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
CheckAwsActivityInBlacklistedRegionRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAwsActivityInBlacklistedRegionRule started========="); Annotation annotation = null; String accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); String url = ruleParam.get(PacmanRuleConstants.URL); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.AWS_SEARCH); if(!StringUtils.isEmpty(formattedUrl)){ url = formattedUrl; } 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<>(); if (!PacmanUtils.doesAllHaveValue(severity,category,url)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } Map<String,Object> regionMap = new HashMap<>(); if (!resourceAttributes.isEmpty()) { try { List<Map<Object,Map<String,Object>>> inValidRegionMap = RulesElasticSearchRepositoryUtil.getInvalidRegions(url, accountId); for(Map<Object,Map<String,Object>> invalidRegion:inValidRegionMap){ if(invalidRegion.containsKey(PacmanRuleConstants.REGION_AND_COUNT) && invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).containsKey("region") && invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).containsKey("issueCount")){ regionMap.put(invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).get("region").toString(),invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).get("issueCount").toString()); } } if (!inValidRegionMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Aws activity in blacklisted region found!!"); annotation.put(PacmanRuleConstants.BLACKLISTED_REGIONS, inValidRegionMap.toString()); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Aws activity in blacklisted region found"); issue.put("invalid_regions", gson.toJson(regionMap) ); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckAwsActivityInBlacklistedRegionRule ended with an annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } } logger.debug("========CheckAwsActivityInBlacklistedRegionRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes); String getHelpText(); }
CheckAwsActivityInBlacklistedRegionRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckAwsActivityInBlacklistedRegionRule started========="); Annotation annotation = null; String accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); String url = ruleParam.get(PacmanRuleConstants.URL); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.AWS_SEARCH); if(!StringUtils.isEmpty(formattedUrl)){ url = formattedUrl; } 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<>(); if (!PacmanUtils.doesAllHaveValue(severity,category,url)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } Map<String,Object> regionMap = new HashMap<>(); if (!resourceAttributes.isEmpty()) { try { List<Map<Object,Map<String,Object>>> inValidRegionMap = RulesElasticSearchRepositoryUtil.getInvalidRegions(url, accountId); for(Map<Object,Map<String,Object>> invalidRegion:inValidRegionMap){ if(invalidRegion.containsKey(PacmanRuleConstants.REGION_AND_COUNT) && invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).containsKey("region") && invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).containsKey("issueCount")){ regionMap.put(invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).get("region").toString(),invalidRegion.get(PacmanRuleConstants.REGION_AND_COUNT).get("issueCount").toString()); } } if (!inValidRegionMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Aws activity in blacklisted region found!!"); annotation.put(PacmanRuleConstants.BLACKLISTED_REGIONS, inValidRegionMap.toString()); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Aws activity in blacklisted region found"); issue.put("invalid_regions", gson.toJson(regionMap) ); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckAwsActivityInBlacklistedRegionRule ended with an annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } } logger.debug("========CheckAwsActivityInBlacklistedRegionRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes); String getHelpText();  }