use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumElementDef in project ranger by apache.
the class TestRangerServiceDefServiceBase method test19populateRangerEnumElementDefToXX.
@Test
public void test19populateRangerEnumElementDefToXX() {
RangerEnumElementDef rangerEnumElementDefObj = new RangerEnumElementDef();
rangerEnumElementDefObj.setLabel("Simple");
rangerEnumElementDefObj.setName("simple");
rangerEnumElementDefObj.setRbKeyLabel(null);
XXEnumElementDef enumElementDefObj = new XXEnumElementDef();
enumElementDefObj.setAddedByUserId(Id);
enumElementDefObj.setCreateTime(new Date());
enumElementDefObj.setEnumdefid(Id);
enumElementDefObj.setId(Id);
enumElementDefObj.setLabel("Simple");
enumElementDefObj.setName("simple");
enumElementDefObj.setOrder(0);
enumElementDefObj.setUpdatedByUserId(Id);
enumElementDefObj.setUpdateTime(new Date());
XXEnumDef enumDefObj = new XXEnumDef();
enumDefObj.setAddedByUserId(Id);
enumDefObj.setCreateTime(new Date());
enumDefObj.setDefaultindex(0);
enumDefObj.setDefid(Id);
enumDefObj.setId(Id);
enumDefObj.setName("authnType");
enumDefObj.setUpdatedByUserId(Id);
enumDefObj.setUpdateTime(new Date());
Mockito.when((XXEnumElementDef) rangerAuditFields.populateAuditFields(enumElementDefObj, enumDefObj)).thenReturn(enumElementDefObj);
XXEnumElementDef dbEnumElementDef = rangerServiceDefService.populateRangerEnumElementDefToXX(rangerEnumElementDefObj, enumElementDefObj, enumDefObj, 1);
Assert.assertNotNull(dbEnumElementDef);
Assert.assertEquals(dbEnumElementDef.getId(), enumElementDefObj.getId());
Assert.assertEquals(dbEnumElementDef.getName(), enumElementDefObj.getName());
Assert.assertEquals(dbEnumElementDef.getLabel(), enumElementDefObj.getLabel());
Assert.assertEquals(dbEnumElementDef.getCreateTime(), enumElementDefObj.getCreateTime());
Assert.assertEquals(dbEnumElementDef.getAddedByUserId(), enumElementDefObj.getAddedByUserId());
Assert.assertEquals(dbEnumElementDef.getUpdateTime(), enumElementDefObj.getUpdateTime());
Assert.assertEquals(dbEnumElementDef.getUpdatedByUserId(), enumElementDefObj.getUpdatedByUserId());
Mockito.verify(rangerAuditFields).populateAuditFields(enumElementDefObj, enumDefObj);
}
use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumElementDef 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.RangerEnumElementDef in project ranger by apache.
the class ValidationTestUtils method createEnumElementDefs.
List<RangerEnumElementDef> createEnumElementDefs(Object[][] input) {
if (input == null) {
return null;
}
List<RangerEnumElementDef> output = new ArrayList<>();
for (Object[] row : input) {
RangerEnumElementDef aDef = mock(RangerEnumElementDef.class);
switch(row.length) {
case 2:
when(aDef.getName()).thenReturn((String) row[1]);
case 1:
when(aDef.getItemId()).thenReturn((Long) row[0]);
}
output.add(aDef);
}
return output;
}
use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumElementDef in project ranger by apache.
the class RangerServiceDefServiceBase method populateXXToRangerEnumElementDef.
public RangerEnumElementDef populateXXToRangerEnumElementDef(XXEnumElementDef xObj) {
RangerEnumElementDef vObj = new RangerEnumElementDef();
vObj.setItemId(xObj.getItemId());
vObj.setName(xObj.getName());
vObj.setLabel(xObj.getLabel());
vObj.setRbKeyLabel(xObj.getRbkeylabel());
return vObj;
}
use of org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumElementDef in project ranger by apache.
the class ServiceDBStore method createServiceDef.
@Override
public RangerServiceDef createServiceDef(RangerServiceDef serviceDef) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("==> ServiceDBStore.createServiceDef(" + serviceDef + ")");
}
XXServiceDef xServiceDef = daoMgr.getXXServiceDef().findByName(serviceDef.getName());
if (xServiceDef != null) {
throw restErrorUtil.createRESTException("service-def with name: " + serviceDef.getName() + " already exists", MessageEnums.ERROR_DUPLICATE_OBJECT);
}
List<RangerServiceConfigDef> configs = serviceDef.getConfigs();
List<RangerResourceDef> resources = serviceDef.getResources();
List<RangerAccessTypeDef> accessTypes = serviceDef.getAccessTypes();
List<RangerPolicyConditionDef> policyConditions = serviceDef.getPolicyConditions();
List<RangerContextEnricherDef> contextEnrichers = serviceDef.getContextEnrichers();
List<RangerEnumDef> enums = serviceDef.getEnums();
RangerDataMaskDef dataMaskDef = serviceDef.getDataMaskDef();
RangerRowFilterDef rowFilterDef = serviceDef.getRowFilterDef();
List<RangerDataMaskTypeDef> dataMaskTypes = dataMaskDef == null || dataMaskDef.getMaskTypes() == null ? new ArrayList<RangerDataMaskTypeDef>() : dataMaskDef.getMaskTypes();
List<RangerAccessTypeDef> dataMaskAccessTypes = dataMaskDef == null || dataMaskDef.getAccessTypes() == null ? new ArrayList<RangerAccessTypeDef>() : dataMaskDef.getAccessTypes();
List<RangerResourceDef> dataMaskResources = dataMaskDef == null || dataMaskDef.getResources() == null ? new ArrayList<RangerResourceDef>() : dataMaskDef.getResources();
List<RangerAccessTypeDef> rowFilterAccessTypes = rowFilterDef == null || rowFilterDef.getAccessTypes() == null ? new ArrayList<RangerAccessTypeDef>() : rowFilterDef.getAccessTypes();
List<RangerResourceDef> rowFilterResources = rowFilterDef == null || rowFilterDef.getResources() == null ? new ArrayList<RangerResourceDef>() : rowFilterDef.getResources();
RangerServiceDefHelper defHelper = new RangerServiceDefHelper(serviceDef, false);
defHelper.patchServiceDefWithDefaultValues();
// While creating, value of version should be 1.
serviceDef.setVersion(Long.valueOf(1));
if (populateExistingBaseFields) {
svcDefServiceWithAssignedId.setPopulateExistingBaseFields(true);
daoMgr.getXXServiceDef().setIdentityInsert(true);
svcDefServiceWithAssignedId.create(serviceDef);
svcDefServiceWithAssignedId.setPopulateExistingBaseFields(false);
daoMgr.getXXServiceDef().updateSequence();
daoMgr.getXXServiceDef().setIdentityInsert(false);
} else {
// following fields will be auto populated
serviceDef.setId(null);
serviceDef.setCreateTime(null);
serviceDef.setUpdateTime(null);
serviceDef = serviceDefService.create(serviceDef);
}
Long serviceDefId = serviceDef.getId();
XXServiceDef createdSvcDef = daoMgr.getXXServiceDef().getById(serviceDefId);
XXServiceConfigDefDao xxServiceConfigDao = daoMgr.getXXServiceConfigDef();
for (int i = 0; i < configs.size(); i++) {
RangerServiceConfigDef config = configs.get(i);
XXServiceConfigDef xConfig = new XXServiceConfigDef();
xConfig = serviceDefService.populateRangerServiceConfigDefToXX(config, xConfig, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
xConfig.setOrder(i);
xConfig = xxServiceConfigDao.create(xConfig);
}
XXResourceDefDao xxResDefDao = daoMgr.getXXResourceDef();
for (int i = 0; i < resources.size(); i++) {
RangerResourceDef resource = resources.get(i);
XXResourceDef parent = xxResDefDao.findByNameAndServiceDefId(resource.getParent(), serviceDefId);
Long parentId = (parent != null) ? parent.getId() : null;
XXResourceDef xResource = new XXResourceDef();
xResource = serviceDefService.populateRangerResourceDefToXX(resource, xResource, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
xResource.setOrder(i);
xResource.setParent(parentId);
xResource = xxResDefDao.create(xResource);
}
XXAccessTypeDefDao xxATDDao = daoMgr.getXXAccessTypeDef();
for (int i = 0; i < accessTypes.size(); i++) {
RangerAccessTypeDef accessType = accessTypes.get(i);
XXAccessTypeDef xAccessType = new XXAccessTypeDef();
xAccessType = serviceDefService.populateRangerAccessTypeDefToXX(accessType, xAccessType, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
xAccessType.setOrder(i);
xAccessType = xxATDDao.create(xAccessType);
Collection<String> impliedGrants = accessType.getImpliedGrants();
XXAccessTypeDefGrantsDao xxATDGrantDao = daoMgr.getXXAccessTypeDefGrants();
for (String impliedGrant : impliedGrants) {
XXAccessTypeDefGrants xImpliedGrant = new XXAccessTypeDefGrants();
xImpliedGrant.setAtdId(xAccessType.getId());
xImpliedGrant.setImpliedGrant(impliedGrant);
xImpliedGrant = xxATDGrantDao.create(xImpliedGrant);
}
}
XXPolicyConditionDefDao xxPolCondDao = daoMgr.getXXPolicyConditionDef();
for (int i = 0; i < policyConditions.size(); i++) {
RangerPolicyConditionDef policyCondition = policyConditions.get(i);
XXPolicyConditionDef xPolicyCondition = new XXPolicyConditionDef();
xPolicyCondition = serviceDefService.populateRangerPolicyConditionDefToXX(policyCondition, xPolicyCondition, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
xPolicyCondition.setOrder(i);
xPolicyCondition = xxPolCondDao.create(xPolicyCondition);
}
XXContextEnricherDefDao xxContextEnricherDao = daoMgr.getXXContextEnricherDef();
for (int i = 0; i < contextEnrichers.size(); i++) {
RangerContextEnricherDef contextEnricher = contextEnrichers.get(i);
XXContextEnricherDef xContextEnricher = new XXContextEnricherDef();
xContextEnricher = serviceDefService.populateRangerContextEnricherDefToXX(contextEnricher, xContextEnricher, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
xContextEnricher.setOrder(i);
xContextEnricher = xxContextEnricherDao.create(xContextEnricher);
}
XXEnumDefDao xxEnumDefDao = daoMgr.getXXEnumDef();
for (RangerEnumDef vEnum : enums) {
XXEnumDef xEnum = new XXEnumDef();
xEnum = serviceDefService.populateRangerEnumDefToXX(vEnum, xEnum, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
xEnum = xxEnumDefDao.create(xEnum);
List<RangerEnumElementDef> elements = vEnum.getElements();
XXEnumElementDefDao xxEnumEleDefDao = daoMgr.getXXEnumElementDef();
for (int i = 0; i < elements.size(); i++) {
RangerEnumElementDef element = elements.get(i);
XXEnumElementDef xElement = new XXEnumElementDef();
xElement = serviceDefService.populateRangerEnumElementDefToXX(element, xElement, xEnum, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
xElement.setOrder(i);
xElement = xxEnumEleDefDao.create(xElement);
}
}
XXDataMaskTypeDefDao xxDataMaskDefDao = daoMgr.getXXDataMaskTypeDef();
for (int i = 0; i < dataMaskTypes.size(); i++) {
RangerDataMaskTypeDef dataMask = dataMaskTypes.get(i);
XXDataMaskTypeDef xDataMaskDef = new XXDataMaskTypeDef();
xDataMaskDef = serviceDefService.populateRangerDataMaskDefToXX(dataMask, xDataMaskDef, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
xDataMaskDef.setOrder(i);
xDataMaskDef = xxDataMaskDefDao.create(xDataMaskDef);
}
List<XXAccessTypeDef> xxAccessTypeDefs = xxATDDao.findByServiceDefId(createdSvcDef.getId());
for (RangerAccessTypeDef accessType : dataMaskAccessTypes) {
if (!isAccessTypeInList(accessType.getName(), xxAccessTypeDefs)) {
throw restErrorUtil.createRESTException("accessType with name: " + accessType.getName() + " does not exists", MessageEnums.DATA_NOT_FOUND);
}
}
for (RangerAccessTypeDef accessType : rowFilterAccessTypes) {
if (!isAccessTypeInList(accessType.getName(), xxAccessTypeDefs)) {
throw restErrorUtil.createRESTException("accessType with name: " + accessType.getName() + " does not exists", MessageEnums.DATA_NOT_FOUND);
}
}
for (XXAccessTypeDef xxAccessTypeDef : xxAccessTypeDefs) {
String dataMaskOptions = null;
String rowFilterOptions = null;
for (RangerAccessTypeDef accessTypeDef : dataMaskAccessTypes) {
if (StringUtils.equals(accessTypeDef.getName(), xxAccessTypeDef.getName())) {
dataMaskOptions = svcDefServiceWithAssignedId.objectToJson(accessTypeDef);
break;
}
}
for (RangerAccessTypeDef accessTypeDef : rowFilterAccessTypes) {
if (StringUtils.equals(accessTypeDef.getName(), xxAccessTypeDef.getName())) {
rowFilterOptions = svcDefServiceWithAssignedId.objectToJson(accessTypeDef);
break;
}
}
if (!StringUtils.equals(dataMaskOptions, xxAccessTypeDef.getDataMaskOptions()) || !StringUtils.equals(rowFilterOptions, xxAccessTypeDef.getRowFilterOptions())) {
xxAccessTypeDef.setDataMaskOptions(dataMaskOptions);
xxAccessTypeDef.setRowFilterOptions(rowFilterOptions);
xxATDDao.update(xxAccessTypeDef);
}
}
List<XXResourceDef> xxResourceDefs = xxResDefDao.findByServiceDefId(createdSvcDef.getId());
for (RangerResourceDef resource : dataMaskResources) {
if (!isResourceInList(resource.getName(), xxResourceDefs)) {
throw restErrorUtil.createRESTException("resource with name: " + resource.getName() + " does not exists", MessageEnums.DATA_NOT_FOUND);
}
}
for (RangerResourceDef resource : rowFilterResources) {
if (!isResourceInList(resource.getName(), xxResourceDefs)) {
throw restErrorUtil.createRESTException("resource with name: " + resource.getName() + " does not exists", MessageEnums.DATA_NOT_FOUND);
}
}
for (XXResourceDef xxResourceDef : xxResourceDefs) {
String dataMaskOptions = null;
String rowFilterOptions = null;
for (RangerResourceDef resource : dataMaskResources) {
if (StringUtils.equals(resource.getName(), xxResourceDef.getName())) {
dataMaskOptions = svcDefServiceWithAssignedId.objectToJson(resource);
break;
}
}
for (RangerResourceDef resource : rowFilterResources) {
if (StringUtils.equals(resource.getName(), xxResourceDef.getName())) {
rowFilterOptions = svcDefServiceWithAssignedId.objectToJson(resource);
break;
}
}
if (!StringUtils.equals(dataMaskOptions, xxResourceDef.getDataMaskOptions()) || !StringUtils.equals(rowFilterOptions, xxResourceDef.getRowFilterOptions())) {
xxResourceDef.setDataMaskOptions(dataMaskOptions);
xxResourceDef.setRowFilterOptions(rowFilterOptions);
xxResDefDao.update(xxResourceDef);
}
}
RangerServiceDef createdServiceDef = serviceDefService.getPopulatedViewObject(createdSvcDef);
dataHistService.createObjectDataHistory(createdServiceDef, RangerDataHistService.ACTION_CREATE);
postCreate(createdServiceDef);
if (LOG.isDebugEnabled()) {
LOG.debug("<== ServiceDBStore.createServiceDef(" + serviceDef + "): " + createdServiceDef);
}
return createdServiceDef;
}
Aggregations