@Test public void test_isValidPolicyItemAccess_happyPath() { RangerPolicyItemAccess access = mock(RangerPolicyItemAccess.class); when(access.getType()).thenReturn("an-Access"); Set<String> validAccesses = Sets.newHashSet(new String[] { "an-access", "another-access" }); for (Boolean allowed : new Boolean[] { null, true } ) { when(access.getIsAllowed()).thenReturn(allowed); Assert.assertTrue(_validator.isValidPolicyItemAccess(access, _failures, validAccesses)); Assert.assertTrue(_failures.isEmpty()); } }
boolean isValidPolicyItemAccess(RangerPolicyItemAccess access, List<ValidationFailureDetails> failures, Set<String> accessTypes) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s)", access, failures, accessTypes)); } boolean valid = true; if (CollectionUtils.isEmpty(accessTypes)) { LOG.debug("isValidPolicyItemAccess: accessTypes was null!"); } else if (access == null) { LOG.debug("isValidPolicyItemAccess: policy item access was null!"); } else { String accessType = access.getType(); if (StringUtils.isBlank(accessType)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isMissing() .becauseOf(error.getMessage("policy item access type")) .errorCode(error.getErrorCode()) .build()); valid = false; } else { String matchedAccessType = getMatchedAccessType(accessType, accessTypes); if (StringUtils.isEmpty(matchedAccessType)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_INVALID; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isSemanticallyIncorrect() .becauseOf(error.getMessage(accessType, accessTypes)) .errorCode(error.getErrorCode()) .build()); valid = false; } else { access.setType(matchedAccessType); } } Boolean isAllowed = access.getIsAllowed(); if (isAllowed != null && isAllowed == false) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_DENY; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type allowed") .isSemanticallyIncorrect() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s): %s", access, failures, accessTypes, valid)); } return valid; }
RangerPolicyValidator extends RangerValidator { boolean isValidPolicyItemAccess(RangerPolicyItemAccess access, List<ValidationFailureDetails> failures, Set<String> accessTypes) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s)", access, failures, accessTypes)); } boolean valid = true; if (CollectionUtils.isEmpty(accessTypes)) { LOG.debug("isValidPolicyItemAccess: accessTypes was null!"); } else if (access == null) { LOG.debug("isValidPolicyItemAccess: policy item access was null!"); } else { String accessType = access.getType(); if (StringUtils.isBlank(accessType)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isMissing() .becauseOf(error.getMessage("policy item access type")) .errorCode(error.getErrorCode()) .build()); valid = false; } else { String matchedAccessType = getMatchedAccessType(accessType, accessTypes); if (StringUtils.isEmpty(matchedAccessType)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_INVALID; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isSemanticallyIncorrect() .becauseOf(error.getMessage(accessType, accessTypes)) .errorCode(error.getErrorCode()) .build()); valid = false; } else { access.setType(matchedAccessType); } } Boolean isAllowed = access.getIsAllowed(); if (isAllowed != null && isAllowed == false) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_DENY; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type allowed") .isSemanticallyIncorrect() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s): %s", access, failures, accessTypes, valid)); } return valid; } }
RangerPolicyValidator extends RangerValidator { boolean isValidPolicyItemAccess(RangerPolicyItemAccess access, List<ValidationFailureDetails> failures, Set<String> accessTypes) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s)", access, failures, accessTypes)); } boolean valid = true; if (CollectionUtils.isEmpty(accessTypes)) { LOG.debug("isValidPolicyItemAccess: accessTypes was null!"); } else if (access == null) { LOG.debug("isValidPolicyItemAccess: policy item access was null!"); } else { String accessType = access.getType(); if (StringUtils.isBlank(accessType)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isMissing() .becauseOf(error.getMessage("policy item access type")) .errorCode(error.getErrorCode()) .build()); valid = false; } else { String matchedAccessType = getMatchedAccessType(accessType, accessTypes); if (StringUtils.isEmpty(matchedAccessType)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_INVALID; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isSemanticallyIncorrect() .becauseOf(error.getMessage(accessType, accessTypes)) .errorCode(error.getErrorCode()) .build()); valid = false; } else { access.setType(matchedAccessType); } } Boolean isAllowed = access.getIsAllowed(); if (isAllowed != null && isAllowed == false) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_DENY; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type allowed") .isSemanticallyIncorrect() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s): %s", access, failures, accessTypes, valid)); } return valid; } RangerPolicyValidator(ServiceStore store); }
RangerPolicyValidator extends RangerValidator { boolean isValidPolicyItemAccess(RangerPolicyItemAccess access, List<ValidationFailureDetails> failures, Set<String> accessTypes) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s)", access, failures, accessTypes)); } boolean valid = true; if (CollectionUtils.isEmpty(accessTypes)) { LOG.debug("isValidPolicyItemAccess: accessTypes was null!"); } else if (access == null) { LOG.debug("isValidPolicyItemAccess: policy item access was null!"); } else { String accessType = access.getType(); if (StringUtils.isBlank(accessType)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isMissing() .becauseOf(error.getMessage("policy item access type")) .errorCode(error.getErrorCode()) .build()); valid = false; } else { String matchedAccessType = getMatchedAccessType(accessType, accessTypes); if (StringUtils.isEmpty(matchedAccessType)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_INVALID; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isSemanticallyIncorrect() .becauseOf(error.getMessage(accessType, accessTypes)) .errorCode(error.getErrorCode()) .build()); valid = false; } else { access.setType(matchedAccessType); } } Boolean isAllowed = access.getIsAllowed(); if (isAllowed != null && isAllowed == false) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_DENY; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type allowed") .isSemanticallyIncorrect() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s): %s", access, failures, accessTypes, valid)); } return valid; } RangerPolicyValidator(ServiceStore store); void validate(RangerPolicy policy, Action action, boolean isAdmin); }
RangerPolicyValidator extends RangerValidator { boolean isValidPolicyItemAccess(RangerPolicyItemAccess access, List<ValidationFailureDetails> failures, Set<String> accessTypes) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s)", access, failures, accessTypes)); } boolean valid = true; if (CollectionUtils.isEmpty(accessTypes)) { LOG.debug("isValidPolicyItemAccess: accessTypes was null!"); } else if (access == null) { LOG.debug("isValidPolicyItemAccess: policy item access was null!"); } else { String accessType = access.getType(); if (StringUtils.isBlank(accessType)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isMissing() .becauseOf(error.getMessage("policy item access type")) .errorCode(error.getErrorCode()) .build()); valid = false; } else { String matchedAccessType = getMatchedAccessType(accessType, accessTypes); if (StringUtils.isEmpty(matchedAccessType)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_INVALID; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type") .isSemanticallyIncorrect() .becauseOf(error.getMessage(accessType, accessTypes)) .errorCode(error.getErrorCode()) .build()); valid = false; } else { access.setType(matchedAccessType); } } Boolean isAllowed = access.getIsAllowed(); if (isAllowed != null && isAllowed == false) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_POLICY_ITEM_ACCESS_TYPE_DENY; failures.add(new ValidationFailureDetailsBuilder() .field("policy item access type allowed") .isSemanticallyIncorrect() .becauseOf(error.getMessage()) .errorCode(error.getErrorCode()) .build()); valid = false; } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isValidPolicyItemAccess(%s, %s, %s): %s", access, failures, accessTypes, valid)); } return valid; } RangerPolicyValidator(ServiceStore store); void validate(RangerPolicy policy, Action action, boolean isAdmin);  }