Search in sources :

Example 16 with RangerPolicyConditionDef

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

the class ServiceDBStore method updateChildObjectsOfServiceDef.

private void updateChildObjectsOfServiceDef(XXServiceDef createdSvcDef, List<RangerServiceConfigDef> configs, List<RangerResourceDef> resources, List<RangerAccessTypeDef> accessTypes, List<RangerPolicyConditionDef> policyConditions, List<RangerContextEnricherDef> contextEnrichers, List<RangerEnumDef> enums, RangerDataMaskDef dataMaskDef, RangerRowFilterDef rowFilterDef) {
    Long serviceDefId = createdSvcDef.getId();
    List<XXServiceConfigDef> xxConfigs = daoMgr.getXXServiceConfigDef().findByServiceDefId(serviceDefId);
    List<XXResourceDef> xxResources = daoMgr.getXXResourceDef().findByServiceDefId(serviceDefId);
    List<XXAccessTypeDef> xxAccessTypes = daoMgr.getXXAccessTypeDef().findByServiceDefId(serviceDefId);
    List<XXPolicyConditionDef> xxPolicyConditions = daoMgr.getXXPolicyConditionDef().findByServiceDefId(serviceDefId);
    List<XXContextEnricherDef> xxContextEnrichers = daoMgr.getXXContextEnricherDef().findByServiceDefId(serviceDefId);
    List<XXEnumDef> xxEnums = daoMgr.getXXEnumDef().findByServiceDefId(serviceDefId);
    XXServiceConfigDefDao xxServiceConfigDao = daoMgr.getXXServiceConfigDef();
    for (int i = 0; i < configs.size(); i++) {
        RangerServiceConfigDef config = configs.get(i);
        boolean found = false;
        for (XXServiceConfigDef xConfig : xxConfigs) {
            if (config.getItemId() != null && config.getItemId().equals(xConfig.getItemId())) {
                found = true;
                xConfig = serviceDefService.populateRangerServiceConfigDefToXX(config, xConfig, createdSvcDef, RangerServiceDefService.OPERATION_UPDATE_CONTEXT);
                xConfig.setOrder(i);
                xConfig = xxServiceConfigDao.update(xConfig);
                config = serviceDefService.populateXXToRangerServiceConfigDef(xConfig);
                break;
            }
        }
        if (!found) {
            XXServiceConfigDef xConfig = new XXServiceConfigDef();
            xConfig = serviceDefService.populateRangerServiceConfigDefToXX(config, xConfig, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
            xConfig.setOrder(i);
            xConfig = xxServiceConfigDao.create(xConfig);
            config = serviceDefService.populateXXToRangerServiceConfigDef(xConfig);
        }
    }
    for (XXServiceConfigDef xConfig : xxConfigs) {
        boolean found = false;
        for (RangerServiceConfigDef config : configs) {
            if (xConfig.getItemId() != null && xConfig.getItemId().equals(config.getItemId())) {
                found = true;
                break;
            }
        }
        if (!found) {
            xxServiceConfigDao.remove(xConfig);
        }
    }
    XXResourceDefDao xxResDefDao = daoMgr.getXXResourceDef();
    for (RangerResourceDef resource : resources) {
        boolean found = false;
        for (XXResourceDef xRes : xxResources) {
            if (resource.getItemId() != null && resource.getItemId().equals(xRes.getItemId())) {
                found = true;
                xRes = serviceDefService.populateRangerResourceDefToXX(resource, xRes, createdSvcDef, RangerServiceDefService.OPERATION_UPDATE_CONTEXT);
                xxResDefDao.update(xRes);
                resource = serviceDefService.populateXXToRangerResourceDef(xRes);
                break;
            }
        }
        if (!found) {
            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.setParent(parentId);
            xResource = xxResDefDao.create(xResource);
        }
    }
    for (XXResourceDef xRes : xxResources) {
        boolean found = false;
        for (RangerResourceDef resource : resources) {
            if (xRes.getItemId() != null && xRes.getItemId().equals(resource.getItemId())) {
                found = true;
                break;
            }
        }
        if (!found) {
            List<XXPolicyResource> policyResList = daoMgr.getXXPolicyResource().findByResDefId(xRes.getId());
            if (!stringUtil.isEmpty(policyResList)) {
                throw restErrorUtil.createRESTException("Policy/Policies are referring to this resource: " + xRes.getName() + ". Please remove such references from policy before updating service-def.", MessageEnums.DATA_NOT_UPDATABLE);
            }
            deleteXXResourceDef(xRes);
        }
    }
    XXAccessTypeDefDao xxATDDao = daoMgr.getXXAccessTypeDef();
    for (int i = 0; i < accessTypes.size(); i++) {
        RangerAccessTypeDef access = accessTypes.get(i);
        boolean found = false;
        for (XXAccessTypeDef xAccess : xxAccessTypes) {
            if (access.getItemId() != null && access.getItemId().equals(xAccess.getItemId())) {
                found = true;
                xAccess = serviceDefService.populateRangerAccessTypeDefToXX(access, xAccess, createdSvcDef, RangerServiceDefService.OPERATION_UPDATE_CONTEXT);
                xAccess.setOrder(i);
                xAccess = xxATDDao.update(xAccess);
                Collection<String> impliedGrants = access.getImpliedGrants();
                XXAccessTypeDefGrantsDao xxATDGrantDao = daoMgr.getXXAccessTypeDefGrants();
                List<String> xxImpliedGrants = xxATDGrantDao.findImpliedGrantsByATDId(xAccess.getId());
                for (String impliedGrant : impliedGrants) {
                    boolean foundGrant = false;
                    for (String xImpliedGrant : xxImpliedGrants) {
                        if (StringUtils.equalsIgnoreCase(impliedGrant, xImpliedGrant)) {
                            foundGrant = true;
                            break;
                        }
                    }
                    if (!foundGrant) {
                        XXAccessTypeDefGrants xImpliedGrant = new XXAccessTypeDefGrants();
                        xImpliedGrant.setAtdId(xAccess.getId());
                        xImpliedGrant.setImpliedGrant(impliedGrant);
                        xImpliedGrant = xxATDGrantDao.create(xImpliedGrant);
                    }
                }
                for (String xImpliedGrant : xxImpliedGrants) {
                    boolean foundGrant = false;
                    for (String impliedGrant : impliedGrants) {
                        if (StringUtils.equalsIgnoreCase(xImpliedGrant, impliedGrant)) {
                            foundGrant = true;
                            break;
                        }
                    }
                    if (!foundGrant) {
                        XXAccessTypeDefGrants xATDGrant = xxATDGrantDao.findByNameAndATDId(xAccess.getId(), xImpliedGrant);
                        xxATDGrantDao.remove(xATDGrant);
                    }
                }
                access = serviceDefService.populateXXToRangerAccessTypeDef(xAccess);
                break;
            }
        }
        if (!found) {
            XXAccessTypeDef xAccessType = new XXAccessTypeDef();
            xAccessType = serviceDefService.populateRangerAccessTypeDefToXX(access, xAccessType, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
            xAccessType.setOrder(i);
            xAccessType = xxATDDao.create(xAccessType);
            Collection<String> impliedGrants = access.getImpliedGrants();
            XXAccessTypeDefGrantsDao xxATDGrantDao = daoMgr.getXXAccessTypeDefGrants();
            for (String impliedGrant : impliedGrants) {
                XXAccessTypeDefGrants xImpliedGrant = new XXAccessTypeDefGrants();
                xImpliedGrant.setAtdId(xAccessType.getId());
                xImpliedGrant.setImpliedGrant(impliedGrant);
                xImpliedGrant = xxATDGrantDao.create(xImpliedGrant);
            }
            access = serviceDefService.populateXXToRangerAccessTypeDef(xAccessType);
        }
    }
    for (XXAccessTypeDef xAccess : xxAccessTypes) {
        boolean found = false;
        for (RangerAccessTypeDef access : accessTypes) {
            if (xAccess.getItemId() != null && xAccess.getItemId().equals(access.getItemId())) {
                found = true;
                break;
            }
        }
        if (!found) {
            List<XXPolicyItemAccess> polItemAccessList = daoMgr.getXXPolicyItemAccess().findByType(xAccess.getId());
            if (!stringUtil.isEmpty(polItemAccessList)) {
                throw restErrorUtil.createRESTException("Policy/Policies are referring to this access-type: " + xAccess.getName() + ". Please remove such references from policy before updating service-def.", MessageEnums.DATA_NOT_UPDATABLE);
            }
            deleteXXAccessTypeDef(xAccess);
        }
    }
    XXPolicyConditionDefDao xxPolCondDao = daoMgr.getXXPolicyConditionDef();
    for (int i = 0; i < policyConditions.size(); i++) {
        RangerPolicyConditionDef condition = policyConditions.get(i);
        boolean found = false;
        for (XXPolicyConditionDef xCondition : xxPolicyConditions) {
            if (condition.getItemId() != null && condition.getItemId().equals(xCondition.getItemId())) {
                found = true;
                xCondition = serviceDefService.populateRangerPolicyConditionDefToXX(condition, xCondition, createdSvcDef, RangerServiceDefService.OPERATION_UPDATE_CONTEXT);
                xCondition.setOrder(i);
                xCondition = xxPolCondDao.update(xCondition);
                condition = serviceDefService.populateXXToRangerPolicyConditionDef(xCondition);
                break;
            }
        }
        if (!found) {
            XXPolicyConditionDef xCondition = new XXPolicyConditionDef();
            xCondition = serviceDefService.populateRangerPolicyConditionDefToXX(condition, xCondition, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
            xCondition.setOrder(i);
            xCondition = xxPolCondDao.create(xCondition);
            condition = serviceDefService.populateXXToRangerPolicyConditionDef(xCondition);
        }
    }
    for (XXPolicyConditionDef xCondition : xxPolicyConditions) {
        boolean found = false;
        for (RangerPolicyConditionDef condition : policyConditions) {
            if (xCondition.getItemId() != null && xCondition.getItemId().equals(condition.getItemId())) {
                found = true;
                break;
            }
        }
        if (!found) {
            List<XXPolicyItemCondition> policyItemCondList = daoMgr.getXXPolicyItemCondition().findByPolicyConditionDefId(xCondition.getId());
            if (!stringUtil.isEmpty(policyItemCondList)) {
                throw restErrorUtil.createRESTException("Policy/Policies are referring to this policy-condition: " + xCondition.getName() + ". Please remove such references from policy before updating service-def.", MessageEnums.DATA_NOT_UPDATABLE);
            }
            for (XXPolicyItemCondition policyItemCond : policyItemCondList) {
                daoMgr.getXXPolicyItemCondition().remove(policyItemCond);
            }
            xxPolCondDao.remove(xCondition);
        }
    }
    XXContextEnricherDefDao xxContextEnricherDao = daoMgr.getXXContextEnricherDef();
    for (int i = 0; i < contextEnrichers.size(); i++) {
        RangerContextEnricherDef context = contextEnrichers.get(i);
        boolean found = false;
        for (XXContextEnricherDef xContext : xxContextEnrichers) {
            if (context.getItemId() != null && context.getItemId().equals(xContext.getItemId())) {
                found = true;
                xContext = serviceDefService.populateRangerContextEnricherDefToXX(context, xContext, createdSvcDef, RangerServiceDefService.OPERATION_UPDATE_CONTEXT);
                xContext.setOrder(i);
                xContext = xxContextEnricherDao.update(xContext);
                context = serviceDefService.populateXXToRangerContextEnricherDef(xContext);
                break;
            }
        }
        if (!found) {
            XXContextEnricherDef xContext = new XXContextEnricherDef();
            xContext = serviceDefService.populateRangerContextEnricherDefToXX(context, xContext, createdSvcDef, RangerServiceDefService.OPERATION_UPDATE_CONTEXT);
            xContext.setOrder(i);
            xContext = xxContextEnricherDao.create(xContext);
            context = serviceDefService.populateXXToRangerContextEnricherDef(xContext);
        }
    }
    for (XXContextEnricherDef xContext : xxContextEnrichers) {
        boolean found = false;
        for (RangerContextEnricherDef context : contextEnrichers) {
            if (xContext.getItemId() != null && xContext.getItemId().equals(context.getItemId())) {
                found = true;
                break;
            }
        }
        if (!found) {
            daoMgr.getXXContextEnricherDef().remove(xContext);
        }
    }
    XXEnumDefDao xxEnumDefDao = daoMgr.getXXEnumDef();
    for (RangerEnumDef enumDef : enums) {
        boolean found = false;
        for (XXEnumDef xEnumDef : xxEnums) {
            if (enumDef.getItemId() != null && enumDef.getItemId().equals(xEnumDef.getItemId())) {
                found = true;
                xEnumDef = serviceDefService.populateRangerEnumDefToXX(enumDef, xEnumDef, createdSvcDef, RangerServiceDefService.OPERATION_UPDATE_CONTEXT);
                xEnumDef = xxEnumDefDao.update(xEnumDef);
                XXEnumElementDefDao xEnumEleDao = daoMgr.getXXEnumElementDef();
                List<XXEnumElementDef> xxEnumEleDefs = xEnumEleDao.findByEnumDefId(xEnumDef.getId());
                List<RangerEnumElementDef> enumEleDefs = enumDef.getElements();
                for (int i = 0; i < enumEleDefs.size(); i++) {
                    RangerEnumElementDef eleDef = enumEleDefs.get(i);
                    boolean foundEle = false;
                    for (XXEnumElementDef xEleDef : xxEnumEleDefs) {
                        if (eleDef.getItemId() != null && eleDef.getItemId().equals(xEleDef.getItemId())) {
                            foundEle = true;
                            xEleDef = serviceDefService.populateRangerEnumElementDefToXX(eleDef, xEleDef, xEnumDef, RangerServiceDefService.OPERATION_UPDATE_CONTEXT);
                            xEleDef.setOrder(i);
                            xEleDef = xEnumEleDao.update(xEleDef);
                            break;
                        }
                    }
                    if (!foundEle) {
                        XXEnumElementDef xElement = new XXEnumElementDef();
                        xElement = serviceDefService.populateRangerEnumElementDefToXX(eleDef, xElement, xEnumDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
                        xElement.setOrder(i);
                        xElement = xEnumEleDao.create(xElement);
                    }
                }
                for (XXEnumElementDef xxEleDef : xxEnumEleDefs) {
                    boolean foundEle = false;
                    for (RangerEnumElementDef enumEle : enumEleDefs) {
                        if (xxEleDef.getItemId() != null && xxEleDef.getItemId().equals(enumEle.getItemId())) {
                            foundEle = true;
                            break;
                        }
                    }
                    if (!foundEle) {
                        xEnumEleDao.remove(xxEleDef);
                    }
                }
                enumDef = serviceDefService.populateXXToRangerEnumDef(xEnumDef);
                break;
            }
        }
        if (!found) {
            XXEnumDef xEnum = new XXEnumDef();
            xEnum = serviceDefService.populateRangerEnumDefToXX(enumDef, xEnum, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
            xEnum = xxEnumDefDao.create(xEnum);
            List<RangerEnumElementDef> elements = enumDef.getElements();
            XXEnumElementDefDao xxEnumEleDefDao = daoMgr.getXXEnumElementDef();
            for (RangerEnumElementDef element : elements) {
                XXEnumElementDef xElement = new XXEnumElementDef();
                xElement = serviceDefService.populateRangerEnumElementDefToXX(element, xElement, xEnum, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
                xElement = xxEnumEleDefDao.create(xElement);
            }
            enumDef = serviceDefService.populateXXToRangerEnumDef(xEnum);
        }
    }
    for (XXEnumDef xEnumDef : xxEnums) {
        boolean found = false;
        for (RangerEnumDef enumDef : enums) {
            if (xEnumDef.getItemId() != null && xEnumDef.getItemId().equals(enumDef.getItemId())) {
                found = true;
                break;
            }
        }
        if (!found) {
            List<XXEnumElementDef> enumEleDefList = daoMgr.getXXEnumElementDef().findByEnumDefId(xEnumDef.getId());
            for (XXEnumElementDef eleDef : enumEleDefList) {
                daoMgr.getXXEnumElementDef().remove(eleDef);
            }
            xxEnumDefDao.remove(xEnumDef);
        }
    }
    List<RangerDataMaskTypeDef> dataMasks = 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();
    XXDataMaskTypeDefDao dataMaskTypeDao = daoMgr.getXXDataMaskTypeDef();
    List<XXDataMaskTypeDef> xxDataMaskTypes = dataMaskTypeDao.findByServiceDefId(serviceDefId);
    List<XXAccessTypeDef> xxAccessTypeDefs = xxATDDao.findByServiceDefId(serviceDefId);
    List<XXResourceDef> xxResourceDefs = xxResDefDao.findByServiceDefId(serviceDefId);
    // create or update dataMasks
    for (int i = 0; i < dataMasks.size(); i++) {
        RangerDataMaskTypeDef dataMask = dataMasks.get(i);
        boolean found = false;
        for (XXDataMaskTypeDef xxDataMask : xxDataMaskTypes) {
            if (xxDataMask.getItemId() != null && xxDataMask.getItemId().equals(dataMask.getItemId())) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Updating existing dataMask with itemId=" + dataMask.getItemId());
                }
                found = true;
                xxDataMask = serviceDefService.populateRangerDataMaskDefToXX(dataMask, xxDataMask, createdSvcDef, RangerServiceDefService.OPERATION_UPDATE_CONTEXT);
                xxDataMask.setOrder(i);
                xxDataMask = dataMaskTypeDao.update(xxDataMask);
                dataMask = serviceDefService.populateXXToRangerDataMaskTypeDef(xxDataMask);
                break;
            }
        }
        if (!found) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Creating dataMask with itemId=" + dataMask.getItemId() + "");
            }
            XXDataMaskTypeDef xxDataMask = new XXDataMaskTypeDef();
            xxDataMask = serviceDefService.populateRangerDataMaskDefToXX(dataMask, xxDataMask, createdSvcDef, RangerServiceDefService.OPERATION_CREATE_CONTEXT);
            xxDataMask.setOrder(i);
            xxDataMask = dataMaskTypeDao.create(xxDataMask);
        }
    }
    // remove dataMasks
    for (XXDataMaskTypeDef xxDataMask : xxDataMaskTypes) {
        boolean found = false;
        for (RangerDataMaskTypeDef dataMask : dataMasks) {
            if (xxDataMask.getItemId() != null && xxDataMask.getItemId().equals(dataMask.getItemId())) {
                found = true;
                break;
            }
        }
        if (!found) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Deleting dataMask with itemId=" + xxDataMask.getItemId());
            }
            dataMaskTypeDao.remove(xxDataMask);
        }
    }
    for (RangerAccessTypeDef accessType : dataMaskAccessTypes) {
        if (!isAccessTypeInList(accessType.getName(), xxAccessTypeDefs)) {
            throw restErrorUtil.createRESTException("accessType with name: " + accessType.getName() + " does not exist", 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);
        }
    }
    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);
        }
    }
}
Also used : XXPolicyConditionDefDao(org.apache.ranger.db.XXPolicyConditionDefDao) VXString(org.apache.ranger.view.VXString) RangerPolicyConditionDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef) XXPolicyConditionDef(org.apache.ranger.entity.XXPolicyConditionDef) XXAccessTypeDef(org.apache.ranger.entity.XXAccessTypeDef) XXServiceConfigDef(org.apache.ranger.entity.XXServiceConfigDef) RangerResourceDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef) RangerServiceConfigDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerServiceConfigDef) RangerDataMaskTypeDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerDataMaskTypeDef) XXAccessTypeDefGrantsDao(org.apache.ranger.db.XXAccessTypeDefGrantsDao) XXEnumElementDef(org.apache.ranger.entity.XXEnumElementDef) XXPolicyResource(org.apache.ranger.entity.XXPolicyResource) XXDataMaskTypeDefDao(org.apache.ranger.db.XXDataMaskTypeDefDao) RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef) XXEnumElementDefDao(org.apache.ranger.db.XXEnumElementDefDao) XXAccessTypeDefDao(org.apache.ranger.db.XXAccessTypeDefDao) XXPolicyItemCondition(org.apache.ranger.entity.XXPolicyItemCondition) XXAccessTypeDefGrants(org.apache.ranger.entity.XXAccessTypeDefGrants) XXEnumDefDao(org.apache.ranger.db.XXEnumDefDao) XXDataMaskTypeDef(org.apache.ranger.entity.XXDataMaskTypeDef) XXResourceDefDao(org.apache.ranger.db.XXResourceDefDao) RangerEnumElementDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumElementDef) XXResourceDef(org.apache.ranger.entity.XXResourceDef) RangerAccessTypeDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef) RangerContextEnricherDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerContextEnricherDef) XXPolicyItemAccess(org.apache.ranger.entity.XXPolicyItemAccess) XXEnumDef(org.apache.ranger.entity.XXEnumDef) XXServiceConfigDefDao(org.apache.ranger.db.XXServiceConfigDefDao) XXContextEnricherDef(org.apache.ranger.entity.XXContextEnricherDef) XXContextEnricherDefDao(org.apache.ranger.db.XXContextEnricherDefDao)

Example 17 with RangerPolicyConditionDef

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

the class RangerSampleSimpleMatcherTest method test_firewallings.

@Test
public void test_firewallings() {
    // create a request for some policyValue, say, country and use it to match against matcher initialized with all sorts of bad data
    RangerAccessRequest request = createRequest("AB");
    RangerSampleSimpleMatcher matcher = new RangerSampleSimpleMatcher();
    // Matcher initialized with null policy should behave sensibly!  It matches everything!
    matcher.setConditionDef(null);
    matcher.setPolicyItemCondition(null);
    matcher.init();
    Assert.assertTrue(matcher.isMatched(request));
    RangerPolicyItemCondition policyItemCondition = Mockito.mock(RangerPolicyItemCondition.class);
    matcher.setConditionDef(null);
    matcher.setPolicyItemCondition(policyItemCondition);
    matcher.init();
    Assert.assertTrue(matcher.isMatched(request));
    RangerPolicyConditionDef conditionDef = Mockito.mock(RangerPolicyConditionDef.class);
    matcher.setConditionDef(conditionDef);
    matcher.setPolicyItemCondition(null);
    matcher.init();
    Assert.assertTrue(matcher.isMatched(request));
    // so should a policy item condition with initialized with null list of values
    Mockito.when(policyItemCondition.getValues()).thenReturn(null);
    matcher.setConditionDef(conditionDef);
    matcher.setPolicyItemCondition(policyItemCondition);
    matcher.init();
    Assert.assertTrue(matcher.isMatched(request));
    // not null item condition with empty condition list
    List<String> values = new ArrayList<String>();
    Mockito.when(policyItemCondition.getValues()).thenReturn(values);
    matcher.setConditionDef(conditionDef);
    matcher.setPolicyItemCondition(policyItemCondition);
    matcher.init();
    Assert.assertTrue(matcher.isMatched(request));
    // values as sensible items in it, however, the conditionDef has null evaluator option, so that too suppresses any check
    values.add("AB");
    Mockito.when(policyItemCondition.getValues()).thenReturn(values);
    Mockito.when(conditionDef.getEvaluatorOptions()).thenReturn(null);
    matcher.setConditionDef(conditionDef);
    matcher.setPolicyItemCondition(policyItemCondition);
    matcher.init();
    Assert.assertTrue(matcher.isMatched(request));
    // If evaluator option on the condition def is non-null then it starts to evaluate for real
    Mockito.when(conditionDef.getEvaluatorOptions()).thenReturn(_conditionOptions);
    matcher.setConditionDef(conditionDef);
    matcher.setPolicyItemCondition(policyItemCondition);
    matcher.init();
    Assert.assertTrue(matcher.isMatched(request));
}
Also used : RangerPolicyItemCondition(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition) RangerPolicyConditionDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef) RangerAccessRequest(org.apache.ranger.plugin.policyengine.RangerAccessRequest) Test(org.junit.Test)

Example 18 with RangerPolicyConditionDef

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

the class RangerServiceDefServiceBase method populateViewBean.

@Override
protected V populateViewBean(T xServiceDef) {
    V serviceDef = super.populateViewBean((T) xServiceDef);
    Long serviceDefId = xServiceDef.getId();
    List<XXServiceConfigDef> xConfigs = daoMgr.getXXServiceConfigDef().findByServiceDefId(serviceDefId);
    if (!stringUtil.isEmpty(xConfigs)) {
        List<RangerServiceConfigDef> configs = new ArrayList<RangerServiceConfigDef>();
        for (XXServiceConfigDef xConfig : xConfigs) {
            RangerServiceConfigDef config = populateXXToRangerServiceConfigDef(xConfig);
            configs.add(config);
        }
        serviceDef.setConfigs(configs);
    }
    List<XXResourceDef> xResources = daoMgr.getXXResourceDef().findByServiceDefId(serviceDefId);
    if (!stringUtil.isEmpty(xResources)) {
        List<RangerResourceDef> resources = new ArrayList<RangerResourceDef>();
        for (XXResourceDef xResource : xResources) {
            RangerResourceDef resource = populateXXToRangerResourceDef(xResource);
            resources.add(resource);
        }
        serviceDef.setResources(resources);
    }
    List<XXAccessTypeDef> xAccessTypes = daoMgr.getXXAccessTypeDef().findByServiceDefId(serviceDefId);
    if (!stringUtil.isEmpty(xAccessTypes)) {
        List<RangerAccessTypeDef> accessTypes = new ArrayList<RangerAccessTypeDef>();
        for (XXAccessTypeDef xAtd : xAccessTypes) {
            RangerAccessTypeDef accessType = populateXXToRangerAccessTypeDef(xAtd);
            accessTypes.add(accessType);
        }
        serviceDef.setAccessTypes(accessTypes);
    }
    List<XXPolicyConditionDef> xPolicyConditions = daoMgr.getXXPolicyConditionDef().findByServiceDefId(serviceDefId);
    if (!stringUtil.isEmpty(xPolicyConditions)) {
        List<RangerPolicyConditionDef> policyConditions = new ArrayList<RangerPolicyConditionDef>();
        for (XXPolicyConditionDef xPolicyCondDef : xPolicyConditions) {
            RangerPolicyConditionDef policyCondition = populateXXToRangerPolicyConditionDef(xPolicyCondDef);
            policyConditions.add(policyCondition);
        }
        serviceDef.setPolicyConditions(policyConditions);
    }
    List<XXContextEnricherDef> xContextEnrichers = daoMgr.getXXContextEnricherDef().findByServiceDefId(serviceDefId);
    if (!stringUtil.isEmpty(xContextEnrichers)) {
        List<RangerContextEnricherDef> contextEnrichers = new ArrayList<RangerContextEnricherDef>();
        for (XXContextEnricherDef xContextEnricherDef : xContextEnrichers) {
            RangerContextEnricherDef contextEnricher = populateXXToRangerContextEnricherDef(xContextEnricherDef);
            contextEnrichers.add(contextEnricher);
        }
        serviceDef.setContextEnrichers(contextEnrichers);
    }
    List<XXEnumDef> xEnumList = daoMgr.getXXEnumDef().findByServiceDefId(serviceDefId);
    if (!stringUtil.isEmpty(xEnumList)) {
        List<RangerEnumDef> enums = new ArrayList<RangerEnumDef>();
        for (XXEnumDef xEnum : xEnumList) {
            RangerEnumDef vEnum = populateXXToRangerEnumDef(xEnum);
            enums.add(vEnum);
        }
        serviceDef.setEnums(enums);
    }
    RangerDataMaskDef dataMaskDef = new RangerDataMaskDef();
    RangerRowFilterDef rowFilterDef = new RangerRowFilterDef();
    List<XXDataMaskTypeDef> xDataMaskTypes = daoMgr.getXXDataMaskTypeDef().findByServiceDefId(serviceDefId);
    if (!stringUtil.isEmpty(xDataMaskTypes)) {
        List<RangerDataMaskTypeDef> dataMaskTypes = new ArrayList<RangerDataMaskTypeDef>();
        for (XXDataMaskTypeDef xDataMaskType : xDataMaskTypes) {
            RangerDataMaskTypeDef dataMaskType = populateXXToRangerDataMaskTypeDef(xDataMaskType);
            dataMaskTypes.add(dataMaskType);
        }
        dataMaskDef.setMaskTypes(dataMaskTypes);
    }
    if (!stringUtil.isEmpty(xResources)) {
        for (XXResourceDef xResource : xResources) {
            if (StringUtils.isNotEmpty(xResource.getDataMaskOptions())) {
                RangerResourceDef dataMaskResource = jsonToObject(xResource.getDataMaskOptions(), RangerResourceDef.class);
                dataMaskDef.getResources().add(dataMaskResource);
            }
            if (StringUtils.isNotEmpty(xResource.getRowFilterOptions())) {
                RangerResourceDef resource = jsonToObject(xResource.getRowFilterOptions(), RangerResourceDef.class);
                rowFilterDef.getResources().add(resource);
            }
        }
    }
    if (!stringUtil.isEmpty(xAccessTypes)) {
        for (XXAccessTypeDef xAtd : xAccessTypes) {
            if (StringUtils.isNotEmpty(xAtd.getDataMaskOptions())) {
                RangerAccessTypeDef dataMaskAccessType = jsonToObject(xAtd.getDataMaskOptions(), RangerAccessTypeDef.class);
                dataMaskDef.getAccessTypes().add(dataMaskAccessType);
            }
            if (StringUtils.isNotEmpty(xAtd.getRowFilterOptions())) {
                RangerAccessTypeDef accessType = jsonToObject(xAtd.getRowFilterOptions(), RangerAccessTypeDef.class);
                rowFilterDef.getAccessTypes().add(accessType);
            }
        }
    }
    serviceDef.setDataMaskDef(dataMaskDef);
    serviceDef.setRowFilterDef(rowFilterDef);
    ServiceDefUtil.normalize(serviceDef);
    return serviceDef;
}
Also used : ArrayList(java.util.ArrayList) RangerDataMaskDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerDataMaskDef) RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef) RangerPolicyConditionDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef) RangerRowFilterDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerRowFilterDef) RangerResourceDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef) RangerServiceConfigDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerServiceConfigDef) RangerDataMaskTypeDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerDataMaskTypeDef) RangerAccessTypeDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef) RangerContextEnricherDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerContextEnricherDef)

Example 19 with RangerPolicyConditionDef

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

the class RangerDefaultPolicyEvaluatorTest method getMockPolicyConditionDef.

RangerPolicyConditionDef getMockPolicyConditionDef(String name, String evaluatorClassName, Map<String, String> evaluatorOptions) {
    // null policy condition def collection should behave sensibly
    RangerPolicyConditionDef aCondition = mock(RangerPolicyConditionDef.class);
    when(aCondition.getName()).thenReturn(name);
    when(aCondition.getEvaluator()).thenReturn(evaluatorClassName);
    when(aCondition.getEvaluatorOptions()).thenReturn(evaluatorOptions);
    return aCondition;
}
Also used : RangerPolicyConditionDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef)

Example 20 with RangerPolicyConditionDef

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

the class ValidationTestUtils method createPolicyConditionDefs.

List<RangerPolicyConditionDef> createPolicyConditionDefs(Object[][] input) {
    List<RangerPolicyConditionDef> result = new ArrayList<>();
    if (input != null) {
        for (Object[] row : input) {
            RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
            when(conditionDef.getItemId()).thenReturn((Long) row[0]);
            when(conditionDef.getName()).thenReturn((String) row[1]);
            when(conditionDef.getEvaluator()).thenReturn((String) row[2]);
            result.add(conditionDef);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) RangerPolicyConditionDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef)

Aggregations

RangerPolicyConditionDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef)22 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)10 RangerAccessTypeDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef)10 RangerContextEnricherDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerContextEnricherDef)10 RangerEnumDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef)10 RangerResourceDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef)10 RangerServiceConfigDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerServiceConfigDef)10 ArrayList (java.util.ArrayList)9 Date (java.util.Date)8 XXPolicyConditionDef (org.apache.ranger.entity.XXPolicyConditionDef)5 XXServiceDef (org.apache.ranger.entity.XXServiceDef)4 Test (org.junit.Test)4 RangerPolicyItemCondition (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition)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 HashSet (java.util.HashSet)2 XXAccessTypeDefDao (org.apache.ranger.db.XXAccessTypeDefDao)2 XXAccessTypeDefGrantsDao (org.apache.ranger.db.XXAccessTypeDefGrantsDao)2