Search in sources :

Example 1 with RangerDaoManager

use of org.apache.ranger.db.RangerDaoManager in project ranger by apache.

the class ServiceDBStore method updatePolicyVersion.

private void updatePolicyVersion(RangerService service, Integer policyDeltaType, RangerPolicy policy, boolean updateServiceInfoRoleVersion) throws Exception {
    if (service == null || service.getId() == null) {
        return;
    }
    XXServiceDao serviceDao = daoMgr.getXXService();
    final XXService serviceDbObj = serviceDao.getById(service.getId());
    if (serviceDbObj == null) {
        LOG.warn("updatePolicyVersion(serviceId=" + service.getId() + "): service not found");
        return;
    }
    final RangerDaoManager daoManager = daoMgr;
    final Long serviceId = serviceDbObj.getId();
    // if this is a tag service, update all services that refer to this tag service
    // so that next policy-download from plugins will get updated tag policies
    boolean isTagService = serviceDbObj.getType() == EmbeddedServiceDefsUtil.instance().getTagServiceDefId();
    if (isTagService) {
        List<XXService> referringServices = serviceDao.findByTagServiceId(serviceId);
        if (CollectionUtils.isNotEmpty(referringServices)) {
            for (XXService referringService : referringServices) {
                final Long referringServiceId = referringService.getId();
                final VERSION_TYPE tagServiceversionType = VERSION_TYPE.POLICY_VERSION;
                Runnable tagServiceVersionUpdater = new ServiceVersionUpdater(daoManager, referringServiceId, tagServiceversionType, policy != null ? policy.getZoneName() : null, policyDeltaType, policy);
                transactionSynchronizationAdapter.executeOnTransactionCommit(tagServiceVersionUpdater);
                Runnable roleVersionUpdater = new ServiceVersionUpdater(daoManager, referringServiceId, VERSION_TYPE.ROLE_VERSION, policy != null ? policy.getZoneName() : null, policyDeltaType, policy);
                transactionSynchronizationAdapter.executeOnTransactionCommit(roleVersionUpdater);
            }
        }
    }
    final VERSION_TYPE versionType = VERSION_TYPE.POLICY_VERSION;
    Runnable serviceVersionUpdater = new ServiceVersionUpdater(daoManager, serviceId, versionType, policy != null ? policy.getZoneName() : null, policyDeltaType, policy);
    transactionSynchronizationAdapter.executeOnTransactionCommit(serviceVersionUpdater);
    if (updateServiceInfoRoleVersion) {
        Runnable roleVersionUpdater = new ServiceVersionUpdater(daoManager, serviceId, VERSION_TYPE.ROLE_VERSION, policy != null ? policy.getZoneName() : null, policyDeltaType, policy);
        transactionSynchronizationAdapter.executeOnTransactionCommit(roleVersionUpdater);
    }
}
Also used : RangerDaoManager(org.apache.ranger.db.RangerDaoManager) XXServiceDao(org.apache.ranger.db.XXServiceDao) XXService(org.apache.ranger.entity.XXService)

Example 2 with RangerDaoManager

use of org.apache.ranger.db.RangerDaoManager in project ranger by apache.

the class ServiceDBStore method persistVersionChange.

public static void persistVersionChange(ServiceVersionUpdater serviceVersionUpdater) {
    RangerDaoManager daoMgr = serviceVersionUpdater.daoManager;
    Long id = serviceVersionUpdater.serviceId;
    VERSION_TYPE versionType = serviceVersionUpdater.versionType;
    XXServiceVersionInfoDao serviceVersionInfoDao = daoMgr.getXXServiceVersionInfo();
    XXServiceVersionInfo serviceVersionInfoDbObj = serviceVersionInfoDao.findByServiceId(id);
    XXService service = daoMgr.getXXService().getById(id);
    Long nextVersion = 1L;
    Date now = new Date();
    if (serviceVersionInfoDbObj != null) {
        if (versionType == VERSION_TYPE.POLICY_VERSION) {
            nextVersion = getNextVersion(serviceVersionInfoDbObj.getPolicyVersion());
            serviceVersionInfoDbObj.setPolicyVersion(nextVersion);
            serviceVersionInfoDbObj.setPolicyUpdateTime(now);
        } else if (versionType == VERSION_TYPE.TAG_VERSION) {
            nextVersion = getNextVersion(serviceVersionInfoDbObj.getTagVersion());
            serviceVersionInfoDbObj.setTagVersion(nextVersion);
            serviceVersionInfoDbObj.setTagUpdateTime(now);
        } else if (versionType == VERSION_TYPE.ROLE_VERSION) {
            // get the LatestRoleVersion from the GlobalTable and update ServiceInfo for a service
            XXGlobalStateDao xxGlobalStateDao = daoMgr.getXXGlobalState();
            if (xxGlobalStateDao != null) {
                Long roleVersion = xxGlobalStateDao.getAppDataVersion("RangerRole");
                if (roleVersion != null) {
                    nextVersion = roleVersion;
                } else {
                    LOG.error("No Global state for 'RoleVersion'. Cannot execute this object:[" + serviceVersionUpdater + "]");
                }
                serviceVersionInfoDbObj.setRoleVersion(nextVersion);
                serviceVersionInfoDbObj.setRoleUpdateTime(now);
            } else {
                LOG.error("No Global state DAO. Cannot execute this object:[" + serviceVersionUpdater + "]");
                return;
            }
        } else {
            LOG.error("Unknown VERSION_TYPE:" + versionType + ". Cannot execute this object:[" + serviceVersionUpdater + "]");
            return;
        }
        serviceVersionUpdater.version = nextVersion;
        serviceVersionInfoDao.update(serviceVersionInfoDbObj);
    } else {
        if (service != null) {
            serviceVersionInfoDbObj = new XXServiceVersionInfo();
            serviceVersionInfoDbObj.setServiceId(service.getId());
            serviceVersionInfoDbObj.setPolicyVersion(nextVersion);
            serviceVersionInfoDbObj.setPolicyUpdateTime(new Date());
            serviceVersionInfoDbObj.setTagVersion(nextVersion);
            serviceVersionInfoDbObj.setTagUpdateTime(new Date());
            serviceVersionInfoDbObj.setRoleVersion(nextVersion);
            serviceVersionInfoDbObj.setRoleUpdateTime(new Date());
            serviceVersionUpdater.version = nextVersion;
            serviceVersionInfoDao.create(serviceVersionInfoDbObj);
        }
    }
    if (service != null) {
        if (versionType == VERSION_TYPE.POLICY_VERSION) {
            persistChangeLog(service, versionType, serviceVersionInfoDbObj.getPolicyVersion(), serviceVersionUpdater);
        } else if (versionType == VERSION_TYPE.TAG_VERSION) {
            persistChangeLog(service, versionType, serviceVersionInfoDbObj.getTagVersion(), serviceVersionUpdater);
        }
    }
}
Also used : RangerDaoManager(org.apache.ranger.db.RangerDaoManager) XXGlobalStateDao(org.apache.ranger.db.XXGlobalStateDao) XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) Date(java.util.Date) XXServiceVersionInfoDao(org.apache.ranger.db.XXServiceVersionInfoDao)

Example 3 with RangerDaoManager

use of org.apache.ranger.db.RangerDaoManager in project ranger by apache.

the class RangerSecurityZoneServiceService method updateServiceInfos.

private void updateServiceInfos(Collection<String> services) {
    if (CollectionUtils.isEmpty(services)) {
        return;
    }
    List<XXServiceVersionInfo> serviceVersionInfos = new ArrayList<>(services.size());
    for (String serviceName : services) {
        serviceVersionInfos.add(daoMgr.getXXServiceVersionInfo().findByServiceName(serviceName));
    }
    for (XXServiceVersionInfo serviceVersionInfo : serviceVersionInfos) {
        final RangerDaoManager finaldaoManager = daoMgr;
        final Long finalServiceId = serviceVersionInfo.getServiceId();
        final ServiceDBStore.VERSION_TYPE versionType = ServiceDBStore.VERSION_TYPE.POLICY_VERSION;
        Runnable serviceVersionUpdater = new ServiceDBStore.ServiceVersionUpdater(finaldaoManager, finalServiceId, versionType, null, RangerPolicyDelta.CHANGE_TYPE_SERVICE_CHANGE, null);
        daoMgr.getRangerTransactionSynchronizationAdapter().executeOnTransactionCommit(serviceVersionUpdater);
    }
}
Also used : RangerDaoManager(org.apache.ranger.db.RangerDaoManager) ServiceDBStore(org.apache.ranger.biz.ServiceDBStore) ArrayList(java.util.ArrayList) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo)

Example 4 with RangerDaoManager

use of org.apache.ranger.db.RangerDaoManager in project ranger by apache.

the class ServiceDBStore method updateServicesForServiceDefUpdate.

// when a service-def is updated, the updated service-def should be made available to plugins
// this is achieved by incrementing policyVersion of all services of this service-def
protected void updateServicesForServiceDefUpdate(RangerServiceDef serviceDef) throws Exception {
    if (serviceDef == null) {
        return;
    }
    final RangerDaoManager daoManager = daoMgr;
    boolean isTagServiceDef = StringUtils.equals(serviceDef.getName(), EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME);
    XXServiceDao serviceDao = daoMgr.getXXService();
    List<XXService> services = serviceDao.findByServiceDefId(serviceDef.getId());
    if (CollectionUtils.isNotEmpty(services)) {
        for (XXService service : services) {
            if (isTagServiceDef) {
                List<XXService> referringServices = serviceDao.findByTagServiceId(service.getId());
                if (CollectionUtils.isNotEmpty(referringServices)) {
                    for (XXService referringService : referringServices) {
                        final Long referringServiceId = referringService.getId();
                        final VERSION_TYPE tagServiceVersionType = VERSION_TYPE.POLICY_VERSION;
                        Runnable tagServiceVersionUpdater = new ServiceVersionUpdater(daoManager, referringServiceId, tagServiceVersionType, RangerPolicyDelta.CHANGE_TYPE_SERVICE_DEF_CHANGE);
                        transactionSynchronizationAdapter.executeOnTransactionCommit(tagServiceVersionUpdater);
                    }
                }
            }
            final Long serviceId = service.getId();
            final VERSION_TYPE versionType = VERSION_TYPE.POLICY_VERSION;
            Runnable serviceVersionUpdater = new ServiceVersionUpdater(daoManager, serviceId, versionType, RangerPolicyDelta.CHANGE_TYPE_SERVICE_DEF_CHANGE);
            transactionSynchronizationAdapter.executeOnTransactionCommit(serviceVersionUpdater);
        }
    }
}
Also used : RangerDaoManager(org.apache.ranger.db.RangerDaoManager) XXServiceDao(org.apache.ranger.db.XXServiceDao) XXService(org.apache.ranger.entity.XXService)

Aggregations

RangerDaoManager (org.apache.ranger.db.RangerDaoManager)4 XXService (org.apache.ranger.entity.XXService)3 XXServiceDao (org.apache.ranger.db.XXServiceDao)2 XXServiceVersionInfo (org.apache.ranger.entity.XXServiceVersionInfo)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 ServiceDBStore (org.apache.ranger.biz.ServiceDBStore)1 XXGlobalStateDao (org.apache.ranger.db.XXGlobalStateDao)1 XXServiceVersionInfoDao (org.apache.ranger.db.XXServiceVersionInfoDao)1