Search in sources :

Example 6 with XXServiceVersionInfo

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

the class RangerServiceService method postCreate.

public RangerService postCreate(XXService xObj) {
    XXServiceVersionInfo serviceVersionInfo = new XXServiceVersionInfo();
    serviceVersionInfo.setServiceId(xObj.getId());
    serviceVersionInfo.setPolicyVersion(1L);
    serviceVersionInfo.setTagVersion(1L);
    Date now = new Date();
    serviceVersionInfo.setPolicyUpdateTime(now);
    serviceVersionInfo.setTagUpdateTime(now);
    XXServiceVersionInfoDao serviceVersionInfoDao = daoMgr.getXXServiceVersionInfo();
    XXServiceVersionInfo createdServiceVersionInfo = serviceVersionInfoDao.create(serviceVersionInfo);
    return createdServiceVersionInfo != null ? super.postCreate(xObj) : null;
}
Also used : XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) Date(java.util.Date) XXServiceVersionInfoDao(org.apache.ranger.db.XXServiceVersionInfoDao)

Example 7 with XXServiceVersionInfo

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

the class PatchForServiceVersionInfo_J10004 method copyVersionsFromServiceToServiceVersionInfo.

public void copyVersionsFromServiceToServiceVersionInfo() {
    List<XXService> allServices = daoManager.getXXService().getAll();
    Date now = new Date();
    for (XXService xService : allServices) {
        boolean needToCreateServiceVersionInfo = false;
        XXServiceVersionInfo serviceVersionInfoDbObj = daoManager.getXXServiceVersionInfo().findByServiceId(xService.getId());
        if (serviceVersionInfoDbObj == null) {
            needToCreateServiceVersionInfo = true;
            serviceVersionInfoDbObj = new XXServiceVersionInfo();
            serviceVersionInfoDbObj.setServiceId(xService.getId());
        }
        serviceVersionInfoDbObj.setPolicyVersion(xService.getPolicyVersion() == null ? 1L : xService.getPolicyVersion());
        serviceVersionInfoDbObj.setTagVersion(xService.getTagVersion());
        serviceVersionInfoDbObj.setPolicyUpdateTime(xService.getPolicyUpdateTime());
        serviceVersionInfoDbObj.setTagUpdateTime(xService.getTagUpdateTime());
        if (needToCreateServiceVersionInfo) {
            daoManager.getXXServiceVersionInfo().create(serviceVersionInfoDbObj);
            logger.info("Created serviceVesionInfo for serviceName [" + xService.getName() + "]");
        } else {
            daoManager.getXXServiceVersionInfo().update(serviceVersionInfoDbObj);
            logger.info("Updated serviceVesionInfo for serviceName [" + xService.getName() + "]");
        }
        // Consider this scenario:
        // 1. ranger-admin is upgraded to use versions from x_service_version_info table;
        // 2. there are updates to policies and/or tags;
        // 3. no plug-ins download service-policies;
        // 4. upgrade is rolled back, for ranger-admin to use versions from x_service table;
        // 5. Now plug-in downloads service-policies.
        // In this scenario, plug-ins will miss the policy/tag updates down in step 2. To ensure that
        // plug-ins get updated policies/tags, we increment versions in x_service table when x_service_version_info
        // table is updated in this patch. This may cause one potentially unnecessary download to plugin in case
        // step 2 above did not take place, but it is safer.
        xService.setPolicyVersion(xService.getPolicyVersion() == null ? 2L : xService.getPolicyVersion() + 1);
        xService.setTagVersion(xService.getTagVersion() + 1);
        xService.setPolicyUpdateTime(now);
        xService.setTagUpdateTime(now);
        daoManager.getXXService().update(xService);
        logger.info("Incremented policy and tag versions for serviceName [" + xService.getName() + "]");
    }
}
Also used : XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) Date(java.util.Date)

Example 8 with XXServiceVersionInfo

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

the class ServiceDBStore method updatePolicyVersion.

private void updatePolicyVersion(RangerService service, boolean isTagVersionUpdateNeeded) throws Exception {
    if (service == null || service.getId() == null) {
        return;
    }
    boolean filterForServicePlugin = RangerConfiguration.getInstance().getBoolean(RangerTagDBRetriever.OPTION_RANGER_FILTER_TAGS_FOR_SERVICE_PLUGIN, false);
    XXServiceDao serviceDao = daoMgr.getXXService();
    XXService serviceDbObj = serviceDao.getById(service.getId());
    if (serviceDbObj == null) {
        LOG.warn("updatePolicyVersion(serviceId=" + service.getId() + "): service not found");
        return;
    }
    XXServiceVersionInfoDao serviceVersionInfoDao = daoMgr.getXXServiceVersionInfo();
    XXServiceVersionInfo serviceVersionInfoDbObj = serviceVersionInfoDao.findByServiceId(service.getId());
    if (serviceVersionInfoDbObj != null) {
        serviceVersionInfoDbObj.setPolicyVersion(getNextVersion(serviceVersionInfoDbObj.getPolicyVersion()));
        serviceVersionInfoDbObj.setPolicyUpdateTime(new Date());
        serviceVersionInfoDao.update(serviceVersionInfoDbObj);
    } else {
        LOG.warn("updatePolicyVersion(service=" + serviceDbObj.getName() + "): serviceVersionInfo not found, creating it..");
        serviceVersionInfoDbObj = new XXServiceVersionInfo();
        serviceVersionInfoDbObj.setServiceId(serviceDbObj.getId());
        serviceVersionInfoDbObj.setPolicyVersion(getNextVersion(serviceDbObj.getPolicyVersion()));
        serviceVersionInfoDbObj.setPolicyUpdateTime(new Date());
        serviceVersionInfoDbObj.setTagVersion(serviceDbObj.getTagVersion());
        serviceVersionInfoDbObj.setTagUpdateTime(serviceDbObj.getTagUpdateTime());
        serviceVersionInfoDao.create(serviceVersionInfoDbObj);
    }
    // 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(serviceDbObj.getId());
        if (CollectionUtils.isNotEmpty(referringServices)) {
            for (XXService referringService : referringServices) {
                serviceVersionInfoDbObj = serviceVersionInfoDao.findByServiceId(referringService.getId());
                if (serviceVersionInfoDbObj != null) {
                    serviceVersionInfoDbObj.setPolicyVersion(getNextVersion(serviceVersionInfoDbObj.getPolicyVersion()));
                    serviceVersionInfoDbObj.setPolicyUpdateTime(new Date());
                    if (filterForServicePlugin && isTagVersionUpdateNeeded) {
                        serviceVersionInfoDbObj.setTagVersion(getNextVersion(serviceVersionInfoDbObj.getTagVersion()));
                        serviceVersionInfoDbObj.setTagUpdateTime(new Date());
                    }
                    serviceVersionInfoDao.update(serviceVersionInfoDbObj);
                } else {
                    LOG.warn("updatePolicyVersion(service=" + referringService.getName() + "): serviceVersionInfo not found, creating it..");
                    serviceVersionInfoDbObj = new XXServiceVersionInfo();
                    serviceVersionInfoDbObj.setServiceId(referringService.getId());
                    serviceVersionInfoDbObj.setPolicyVersion(getNextVersion(referringService.getPolicyVersion()));
                    serviceVersionInfoDbObj.setPolicyUpdateTime(new Date());
                    if (filterForServicePlugin && isTagVersionUpdateNeeded) {
                        serviceVersionInfoDbObj.setTagVersion(getNextVersion(referringService.getTagVersion()));
                        serviceVersionInfoDbObj.setTagUpdateTime(new Date());
                    } else {
                        serviceVersionInfoDbObj.setTagVersion(referringService.getTagVersion());
                        serviceVersionInfoDbObj.setTagUpdateTime(referringService.getTagUpdateTime());
                    }
                    serviceVersionInfoDao.create(serviceVersionInfoDbObj);
                }
            }
        }
    }
}
Also used : XXServiceDao(org.apache.ranger.db.XXServiceDao) XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) Date(java.util.Date) XXServiceVersionInfoDao(org.apache.ranger.db.XXServiceVersionInfoDao)

Example 9 with XXServiceVersionInfo

use of org.apache.ranger.entity.XXServiceVersionInfo 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;
    }
    boolean isTagServiceDef = StringUtils.equals(serviceDef.getName(), EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME);
    XXServiceDao serviceDao = daoMgr.getXXService();
    XXServiceVersionInfoDao serviceVersionInfoDao = daoMgr.getXXServiceVersionInfo();
    List<XXService> services = serviceDao.findByServiceDefId(serviceDef.getId());
    if (CollectionUtils.isNotEmpty(services)) {
        for (XXService service : services) {
            XXServiceVersionInfo serviceVersionInfo = serviceVersionInfoDao.findByServiceId(service.getId());
            if (serviceVersionInfo != null) {
                serviceVersionInfo.setPolicyVersion(getNextVersion(serviceVersionInfo.getPolicyVersion()));
                serviceVersionInfo.setPolicyUpdateTime(serviceDef.getUpdateTime());
                serviceVersionInfoDao.update(serviceVersionInfo);
            } else {
                LOG.warn("updateServicesForServiceDefUpdate(service=" + service.getName() + "): serviceVersionInfo not found, creating it..");
                serviceVersionInfo = new XXServiceVersionInfo();
                serviceVersionInfo.setServiceId(service.getId());
                serviceVersionInfo.setPolicyVersion(getNextVersion(service.getPolicyVersion()));
                serviceVersionInfo.setTagVersion(service.getTagVersion());
                serviceVersionInfo.setPolicyUpdateTime(new Date());
                serviceVersionInfo.setTagUpdateTime(service.getTagUpdateTime());
                serviceVersionInfoDao.create(serviceVersionInfo);
            }
            if (isTagServiceDef) {
                List<XXService> referrringServices = serviceDao.findByTagServiceId(service.getId());
                if (CollectionUtils.isNotEmpty(referrringServices)) {
                    for (XXService referringService : referrringServices) {
                        serviceVersionInfo = serviceVersionInfoDao.findByServiceId(referringService.getId());
                        if (serviceVersionInfo != null) {
                            serviceVersionInfo.setPolicyVersion(getNextVersion(serviceVersionInfo.getPolicyVersion()));
                            serviceVersionInfo.setPolicyUpdateTime(serviceDef.getUpdateTime());
                            serviceVersionInfoDao.update(serviceVersionInfo);
                        } else {
                            LOG.warn("updateServicesForServiceDefUpdate(service=" + referringService.getName() + "): serviceVersionInfo not found, creating it..");
                            serviceVersionInfo = new XXServiceVersionInfo();
                            serviceVersionInfo.setServiceId(referringService.getId());
                            serviceVersionInfo.setPolicyVersion(getNextVersion(referringService.getPolicyVersion()));
                            serviceVersionInfo.setTagVersion(referringService.getTagVersion());
                            serviceVersionInfo.setPolicyUpdateTime(new Date());
                            serviceVersionInfo.setTagUpdateTime(referringService.getTagUpdateTime());
                            serviceVersionInfoDao.create(serviceVersionInfo);
                        }
                    }
                }
            }
        }
    }
}
Also used : XXServiceDao(org.apache.ranger.db.XXServiceDao) XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) Date(java.util.Date) XXServiceVersionInfoDao(org.apache.ranger.db.XXServiceVersionInfoDao)

Example 10 with XXServiceVersionInfo

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

the class ServiceDBStore method getServicePolicies.

@Override
public ServicePolicies getServicePolicies(String serviceName) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.getServicePolicies(" + serviceName + ")");
    }
    ServicePolicies ret = null;
    XXService serviceDbObj = daoMgr.getXXService().findByName(serviceName);
    if (serviceDbObj == null) {
        throw new Exception("service does not exist. name=" + serviceName);
    }
    XXServiceVersionInfo serviceVersionInfoDbObj = daoMgr.getXXServiceVersionInfo().findByServiceName(serviceName);
    if (serviceVersionInfoDbObj == null) {
        LOG.warn("serviceVersionInfo does not exist. name=" + serviceName);
    }
    RangerServiceDef serviceDef = getServiceDef(serviceDbObj.getType());
    if (serviceDef == null) {
        throw new Exception("service-def does not exist. id=" + serviceDbObj.getType());
    }
    List<RangerPolicy> policies = null;
    ServicePolicies.TagPolicies tagPolicies = null;
    String auditMode = getAuditMode(serviceDef.getName(), serviceName);
    if (serviceDbObj.getIsenabled()) {
        if (serviceDbObj.getTagService() != null) {
            XXService tagServiceDbObj = daoMgr.getXXService().getById(serviceDbObj.getTagService());
            if (tagServiceDbObj != null && tagServiceDbObj.getIsenabled()) {
                RangerServiceDef tagServiceDef = getServiceDef(tagServiceDbObj.getType());
                if (tagServiceDef == null) {
                    throw new Exception("service-def does not exist. id=" + tagServiceDbObj.getType());
                }
                XXServiceVersionInfo tagServiceVersionInfoDbObj = daoMgr.getXXServiceVersionInfo().findByServiceId(serviceDbObj.getTagService());
                if (tagServiceVersionInfoDbObj == null) {
                    LOG.warn("serviceVersionInfo does not exist. name=" + tagServiceDbObj.getName());
                }
                tagPolicies = new ServicePolicies.TagPolicies();
                tagPolicies.setServiceId(tagServiceDbObj.getId());
                tagPolicies.setServiceName(tagServiceDbObj.getName());
                tagPolicies.setPolicyVersion(tagServiceVersionInfoDbObj == null ? null : tagServiceVersionInfoDbObj.getPolicyVersion());
                tagPolicies.setPolicyUpdateTime(tagServiceVersionInfoDbObj == null ? null : tagServiceVersionInfoDbObj.getPolicyUpdateTime());
                tagPolicies.setPolicies(getServicePoliciesFromDb(tagServiceDbObj));
                tagPolicies.setServiceDef(tagServiceDef);
                tagPolicies.setAuditMode(auditMode);
            }
        }
        policies = getServicePoliciesFromDb(serviceDbObj);
    } else {
        policies = new ArrayList<RangerPolicy>();
    }
    ret = new ServicePolicies();
    ret.setServiceId(serviceDbObj.getId());
    ret.setServiceName(serviceDbObj.getName());
    ret.setPolicyVersion(serviceVersionInfoDbObj == null ? null : serviceVersionInfoDbObj.getPolicyVersion());
    ret.setPolicyUpdateTime(serviceVersionInfoDbObj == null ? null : serviceVersionInfoDbObj.getPolicyUpdateTime());
    ret.setPolicies(policies);
    ret.setServiceDef(serviceDef);
    ret.setAuditMode(auditMode);
    ret.setTagPolicies(tagPolicies);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceDBStore.getServicePolicies(" + serviceName + "): count=" + ((ret == null || ret.getPolicies() == null) ? 0 : ret.getPolicies().size()));
    }
    return ret;
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) ServicePolicies(org.apache.ranger.plugin.util.ServicePolicies) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) VXString(org.apache.ranger.view.VXString) XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException)

Aggregations

XXServiceVersionInfo (org.apache.ranger.entity.XXServiceVersionInfo)16 XXService (org.apache.ranger.entity.XXService)14 Date (java.util.Date)8 XXServiceVersionInfoDao (org.apache.ranger.db.XXServiceVersionInfoDao)8 ArrayList (java.util.ArrayList)5 XXServiceDef (org.apache.ranger.entity.XXServiceDef)5 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)4 RangerService (org.apache.ranger.plugin.model.RangerService)4 Test (org.junit.Test)4 XXPortalUserDao (org.apache.ranger.db.XXPortalUserDao)3 XXServiceConfigMapDao (org.apache.ranger.db.XXServiceConfigMapDao)3 XXServiceDao (org.apache.ranger.db.XXServiceDao)3 XXPortalUser (org.apache.ranger.entity.XXPortalUser)3 XXServiceConfigMap (org.apache.ranger.entity.XXServiceConfigMap)3 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 PList (org.apache.ranger.plugin.store.PList)2 ServicePolicies (org.apache.ranger.plugin.util.ServicePolicies)2 ServiceTags (org.apache.ranger.plugin.util.ServiceTags)2 JSONException (org.codehaus.jettison.json.JSONException)2