use of org.apache.ranger.db.XXServiceDao in project ranger by apache.
the class TestTagREST method test54getSecureServiceTagsIfUpdatedIsKeyAdminFalse.
@Test
public void test54getSecureServiceTagsIfUpdatedIsKeyAdminFalse() {
boolean isAdmin = false;
boolean isKeyAdmin = false;
boolean isAllowed = true;
ServiceTags oldServiceTag = new ServiceTags();
oldServiceTag.setServiceName(serviceName);
oldServiceTag.setTagVersion(5L);
XXService xService = new XXService();
xService.setId(id);
xService.setName(serviceName);
xService.setType(5L);
XXServiceDef xServiceDef = new XXServiceDef();
xServiceDef.setId(id);
xServiceDef.setVersion(5L);
xServiceDef.setImplclassname("org.apache.ranger.services.kms.RangerServiceKMS");
RangerService rangerService = new RangerService();
rangerService.setId(id);
rangerService.setName(serviceName);
XXServiceDao xXServiceDao = Mockito.mock(XXServiceDao.class);
XXServiceDefDao xXServiceDefDao = Mockito.mock(XXServiceDefDao.class);
Mockito.when(bizUtil.isAdmin()).thenReturn(isAdmin);
Mockito.when(bizUtil.isKeyAdmin()).thenReturn(isKeyAdmin);
Mockito.when(daoManager.getXXService()).thenReturn(xXServiceDao);
Mockito.when(xXServiceDao.findByName(serviceName)).thenReturn(xService);
Mockito.when(daoManager.getXXServiceDef()).thenReturn(xXServiceDefDao);
Mockito.when(xXServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef);
try {
Mockito.when(svcStore.getServiceByName(serviceName)).thenReturn(rangerService);
} catch (Exception e) {
}
Mockito.when(bizUtil.isUserAllowed(rangerService, Allowed_User_List_For_Tag_Download)).thenReturn(isAllowed);
try {
Mockito.when(tagStore.getServiceTagsIfUpdated(serviceName, lastKnownVersion)).thenReturn(oldServiceTag);
} catch (Exception e) {
}
ServiceTags result = tagREST.getSecureServiceTagsIfUpdated(serviceName, lastKnownVersion, 0L, pluginId, null);
Assert.assertNotNull(result.getServiceName());
Assert.assertEquals(result.getServiceName(), oldServiceTag.getServiceName());
Assert.assertEquals(result.getTagVersion(), oldServiceTag.getTagVersion());
Mockito.verify(bizUtil).isAdmin();
Mockito.verify(bizUtil).isKeyAdmin();
Mockito.verify(daoManager).getXXService();
Mockito.verify(xXServiceDao).findByName(serviceName);
Mockito.verify(daoManager).getXXServiceDef();
Mockito.verify(xXServiceDefDao).getById(xService.getType());
try {
Mockito.verify(svcStore).getServiceByName(serviceName);
} catch (Exception e) {
}
Mockito.verify(bizUtil).isUserAllowed(rangerService, Allowed_User_List_For_Tag_Download);
try {
Mockito.verify(tagStore).getServiceTagsIfUpdated(serviceName, lastKnownVersion);
} catch (Exception e) {
}
}
use of org.apache.ranger.db.XXServiceDao in project ranger by apache.
the class TestTagREST method test52getSecureServiceTagsIfUpdatedIsKeyAdminTrue.
@Test
public void test52getSecureServiceTagsIfUpdatedIsKeyAdminTrue() {
boolean isAdmin = false;
boolean isKeyAdmin = true;
ServiceTags oldServiceTag = new ServiceTags();
oldServiceTag.setServiceName(serviceName);
oldServiceTag.setTagVersion(5L);
XXService xService = new XXService();
xService.setId(id);
xService.setName(serviceName);
xService.setType(5L);
XXServiceDef xServiceDef = new XXServiceDef();
xServiceDef.setId(id);
xServiceDef.setVersion(5L);
xServiceDef.setImplclassname("org.apache.ranger.services.kms.RangerServiceKMS");
RangerService rangerService = new RangerService();
rangerService.setId(id);
rangerService.setName(serviceName);
XXServiceDao xXServiceDao = Mockito.mock(XXServiceDao.class);
XXServiceDefDao xXServiceDefDao = Mockito.mock(XXServiceDefDao.class);
Mockito.when(bizUtil.isAdmin()).thenReturn(isAdmin);
Mockito.when(bizUtil.isKeyAdmin()).thenReturn(isKeyAdmin);
Mockito.when(daoManager.getXXService()).thenReturn(xXServiceDao);
Mockito.when(xXServiceDao.findByName(serviceName)).thenReturn(xService);
Mockito.when(daoManager.getXXServiceDef()).thenReturn(xXServiceDefDao);
Mockito.when(xXServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef);
try {
Mockito.when(svcStore.getServiceByName(serviceName)).thenReturn(rangerService);
} catch (Exception e) {
}
try {
Mockito.when(tagStore.getServiceTagsIfUpdated(serviceName, lastKnownVersion)).thenReturn(oldServiceTag);
} catch (Exception e) {
}
ServiceTags result = tagREST.getSecureServiceTagsIfUpdated(serviceName, lastKnownVersion, 0L, pluginId, null);
Assert.assertNotNull(result.getServiceName());
Assert.assertEquals(result.getServiceName(), oldServiceTag.getServiceName());
Assert.assertEquals(result.getTagVersion(), oldServiceTag.getTagVersion());
Mockito.verify(bizUtil).isAdmin();
Mockito.verify(bizUtil).isKeyAdmin();
Mockito.verify(daoManager).getXXService();
Mockito.verify(xXServiceDao).findByName(serviceName);
Mockito.verify(daoManager).getXXServiceDef();
Mockito.verify(xXServiceDefDao).getById(xService.getType());
try {
Mockito.verify(svcStore).getServiceByName(serviceName);
} catch (Exception e) {
}
try {
Mockito.verify(tagStore).getServiceTagsIfUpdated(serviceName, lastKnownVersion);
} catch (Exception e) {
}
}
use of org.apache.ranger.db.XXServiceDao in project ranger by apache.
the class PatchForAtlasServiceDefUpdate_J10013 method updateAtlasServiceDef.
private void updateAtlasServiceDef() {
String serviceDefName = EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_ATLAS_NAME;
XXServiceDefDao serviceDefDao = daoMgr.getXXServiceDef();
XXServiceDef serviceDef = serviceDefDao.findByName(serviceDefName);
// if service-def named 'atlas' does not exist then no need to process this patch further.
if (serviceDef == null) {
LOG.info(serviceDefName + ": service-def not found. No patching is needed");
return;
}
// if older atlas service-def doesn't exist then no need to process this patch further.
if (!checkIfHasOlderServiceDef(serviceDef)) {
LOG.info("Older version of " + serviceDefName + " service-def not found. No patching is needed");
return;
}
String suffix = null;
for (int i = 1; true; i++) {
suffix = ".v" + i;
if (serviceDefDao.findByName(serviceDefName + suffix) == null) {
break;
}
}
String serviceDefNewName = serviceDefName + suffix;
LOG.info("Renaming service-def " + serviceDefName + " as " + serviceDefNewName);
serviceDef.setName(serviceDefNewName);
serviceDefDao.update(serviceDef);
LOG.info("Renamed service-def " + serviceDefName + " as " + serviceDefNewName);
XXServiceDao serviceDao = daoMgr.getXXService();
List<XXService> services = serviceDao.findByServiceDefId(serviceDef.getId());
if (CollectionUtils.isNotEmpty(services)) {
for (XXService service : services) {
String serviceName = service.getName();
String serviceNewName = serviceName + suffix;
LOG.info("Renaming service " + serviceName + " as " + serviceNewName);
if (serviceDao.findByName(serviceNewName) != null) {
LOG.warn("Another service named " + serviceNewName + " already exists. Not renaming " + serviceName);
continue;
}
service.setName(serviceNewName);
serviceDao.update(service);
LOG.info("Renamed service " + serviceName + " as " + serviceNewName);
}
}
}
use of org.apache.ranger.db.XXServiceDao 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);
}
}
}
}
}
use of org.apache.ranger.db.XXServiceDao 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);
}
}
}
}
}
}
}
Aggregations