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;
}
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;
}
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");
}
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;
}
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;
}
Aggregations