@Test public final void test_isValidResourceGraph() { Object[][] data_bad = new Object[][] { { "db", null, null, null, null, "" , 10 }, { "table", null, null, null, null, "db", 20 }, { "column-family", null, null, null, null, "table", null }, { "column", null, null, null, null, "column-family", 20 }, { "udf", null, null, null, null, "db", 10 }, }; List<RangerResourceDef> resourceDefs = _utils.createResourceDefs(data_bad); when(_serviceDef.getResources()).thenReturn(resourceDefs); when(_serviceDef.getName()).thenReturn("service-name"); when(_serviceDef.getUpdateTime()).thenReturn(new Date()); _failures.clear(); assertFalse(_validator.isValidResourceGraph(_serviceDef, _failures)); _utils.checkFailureForMissingValue(_failures, "resource level"); _utils.checkFailureForSemanticError(_failures, "resource level", "20"); _utils.checkFailureForSemanticError(_failures, "resource level", "10"); data_bad = new Object[][] { { "db", null, null, null, null, "" , 10 }, { "table", null, null, null, null, "db", 20 }, { "column-family", null, null, null, null, "table", 15 }, { "column", null, null, null, null, "column-family", 30 }, { "udf", null, null, null, null, "db", 15 }, }; resourceDefs = _utils.createResourceDefs(data_bad); when(_serviceDef.getResources()).thenReturn(resourceDefs); when(_serviceDef.getName()).thenReturn("service-name"); when(_serviceDef.getUpdateTime()).thenReturn(new Date()); _failures.clear(); assertFalse(_validator.isValidResourceGraph(_serviceDef, _failures)); _utils.checkFailureForSemanticError(_failures, "resource level", "15"); Object[][] data_good = new Object[][] { { "db", null, null, null, null, "" , -10 }, { "table", null, null, null, null, "db", 0 }, { "column", null, null, null, null, "table", 10 }, { "udf", null, null, null, null, "db", 0 }, }; resourceDefs = _utils.createResourceDefs(data_good); when(_serviceDef.getResources()).thenReturn(resourceDefs); _failures.clear(); assertTrue(_validator.isValidResourceGraph(_serviceDef, _failures)); assertTrue(_failures.isEmpty()); Object[][] data_cycles = new Object[][] { { "db", null, null, null, null, "column" , -10 }, { "table", null, null, null, null, "db", 0 }, { "column", null, null, null, null, "table", 10 }, { "udf", null, null, null, null, "db", -5 }, }; resourceDefs = _utils.createResourceDefs(data_cycles); when(_serviceDef.getResources()).thenReturn(resourceDefs); _failures.clear(); assertFalse("Graph was valid!", _validator.isValidResourceGraph(_serviceDef, _failures)); assertFalse(_failures.isEmpty()); _utils.checkFailureForSemanticError(_failures, "resource graph"); data_bad = new Object[][] { { "db", null, null, null, null, "" , -10 }, { "table", null, null, true, null, "db", 0 }, { "column", null, null, null, null, "table", 10 }, { "udf", null, null, null, null, "db", 0 }, }; resourceDefs = _utils.createResourceDefs(data_bad); when(_serviceDef.getResources()).thenReturn(resourceDefs); _failures.clear(); assertFalse(_validator.isValidResourceGraph(_serviceDef, _failures)); assertFalse(_failures.isEmpty()); data_good = new Object[][] { { "db", null, null, true, null, "" , -10 }, { "table", null, null, null, null, "db", 0 }, { "column", null, null, null, null, "table", 10 }, { "udf", null, null, true, null, "db", 0 }, }; resourceDefs = _utils.createResourceDefs(data_good); when(_serviceDef.getResources()).thenReturn(resourceDefs); _failures.clear(); assertTrue(_validator.isValidResourceGraph(_serviceDef, _failures)); assertTrue(_failures.isEmpty()); }
boolean isValidResourceGraph(RangerServiceDef serviceDef, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerServiceDefValidator.isValidResourceGraph(%s, %s)", serviceDef, failures)); } boolean valid = true; RangerServiceDefHelper defHelper = _factory.createServiceDefHelper(serviceDef, false); if (!defHelper.isResourceGraphValid()) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_RESOURCE_GRAPH_INVALID; failures.add(new ValidationFailureDetailsBuilder() .field("resource graph") .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage()) .build()); valid = false; } for(int policyType : RangerPolicy.POLICY_TYPES) { Set<List<RangerResourceDef>> hierarchies = defHelper.getResourceHierarchies(policyType); for (List<RangerResourceDef> aHierarchy : hierarchies) { Set<Integer> levels = new HashSet<Integer>(aHierarchy.size()); for (RangerResourceDef resourceDef : aHierarchy) { valid = isUnique(resourceDef.getLevel(), levels, "resource level", "resources", failures) && valid; } int lastResourceLevel = Integer.MIN_VALUE; for (RangerResourceDef resourceDef : aHierarchy) { Integer resourceDefLevel = resourceDef.getLevel(); if (resourceDefLevel == null || resourceDefLevel < lastResourceLevel) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_INVALID_SERVICE_RESOURCE_LEVELS; failures.add(new ValidationFailureDetailsBuilder() .field("resource level") .subField(String.valueOf(resourceDefLevel)) .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage()) .build()); valid = false; break; } else { lastResourceLevel = resourceDef.getLevel(); } } } } List<RangerResourceDef> resources = serviceDef.getResources(); List<String> resourceNames = new ArrayList<>(resources.size()); for (RangerResourceDef resourceDef : resources) { resourceNames.add(resourceDef.getName()); } for (String resourceName : resourceNames) { for (int policyType : RangerPolicy.POLICY_TYPES) { Set<List<RangerResourceDef>> hierarchies = defHelper.getResourceHierarchies(policyType); for (List<RangerResourceDef> aHierarchy : hierarchies) { boolean foundOptionalResource = false; for (RangerResourceDef resourceDef : aHierarchy) { if (!foundOptionalResource) { if (resourceDef.getName().equalsIgnoreCase(resourceName) && !Boolean.TRUE.equals(resourceDef.getMandatory())) { foundOptionalResource = true; } } else { if (Boolean.TRUE.equals(resourceDef.getMandatory())) { valid = false; ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_INVALID_MANADORY_VALUE_FOR_SERVICE_RESOURCE; failures.add(new ValidationFailureDetailsBuilder() .field(resourceDef.getName()) .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(resourceDef.getName(), resourceName)) .build()); } } } } } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerServiceDefValidator.isValidResourceGraph(%s, %s): %s", serviceDef, failures, valid)); } return valid; }
RangerServiceDefValidator extends RangerValidator { boolean isValidResourceGraph(RangerServiceDef serviceDef, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerServiceDefValidator.isValidResourceGraph(%s, %s)", serviceDef, failures)); } boolean valid = true; RangerServiceDefHelper defHelper = _factory.createServiceDefHelper(serviceDef, false); if (!defHelper.isResourceGraphValid()) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_RESOURCE_GRAPH_INVALID; failures.add(new ValidationFailureDetailsBuilder() .field("resource graph") .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage()) .build()); valid = false; } for(int policyType : RangerPolicy.POLICY_TYPES) { Set<List<RangerResourceDef>> hierarchies = defHelper.getResourceHierarchies(policyType); for (List<RangerResourceDef> aHierarchy : hierarchies) { Set<Integer> levels = new HashSet<Integer>(aHierarchy.size()); for (RangerResourceDef resourceDef : aHierarchy) { valid = isUnique(resourceDef.getLevel(), levels, "resource level", "resources", failures) && valid; } int lastResourceLevel = Integer.MIN_VALUE; for (RangerResourceDef resourceDef : aHierarchy) { Integer resourceDefLevel = resourceDef.getLevel(); if (resourceDefLevel == null || resourceDefLevel < lastResourceLevel) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_INVALID_SERVICE_RESOURCE_LEVELS; failures.add(new ValidationFailureDetailsBuilder() .field("resource level") .subField(String.valueOf(resourceDefLevel)) .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage()) .build()); valid = false; break; } else { lastResourceLevel = resourceDef.getLevel(); } } } } List<RangerResourceDef> resources = serviceDef.getResources(); List<String> resourceNames = new ArrayList<>(resources.size()); for (RangerResourceDef resourceDef : resources) { resourceNames.add(resourceDef.getName()); } for (String resourceName : resourceNames) { for (int policyType : RangerPolicy.POLICY_TYPES) { Set<List<RangerResourceDef>> hierarchies = defHelper.getResourceHierarchies(policyType); for (List<RangerResourceDef> aHierarchy : hierarchies) { boolean foundOptionalResource = false; for (RangerResourceDef resourceDef : aHierarchy) { if (!foundOptionalResource) { if (resourceDef.getName().equalsIgnoreCase(resourceName) && !Boolean.TRUE.equals(resourceDef.getMandatory())) { foundOptionalResource = true; } } else { if (Boolean.TRUE.equals(resourceDef.getMandatory())) { valid = false; ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_INVALID_MANADORY_VALUE_FOR_SERVICE_RESOURCE; failures.add(new ValidationFailureDetailsBuilder() .field(resourceDef.getName()) .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(resourceDef.getName(), resourceName)) .build()); } } } } } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerServiceDefValidator.isValidResourceGraph(%s, %s): %s", serviceDef, failures, valid)); } return valid; } }
RangerServiceDefValidator extends RangerValidator { boolean isValidResourceGraph(RangerServiceDef serviceDef, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerServiceDefValidator.isValidResourceGraph(%s, %s)", serviceDef, failures)); } boolean valid = true; RangerServiceDefHelper defHelper = _factory.createServiceDefHelper(serviceDef, false); if (!defHelper.isResourceGraphValid()) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_RESOURCE_GRAPH_INVALID; failures.add(new ValidationFailureDetailsBuilder() .field("resource graph") .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage()) .build()); valid = false; } for(int policyType : RangerPolicy.POLICY_TYPES) { Set<List<RangerResourceDef>> hierarchies = defHelper.getResourceHierarchies(policyType); for (List<RangerResourceDef> aHierarchy : hierarchies) { Set<Integer> levels = new HashSet<Integer>(aHierarchy.size()); for (RangerResourceDef resourceDef : aHierarchy) { valid = isUnique(resourceDef.getLevel(), levels, "resource level", "resources", failures) && valid; } int lastResourceLevel = Integer.MIN_VALUE; for (RangerResourceDef resourceDef : aHierarchy) { Integer resourceDefLevel = resourceDef.getLevel(); if (resourceDefLevel == null || resourceDefLevel < lastResourceLevel) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_INVALID_SERVICE_RESOURCE_LEVELS; failures.add(new ValidationFailureDetailsBuilder() .field("resource level") .subField(String.valueOf(resourceDefLevel)) .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage()) .build()); valid = false; break; } else { lastResourceLevel = resourceDef.getLevel(); } } } } List<RangerResourceDef> resources = serviceDef.getResources(); List<String> resourceNames = new ArrayList<>(resources.size()); for (RangerResourceDef resourceDef : resources) { resourceNames.add(resourceDef.getName()); } for (String resourceName : resourceNames) { for (int policyType : RangerPolicy.POLICY_TYPES) { Set<List<RangerResourceDef>> hierarchies = defHelper.getResourceHierarchies(policyType); for (List<RangerResourceDef> aHierarchy : hierarchies) { boolean foundOptionalResource = false; for (RangerResourceDef resourceDef : aHierarchy) { if (!foundOptionalResource) { if (resourceDef.getName().equalsIgnoreCase(resourceName) && !Boolean.TRUE.equals(resourceDef.getMandatory())) { foundOptionalResource = true; } } else { if (Boolean.TRUE.equals(resourceDef.getMandatory())) { valid = false; ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_INVALID_MANADORY_VALUE_FOR_SERVICE_RESOURCE; failures.add(new ValidationFailureDetailsBuilder() .field(resourceDef.getName()) .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(resourceDef.getName(), resourceName)) .build()); } } } } } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerServiceDefValidator.isValidResourceGraph(%s, %s): %s", serviceDef, failures, valid)); } return valid; } RangerServiceDefValidator(ServiceStore store); }
RangerServiceDefValidator extends RangerValidator { boolean isValidResourceGraph(RangerServiceDef serviceDef, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerServiceDefValidator.isValidResourceGraph(%s, %s)", serviceDef, failures)); } boolean valid = true; RangerServiceDefHelper defHelper = _factory.createServiceDefHelper(serviceDef, false); if (!defHelper.isResourceGraphValid()) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_RESOURCE_GRAPH_INVALID; failures.add(new ValidationFailureDetailsBuilder() .field("resource graph") .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage()) .build()); valid = false; } for(int policyType : RangerPolicy.POLICY_TYPES) { Set<List<RangerResourceDef>> hierarchies = defHelper.getResourceHierarchies(policyType); for (List<RangerResourceDef> aHierarchy : hierarchies) { Set<Integer> levels = new HashSet<Integer>(aHierarchy.size()); for (RangerResourceDef resourceDef : aHierarchy) { valid = isUnique(resourceDef.getLevel(), levels, "resource level", "resources", failures) && valid; } int lastResourceLevel = Integer.MIN_VALUE; for (RangerResourceDef resourceDef : aHierarchy) { Integer resourceDefLevel = resourceDef.getLevel(); if (resourceDefLevel == null || resourceDefLevel < lastResourceLevel) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_INVALID_SERVICE_RESOURCE_LEVELS; failures.add(new ValidationFailureDetailsBuilder() .field("resource level") .subField(String.valueOf(resourceDefLevel)) .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage()) .build()); valid = false; break; } else { lastResourceLevel = resourceDef.getLevel(); } } } } List<RangerResourceDef> resources = serviceDef.getResources(); List<String> resourceNames = new ArrayList<>(resources.size()); for (RangerResourceDef resourceDef : resources) { resourceNames.add(resourceDef.getName()); } for (String resourceName : resourceNames) { for (int policyType : RangerPolicy.POLICY_TYPES) { Set<List<RangerResourceDef>> hierarchies = defHelper.getResourceHierarchies(policyType); for (List<RangerResourceDef> aHierarchy : hierarchies) { boolean foundOptionalResource = false; for (RangerResourceDef resourceDef : aHierarchy) { if (!foundOptionalResource) { if (resourceDef.getName().equalsIgnoreCase(resourceName) && !Boolean.TRUE.equals(resourceDef.getMandatory())) { foundOptionalResource = true; } } else { if (Boolean.TRUE.equals(resourceDef.getMandatory())) { valid = false; ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_INVALID_MANADORY_VALUE_FOR_SERVICE_RESOURCE; failures.add(new ValidationFailureDetailsBuilder() .field(resourceDef.getName()) .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(resourceDef.getName(), resourceName)) .build()); } } } } } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerServiceDefValidator.isValidResourceGraph(%s, %s): %s", serviceDef, failures, valid)); } return valid; } RangerServiceDefValidator(ServiceStore store); void validate(final RangerServiceDef serviceDef, final Action action); boolean isValidResources(RangerServiceDef serviceDef, List<ValidationFailureDetails> failures, final Action action); }
RangerServiceDefValidator extends RangerValidator { boolean isValidResourceGraph(RangerServiceDef serviceDef, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerServiceDefValidator.isValidResourceGraph(%s, %s)", serviceDef, failures)); } boolean valid = true; RangerServiceDefHelper defHelper = _factory.createServiceDefHelper(serviceDef, false); if (!defHelper.isResourceGraphValid()) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_RESOURCE_GRAPH_INVALID; failures.add(new ValidationFailureDetailsBuilder() .field("resource graph") .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage()) .build()); valid = false; } for(int policyType : RangerPolicy.POLICY_TYPES) { Set<List<RangerResourceDef>> hierarchies = defHelper.getResourceHierarchies(policyType); for (List<RangerResourceDef> aHierarchy : hierarchies) { Set<Integer> levels = new HashSet<Integer>(aHierarchy.size()); for (RangerResourceDef resourceDef : aHierarchy) { valid = isUnique(resourceDef.getLevel(), levels, "resource level", "resources", failures) && valid; } int lastResourceLevel = Integer.MIN_VALUE; for (RangerResourceDef resourceDef : aHierarchy) { Integer resourceDefLevel = resourceDef.getLevel(); if (resourceDefLevel == null || resourceDefLevel < lastResourceLevel) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_INVALID_SERVICE_RESOURCE_LEVELS; failures.add(new ValidationFailureDetailsBuilder() .field("resource level") .subField(String.valueOf(resourceDefLevel)) .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage()) .build()); valid = false; break; } else { lastResourceLevel = resourceDef.getLevel(); } } } } List<RangerResourceDef> resources = serviceDef.getResources(); List<String> resourceNames = new ArrayList<>(resources.size()); for (RangerResourceDef resourceDef : resources) { resourceNames.add(resourceDef.getName()); } for (String resourceName : resourceNames) { for (int policyType : RangerPolicy.POLICY_TYPES) { Set<List<RangerResourceDef>> hierarchies = defHelper.getResourceHierarchies(policyType); for (List<RangerResourceDef> aHierarchy : hierarchies) { boolean foundOptionalResource = false; for (RangerResourceDef resourceDef : aHierarchy) { if (!foundOptionalResource) { if (resourceDef.getName().equalsIgnoreCase(resourceName) && !Boolean.TRUE.equals(resourceDef.getMandatory())) { foundOptionalResource = true; } } else { if (Boolean.TRUE.equals(resourceDef.getMandatory())) { valid = false; ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_INVALID_MANADORY_VALUE_FOR_SERVICE_RESOURCE; failures.add(new ValidationFailureDetailsBuilder() .field(resourceDef.getName()) .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(resourceDef.getName(), resourceName)) .build()); } } } } } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerServiceDefValidator.isValidResourceGraph(%s, %s): %s", serviceDef, failures, valid)); } return valid; } RangerServiceDefValidator(ServiceStore store); void validate(final RangerServiceDef serviceDef, final Action action); boolean isValidResources(RangerServiceDef serviceDef, List<ValidationFailureDetails> failures, final Action action);  }