RangerPolicyValidator extends RangerValidator { boolean isPolicyResourceUnique(RangerPolicy policy, final List<ValidationFailureDetails> failures, Action action) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerPolicyValidator.isPolicyResourceUnique(%s, %s, %s)", policy, failures, action)); } boolean valid = true; if (!Boolean.TRUE.equals(policy.getIsEnabled())) { LOG.debug("Policy is disabled. Skipping resource uniqueness validation."); } else { RangerPolicyResourceSignature policySignature = _factory.createPolicyResourceSignature(policy); String signature = policySignature.getSignature(); List<RangerPolicy> policies = getPoliciesForResourceSignature(policy.getService(), signature); if (CollectionUtils.isNotEmpty(policies)) { ValidationErrorCode error = ValidationErrorCode.POLICY_VALIDATION_ERR_DUPLICATE_POLICY_RESOURCE; RangerPolicy matchedPolicy = policies.iterator().next(); if (action == Action.CREATE || (action == Action.UPDATE && (policies.size() > 1 || !matchedPolicy.getId().equals(policy.getId())))) { failures.add(new ValidationFailureDetailsBuilder() .field("resources") .isSemanticallyIncorrect() .becauseOf(error.getMessage(matchedPolicy.getName(), policy.getService())) .errorCode(error.getErrorCode()) .build()); valid = false; } } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerPolicyValidator.isPolicyResourceUnique(%s, %s, %s): %s", policy, failures, action, valid)); } return valid; } RangerPolicyValidator(ServiceStore store); void validate(RangerPolicy policy, Action action, boolean isAdmin);  }
@Test public final void test_isPolicyResourceUnique() throws Exception { RangerPolicyResourceSignature signature = mock(RangerPolicyResourceSignature.class); String hash = "hash-1"; when(signature.getSignature()).thenReturn(hash); when(_factory.createPolicyResourceSignature(_policy)).thenReturn(signature); when(_policy.getService()).thenReturn("service-name"); List<RangerPolicy> policies = null; when(_store.getPoliciesByResourceSignature("service-name", hash, true)).thenReturn(policies); policies = new ArrayList<>(); for (Action action : cu) { Assert.assertTrue(_validator.isPolicyResourceUnique(_policy, _failures, action)); Assert.assertTrue(_validator.isPolicyResourceUnique(_policy, _failures, action)); } RangerPolicy policy1 = mock(RangerPolicy.class); policies.add(policy1); when(_store.getPoliciesByResourceSignature("service-name", hash, true)).thenReturn(policies); when(_policy.getIsEnabled()).thenReturn(true); _failures.clear(); Assert.assertFalse(_validator.isPolicyResourceUnique(_policy, _failures, Action.CREATE)); _utils.checkFailureForSemanticError(_failures, "resources"); when(_policy.getIsEnabled()).thenReturn(false); _failures.clear(); Assert.assertTrue(_validator.isPolicyResourceUnique(_policy, _failures, Action.CREATE)); Assert.assertTrue("failures collection wasn't empty!", _failures.isEmpty()); when(_policy.getIsEnabled()).thenReturn(true); when(policy1.getId()).thenReturn(103L); when(_policy.getId()).thenReturn(103L); Assert.assertTrue(_validator.isPolicyResourceUnique(_policy, _failures, Action.UPDATE)); when(policy1.getId()).thenReturn(104L); Assert.assertFalse(_validator.isPolicyResourceUnique(_policy, _failures, Action.UPDATE)); _utils.checkFailureForSemanticError(_failures, "resources"); when(_policy.getIsEnabled()).thenReturn(false); _failures.clear(); Assert.assertTrue(_validator.isPolicyResourceUnique(_policy, _failures, Action.UPDATE)); Assert.assertTrue("failures collection wasn't empty!", _failures.isEmpty()); RangerPolicy policy2 = mock(RangerPolicy.class); when(policy2.getId()).thenReturn(103L); policies.add(policy2); when(_policy.getIsEnabled()).thenReturn(true); Assert.assertFalse(_validator.isPolicyResourceUnique(_policy, _failures, Action.UPDATE)); _utils.checkFailureForSemanticError(_failures, "resources"); when(_policy.getIsEnabled()).thenReturn(false); _failures.clear(); Assert.assertTrue(_validator.isPolicyResourceUnique(_policy, _failures, Action.UPDATE)); Assert.assertTrue("failures collection wasn't empty!", _failures.isEmpty()); }