use of org.apache.ranger.entity.XXService in project ranger by apache.
the class TestTagREST method test55getSecureServiceTagsIfUpdatedIsAdminFalse.
@Test
public void test55getSecureServiceTagsIfUpdatedIsAdminFalse() {
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);
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.entity.XXService 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.entity.XXService 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.entity.XXService in project ranger by apache.
the class TagREST method getSecureServiceTagsIfUpdated.
@GET
@Path(TagRESTConstants.TAGS_SECURE_DOWNLOAD + "{serviceName}")
@Produces({ "application/json", "application/xml" })
public ServiceTags getSecureServiceTagsIfUpdated(@PathParam("serviceName") String serviceName, @QueryParam(TagRESTConstants.LAST_KNOWN_TAG_VERSION_PARAM) Long lastKnownVersion, @DefaultValue("0") @QueryParam(TagRESTConstants.LAST_ACTIVATION_TIME) Long lastActivationTime, @QueryParam("pluginId") String pluginId, @Context HttpServletRequest request) {
if (LOG.isDebugEnabled()) {
LOG.debug("==> TagREST.getSecureServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + ", " + lastActivationTime + ", " + pluginId + ")");
}
ServiceTags ret = null;
int httpCode = HttpServletResponse.SC_OK;
String logMsg = null;
boolean isAllowed = false;
boolean isAdmin = bizUtil.isAdmin();
boolean isKeyAdmin = bizUtil.isKeyAdmin();
Long downloadedVersion = null;
try {
XXService xService = daoManager.getXXService().findByName(serviceName);
if (xService == null) {
LOG.error("Requested Service not found. serviceName=" + serviceName);
throw restErrorUtil.createRESTException(HttpServletResponse.SC_NOT_FOUND, "Service:" + serviceName + " not found", false);
}
XXServiceDef xServiceDef = daoManager.getXXServiceDef().getById(xService.getType());
RangerService rangerService = svcStore.getServiceByName(serviceName);
if (StringUtils.equals(xServiceDef.getImplclassname(), EmbeddedServiceDefsUtil.KMS_IMPL_CLASS_NAME)) {
if (isKeyAdmin) {
isAllowed = true;
} else {
isAllowed = bizUtil.isUserAllowed(rangerService, Allowed_User_List_For_Tag_Download);
}
} else {
if (isAdmin) {
isAllowed = true;
} else {
isAllowed = bizUtil.isUserAllowed(rangerService, Allowed_User_List_For_Tag_Download);
}
}
if (isAllowed) {
ret = tagStore.getServiceTagsIfUpdated(serviceName, lastKnownVersion);
if (ret == null) {
downloadedVersion = lastKnownVersion;
httpCode = HttpServletResponse.SC_NOT_MODIFIED;
logMsg = "No change since last update";
} else {
downloadedVersion = ret.getTagVersion();
httpCode = HttpServletResponse.SC_OK;
logMsg = "Returning " + (ret.getTags() != null ? ret.getTags().size() : 0) + " tags. Tag version=" + ret.getTagVersion();
}
} else {
LOG.error("getSecureServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + ", " + lastActivationTime + ") failed as User doesn't have permission to download tags");
httpCode = HttpServletResponse.SC_UNAUTHORIZED;
logMsg = "User doesn't have permission to download tags";
}
} catch (WebApplicationException webException) {
httpCode = webException.getResponse().getStatus();
logMsg = webException.getResponse().getEntity().toString();
} catch (Exception excp) {
httpCode = HttpServletResponse.SC_BAD_REQUEST;
logMsg = excp.getMessage();
} finally {
assetMgr.createPluginInfo(serviceName, pluginId, request, RangerPluginInfo.ENTITY_TYPE_TAGS, downloadedVersion, lastKnownVersion, lastActivationTime, httpCode);
}
if (httpCode != HttpServletResponse.SC_OK) {
boolean logError = httpCode != HttpServletResponse.SC_NOT_MODIFIED;
throw restErrorUtil.createRESTException(httpCode, logMsg, logError);
}
if (LOG.isDebugEnabled()) {
LOG.debug("<== TagREST.getSecureServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + ", " + lastActivationTime + ", " + pluginId + ")");
}
return ret;
}
use of org.apache.ranger.entity.XXService in project ranger by apache.
the class RangerDataHistService method createObjectDataHistory.
public void createObjectDataHistory(RangerBaseModelObject baseModelObj, String action) {
if (baseModelObj == null || action == null) {
throw restErrorUtil.createRESTException("Error while creating DataHistory. " + "Object or Action can not be null.", MessageEnums.DATA_NOT_FOUND);
}
Integer classType = null;
String objectName = null;
String content = null;
Long objectId = baseModelObj.getId();
String objectGuid = baseModelObj.getGuid();
Date currentDate = DateUtil.getUTCDate();
XXDataHist xDataHist = new XXDataHist();
xDataHist.setObjectId(baseModelObj.getId());
xDataHist.setObjectGuid(objectGuid);
xDataHist.setCreateTime(currentDate);
xDataHist.setAction(action);
xDataHist.setVersion(baseModelObj.getVersion());
xDataHist.setUpdateTime(currentDate);
xDataHist.setFromTime(currentDate);
if (baseModelObj instanceof RangerServiceDef) {
RangerServiceDef serviceDef = (RangerServiceDef) baseModelObj;
objectName = serviceDef.getName();
classType = AppConstants.CLASS_TYPE_XA_SERVICE_DEF;
content = writeObjectAsString(serviceDef);
} else if (baseModelObj instanceof RangerService) {
RangerService service = (RangerService) baseModelObj;
objectName = service.getName();
classType = AppConstants.CLASS_TYPE_XA_SERVICE;
content = writeObjectAsString(service);
} else if (baseModelObj instanceof RangerPolicy) {
RangerPolicy policy = (RangerPolicy) baseModelObj;
objectName = policy.getName();
classType = AppConstants.CLASS_TYPE_RANGER_POLICY;
XXService xXService = daoMgr.getXXService().findByName(policy.getService());
XXServiceDef xxServiceDef = null;
if (xXService != null) {
xxServiceDef = daoMgr.getXXServiceDef().getById(xXService.getType());
}
if (xxServiceDef != null) {
policy.setServiceType(xxServiceDef.getName());
}
content = writeObjectAsString(policy);
}
xDataHist.setObjectClassType(classType);
xDataHist.setObjectName(objectName);
xDataHist.setContent(content);
xDataHist = daoMgr.getXXDataHist().create(xDataHist);
if (ACTION_UPDATE.equalsIgnoreCase(action) || ACTION_DELETE.equalsIgnoreCase(action)) {
XXDataHist prevHist = daoMgr.getXXDataHist().findLatestByObjectClassTypeAndObjectId(classType, objectId);
if (prevHist == null) {
throw restErrorUtil.createRESTException("Error updating DataHistory Object. ObjectName: " + objectName, MessageEnums.DATA_NOT_UPDATABLE);
}
prevHist.setUpdateTime(currentDate);
prevHist.setToTime(currentDate);
prevHist.setObjectName(objectName);
prevHist = daoMgr.getXXDataHist().update(prevHist);
}
}
Aggregations