Search in sources :

Example 16 with RangerEnumDef

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

the class RangerServiceDefServiceBase method populateXXToRangerEnumDef.

public RangerEnumDef populateXXToRangerEnumDef(XXEnumDef xObj) {
    RangerEnumDef vObj = new RangerEnumDef();
    vObj.setItemId(xObj.getItemId());
    vObj.setName(xObj.getName());
    vObj.setDefaultIndex(xObj.getDefaultindex());
    List<XXEnumElementDef> xElements = daoMgr.getXXEnumElementDef().findByEnumDefId(xObj.getId());
    List<RangerEnumElementDef> elements = new ArrayList<RangerEnumElementDef>();
    for (XXEnumElementDef xEle : xElements) {
        RangerEnumElementDef element = populateXXToRangerEnumElementDef(xEle);
        elements.add(element);
    }
    vObj.setElements(elements);
    return vObj;
}
Also used : RangerEnumElementDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumElementDef) ArrayList(java.util.ArrayList) RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef)

Example 17 with RangerEnumDef

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

the class TestRangerValidator method test_getEnumDefaultIndex.

@Test
public void test_getEnumDefaultIndex() {
    RangerEnumDef enumDef = mock(RangerEnumDef.class);
    Assert.assertEquals(-1, _validator.getEnumDefaultIndex(null));
    when(enumDef.getDefaultIndex()).thenReturn(null);
    Assert.assertEquals(0, _validator.getEnumDefaultIndex(enumDef));
    when(enumDef.getDefaultIndex()).thenReturn(-5);
    Assert.assertEquals(-5, _validator.getEnumDefaultIndex(enumDef));
}
Also used : RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef) Test(org.junit.Test)

Example 18 with RangerEnumDef

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

the class ValidationTestUtils method createEnumDefs.

List<RangerEnumDef> createEnumDefs(Object[][] input) {
    if (input == null) {
        return null;
    }
    List<RangerEnumDef> defs = new ArrayList<>();
    for (Object[] row : input) {
        RangerEnumDef enumDef = mock(RangerEnumDef.class);
        switch(row.length) {
            case 3:
                List<RangerEnumElementDef> elements = createEnumElementDefs((String[]) row[2]);
                when(enumDef.getElements()).thenReturn(elements);
                // by default set default index to last element
                when(enumDef.getDefaultIndex()).thenReturn(elements.size() - 1);
            case 2:
                String enumName = (String) row[1];
                when(enumDef.getName()).thenReturn(enumName);
            case 1:
                when(enumDef.getItemId()).thenReturn((Long) row[0]);
        }
        defs.add(enumDef);
    }
    return defs;
}
Also used : RangerEnumElementDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumElementDef) ArrayList(java.util.ArrayList) RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef)

Example 19 with RangerEnumDef

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

the class RangerServiceDefValidator method isValidEnums.

boolean isValidEnums(List<RangerEnumDef> enumDefs, List<ValidationFailureDetails> failures) {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("==> RangerServiceDefValidator.isValidEnums(%s, %s)", enumDefs, failures));
    }
    boolean valid = true;
    if (CollectionUtils.isEmpty(enumDefs)) {
        LOG.debug("enum def collection passed in was null/empty. Ok.");
    } else {
        Set<String> names = new HashSet<>();
        Set<Long> ids = new HashSet<>();
        for (RangerEnumDef enumDef : enumDefs) {
            if (enumDef == null) {
                ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_ENUM_DEF_NULL_OBJECT;
                failures.add(new ValidationFailureDetailsBuilder().field("enum def").isMissing().errorCode(error.getErrorCode()).becauseOf(error.getMessage()).build());
                valid = false;
            } else {
                // enum-names and ids must non-blank and be unique to a service definition
                String enumName = enumDef.getName();
                valid = isUnique(enumName, names, "enum def name", "enum defs", failures) && valid;
                valid = isUnique(enumDef.getItemId(), ids, "enum def itemId", "enum defs", failures) && valid;
                // enum must contain at least one valid value and those values should be non-blank and distinct
                if (CollectionUtils.isEmpty(enumDef.getElements())) {
                    ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_ENUM_DEF_NO_VALUES;
                    failures.add(new ValidationFailureDetailsBuilder().field("enum values").subField(enumName).isMissing().errorCode(error.getErrorCode()).becauseOf(error.getMessage(enumName)).build());
                    valid = false;
                } else {
                    valid = isValidEnumElements(enumDef.getElements(), failures, enumName) && valid;
                    // default index should be valid
                    int defaultIndex = getEnumDefaultIndex(enumDef);
                    if (defaultIndex < 0 || defaultIndex >= enumDef.getElements().size()) {
                        // max index is one less than the size of the elements list
                        ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_ENUM_DEF_INVALID_DEFAULT_INDEX;
                        failures.add(new ValidationFailureDetailsBuilder().field("enum default index").subField(enumName).isSemanticallyIncorrect().errorCode(error.getErrorCode()).becauseOf(error.getMessage(defaultIndex, enumName)).build());
                        valid = false;
                    }
                }
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("<== RangerServiceDefValidator.isValidEnums(%s, %s): %s", enumDefs, failures, valid));
    }
    return valid;
}
Also used : RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef) ValidationErrorCode(org.apache.ranger.plugin.errors.ValidationErrorCode) HashSet(java.util.HashSet)

Example 20 with RangerEnumDef

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

the class TestRangerServiceDefServiceBase 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)

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