@Test public void test()throws Exception{ Method method = new Method(); method.setApiKeyRequired(true); method.setHttpMethod("Get"); Map<String, Method> resourceMethods = new HashMap(); resourceMethods.put("1", method); Resource resource = new Resource(); resource.setResourceMethods(resourceMethods); Collection<Resource> li = new ArrayList<>(); li.add(resource); GetResourcesResult resourceResult = new GetResourcesResult(); resourceResult.setItems(li); GetMethodResult methodResult = new GetMethodResult(); methodResult.setAuthorizationType("AuthorizationType"); methodResult.setApiKeyRequired(false); methodResult.setHttpMethod("Get"); Collection<Resource> emptyList = new ArrayList<>(); GetResourcesResult emptyRulesResult = new GetResourcesResult (); emptyRulesResult.setItems(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", apiGatewayClient); CheckForApiGatewayProtected spy = Mockito.spy(new CheckForApiGatewayProtected()); Mockito.doReturn(map).when((BaseRule)spy).getClientFor(anyObject(), anyString(), anyObject()); when(apiGatewayClient.getResources(anyObject())).thenReturn(resourceResult); when(apiGatewayClient.getMethod(anyObject())).thenReturn(methodResult); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(apiGatewayClient.getResources(anyObject())).thenReturn(emptyRulesResult); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(apiGatewayClient.getResources(anyObject())).thenThrow(new RuleExecutionFailedExeption()); assertThatThrownBy( () -> checkForApiGatewayProtected.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> checkForApiGatewayProtected.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("========CheckForApiGatewayProtected started========="); Annotation annotation = null; String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String resourceId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); String authType = ruleParam.get(PacmanRuleConstants.AUTH_TYPE); 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> authTypeMap = new HashMap<>(); if (!PacmanUtils.doesAllHaveValue(authType, tagsSplitter, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } AmazonApiGatewayClient apiGatewayClient = getClient( roleIdentifyingString, ruleParam); List<String> authTypeList = PacmanUtils.splitStringToAList(authType, tagsSplitter); try { for (Resource resource : getResourceList(resourceId, apiGatewayClient)) { Map<String, Method> httpMethodMap = resource .getResourceMethods(); if (httpMethodMap != null) { for (Map.Entry<String, Method> httpMethod : httpMethodMap .entrySet()) { GetMethodResult methodResult = getGetMethodResult( resource, resourceId, httpMethod, apiGatewayClient); String authorisationType = methodResult .getAuthorizationType(); boolean isApiKeyRequired = methodResult .getApiKeyRequired(); boolean isAuthType = false; logger.info("========Checking is auth type true========="); for (String authorType : authTypeList) { if (authorisationType.equalsIgnoreCase(authorType)) { isAuthType = true; } else { authTypeMap.put(authorType, "Not found"); } } if (!(isAuthType || isApiKeyRequired)) { if (annotation == null) { annotation = Annotation.buildAnnotation( ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); } formateAPIMethodDetails(resource.getPath(), httpMethod.getKey(), String.valueOf(isApiKeyRequired)); } } } } } catch (Exception exception) { logger.error("error: ", exception); throw new RuleExecutionFailedExeption(exception.getMessage()); } if (annotation == null) { logger.debug("========CheckForApiGatewayProtected ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } else { annotation.put("APIMethods", apiMethodsDes); annotation.put(PacmanSdkConstants.DESCRIPTION, "API gateway is not protected.And the API methods description are " + apiMethodsDes); issue.put(PacmanRuleConstants.VIOLATION_REASON, "API gateway is not protected"); issue.put("apiMethods", apiMethodsDes); issue.put("authType", gson.toJson(authTypeMap)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); apiMethodsDes = null; logger.debug("========CheckForApiGatewayProtected ended with annotation : {}=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } }
CheckForApiGatewayProtected extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckForApiGatewayProtected started========="); Annotation annotation = null; String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String resourceId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); String authType = ruleParam.get(PacmanRuleConstants.AUTH_TYPE); 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> authTypeMap = new HashMap<>(); if (!PacmanUtils.doesAllHaveValue(authType, tagsSplitter, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } AmazonApiGatewayClient apiGatewayClient = getClient( roleIdentifyingString, ruleParam); List<String> authTypeList = PacmanUtils.splitStringToAList(authType, tagsSplitter); try { for (Resource resource : getResourceList(resourceId, apiGatewayClient)) { Map<String, Method> httpMethodMap = resource .getResourceMethods(); if (httpMethodMap != null) { for (Map.Entry<String, Method> httpMethod : httpMethodMap .entrySet()) { GetMethodResult methodResult = getGetMethodResult( resource, resourceId, httpMethod, apiGatewayClient); String authorisationType = methodResult .getAuthorizationType(); boolean isApiKeyRequired = methodResult .getApiKeyRequired(); boolean isAuthType = false; logger.info("========Checking is auth type true========="); for (String authorType : authTypeList) { if (authorisationType.equalsIgnoreCase(authorType)) { isAuthType = true; } else { authTypeMap.put(authorType, "Not found"); } } if (!(isAuthType || isApiKeyRequired)) { if (annotation == null) { annotation = Annotation.buildAnnotation( ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); } formateAPIMethodDetails(resource.getPath(), httpMethod.getKey(), String.valueOf(isApiKeyRequired)); } } } } } catch (Exception exception) { logger.error("error: ", exception); throw new RuleExecutionFailedExeption(exception.getMessage()); } if (annotation == null) { logger.debug("========CheckForApiGatewayProtected ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } else { annotation.put("APIMethods", apiMethodsDes); annotation.put(PacmanSdkConstants.DESCRIPTION, "API gateway is not protected.And the API methods description are " + apiMethodsDes); issue.put(PacmanRuleConstants.VIOLATION_REASON, "API gateway is not protected"); issue.put("apiMethods", apiMethodsDes); issue.put("authType", gson.toJson(authTypeMap)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); apiMethodsDes = null; logger.debug("========CheckForApiGatewayProtected ended with annotation : {}=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } }
CheckForApiGatewayProtected extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckForApiGatewayProtected started========="); Annotation annotation = null; String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String resourceId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); String authType = ruleParam.get(PacmanRuleConstants.AUTH_TYPE); 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> authTypeMap = new HashMap<>(); if (!PacmanUtils.doesAllHaveValue(authType, tagsSplitter, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } AmazonApiGatewayClient apiGatewayClient = getClient( roleIdentifyingString, ruleParam); List<String> authTypeList = PacmanUtils.splitStringToAList(authType, tagsSplitter); try { for (Resource resource : getResourceList(resourceId, apiGatewayClient)) { Map<String, Method> httpMethodMap = resource .getResourceMethods(); if (httpMethodMap != null) { for (Map.Entry<String, Method> httpMethod : httpMethodMap .entrySet()) { GetMethodResult methodResult = getGetMethodResult( resource, resourceId, httpMethod, apiGatewayClient); String authorisationType = methodResult .getAuthorizationType(); boolean isApiKeyRequired = methodResult .getApiKeyRequired(); boolean isAuthType = false; logger.info("========Checking is auth type true========="); for (String authorType : authTypeList) { if (authorisationType.equalsIgnoreCase(authorType)) { isAuthType = true; } else { authTypeMap.put(authorType, "Not found"); } } if (!(isAuthType || isApiKeyRequired)) { if (annotation == null) { annotation = Annotation.buildAnnotation( ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); } formateAPIMethodDetails(resource.getPath(), httpMethod.getKey(), String.valueOf(isApiKeyRequired)); } } } } } catch (Exception exception) { logger.error("error: ", exception); throw new RuleExecutionFailedExeption(exception.getMessage()); } if (annotation == null) { logger.debug("========CheckForApiGatewayProtected ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } else { annotation.put("APIMethods", apiMethodsDes); annotation.put(PacmanSdkConstants.DESCRIPTION, "API gateway is not protected.And the API methods description are " + apiMethodsDes); issue.put(PacmanRuleConstants.VIOLATION_REASON, "API gateway is not protected"); issue.put("apiMethods", apiMethodsDes); issue.put("authType", gson.toJson(authTypeMap)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); apiMethodsDes = null; logger.debug("========CheckForApiGatewayProtected ended with annotation : {}=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } }  }
CheckForApiGatewayProtected extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckForApiGatewayProtected started========="); Annotation annotation = null; String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String resourceId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); String authType = ruleParam.get(PacmanRuleConstants.AUTH_TYPE); 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> authTypeMap = new HashMap<>(); if (!PacmanUtils.doesAllHaveValue(authType, tagsSplitter, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } AmazonApiGatewayClient apiGatewayClient = getClient( roleIdentifyingString, ruleParam); List<String> authTypeList = PacmanUtils.splitStringToAList(authType, tagsSplitter); try { for (Resource resource : getResourceList(resourceId, apiGatewayClient)) { Map<String, Method> httpMethodMap = resource .getResourceMethods(); if (httpMethodMap != null) { for (Map.Entry<String, Method> httpMethod : httpMethodMap .entrySet()) { GetMethodResult methodResult = getGetMethodResult( resource, resourceId, httpMethod, apiGatewayClient); String authorisationType = methodResult .getAuthorizationType(); boolean isApiKeyRequired = methodResult .getApiKeyRequired(); boolean isAuthType = false; logger.info("========Checking is auth type true========="); for (String authorType : authTypeList) { if (authorisationType.equalsIgnoreCase(authorType)) { isAuthType = true; } else { authTypeMap.put(authorType, "Not found"); } } if (!(isAuthType || isApiKeyRequired)) { if (annotation == null) { annotation = Annotation.buildAnnotation( ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); } formateAPIMethodDetails(resource.getPath(), httpMethod.getKey(), String.valueOf(isApiKeyRequired)); } } } } } catch (Exception exception) { logger.error("error: ", exception); throw new RuleExecutionFailedExeption(exception.getMessage()); } if (annotation == null) { logger.debug("========CheckForApiGatewayProtected ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } else { annotation.put("APIMethods", apiMethodsDes); annotation.put(PacmanSdkConstants.DESCRIPTION, "API gateway is not protected.And the API methods description are " + apiMethodsDes); issue.put(PacmanRuleConstants.VIOLATION_REASON, "API gateway is not protected"); issue.put("apiMethods", apiMethodsDes); issue.put("authType", gson.toJson(authTypeMap)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); apiMethodsDes = null; logger.debug("========CheckForApiGatewayProtected ended with annotation : {}=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } }  @Override RuleResult execute(Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText(); }
CheckForApiGatewayProtected extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckForApiGatewayProtected started========="); Annotation annotation = null; String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String resourceId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); String authType = ruleParam.get(PacmanRuleConstants.AUTH_TYPE); 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> authTypeMap = new HashMap<>(); if (!PacmanUtils.doesAllHaveValue(authType, tagsSplitter, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } AmazonApiGatewayClient apiGatewayClient = getClient( roleIdentifyingString, ruleParam); List<String> authTypeList = PacmanUtils.splitStringToAList(authType, tagsSplitter); try { for (Resource resource : getResourceList(resourceId, apiGatewayClient)) { Map<String, Method> httpMethodMap = resource .getResourceMethods(); if (httpMethodMap != null) { for (Map.Entry<String, Method> httpMethod : httpMethodMap .entrySet()) { GetMethodResult methodResult = getGetMethodResult( resource, resourceId, httpMethod, apiGatewayClient); String authorisationType = methodResult .getAuthorizationType(); boolean isApiKeyRequired = methodResult .getApiKeyRequired(); boolean isAuthType = false; logger.info("========Checking is auth type true========="); for (String authorType : authTypeList) { if (authorisationType.equalsIgnoreCase(authorType)) { isAuthType = true; } else { authTypeMap.put(authorType, "Not found"); } } if (!(isAuthType || isApiKeyRequired)) { if (annotation == null) { annotation = Annotation.buildAnnotation( ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); } formateAPIMethodDetails(resource.getPath(), httpMethod.getKey(), String.valueOf(isApiKeyRequired)); } } } } } catch (Exception exception) { logger.error("error: ", exception); throw new RuleExecutionFailedExeption(exception.getMessage()); } if (annotation == null) { logger.debug("========CheckForApiGatewayProtected ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } else { annotation.put("APIMethods", apiMethodsDes); annotation.put(PacmanSdkConstants.DESCRIPTION, "API gateway is not protected.And the API methods description are " + apiMethodsDes); issue.put(PacmanRuleConstants.VIOLATION_REASON, "API gateway is not protected"); issue.put("apiMethods", apiMethodsDes); issue.put("authType", gson.toJson(authTypeMap)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); apiMethodsDes = null; logger.debug("========CheckForApiGatewayProtected ended with annotation : {}=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } }  @Override RuleResult execute(Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText();  }