Search in sources :

Example 6 with XXServiceVersionInfoDao

use of org.apache.ranger.db.XXServiceVersionInfoDao 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 7 with XXServiceVersionInfoDao

use of org.apache.ranger.db.XXServiceVersionInfoDao 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 8 with XXServiceVersionInfoDao

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

the class RangerServiceService method preDelete.

protected XXService preDelete(Long id) {
    XXService ret = super.preDelete(id);
    if (ret != null) {
        XXServiceVersionInfoDao serviceVersionInfoDao = daoMgr.getXXServiceVersionInfo();
        XXServiceVersionInfo serviceVersionInfo = serviceVersionInfoDao.findByServiceId(id);
        if (serviceVersionInfo != null) {
            serviceVersionInfoDao.remove(serviceVersionInfo.getId());
        }
    }
    return ret;
}
Also used : XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) XXServiceVersionInfoDao(org.apache.ranger.db.XXServiceVersionInfoDao)

Example 9 with XXServiceVersionInfoDao

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

the class TestRangerServiceService method test4GetPopulatedViewObject.

@Test
public void test4GetPopulatedViewObject() {
    XXServiceConfigMapDao xServiceConfigMapDao = Mockito.mock(XXServiceConfigMapDao.class);
    XXPortalUserDao xPortalUserDao = Mockito.mock(XXPortalUserDao.class);
    XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    XXService xService = xService();
    String name = "fdfdfds";
    List<XXServiceConfigMap> svcConfigMapList = new ArrayList<XXServiceConfigMap>();
    XXServiceConfigMap xConfMap = new XXServiceConfigMap();
    xConfMap.setAddedByUserId(null);
    xConfMap.setConfigkey(name);
    xConfMap.setConfigvalue(name);
    xConfMap.setCreateTime(new Date());
    xConfMap.setServiceId(null);
    xConfMap.setUpdatedByUserId(null);
    xConfMap.setUpdateTime(new Date());
    svcConfigMapList.add(xConfMap);
    XXPortalUser tUser = new XXPortalUser();
    tUser.setAddedByUserId(userId);
    tUser.setCreateTime(new Date());
    tUser.setEmailAddress("test@gmail.com");
    tUser.setFirstName(name);
    tUser.setId(userId);
    tUser.setLastName(name);
    XXServiceDef xServiceDef = new XXServiceDef();
    xServiceDef.setAddedByUserId(userId);
    xServiceDef.setCreateTime(new Date());
    xServiceDef.setDescription("test");
    xServiceDef.setGuid("1427365526516_835_0");
    xServiceDef.setId(userId);
    XXServiceVersionInfoDao xServiceVersionInfoDao = Mockito.mock(XXServiceVersionInfoDao.class);
    XXServiceVersionInfo serviceVersionInfo = new XXServiceVersionInfo();
    serviceVersionInfo.setServiceId(xService.getId());
    serviceVersionInfo.setPolicyVersion(xService.getPolicyVersion());
    serviceVersionInfo.setPolicyUpdateTime(xService.getPolicyUpdateTime());
    serviceVersionInfo.setTagVersion(xService.getTagVersion());
    serviceVersionInfo.setTagUpdateTime(xService.getTagUpdateTime());
    Mockito.when(daoManager.getXXServiceVersionInfo()).thenReturn(xServiceVersionInfoDao);
    Mockito.when(xServiceVersionInfoDao.findByServiceId(xService.getId())).thenReturn(serviceVersionInfo);
    Mockito.when(daoManager.getXXPortalUser()).thenReturn(xPortalUserDao);
    Mockito.when(xPortalUserDao.getById(userId)).thenReturn(tUser);
    Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao);
    Mockito.when(xServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef);
    Mockito.when(daoManager.getXXServiceConfigMap()).thenReturn(xServiceConfigMapDao);
    Mockito.when(xServiceConfigMapDao.findByServiceId(xService.getId())).thenReturn(svcConfigMapList);
    RangerService dbService = serviceService.getPopulatedViewObject(xService);
    Assert.assertNotNull(dbService);
    Assert.assertEquals(userId, dbService.getId());
    Assert.assertEquals(xService.getAddedByUserId(), dbService.getId());
    Assert.assertEquals(xService.getId(), dbService.getId());
    Assert.assertEquals(xService.getDescription(), dbService.getDescription());
    Assert.assertEquals(xService.getGuid(), dbService.getGuid());
    Assert.assertEquals(xService.getName(), dbService.getName());
    Assert.assertEquals(xService.getPolicyUpdateTime(), dbService.getPolicyUpdateTime());
    Assert.assertEquals(xService.getPolicyVersion(), dbService.getPolicyVersion());
    Assert.assertEquals(xService.getVersion(), dbService.getVersion());
    Mockito.verify(daoManager).getXXServiceDef();
    Mockito.verify(daoManager).getXXServiceConfigMap();
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) ArrayList(java.util.ArrayList) XXServiceConfigMapDao(org.apache.ranger.db.XXServiceConfigMapDao) Date(java.util.Date) XXServiceConfigMap(org.apache.ranger.entity.XXServiceConfigMap) XXPortalUser(org.apache.ranger.entity.XXPortalUser) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) XXPortalUserDao(org.apache.ranger.db.XXPortalUserDao) RangerService(org.apache.ranger.plugin.model.RangerService) XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) XXServiceVersionInfoDao(org.apache.ranger.db.XXServiceVersionInfoDao) Test(org.junit.Test)

Aggregations

XXServiceVersionInfoDao (org.apache.ranger.db.XXServiceVersionInfoDao)9 XXServiceVersionInfo (org.apache.ranger.entity.XXServiceVersionInfo)8 Date (java.util.Date)7 XXService (org.apache.ranger.entity.XXService)7 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)4 XXServiceDef (org.apache.ranger.entity.XXServiceDef)4 RangerService (org.apache.ranger.plugin.model.RangerService)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 XXTagAttributeDefDao (org.apache.ranger.db.XXTagAttributeDefDao)1 XXTagAttributeDef (org.apache.ranger.entity.XXTagAttributeDef)1 XXTagDef (org.apache.ranger.entity.XXTagDef)1 RangerTagDef (org.apache.ranger.plugin.model.RangerTagDef)1