Search in sources :

Example 6 with RangerEnumDef

use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef in project ranger by apache.

the class TestPublicAPIsv2 method rangerServiceDef.

private RangerServiceDef rangerServiceDef() {
    List<RangerServiceConfigDef> configs = new ArrayList<RangerServiceConfigDef>();
    List<RangerResourceDef> resources = new ArrayList<RangerResourceDef>();
    List<RangerAccessTypeDef> accessTypes = new ArrayList<RangerAccessTypeDef>();
    List<RangerPolicyConditionDef> policyConditions = new ArrayList<RangerPolicyConditionDef>();
    List<RangerContextEnricherDef> contextEnrichers = new ArrayList<RangerContextEnricherDef>();
    List<RangerEnumDef> enums = new ArrayList<RangerEnumDef>();
    RangerServiceDef rangerServiceDef = new RangerServiceDef();
    rangerServiceDef.setId(Id);
    rangerServiceDef.setName("RangerServiceHdfs");
    rangerServiceDef.setImplClass("RangerServiceHdfs");
    rangerServiceDef.setLabel("HDFS Repository");
    rangerServiceDef.setDescription("HDFS Repository");
    rangerServiceDef.setRbKeyDescription(null);
    rangerServiceDef.setUpdatedBy("Admin");
    rangerServiceDef.setUpdateTime(new Date());
    rangerServiceDef.setConfigs(configs);
    rangerServiceDef.setResources(resources);
    rangerServiceDef.setAccessTypes(accessTypes);
    rangerServiceDef.setPolicyConditions(policyConditions);
    rangerServiceDef.setContextEnrichers(contextEnrichers);
    rangerServiceDef.setEnums(enums);
    return rangerServiceDef;
}
Also used : RangerServiceConfigDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerServiceConfigDef) ArrayList(java.util.ArrayList) RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef) RangerPolicyConditionDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef) Date(java.util.Date) RangerAccessTypeDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef) RangerContextEnricherDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerContextEnricherDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) RangerResourceDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef)

Example 7 with RangerEnumDef

use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef in project ranger by apache.

the class TestServiceREST method rangerServiceDef.

private RangerServiceDef rangerServiceDef() {
    List<RangerServiceConfigDef> configs = new ArrayList<RangerServiceConfigDef>();
    List<RangerResourceDef> resources = new ArrayList<RangerResourceDef>();
    List<RangerAccessTypeDef> accessTypes = new ArrayList<RangerAccessTypeDef>();
    List<RangerPolicyConditionDef> policyConditions = new ArrayList<RangerPolicyConditionDef>();
    List<RangerContextEnricherDef> contextEnrichers = new ArrayList<RangerContextEnricherDef>();
    List<RangerEnumDef> enums = new ArrayList<RangerEnumDef>();
    RangerServiceDef rangerServiceDef = new RangerServiceDef();
    rangerServiceDef.setId(Id);
    rangerServiceDef.setImplClass("RangerServiceHdfs");
    rangerServiceDef.setLabel("HDFS Repository");
    rangerServiceDef.setDescription("HDFS Repository");
    rangerServiceDef.setRbKeyDescription(null);
    rangerServiceDef.setUpdatedBy("Admin");
    rangerServiceDef.setUpdateTime(new Date());
    rangerServiceDef.setConfigs(configs);
    rangerServiceDef.setResources(resources);
    rangerServiceDef.setAccessTypes(accessTypes);
    rangerServiceDef.setPolicyConditions(policyConditions);
    rangerServiceDef.setContextEnrichers(contextEnrichers);
    rangerServiceDef.setEnums(enums);
    return rangerServiceDef;
}
Also used : RangerServiceConfigDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerServiceConfigDef) ArrayList(java.util.ArrayList) RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef) RangerPolicyConditionDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef) Date(java.util.Date) RangerAccessTypeDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef) RangerContextEnricherDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerContextEnricherDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) RangerResourceDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef)

Example 8 with RangerEnumDef

use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef in project ranger by apache.

the class TestRangerServiceDefValidator method test_isValidEnums_failures.

@Test
public final void test_isValidEnums_failures() {
    // null elements in enum def list are a failure
    List<RangerEnumDef> input = _utils.createEnumDefs(enums_good);
    input.add(null);
    assertFalse(_validator.isValidEnums(input, _failures));
    _utils.checkFailureForMissingValue(_failures, "enum def");
    // enum names should be valid
    input = _utils.createEnumDefs(enums_bad_enumName_null);
    _failures.clear();
    assertFalse(_validator.isValidEnums(input, _failures));
    _utils.checkFailureForMissingValue(_failures, "enum def name");
    input = _utils.createEnumDefs(enums_bad_enumName_blank);
    _failures.clear();
    assertFalse(_validator.isValidEnums(input, _failures));
    _utils.checkFailureForMissingValue(_failures, "enum def name");
    _utils.checkFailureForSemanticError(_failures, "enum def itemId", "1");
    // enum elements collection should not be null or empty
    input = _utils.createEnumDefs(enums_good);
    RangerEnumDef anEnumDef = mock(RangerEnumDef.class);
    when(anEnumDef.getName()).thenReturn("anEnum");
    when(anEnumDef.getElements()).thenReturn(null);
    input.add(anEnumDef);
    _failures.clear();
    assertFalse(_validator.isValidEnums(input, _failures));
    _utils.checkFailureForMissingValue(_failures, "enum values", "anEnum");
    input = _utils.createEnumDefs(enums_bad_Elements_empty);
    _failures.clear();
    assertFalse(_validator.isValidEnums(input, _failures));
    _utils.checkFailureForMissingValue(_failures, "enum values", "anEnum");
    _utils.checkFailureForMissingValue(_failures, "enum def itemId");
    // enum names should be distinct -- exact match
    input = _utils.createEnumDefs(enums_good);
    // add an element with same name as the first element
    String name = input.iterator().next().getName();
    when(anEnumDef.getName()).thenReturn(name);
    List<RangerEnumElementDef> elementDefs = _utils.createEnumElementDefs(new String[] { "val1", "val2" });
    when(anEnumDef.getElements()).thenReturn(elementDefs);
    input.add(anEnumDef);
    _failures.clear();
    assertFalse(_validator.isValidEnums(input, _failures));
    _utils.checkFailureForSemanticError(_failures, "enum def name", name);
    // enum names should be distinct -- case insensitive
    input = _utils.createEnumDefs(enums_bad_enumName_duplicate_differentCase);
    _failures.clear();
    assertFalse(_validator.isValidEnums(input, _failures));
    _utils.checkFailureForSemanticError(_failures, "enum def name", "Authentication-Type");
    // enum default index should be right
    input = _utils.createEnumDefs(enums_good);
    // set the index of 1st on to be less than 0
    when(input.iterator().next().getDefaultIndex()).thenReturn(-1);
    _failures.clear();
    assertFalse(_validator.isValidEnums(input, _failures));
    _utils.checkFailureForSemanticError(_failures, "enum default index", "authentication-type");
    // set the index to be more than number of elements
    when(input.iterator().next().getDefaultIndex()).thenReturn(2);
    _failures.clear();
    assertFalse(_validator.isValidEnums(input, _failures));
    _utils.checkFailureForSemanticError(_failures, "enum default index", "authentication-type");
}
Also used : RangerEnumElementDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumElementDef) RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef) Test(org.junit.Test)

Example 9 with RangerEnumDef

use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef in project ranger by apache.

the class RangerServiceDefValidator method isValid.

boolean isValid(final RangerServiceDef serviceDef, final Action action, final List<ValidationFailureDetails> failures) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerServiceDefValidator.isValid(" + serviceDef + ")");
    }
    if (!(action == Action.CREATE || action == Action.UPDATE)) {
        throw new IllegalArgumentException("isValid(RangerServiceDef, ...) is only supported for CREATE/UPDATE");
    }
    boolean valid = true;
    if (serviceDef == null) {
        ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_NULL_SERVICE_DEF_OBJECT;
        failures.add(new ValidationFailureDetailsBuilder().field("service def").isMissing().errorCode(error.getErrorCode()).becauseOf(error.getMessage(action)).build());
        valid = false;
    } else {
        Long id = serviceDef.getId();
        valid = isValidServiceDefId(id, action, failures) && valid;
        valid = isValidServiceDefName(serviceDef.getName(), id, action, failures) && valid;
        valid = isValidAccessTypes(serviceDef.getAccessTypes(), failures) && valid;
        if (isValidResources(serviceDef, failures)) {
            // Semantic check of resource graph can only be done if resources are "syntactically" valid
            valid = isValidResourceGraph(serviceDef, failures) && valid;
        } else {
            valid = false;
        }
        List<RangerEnumDef> enumDefs = serviceDef.getEnums();
        if (isValidEnums(enumDefs, failures)) {
            // config def validation requires valid enums
            valid = isValidConfigs(serviceDef.getConfigs(), enumDefs, failures) && valid;
        } else {
            valid = false;
        }
        valid = isValidPolicyConditions(serviceDef.getPolicyConditions(), failures) && valid;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerServiceDefValidator.isValid(" + serviceDef + "): " + valid);
    }
    return valid;
}
Also used : RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef) ValidationErrorCode(org.apache.ranger.plugin.errors.ValidationErrorCode)

Example 10 with RangerEnumDef

use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef in project ranger by apache.

the class RangerServiceDefValidator method isValidConfigOfEnumType.

boolean isValidConfigOfEnumType(RangerServiceConfigDef configDef, List<RangerEnumDef> enumDefs, List<ValidationFailureDetails> failures) {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("==> RangerServiceDefValidator.isValidConfigOfEnumType(%s, %s, %s)", configDef, enumDefs, failures));
    }
    boolean valid = true;
    if (!"enum".equals(configDef.getType())) {
        LOG.debug("ConfigDef wasn't of enum type!");
    } else {
        Map<String, RangerEnumDef> enumDefsMap = getEnumDefMap(enumDefs);
        Set<String> enumTypes = enumDefsMap.keySet();
        String subType = configDef.getSubType();
        String configName = configDef.getName();
        if (!enumTypes.contains(subType)) {
            ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_CONFIG_DEF_UNKNOWN_ENUM;
            failures.add(new ValidationFailureDetailsBuilder().field("config def subtype").subField(configName).isSemanticallyIncorrect().errorCode(error.getErrorCode()).becauseOf(error.getMessage(subType, configName, enumTypes)).build());
            valid = false;
        } else {
            // default value check is possible only if sub-type is correctly configured
            String defaultValue = configDef.getDefaultValue();
            if (StringUtils.isNotBlank(defaultValue)) {
                RangerEnumDef enumDef = enumDefsMap.get(subType);
                Set<String> enumValues = getEnumValues(enumDef);
                if (!enumValues.contains(defaultValue)) {
                    ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_CONFIG_DEF_UNKNOWN_ENUM_VALUE;
                    failures.add(new ValidationFailureDetailsBuilder().field("config def default value").subField(configName).isSemanticallyIncorrect().errorCode(error.getErrorCode()).becauseOf(error.getMessage(defaultValue, configName, enumValues, subType)).build());
                    valid = false;
                }
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("<== RangerServiceDefValidator.isValidConfigOfEnumType(%s, %s, %s): %s", configDef, enumDefs, failures, valid));
    }
    return valid;
}
Also used : RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef) ValidationErrorCode(org.apache.ranger.plugin.errors.ValidationErrorCode)

Aggregations

RangerEnumDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef)20 ArrayList (java.util.ArrayList)10 RangerAccessTypeDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef)10 RangerContextEnricherDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerContextEnricherDef)10 RangerPolicyConditionDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef)10 RangerResourceDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef)10 RangerServiceConfigDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerServiceConfigDef)10 Date (java.util.Date)8 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)8 XXEnumDef (org.apache.ranger.entity.XXEnumDef)5 RangerEnumElementDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumElementDef)5 Test (org.junit.Test)5 XXServiceDef (org.apache.ranger.entity.XXServiceDef)4 XXEnumElementDefDao (org.apache.ranger.db.XXEnumElementDefDao)3 XXEnumElementDef (org.apache.ranger.entity.XXEnumElementDef)3 ValidationErrorCode (org.apache.ranger.plugin.errors.ValidationErrorCode)3 RangerDataMaskDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerDataMaskDef)3 RangerDataMaskTypeDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerDataMaskTypeDef)3 RangerRowFilterDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerRowFilterDef)3 VXString (org.apache.ranger.view.VXString)3