Search in sources :

Example 6 with XXPolicyConditionDef

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

the class PatchForUpdatingPolicyJson_J10019 method addPolicyConditionDefRef.

private void addPolicyConditionDefRef(String serviceType, Long policyId, Set<String> conditions) throws Exception {
    logger.info("==> addPolicyConditionDefRef(id=" + policyId + ")");
    // insert policy-id, conditionName into Ref table
    Map<String, Long> serviceDefConditionNameIDMap = conditionNameIdMap.get(serviceType);
    if (serviceDefConditionNameIDMap == null) {
        serviceDefConditionNameIDMap = new HashMap<>();
        conditionNameIdMap.put(serviceType, serviceDefConditionNameIDMap);
        XXServiceDef dbServiceDef = daoMgr.getXXServiceDef().findByName(serviceType);
        for (XXPolicyConditionDef conditionDef : daoMgr.getXXPolicyConditionDef().findByServiceDefId(dbServiceDef.getId())) {
            serviceDefConditionNameIDMap.put(conditionDef.getName(), conditionDef.getId());
        }
    }
    XXPolicyRefConditionDao policyRefConditionDao = daoMgr.getXXPolicyRefCondition();
    for (String condition : conditions) {
        Long conditionDefId = serviceDefConditionNameIDMap.get(condition);
        if (conditionDefId == null) {
            throw new Exception(condition + ": unknown condition in policy [id=" + policyId + "; serviceType=" + serviceType + "]. Known conditions are: " + serviceDefConditionNameIDMap.keySet());
        }
        XXPolicyRefCondition policyRefCondition = new XXPolicyRefCondition();
        policyRefCondition.setPolicyId(policyId);
        policyRefCondition.setConditionName(condition);
        policyRefCondition.setConditionDefId(conditionDefId);
        policyRefConditionDao.create(policyRefCondition);
    }
    logger.info("<== addPolicyConditionDefRef(id=" + policyId + ")");
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXPolicyConditionDef(org.apache.ranger.entity.XXPolicyConditionDef) XXPolicyRefCondition(org.apache.ranger.entity.XXPolicyRefCondition) XXPolicyRefConditionDao(org.apache.ranger.db.XXPolicyRefConditionDao)

Example 7 with XXPolicyConditionDef

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

the class PatchMigration_J10002 method getPolicyItemListForRes.

private List<RangerPolicyItem> getPolicyItemListForRes(XXResource xRes, XXServiceDef svcDef) {
    List<RangerPolicyItem> policyItems = new ArrayList<RangerPolicyItem>();
    SearchCriteria sc = new SearchCriteria();
    sc.addParam("resourceId", xRes.getId());
    List<VXPermMap> permMapList = xPermMapService.searchXPermMaps(sc).getVXPermMaps();
    HashMap<String, List<VXPermMap>> sortedPermMap = new HashMap<String, List<VXPermMap>>();
    // re-group the list with permGroup as the key
    if (permMapList != null) {
        for (VXPermMap permMap : permMapList) {
            String permGrp = permMap.getPermGroup();
            List<VXPermMap> sortedList = sortedPermMap.get(permGrp);
            if (sortedList == null) {
                sortedList = new ArrayList<VXPermMap>();
                sortedPermMap.put(permGrp, sortedList);
            }
            sortedList.add(permMap);
        }
    }
    for (Entry<String, List<VXPermMap>> entry : sortedPermMap.entrySet()) {
        List<String> userList = new ArrayList<String>();
        List<String> groupList = new ArrayList<String>();
        List<RangerPolicyItemAccess> accessList = new ArrayList<RangerPolicyItemAccess>();
        String ipAddress = null;
        RangerPolicy.RangerPolicyItem policyItem = new RangerPolicy.RangerPolicyItem();
        for (VXPermMap permMap : entry.getValue()) {
            if (permMap.getPermFor() == AppConstants.XA_PERM_FOR_USER) {
                String userName = getUserName(permMap);
                if (!userList.contains(userName)) {
                    userList.add(userName);
                }
            } else if (permMap.getPermFor() == AppConstants.XA_PERM_FOR_GROUP) {
                String groupName = getGroupName(permMap);
                if (!groupList.contains(groupName)) {
                    groupList.add(groupName);
                }
            }
            String accessType = ServiceUtil.toAccessType(permMap.getPermType());
            if (StringUtils.isBlank(accessType) || unsupportedLegacyPermTypes.contains(accessType)) {
                logger.info(accessType + ": is not a valid access-type, ignoring accesstype for policy: " + xRes.getPolicyName());
                continue;
            }
            if (StringUtils.equalsIgnoreCase(accessType, "Admin")) {
                policyItem.setDelegateAdmin(Boolean.TRUE);
                if (svcDef.getId() == EmbeddedServiceDefsUtil.instance().getHBaseServiceDefId()) {
                    addAccessType(accessType, accessList);
                }
            } else {
                addAccessType(accessType, accessList);
            }
            ipAddress = permMap.getIpAddress();
        }
        if (CollectionUtils.isEmpty(accessList)) {
            logger.info("no access specified. ignoring policyItem for policy: " + xRes.getPolicyName());
            continue;
        }
        if (CollectionUtils.isEmpty(userList) && CollectionUtils.isEmpty(groupList)) {
            logger.info("no user or group specified. ignoring policyItem for policy: " + xRes.getPolicyName());
            continue;
        }
        policyItem.setUsers(userList);
        policyItem.setGroups(groupList);
        policyItem.setAccesses(accessList);
        if (ipAddress != null && !ipAddress.isEmpty()) {
            XXPolicyConditionDef policyCond = daoMgr.getXXPolicyConditionDef().findByServiceDefIdAndName(svcDef.getId(), "ip-range");
            if (policyCond != null) {
                RangerPolicy.RangerPolicyItemCondition ipCondition = new RangerPolicy.RangerPolicyItemCondition("ip-range", Collections.singletonList(ipAddress));
                policyItem.getConditions().add(ipCondition);
            }
        }
        policyItems.add(policyItem);
    }
    return policyItems;
}
Also used : VXPermMap(org.apache.ranger.view.VXPermMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem) SearchCriteria(org.apache.ranger.common.SearchCriteria) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) XXPolicyConditionDef(org.apache.ranger.entity.XXPolicyConditionDef) RangerPolicyItemAccess(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with XXPolicyConditionDef

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

the class PatchForMigratingOldRegimePolicyJson_J10046 method addPolicyConditionDefRef.

private void addPolicyConditionDefRef(String serviceType, Long policyId, Set<String> conditions) throws Exception {
    logger.info("==> addPolicyConditionDefRef(id=" + policyId + ")");
    // insert policy-id, conditionName into Ref table
    Map<String, Long> serviceDefConditionNameIDMap = conditionNameIdMap.get(serviceType);
    if (serviceDefConditionNameIDMap == null) {
        serviceDefConditionNameIDMap = new HashMap<>();
        conditionNameIdMap.put(serviceType, serviceDefConditionNameIDMap);
        XXServiceDef dbServiceDef = daoMgr.getXXServiceDef().findByName(serviceType);
        for (XXPolicyConditionDef conditionDef : daoMgr.getXXPolicyConditionDef().findByServiceDefId(dbServiceDef.getId())) {
            serviceDefConditionNameIDMap.put(conditionDef.getName(), conditionDef.getId());
        }
    }
    XXPolicyRefConditionDao policyRefConditionDao = daoMgr.getXXPolicyRefCondition();
    for (String condition : conditions) {
        Long conditionDefId = serviceDefConditionNameIDMap.get(condition);
        if (conditionDefId == null) {
            throw new Exception(condition + ": unknown condition in policy [id=" + policyId + "; serviceType=" + serviceType + "]. Known conditions are: " + serviceDefConditionNameIDMap.keySet());
        }
        XXPolicyRefCondition policyRefCondition = new XXPolicyRefCondition();
        policyRefCondition.setPolicyId(policyId);
        policyRefCondition.setConditionName(condition);
        policyRefCondition.setConditionDefId(conditionDefId);
        policyRefConditionDao.create(policyRefCondition);
    }
    logger.info("<== addPolicyConditionDefRef(id=" + policyId + ")");
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXPolicyConditionDef(org.apache.ranger.entity.XXPolicyConditionDef) XXPolicyRefCondition(org.apache.ranger.entity.XXPolicyRefCondition) XXPolicyRefConditionDao(org.apache.ranger.db.XXPolicyRefConditionDao)

Example 9 with XXPolicyConditionDef

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

the class PolicyRefUpdater method createNewPolMappingForRefTable.

public void createNewPolMappingForRefTable(RangerPolicy policy, XXPolicy xPolicy, XXServiceDef xServiceDef) throws Exception {
    if (policy == null) {
        return;
    }
    cleanupRefTables(policy);
    final Set<String> resourceNames = policy.getResources().keySet();
    final Set<String> roleNames = new HashSet<>();
    final Set<String> groupNames = new HashSet<>();
    final Set<String> userNames = new HashSet<>();
    final Set<String> accessTypes = new HashSet<>();
    final Set<String> conditionTypes = new HashSet<>();
    final Set<String> dataMaskTypes = new HashSet<>();
    boolean oldBulkMode = RangerBizUtil.isBulkMode();
    List<RangerPolicy.RangerPolicyItemCondition> rangerPolicyConditions = policy.getConditions();
    if (CollectionUtils.isNotEmpty(rangerPolicyConditions)) {
        for (RangerPolicy.RangerPolicyItemCondition condition : rangerPolicyConditions) {
            conditionTypes.add(condition.getType());
        }
    }
    for (List<? extends RangerPolicyItem> policyItems : getAllPolicyItems(policy)) {
        if (CollectionUtils.isEmpty(policyItems)) {
            continue;
        }
        for (RangerPolicyItem policyItem : policyItems) {
            roleNames.addAll(policyItem.getRoles());
            groupNames.addAll(policyItem.getGroups());
            userNames.addAll(policyItem.getUsers());
            if (CollectionUtils.isNotEmpty(policyItem.getAccesses())) {
                for (RangerPolicyItemAccess access : policyItem.getAccesses()) {
                    accessTypes.add(access.getType());
                }
            }
            if (CollectionUtils.isNotEmpty(policyItem.getConditions())) {
                for (RangerPolicyItemCondition condition : policyItem.getConditions()) {
                    conditionTypes.add(condition.getType());
                }
            }
            if (policyItem instanceof RangerDataMaskPolicyItem) {
                RangerPolicyItemDataMaskInfo dataMaskInfo = ((RangerDataMaskPolicyItem) policyItem).getDataMaskInfo();
                dataMaskTypes.add(dataMaskInfo.getDataMaskType());
            }
        }
    }
    List<XXPolicyRefResource> xPolResources = new ArrayList<>();
    for (String resource : resourceNames) {
        XXResourceDef xResDef = daoMgr.getXXResourceDef().findByNameAndPolicyId(resource, policy.getId());
        if (xResDef == null) {
            throw new Exception(resource + ": is not a valid resource-type. policy='" + policy.getName() + "' service='" + policy.getService() + "'");
        }
        XXPolicyRefResource xPolRes = rangerAuditFields.populateAuditFields(new XXPolicyRefResource(), xPolicy);
        xPolRes.setPolicyId(policy.getId());
        xPolRes.setResourceDefId(xResDef.getId());
        xPolRes.setResourceName(resource);
        xPolResources.add(xPolRes);
    }
    daoMgr.getXXPolicyRefResource().batchCreate(xPolResources);
    final boolean isAdmin = rangerBizUtil.checkAdminAccess();
    List<XXPolicyRefRole> xPolRoles = new ArrayList<>();
    for (String role : roleNames) {
        if (StringUtils.isBlank(role)) {
            continue;
        }
        PolicyPrincipalAssociator associator = new PolicyPrincipalAssociator(PRINCIPAL_TYPE.ROLE, role, xPolicy);
        if (!associator.doAssociate(false)) {
            if (isAdmin) {
                rangerTransactionSynchronizationAdapter.executeOnTransactionCommit(associator);
            } else {
                VXResponse gjResponse = new VXResponse();
                gjResponse.setStatusCode(HttpServletResponse.SC_BAD_REQUEST);
                gjResponse.setMsgDesc("Operation denied. Role name: " + role + " specified in policy does not exist in ranger admin.");
                throw restErrorUtil.generateRESTException(gjResponse);
            }
        }
    }
    RangerBizUtil.setBulkMode(oldBulkMode);
    daoMgr.getXXPolicyRefRole().batchCreate(xPolRoles);
    for (String group : groupNames) {
        if (StringUtils.isBlank(group)) {
            continue;
        }
        PolicyPrincipalAssociator associator = new PolicyPrincipalAssociator(PRINCIPAL_TYPE.GROUP, group, xPolicy);
        if (!associator.doAssociate(false)) {
            if (isAdmin) {
                rangerTransactionSynchronizationAdapter.executeOnTransactionCommit(associator);
            } else {
                VXResponse gjResponse = new VXResponse();
                gjResponse.setStatusCode(HttpServletResponse.SC_BAD_REQUEST);
                gjResponse.setMsgDesc("Operation denied. Group name: " + group + " specified in policy does not exist in ranger admin.");
                throw restErrorUtil.generateRESTException(gjResponse);
            }
        }
    }
    for (String user : userNames) {
        if (StringUtils.isBlank(user)) {
            continue;
        }
        PolicyPrincipalAssociator associator = new PolicyPrincipalAssociator(PRINCIPAL_TYPE.USER, user, xPolicy);
        if (!associator.doAssociate(false)) {
            if (isAdmin) {
                rangerTransactionSynchronizationAdapter.executeOnTransactionCommit(associator);
            } else {
                VXResponse gjResponse = new VXResponse();
                gjResponse.setStatusCode(HttpServletResponse.SC_BAD_REQUEST);
                gjResponse.setMsgDesc("Operation denied. User name: " + user + " specified in policy does not exist in ranger admin.");
                throw restErrorUtil.generateRESTException(gjResponse);
            }
        }
    }
    List<XXPolicyRefAccessType> xPolAccesses = new ArrayList<>();
    for (String accessType : accessTypes) {
        XXAccessTypeDef xAccTypeDef = daoMgr.getXXAccessTypeDef().findByNameAndServiceId(accessType, xPolicy.getService());
        if (xAccTypeDef == null) {
            throw new Exception(accessType + ": is not a valid access-type. policy='" + policy.getName() + "' service='" + policy.getService() + "'");
        }
        XXPolicyRefAccessType xPolAccess = rangerAuditFields.populateAuditFields(new XXPolicyRefAccessType(), xPolicy);
        xPolAccess.setPolicyId(policy.getId());
        xPolAccess.setAccessDefId(xAccTypeDef.getId());
        xPolAccess.setAccessTypeName(accessType);
        xPolAccesses.add(xPolAccess);
    }
    daoMgr.getXXPolicyRefAccessType().batchCreate(xPolAccesses);
    List<XXPolicyRefCondition> xPolConds = new ArrayList<>();
    for (String condition : conditionTypes) {
        XXPolicyConditionDef xPolCondDef = daoMgr.getXXPolicyConditionDef().findByServiceDefIdAndName(xServiceDef.getId(), condition);
        if (xPolCondDef == null) {
            throw new Exception(condition + ": is not a valid condition-type. policy='" + xPolicy.getName() + "' service='" + xPolicy.getService() + "'");
        }
        XXPolicyRefCondition xPolCond = rangerAuditFields.populateAuditFields(new XXPolicyRefCondition(), xPolicy);
        xPolCond.setPolicyId(policy.getId());
        xPolCond.setConditionDefId(xPolCondDef.getId());
        xPolCond.setConditionName(condition);
        xPolConds.add(xPolCond);
    }
    daoMgr.getXXPolicyRefCondition().batchCreate(xPolConds);
    List<XXPolicyRefDataMaskType> xxDataMaskInfos = new ArrayList<>();
    for (String dataMaskType : dataMaskTypes) {
        XXDataMaskTypeDef dataMaskDef = daoMgr.getXXDataMaskTypeDef().findByNameAndServiceId(dataMaskType, xPolicy.getService());
        if (dataMaskDef == null) {
            throw new Exception(dataMaskType + ": is not a valid datamask-type. policy='" + policy.getName() + "' service='" + policy.getService() + "'");
        }
        XXPolicyRefDataMaskType xxDataMaskInfo = new XXPolicyRefDataMaskType();
        xxDataMaskInfo.setPolicyId(policy.getId());
        xxDataMaskInfo.setDataMaskDefId(dataMaskDef.getId());
        xxDataMaskInfo.setDataMaskTypeName(dataMaskType);
        xxDataMaskInfos.add(xxDataMaskInfo);
    }
    daoMgr.getXXPolicyRefDataMaskType().batchCreate(xxDataMaskInfos);
}
Also used : ArrayList(java.util.ArrayList) XXPolicyRefRole(org.apache.ranger.entity.XXPolicyRefRole) RangerPolicyItemCondition(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) XXPolicyConditionDef(org.apache.ranger.entity.XXPolicyConditionDef) XXDataMaskTypeDef(org.apache.ranger.entity.XXDataMaskTypeDef) XXPolicyRefCondition(org.apache.ranger.entity.XXPolicyRefCondition) XXAccessTypeDef(org.apache.ranger.entity.XXAccessTypeDef) RangerPolicyItemDataMaskInfo(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemDataMaskInfo) HashSet(java.util.HashSet) VXResponse(org.apache.ranger.view.VXResponse) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem) XXResourceDef(org.apache.ranger.entity.XXResourceDef) XXPolicyRefAccessType(org.apache.ranger.entity.XXPolicyRefAccessType) XXPolicyRefDataMaskType(org.apache.ranger.entity.XXPolicyRefDataMaskType) XXPolicyRefResource(org.apache.ranger.entity.XXPolicyRefResource) RangerDataMaskPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerDataMaskPolicyItem) RangerPolicyItemAccess(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess) RangerPolicyItemCondition(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition)

Example 10 with XXPolicyConditionDef

use of org.apache.ranger.entity.XXPolicyConditionDef 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();
    if (CollectionUtils.isNotEmpty(resources)) {
        RangerServiceDefValidator validator = new RangerServiceDefValidator(this);
        List<ValidationFailureDetails> failures = new ArrayList<>();
        boolean isValidResources = validator.isValidResources(serviceDef, failures, RangerValidator.Action.CREATE);
        if (!isValidResources) {
            throw restErrorUtil.createRESTException("service-def with name: " + serviceDef.getName() + " has invalid resources:[" + failures.toString() + "]", MessageEnums.INVALID_INPUT_DATA);
        }
    }
    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;
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) ArrayList(java.util.ArrayList) RangerDataMaskDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerDataMaskDef) XXPolicyConditionDefDao(org.apache.ranger.db.XXPolicyConditionDefDao) RangerPolicyConditionDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef) VXString(org.apache.ranger.view.VXString) 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) RangerServiceDefHelper(org.apache.ranger.plugin.model.validation.RangerServiceDefHelper) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) XXDataMaskTypeDefDao(org.apache.ranger.db.XXDataMaskTypeDefDao) RangerEnumDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef) XXEnumElementDefDao(org.apache.ranger.db.XXEnumElementDefDao) RangerRowFilterDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerRowFilterDef) XXAccessTypeDefDao(org.apache.ranger.db.XXAccessTypeDefDao) XXAccessTypeDefGrants(org.apache.ranger.entity.XXAccessTypeDefGrants) XXEnumDefDao(org.apache.ranger.db.XXEnumDefDao) XXDataMaskTypeDef(org.apache.ranger.entity.XXDataMaskTypeDef) RangerServiceDefValidator(org.apache.ranger.plugin.model.validation.RangerServiceDefValidator) XXResourceDefDao(org.apache.ranger.db.XXResourceDefDao) RangerEnumElementDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumElementDef) XXResourceDef(org.apache.ranger.entity.XXResourceDef) ValidationFailureDetails(org.apache.ranger.plugin.model.validation.ValidationFailureDetails) RangerAccessTypeDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef) RangerContextEnricherDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerContextEnricherDef) XXEnumDef(org.apache.ranger.entity.XXEnumDef) XXServiceConfigDefDao(org.apache.ranger.db.XXServiceConfigDefDao) XXContextEnricherDef(org.apache.ranger.entity.XXContextEnricherDef) XXContextEnricherDefDao(org.apache.ranger.db.XXContextEnricherDefDao)

Aggregations

XXPolicyConditionDef (org.apache.ranger.entity.XXPolicyConditionDef)12 XXAccessTypeDef (org.apache.ranger.entity.XXAccessTypeDef)5 XXPolicyRefCondition (org.apache.ranger.entity.XXPolicyRefCondition)5 XXServiceDef (org.apache.ranger.entity.XXServiceDef)5 RangerPolicyConditionDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef)5 XXDataMaskTypeDef (org.apache.ranger.entity.XXDataMaskTypeDef)4 XXResourceDef (org.apache.ranger.entity.XXResourceDef)4 ArrayList (java.util.ArrayList)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 XXEnumDef (org.apache.ranger.entity.XXEnumDef)3 XXEnumElementDef (org.apache.ranger.entity.XXEnumElementDef)3 XXServiceConfigDef (org.apache.ranger.entity.XXServiceConfigDef)3 RangerPolicyItemAccess (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess)3 VXString (org.apache.ranger.view.VXString)3 Date (java.util.Date)2