Search in sources :

Example 1 with XXResourceDef

use of org.apache.ranger.entity.XXResourceDef in project ranger by apache.

the class ServiceDBStore method deleteServiceDef.

public void deleteServiceDef(Long serviceDefId, Boolean forceDelete) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.deleteServiceDef(" + serviceDefId + ", " + forceDelete + ")");
    }
    bizUtil.blockAuditorRoleUser();
    UserSessionBase session = ContextUtil.getCurrentUserSession();
    if (session == null) {
        throw restErrorUtil.createRESTException("UserSession cannot be null, only Admin can update service-def", MessageEnums.OPER_NO_PERMISSION);
    }
    if (!session.isKeyAdmin() && !session.isUserAdmin()) {
        throw restErrorUtil.createRESTException("User is not allowed to update service-def, only Admin can update service-def", MessageEnums.OPER_NO_PERMISSION);
    }
    RangerServiceDef serviceDef = getServiceDef(serviceDefId);
    if (serviceDef == null) {
        throw restErrorUtil.createRESTException("No Service Definiton found for Id: " + serviceDefId, MessageEnums.DATA_NOT_FOUND);
    }
    List<XXService> serviceList = daoMgr.getXXService().findByServiceDefId(serviceDefId);
    if (!forceDelete) {
        if (CollectionUtils.isNotEmpty(serviceList)) {
            throw restErrorUtil.createRESTException("Services exists under given service definition, can't delete Service-Def: " + serviceDef.getName(), MessageEnums.OPER_NOT_ALLOWED_FOR_ENTITY);
        }
    }
    XXDataMaskTypeDefDao dataMaskDao = daoMgr.getXXDataMaskTypeDef();
    List<XXDataMaskTypeDef> dataMaskDefs = dataMaskDao.findByServiceDefId(serviceDefId);
    for (XXDataMaskTypeDef dataMaskDef : dataMaskDefs) {
        dataMaskDao.remove(dataMaskDef);
    }
    List<XXAccessTypeDef> accTypeDefs = daoMgr.getXXAccessTypeDef().findByServiceDefId(serviceDefId);
    for (XXAccessTypeDef accessType : accTypeDefs) {
        deleteXXAccessTypeDef(accessType);
    }
    XXContextEnricherDefDao xContextEnricherDao = daoMgr.getXXContextEnricherDef();
    List<XXContextEnricherDef> contextEnrichers = xContextEnricherDao.findByServiceDefId(serviceDefId);
    for (XXContextEnricherDef context : contextEnrichers) {
        xContextEnricherDao.remove(context);
    }
    XXEnumDefDao enumDefDao = daoMgr.getXXEnumDef();
    List<XXEnumDef> enumDefList = enumDefDao.findByServiceDefId(serviceDefId);
    for (XXEnumDef enumDef : enumDefList) {
        List<XXEnumElementDef> enumEleDefList = daoMgr.getXXEnumElementDef().findByEnumDefId(enumDef.getId());
        for (XXEnumElementDef eleDef : enumEleDefList) {
            daoMgr.getXXEnumElementDef().remove(eleDef);
        }
        enumDefDao.remove(enumDef);
    }
    XXPolicyConditionDefDao policyCondDao = daoMgr.getXXPolicyConditionDef();
    List<XXPolicyConditionDef> policyCondList = policyCondDao.findByServiceDefId(serviceDefId);
    for (XXPolicyConditionDef policyCond : policyCondList) {
        List<XXPolicyItemCondition> policyItemCondList = daoMgr.getXXPolicyItemCondition().findByPolicyConditionDefId(policyCond.getId());
        for (XXPolicyItemCondition policyItemCond : policyItemCondList) {
            daoMgr.getXXPolicyItemCondition().remove(policyItemCond);
        }
        policyCondDao.remove(policyCond);
    }
    List<XXResourceDef> resDefList = daoMgr.getXXResourceDef().findByServiceDefId(serviceDefId);
    for (XXResourceDef resDef : resDefList) {
        deleteXXResourceDef(resDef);
    }
    XXServiceConfigDefDao configDefDao = daoMgr.getXXServiceConfigDef();
    List<XXServiceConfigDef> configDefList = configDefDao.findByServiceDefId(serviceDefId);
    for (XXServiceConfigDef configDef : configDefList) {
        configDefDao.remove(configDef);
    }
    if (CollectionUtils.isNotEmpty(serviceList)) {
        for (XXService service : serviceList) {
            deleteService(service.getId());
        }
    }
    Long version = serviceDef.getVersion();
    if (version == null) {
        version = Long.valueOf(1);
        LOG.info("Found Version Value: `null`, so setting value of version to 1, While updating object, version should not be null.");
    } else {
        version = Long.valueOf(version.longValue() + 1);
    }
    serviceDef.setVersion(version);
    serviceDefService.delete(serviceDef);
    LOG.info("ServiceDefinition has been deleted successfully. Service-Def Name: " + serviceDef.getName());
    dataHistService.createObjectDataHistory(serviceDef, RangerDataHistService.ACTION_DELETE);
    postDelete(serviceDef);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceDBStore.deleteServiceDef(" + serviceDefId + ", " + forceDelete + ")");
    }
}
Also used : XXDataMaskTypeDefDao(org.apache.ranger.db.XXDataMaskTypeDefDao) XXPolicyConditionDefDao(org.apache.ranger.db.XXPolicyConditionDefDao) XXPolicyItemCondition(org.apache.ranger.entity.XXPolicyItemCondition) UserSessionBase(org.apache.ranger.common.UserSessionBase) XXDataMaskTypeDef(org.apache.ranger.entity.XXDataMaskTypeDef) XXEnumDefDao(org.apache.ranger.db.XXEnumDefDao) XXPolicyConditionDef(org.apache.ranger.entity.XXPolicyConditionDef) XXAccessTypeDef(org.apache.ranger.entity.XXAccessTypeDef) XXServiceConfigDef(org.apache.ranger.entity.XXServiceConfigDef) XXResourceDef(org.apache.ranger.entity.XXResourceDef) XXEnumElementDef(org.apache.ranger.entity.XXEnumElementDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) XXEnumDef(org.apache.ranger.entity.XXEnumDef) XXService(org.apache.ranger.entity.XXService) XXServiceConfigDefDao(org.apache.ranger.db.XXServiceConfigDefDao) XXContextEnricherDef(org.apache.ranger.entity.XXContextEnricherDef) XXContextEnricherDefDao(org.apache.ranger.db.XXContextEnricherDefDao)

Example 2 with XXResourceDef

use of org.apache.ranger.entity.XXResourceDef in project ranger by apache.

the class ServiceDBStore method deleteXXResourceDef.

public void deleteXXResourceDef(XXResourceDef xRes) {
    List<XXResourceDef> xChildObjs = daoMgr.getXXResourceDef().findByParentResId(xRes.getId());
    for (XXResourceDef childRes : xChildObjs) {
        deleteXXResourceDef(childRes);
    }
    List<XXPolicyResource> xxResources = daoMgr.getXXPolicyResource().findByResDefId(xRes.getId());
    for (XXPolicyResource xPolRes : xxResources) {
        deleteXXPolicyResource(xPolRes);
    }
    daoMgr.getXXResourceDef().remove(xRes);
}
Also used : XXPolicyResource(org.apache.ranger.entity.XXPolicyResource) XXResourceDef(org.apache.ranger.entity.XXResourceDef)

Example 3 with XXResourceDef

use of org.apache.ranger.entity.XXResourceDef in project ranger by apache.

the class TagDBStore method createResourceForServiceResource.

private void createResourceForServiceResource(Long resourceId, RangerServiceResource serviceResource) {
    String serviceName = serviceResource.getServiceName();
    XXService xService = daoManager.getXXService().findByName(serviceName);
    if (xService == null) {
        throw errorUtil.createRESTException("No Service found with name: " + serviceName, MessageEnums.ERROR_CREATING_OBJECT);
    }
    XXServiceDef xServiceDef = daoManager.getXXServiceDef().getById(xService.getType());
    if (xServiceDef == null) {
        throw errorUtil.createRESTException("No Service-Def found with ID: " + xService.getType(), MessageEnums.ERROR_CREATING_OBJECT);
    }
    Map<String, RangerPolicy.RangerPolicyResource> resElements = serviceResource.getResourceElements();
    for (Map.Entry<String, RangerPolicyResource> resElement : resElements.entrySet()) {
        XXResourceDef xResDef = daoManager.getXXResourceDef().findByNameAndServiceDefId(resElement.getKey(), xServiceDef.getId());
        if (xResDef == null) {
            LOG.error("TagDBStore.createResource: ResourceType is not valid [" + resElement.getKey() + "]");
            throw errorUtil.createRESTException("Resource Type is not valid [" + resElement.getKey() + "]", MessageEnums.DATA_NOT_FOUND);
        }
        RangerPolicyResource policyRes = resElement.getValue();
        XXServiceResourceElement resourceElement = new XXServiceResourceElement();
        resourceElement.setIsExcludes(policyRes.getIsExcludes());
        resourceElement.setIsRecursive(policyRes.getIsRecursive());
        resourceElement.setResDefId(xResDef.getId());
        resourceElement.setResourceId(resourceId);
        resourceElement = rangerAuditFields.populateAuditFieldsForCreate(resourceElement);
        resourceElement = daoManager.getXXServiceResourceElement().create(resourceElement);
        int sortOrder = 1;
        for (String resVal : policyRes.getValues()) {
            XXServiceResourceElementValue resourceElementValue = new XXServiceResourceElementValue();
            resourceElementValue.setResElementId(resourceElement.getId());
            resourceElementValue.setValue(resVal);
            resourceElementValue.setSortOrder(sortOrder);
            resourceElementValue = rangerAuditFields.populateAuditFieldsForCreate(resourceElementValue);
            resourceElementValue = daoManager.getXXServiceResourceElementValue().create(resourceElementValue);
            sortOrder++;
        }
    }
}
Also used : XXServiceResourceElementValue(org.apache.ranger.entity.XXServiceResourceElementValue) XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) XXService(org.apache.ranger.entity.XXService) HashMap(java.util.HashMap) XXTagResourceMap(org.apache.ranger.entity.XXTagResourceMap) Map(java.util.Map) XXResourceDef(org.apache.ranger.entity.XXResourceDef) XXServiceResourceElement(org.apache.ranger.entity.XXServiceResourceElement)

Example 4 with XXResourceDef

use of org.apache.ranger.entity.XXResourceDef in project ranger by apache.

the class TestRangerServiceDefServiceBase method test5populateRangerResourceDefToXX.

@Test
public void test5populateRangerResourceDefToXX() {
    RangerResourceDef rangerResourceDefObj = new RangerResourceDef();
    rangerResourceDefObj.setDescription("HDFS Repository");
    rangerResourceDefObj.setExcludesSupported(false);
    rangerResourceDefObj.setLabel("HDFS Repository");
    rangerResourceDefObj.setName("HDFs");
    XXResourceDef resourceDefObj = new XXResourceDef();
    resourceDefObj.setAddedByUserId(Id);
    resourceDefObj.setCreateTime(new Date());
    resourceDefObj.setDefid(Id);
    resourceDefObj.setDescription("HDFS Repository");
    resourceDefObj.setId(Id);
    XXServiceDef serviceDefObj = new XXServiceDef();
    serviceDefObj.setAddedByUserId(Id);
    serviceDefObj.setCreateTime(new Date());
    serviceDefObj.setDescription("HDFS Repository");
    serviceDefObj.setGuid("1427365526516_835_0");
    serviceDefObj.setId(Id);
    Mockito.when((XXResourceDef) rangerAuditFields.populateAuditFields(resourceDefObj, serviceDefObj)).thenReturn(resourceDefObj);
    XXResourceDef dbResourceDef = rangerServiceDefService.populateRangerResourceDefToXX(rangerResourceDefObj, resourceDefObj, serviceDefObj, 1);
    Assert.assertNotNull(dbResourceDef);
    Assert.assertEquals(dbResourceDef, resourceDefObj);
    Assert.assertEquals(dbResourceDef.getId(), resourceDefObj.getId());
    Assert.assertEquals(dbResourceDef.getLabel(), resourceDefObj.getLabel());
    Assert.assertEquals(dbResourceDef.getName(), resourceDefObj.getName());
    Assert.assertEquals(dbResourceDef.getDescription(), resourceDefObj.getDescription());
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXResourceDef(org.apache.ranger.entity.XXResourceDef) Date(java.util.Date) RangerResourceDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef) Test(org.junit.Test)

Example 5 with XXResourceDef

use of org.apache.ranger.entity.XXResourceDef in project ranger by apache.

the class ServiceDBStore method createNewResourcesForPolicy.

private void createNewResourcesForPolicy(RangerPolicy policy, XXPolicy xPolicy, Map<String, RangerPolicyResource> resources) throws Exception {
    for (Entry<String, RangerPolicyResource> resource : resources.entrySet()) {
        RangerPolicyResource policyRes = resource.getValue();
        XXResourceDef xResDef = daoMgr.getXXResourceDef().findByNameAndPolicyId(resource.getKey(), policy.getId());
        if (xResDef == null) {
            throw new Exception(resource.getKey() + ": is not a valid resource-type. policy='" + policy.getName() + "' service='" + policy.getService() + "'");
        }
        XXPolicyResource xPolRes = new XXPolicyResource();
        xPolRes = rangerAuditFields.populateAuditFields(xPolRes, xPolicy);
        xPolRes.setIsExcludes(policyRes.getIsExcludes());
        xPolRes.setIsRecursive(policyRes.getIsRecursive());
        xPolRes.setPolicyId(policy.getId());
        xPolRes.setResDefId(xResDef.getId());
        xPolRes = daoMgr.getXXPolicyResource().create(xPolRes);
        List<String> values = policyRes.getValues();
        if (CollectionUtils.isNotEmpty(values)) {
            Set<String> uniqueValues = new LinkedHashSet<String>(values);
            int i = 0;
            if (CollectionUtils.isNotEmpty(uniqueValues)) {
                for (String uniqValue : uniqueValues) {
                    if (!StringUtils.isEmpty(uniqValue)) {
                        XXPolicyResourceMap xPolResMap = new XXPolicyResourceMap();
                        xPolResMap = (XXPolicyResourceMap) rangerAuditFields.populateAuditFields(xPolResMap, xPolRes);
                        xPolResMap.setResourceId(xPolRes.getId());
                        xPolResMap.setValue(uniqValue);
                        xPolResMap.setOrder(i);
                        xPolResMap = daoMgr.getXXPolicyResourceMap().create(xPolResMap);
                        i++;
                    }
                }
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XXPolicyResource(org.apache.ranger.entity.XXPolicyResource) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) XXPolicyResourceMap(org.apache.ranger.entity.XXPolicyResourceMap) VXString(org.apache.ranger.view.VXString) XXResourceDef(org.apache.ranger.entity.XXResourceDef) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException)

Aggregations

XXResourceDef (org.apache.ranger.entity.XXResourceDef)12 RangerResourceDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef)5 XXResourceDefDao (org.apache.ranger.db.XXResourceDefDao)4 XXAccessTypeDef (org.apache.ranger.entity.XXAccessTypeDef)4 XXPolicyResource (org.apache.ranger.entity.XXPolicyResource)4 XXAccessTypeDefDao (org.apache.ranger.db.XXAccessTypeDefDao)3 XXContextEnricherDefDao (org.apache.ranger.db.XXContextEnricherDefDao)3 XXDataMaskTypeDefDao (org.apache.ranger.db.XXDataMaskTypeDefDao)3 XXEnumDefDao (org.apache.ranger.db.XXEnumDefDao)3 XXPolicyConditionDefDao (org.apache.ranger.db.XXPolicyConditionDefDao)3 XXServiceConfigDefDao (org.apache.ranger.db.XXServiceConfigDefDao)3 XXContextEnricherDef (org.apache.ranger.entity.XXContextEnricherDef)3 XXDataMaskTypeDef (org.apache.ranger.entity.XXDataMaskTypeDef)3 XXEnumDef (org.apache.ranger.entity.XXEnumDef)3 XXEnumElementDef (org.apache.ranger.entity.XXEnumElementDef)3 XXPolicyConditionDef (org.apache.ranger.entity.XXPolicyConditionDef)3 XXServiceConfigDef (org.apache.ranger.entity.XXServiceConfigDef)3 RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)3 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)3 VXString (org.apache.ranger.view.VXString)3