@Test public void executeTest() throws Exception { mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( true); when(PacmanUtils.formatUrl(anyObject(),anyString())).thenReturn("host"); when(PacmanUtils.getMemberOf(anyString(),anyString())).thenReturn(CommonTestUtils.getJsonArray()); assertThat(serviceAccountAdministratorsRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); assertThat(serviceAccountAdministratorsRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getOneMoreMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.getMemberOf(anyString(),anyString())).thenThrow(new Exception()); assertThatThrownBy( () -> serviceAccountAdministratorsRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(RuleExecutionFailedExeption.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> serviceAccountAdministratorsRule.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("========ServiceAccountAdministratorsRule started========="); Annotation annotation = null; String resourceId = null; String description = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceAccountEsURL = null; Boolean isServiceAccountValidException = false; MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_SERVICE_ACCOUNT_ES_URL); if(!StringUtils.isEmpty(formattedUrl)){ serviceAccountEsURL = formattedUrl; } if (!PacmanUtils.doesAllHaveValue(severity, category, serviceAccountEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { resourceId = StringUtils.trim(resourceAttributes .get(PacmanSdkConstants.RESOURCE_ID)); JsonArray memberOfList = new JsonArray(); try { memberOfList = PacmanUtils.getMemberOf(resourceId, serviceAccountEsURL); } catch (Exception e) { logger.error("unable to determine" ,e); throw new RuleExecutionFailedExeption("unable to determine" + e); } if (memberOfList.size() > 0) { for (int j = 0; j < memberOfList.size(); j++) { String memberOfObject = memberOfList.get(j).getAsString(); if ((memberOfObject.startsWith(PacmanRuleConstants.R_WIN) && memberOfObject .endsWith(PacmanRuleConstants.ADMIN)) || (memberOfObject .startsWith(PacmanRuleConstants.R_RHEL) && memberOfObject .endsWith(PacmanRuleConstants.ADMIN))) { description = resourceAttributes .get(PacmanRuleConstants.DESCRIPTION); if (description.contains("RP") && description.contains("FND")) { isServiceAccountValidException = true; } if (!isServiceAccountValidException) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation .put(PacmanSdkConstants.DESCRIPTION, "Service account with an invalid exception on file found !!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Service account's description doesn't contain RP & FND!!"); issue.put("member_of_service_account", memberOfObject); issue.put("service_account_description", description); issueList.add(issue); annotation .put("issueDetails", issueList.toString()); logger.debug( "========ServiceAccountAdministratorsRule ended with an annotation : {}=========", annotation); return new RuleResult( PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } } } } logger.debug("========ServiceAccountAdministratorsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }
ServiceAccountAdministratorsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ServiceAccountAdministratorsRule started========="); Annotation annotation = null; String resourceId = null; String description = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceAccountEsURL = null; Boolean isServiceAccountValidException = false; MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_SERVICE_ACCOUNT_ES_URL); if(!StringUtils.isEmpty(formattedUrl)){ serviceAccountEsURL = formattedUrl; } if (!PacmanUtils.doesAllHaveValue(severity, category, serviceAccountEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { resourceId = StringUtils.trim(resourceAttributes .get(PacmanSdkConstants.RESOURCE_ID)); JsonArray memberOfList = new JsonArray(); try { memberOfList = PacmanUtils.getMemberOf(resourceId, serviceAccountEsURL); } catch (Exception e) { logger.error("unable to determine" ,e); throw new RuleExecutionFailedExeption("unable to determine" + e); } if (memberOfList.size() > 0) { for (int j = 0; j < memberOfList.size(); j++) { String memberOfObject = memberOfList.get(j).getAsString(); if ((memberOfObject.startsWith(PacmanRuleConstants.R_WIN) && memberOfObject .endsWith(PacmanRuleConstants.ADMIN)) || (memberOfObject .startsWith(PacmanRuleConstants.R_RHEL) && memberOfObject .endsWith(PacmanRuleConstants.ADMIN))) { description = resourceAttributes .get(PacmanRuleConstants.DESCRIPTION); if (description.contains("RP") && description.contains("FND")) { isServiceAccountValidException = true; } if (!isServiceAccountValidException) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation .put(PacmanSdkConstants.DESCRIPTION, "Service account with an invalid exception on file found !!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Service account's description doesn't contain RP & FND!!"); issue.put("member_of_service_account", memberOfObject); issue.put("service_account_description", description); issueList.add(issue); annotation .put("issueDetails", issueList.toString()); logger.debug( "========ServiceAccountAdministratorsRule ended with an annotation : {}=========", annotation); return new RuleResult( PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } } } } logger.debug("========ServiceAccountAdministratorsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } }
ServiceAccountAdministratorsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ServiceAccountAdministratorsRule started========="); Annotation annotation = null; String resourceId = null; String description = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceAccountEsURL = null; Boolean isServiceAccountValidException = false; MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_SERVICE_ACCOUNT_ES_URL); if(!StringUtils.isEmpty(formattedUrl)){ serviceAccountEsURL = formattedUrl; } if (!PacmanUtils.doesAllHaveValue(severity, category, serviceAccountEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { resourceId = StringUtils.trim(resourceAttributes .get(PacmanSdkConstants.RESOURCE_ID)); JsonArray memberOfList = new JsonArray(); try { memberOfList = PacmanUtils.getMemberOf(resourceId, serviceAccountEsURL); } catch (Exception e) { logger.error("unable to determine" ,e); throw new RuleExecutionFailedExeption("unable to determine" + e); } if (memberOfList.size() > 0) { for (int j = 0; j < memberOfList.size(); j++) { String memberOfObject = memberOfList.get(j).getAsString(); if ((memberOfObject.startsWith(PacmanRuleConstants.R_WIN) && memberOfObject .endsWith(PacmanRuleConstants.ADMIN)) || (memberOfObject .startsWith(PacmanRuleConstants.R_RHEL) && memberOfObject .endsWith(PacmanRuleConstants.ADMIN))) { description = resourceAttributes .get(PacmanRuleConstants.DESCRIPTION); if (description.contains("RP") && description.contains("FND")) { isServiceAccountValidException = true; } if (!isServiceAccountValidException) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation .put(PacmanSdkConstants.DESCRIPTION, "Service account with an invalid exception on file found !!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Service account's description doesn't contain RP & FND!!"); issue.put("member_of_service_account", memberOfObject); issue.put("service_account_description", description); issueList.add(issue); annotation .put("issueDetails", issueList.toString()); logger.debug( "========ServiceAccountAdministratorsRule ended with an annotation : {}=========", annotation); return new RuleResult( PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } } } } logger.debug("========ServiceAccountAdministratorsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  }
ServiceAccountAdministratorsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ServiceAccountAdministratorsRule started========="); Annotation annotation = null; String resourceId = null; String description = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceAccountEsURL = null; Boolean isServiceAccountValidException = false; MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_SERVICE_ACCOUNT_ES_URL); if(!StringUtils.isEmpty(formattedUrl)){ serviceAccountEsURL = formattedUrl; } if (!PacmanUtils.doesAllHaveValue(severity, category, serviceAccountEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { resourceId = StringUtils.trim(resourceAttributes .get(PacmanSdkConstants.RESOURCE_ID)); JsonArray memberOfList = new JsonArray(); try { memberOfList = PacmanUtils.getMemberOf(resourceId, serviceAccountEsURL); } catch (Exception e) { logger.error("unable to determine" ,e); throw new RuleExecutionFailedExeption("unable to determine" + e); } if (memberOfList.size() > 0) { for (int j = 0; j < memberOfList.size(); j++) { String memberOfObject = memberOfList.get(j).getAsString(); if ((memberOfObject.startsWith(PacmanRuleConstants.R_WIN) && memberOfObject .endsWith(PacmanRuleConstants.ADMIN)) || (memberOfObject .startsWith(PacmanRuleConstants.R_RHEL) && memberOfObject .endsWith(PacmanRuleConstants.ADMIN))) { description = resourceAttributes .get(PacmanRuleConstants.DESCRIPTION); if (description.contains("RP") && description.contains("FND")) { isServiceAccountValidException = true; } if (!isServiceAccountValidException) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation .put(PacmanSdkConstants.DESCRIPTION, "Service account with an invalid exception on file found !!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Service account's description doesn't contain RP & FND!!"); issue.put("member_of_service_account", memberOfObject); issue.put("service_account_description", description); issueList.add(issue); annotation .put("issueDetails", issueList.toString()); logger.debug( "========ServiceAccountAdministratorsRule ended with an annotation : {}=========", annotation); return new RuleResult( PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } } } } logger.debug("========ServiceAccountAdministratorsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText(); }
ServiceAccountAdministratorsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========ServiceAccountAdministratorsRule started========="); Annotation annotation = null; String resourceId = null; String description = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceAccountEsURL = null; Boolean isServiceAccountValidException = false; MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_SERVICE_ACCOUNT_ES_URL); if(!StringUtils.isEmpty(formattedUrl)){ serviceAccountEsURL = formattedUrl; } if (!PacmanUtils.doesAllHaveValue(severity, category, serviceAccountEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { resourceId = StringUtils.trim(resourceAttributes .get(PacmanSdkConstants.RESOURCE_ID)); JsonArray memberOfList = new JsonArray(); try { memberOfList = PacmanUtils.getMemberOf(resourceId, serviceAccountEsURL); } catch (Exception e) { logger.error("unable to determine" ,e); throw new RuleExecutionFailedExeption("unable to determine" + e); } if (memberOfList.size() > 0) { for (int j = 0; j < memberOfList.size(); j++) { String memberOfObject = memberOfList.get(j).getAsString(); if ((memberOfObject.startsWith(PacmanRuleConstants.R_WIN) && memberOfObject .endsWith(PacmanRuleConstants.ADMIN)) || (memberOfObject .startsWith(PacmanRuleConstants.R_RHEL) && memberOfObject .endsWith(PacmanRuleConstants.ADMIN))) { description = resourceAttributes .get(PacmanRuleConstants.DESCRIPTION); if (description.contains("RP") && description.contains("FND")) { isServiceAccountValidException = true; } if (!isServiceAccountValidException) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation .put(PacmanSdkConstants.DESCRIPTION, "Service account with an invalid exception on file found !!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Service account's description doesn't contain RP & FND!!"); issue.put("member_of_service_account", memberOfObject); issue.put("service_account_description", description); issueList.add(issue); annotation .put("issueDetails", issueList.toString()); logger.debug( "========ServiceAccountAdministratorsRule ended with an annotation : {}=========", annotation); return new RuleResult( PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } } } } logger.debug("========ServiceAccountAdministratorsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText();  }