@Test public void executeTest() throws Exception { mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(), anyString())).thenReturn( true); when(PacmanUtils.formatUrl(anyObject(),anyString())).thenReturn("host"); when(PacmanUtils.getSeviceLimit(anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getMapString("r_123 ")); assertThat(serviceLimitRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.getSeviceLimit(anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getEmptyMapString()); assertThat(serviceLimitRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> serviceLimitRule.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("========ServiceLimitRule started========="); Annotation annotation = null; String accountId = null; String checkId = StringUtils.trim(ruleParam.get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isNullOrEmpty(formattedUrl)){ serviceEsURL = formattedUrl; } 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(checkId,severity,category,serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { accountId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.ACCOUNTID)); Map<String,String> serviceLimitMap = PacmanUtils.getSeviceLimit(checkId,accountId,serviceEsURL); if (!serviceLimitMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Service limit exceeded for: "+serviceLimitMap); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); for (Map.Entry<String, String> entry : serviceLimitMap.entrySet()) { if (!entry.getKey().equalsIgnoreCase(PacmanRuleConstants.SERVICE_LIMIT_STATUS_RED)) { annotation.put(entry.getKey(), entry.getValue()); } if (entry.getKey().equalsIgnoreCase(PacmanRuleConstants.SERVICE_LIMIT_STATUS_RED)) { annotation.put(PacmanSdkConstants.SECURITY,PacmanSdkConstants.SEV_HIGH); } } issue.put(PacmanRuleConstants.VIOLATION_REASON, "Service limit exceeded"); issue.put("service_limit", gson.toJson(serviceLimitMap) ); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========ServiceLimitRule ended with an annotation : {}=======", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========ServiceLimitRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
ServiceLimitRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ServiceLimitRule started========="); Annotation annotation = null; String accountId = null; String checkId = StringUtils.trim(ruleParam.get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isNullOrEmpty(formattedUrl)){ serviceEsURL = formattedUrl; } 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(checkId,severity,category,serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { accountId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.ACCOUNTID)); Map<String,String> serviceLimitMap = PacmanUtils.getSeviceLimit(checkId,accountId,serviceEsURL); if (!serviceLimitMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Service limit exceeded for: "+serviceLimitMap); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); for (Map.Entry<String, String> entry : serviceLimitMap.entrySet()) { if (!entry.getKey().equalsIgnoreCase(PacmanRuleConstants.SERVICE_LIMIT_STATUS_RED)) { annotation.put(entry.getKey(), entry.getValue()); } if (entry.getKey().equalsIgnoreCase(PacmanRuleConstants.SERVICE_LIMIT_STATUS_RED)) { annotation.put(PacmanSdkConstants.SECURITY,PacmanSdkConstants.SEV_HIGH); } } issue.put(PacmanRuleConstants.VIOLATION_REASON, "Service limit exceeded"); issue.put("service_limit", gson.toJson(serviceLimitMap) ); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========ServiceLimitRule ended with an annotation : {}=======", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========ServiceLimitRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
ServiceLimitRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ServiceLimitRule started========="); Annotation annotation = null; String accountId = null; String checkId = StringUtils.trim(ruleParam.get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isNullOrEmpty(formattedUrl)){ serviceEsURL = formattedUrl; } 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(checkId,severity,category,serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { accountId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.ACCOUNTID)); Map<String,String> serviceLimitMap = PacmanUtils.getSeviceLimit(checkId,accountId,serviceEsURL); if (!serviceLimitMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Service limit exceeded for: "+serviceLimitMap); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); for (Map.Entry<String, String> entry : serviceLimitMap.entrySet()) { if (!entry.getKey().equalsIgnoreCase(PacmanRuleConstants.SERVICE_LIMIT_STATUS_RED)) { annotation.put(entry.getKey(), entry.getValue()); } if (entry.getKey().equalsIgnoreCase(PacmanRuleConstants.SERVICE_LIMIT_STATUS_RED)) { annotation.put(PacmanSdkConstants.SECURITY,PacmanSdkConstants.SEV_HIGH); } } issue.put(PacmanRuleConstants.VIOLATION_REASON, "Service limit exceeded"); issue.put("service_limit", gson.toJson(serviceLimitMap) ); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========ServiceLimitRule ended with an annotation : {}=======", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========ServiceLimitRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
ServiceLimitRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ServiceLimitRule started========="); Annotation annotation = null; String accountId = null; String checkId = StringUtils.trim(ruleParam.get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isNullOrEmpty(formattedUrl)){ serviceEsURL = formattedUrl; } 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(checkId,severity,category,serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { accountId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.ACCOUNTID)); Map<String,String> serviceLimitMap = PacmanUtils.getSeviceLimit(checkId,accountId,serviceEsURL); if (!serviceLimitMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Service limit exceeded for: "+serviceLimitMap); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); for (Map.Entry<String, String> entry : serviceLimitMap.entrySet()) { if (!entry.getKey().equalsIgnoreCase(PacmanRuleConstants.SERVICE_LIMIT_STATUS_RED)) { annotation.put(entry.getKey(), entry.getValue()); } if (entry.getKey().equalsIgnoreCase(PacmanRuleConstants.SERVICE_LIMIT_STATUS_RED)) { annotation.put(PacmanSdkConstants.SECURITY,PacmanSdkConstants.SEV_HIGH); } } issue.put(PacmanRuleConstants.VIOLATION_REASON, "Service limit exceeded"); issue.put("service_limit", gson.toJson(serviceLimitMap) ); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========ServiceLimitRule ended with an annotation : {}=======", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========ServiceLimitRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes); @Override String getHelpText(); }
ServiceLimitRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ServiceLimitRule started========="); Annotation annotation = null; String accountId = null; String checkId = StringUtils.trim(ruleParam.get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isNullOrEmpty(formattedUrl)){ serviceEsURL = formattedUrl; } 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(checkId,severity,category,serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { accountId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.ACCOUNTID)); Map<String,String> serviceLimitMap = PacmanUtils.getSeviceLimit(checkId,accountId,serviceEsURL); if (!serviceLimitMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Service limit exceeded for: "+serviceLimitMap); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); for (Map.Entry<String, String> entry : serviceLimitMap.entrySet()) { if (!entry.getKey().equalsIgnoreCase(PacmanRuleConstants.SERVICE_LIMIT_STATUS_RED)) { annotation.put(entry.getKey(), entry.getValue()); } if (entry.getKey().equalsIgnoreCase(PacmanRuleConstants.SERVICE_LIMIT_STATUS_RED)) { annotation.put(PacmanSdkConstants.SECURITY,PacmanSdkConstants.SEV_HIGH); } } issue.put(PacmanRuleConstants.VIOLATION_REASON, "Service limit exceeded"); issue.put("service_limit", gson.toJson(serviceLimitMap) ); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========ServiceLimitRule ended with an annotation : {}=======", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========ServiceLimitRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes); @Override String getHelpText();  }