TagREST { @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, @DefaultValue("false") @QueryParam(RangerRESTUtils.REST_PARAM_SUPPORTS_TAG_DELTAS) Boolean supportsTagDeltas, @DefaultValue("") @QueryParam(RangerRESTUtils.REST_PARAM_CAPABILITIES) String pluginCapabilities, @Context HttpServletRequest request) { if(LOG.isDebugEnabled()) { LOG.debug("==> TagREST.getSecureServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + ", " + lastActivationTime + ", " + pluginId + ", " + supportsTagDeltas + ")"); } 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; String clusterName = null; if (request != null) { clusterName = !StringUtils.isEmpty(request.getParameter(SearchFilter.CLUSTER_NAME)) ? request.getParameter(SearchFilter.CLUSTER_NAME) : ""; } 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, !supportsTagDeltas); 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, clusterName, pluginCapabilities); } 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 + ", " + supportsTagDeltas + ")"); } return ret; } TagREST(); @PostConstruct void initStore(); @POST @Path(TagRESTConstants.TAGDEFS_RESOURCE) @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTagDef createTagDef(RangerTagDef tagDef, @DefaultValue("true") @QueryParam("updateIfExists") boolean updateIfExists); @PUT @Path(TagRESTConstants.TAGDEF_RESOURCE + "{id}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTagDef updateTagDef(@PathParam("id") Long id, RangerTagDef tagDef); @DELETE @Path(TagRESTConstants.TAGDEF_RESOURCE + "{id}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") void deleteTagDef(@PathParam("id") Long id); @DELETE @Path(TagRESTConstants.TAGDEF_RESOURCE + "guid/{guid}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") void deleteTagDefByGuid(@PathParam("guid") String guid); @GET @Path(TagRESTConstants.TAGDEF_RESOURCE + "{id}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTagDef getTagDef(@PathParam("id") Long id); @GET @Path(TagRESTConstants.TAGDEF_RESOURCE + "guid/{guid}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTagDef getTagDefByGuid(@PathParam("guid") String guid); @GET @Path(TagRESTConstants.TAGDEF_RESOURCE + "name/{name}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTagDef getTagDefByName(@PathParam("name") String name); @GET @Path(TagRESTConstants.TAGDEFS_RESOURCE) @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") List<RangerTagDef> getAllTagDefs(); @GET @Path(TagRESTConstants.TAGTYPES_RESOURCE) @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") List<String> getTagTypes(); @POST @Path(TagRESTConstants.TAGS_RESOURCE) @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTag createTag(RangerTag tag, @DefaultValue("true") @QueryParam("updateIfExists") boolean updateIfExists); @PUT @Path(TagRESTConstants.TAG_RESOURCE + "{id}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTag updateTag(@PathParam("id") Long id, RangerTag tag); @PUT @Path(TagRESTConstants.TAG_RESOURCE + "guid/{guid}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTag updateTagByGuid(@PathParam("guid") String guid, RangerTag tag); @DELETE @Path(TagRESTConstants.TAG_RESOURCE + "{id}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") void deleteTag(@PathParam("id") Long id); @DELETE @Path(TagRESTConstants.TAG_RESOURCE + "guid/{guid}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") void deleteTagByGuid(@PathParam("guid") String guid); @GET @Path(TagRESTConstants.TAG_RESOURCE + "{id}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTag getTag(@PathParam("id") Long id); @GET @Path(TagRESTConstants.TAG_RESOURCE + "guid/{guid}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTag getTagByGuid(@PathParam("guid") String guid); @GET @Path(TagRESTConstants.TAGS_RESOURCE + "type/{type}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") List<RangerTag> getTagsByType(@PathParam("type") String type); @GET @Path(TagRESTConstants.TAGS_RESOURCE) @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") List<RangerTag> getAllTags(); @POST @Path(TagRESTConstants.RESOURCES_RESOURCE) @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerServiceResource createServiceResource(RangerServiceResource resource, @DefaultValue("true") @QueryParam("updateIfExists") boolean updateIfExists); @PUT @Path(TagRESTConstants.RESOURCE_RESOURCE + "{id}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerServiceResource updateServiceResource(@PathParam("id") Long id, RangerServiceResource resource); @PUT @Path(TagRESTConstants.RESOURCE_RESOURCE + "guid/{guid}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerServiceResource updateServiceResourceByGuid(@PathParam("guid") String guid, RangerServiceResource resource); @DELETE @Path(TagRESTConstants.RESOURCE_RESOURCE + "{id}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") void deleteServiceResource(@PathParam("id") Long id); @DELETE @Path(TagRESTConstants.RESOURCE_RESOURCE + "guid/{guid}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") void deleteServiceResourceByGuid(@PathParam("guid") String guid, @DefaultValue("false") @QueryParam("deleteReferences") boolean deleteReferences); @GET @Path(TagRESTConstants.RESOURCE_RESOURCE + "{id}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerServiceResource getServiceResource(@PathParam("id") Long id); @GET @Path(TagRESTConstants.RESOURCE_RESOURCE + "guid/{guid}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerServiceResource getServiceResourceByGuid(@PathParam("guid") String guid); @GET @Path(TagRESTConstants.RESOURCES_RESOURCE + "service/{serviceName}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") List<RangerServiceResource> getServiceResourcesByService(@PathParam("serviceName") String serviceName); @GET @Path(TagRESTConstants.RESOURCE_RESOURCE + "service/{serviceName}/signature/{resourceSignature}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerServiceResource getServiceResourceByServiceAndResourceSignature(@PathParam("serviceName") String serviceName,
                                                                       @PathParam("resourceSignature") String resourceSignature); @GET @Path(TagRESTConstants.RESOURCES_RESOURCE) @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") List<RangerServiceResource> getAllServiceResources(); @POST @Path(TagRESTConstants.TAGRESOURCEMAPS_RESOURCE) @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTagResourceMap createTagResourceMap(@QueryParam("tag-guid") String tagGuid, @QueryParam("resource-guid") String resourceGuid,
                                                     @DefaultValue("false") @QueryParam("lenient") boolean lenient); @DELETE @Path(TagRESTConstants.TAGRESOURCEMAP_RESOURCE + "{id}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") void deleteTagResourceMap(@PathParam("id") Long id); @DELETE @Path(TagRESTConstants.TAGRESOURCEMAP_RESOURCE + "guid/{guid}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") void deleteTagResourceMapByGuid(@PathParam("guid") String guid); @DELETE @Path(TagRESTConstants.TAGRESOURCEMAPS_RESOURCE) @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") void deleteTagResourceMap(@QueryParam("tag-guid") String tagGuid, @QueryParam("resource-guid") String resourceGuid); @GET @Path(TagRESTConstants.TAGRESOURCEMAP_RESOURCE + "{id}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTagResourceMap getTagResourceMap(@PathParam("id") Long id); @GET @Path(TagRESTConstants.TAGRESOURCEMAP_RESOURCE + "guid/{guid}") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTagResourceMap getTagResourceMapByGuid(@PathParam("guid") String guid); @GET @Path(TagRESTConstants.TAGRESOURCEMAP_RESOURCE + "tag-resource-guid") @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") RangerTagResourceMap getTagResourceMap(@QueryParam("tagGuid") String tagGuid, @QueryParam("resourceGuid") String resourceGuid); @GET @Path(TagRESTConstants.TAGRESOURCEMAPS_RESOURCE) @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") List<RangerTagResourceMap> getAllTagResourceMaps(); @PUT @Path(TagRESTConstants.IMPORT_SERVICETAGS_RESOURCE) @Produces({ "application/json", "application/xml" }) @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") void importServiceTags(ServiceTags serviceTags); @GET @Path(TagRESTConstants.TAGS_DOWNLOAD + "{serviceName}") @Produces({ "application/json", "application/xml" }) ServiceTags getServiceTagsIfUpdated(@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,
                                               @DefaultValue("false") @QueryParam(RangerRESTUtils.REST_PARAM_SUPPORTS_TAG_DELTAS) Boolean supportsTagDeltas,
                                               @DefaultValue("") @QueryParam(RangerRESTUtils.REST_PARAM_CAPABILITIES) String pluginCapabilities,
                                               @Context HttpServletRequest request); @GET @Path(TagRESTConstants.TAGS_SECURE_DOWNLOAD + "{serviceName}") @Produces({ "application/json", "application/xml" }) 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,
                                                     @DefaultValue("false") @QueryParam(RangerRESTUtils.REST_PARAM_SUPPORTS_TAG_DELTAS) Boolean supportsTagDeltas,
                                                     @DefaultValue("") @QueryParam(RangerRESTUtils.REST_PARAM_CAPABILITIES) String pluginCapabilities,
                                                     @Context HttpServletRequest request); @DELETE @Path("/server/tagdeltas") @PreAuthorize("hasRole('ROLE_SYS_ADMIN')") void deleteTagDeltas(@DefaultValue("3") @QueryParam("days") Integer olderThan, @Context HttpServletRequest request); static final String Allowed_User_List_For_Tag_Download; }
@Test public void test56getSecureServiceTagsIfUpdatedIsAllowedFalse() { boolean isAdmin = false; boolean isKeyAdmin = false; boolean isAllowed = false; 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); Mockito.when(restErrorUtil.createRESTException(Mockito.anyInt(), Mockito.anyString(), Mockito.anyBoolean())).thenThrow(new WebApplicationException()); thrown.expect(WebApplicationException.class); tagREST.getSecureServiceTagsIfUpdated(serviceName, lastKnownVersion, 0L, pluginId, false, capabilityVector, null); 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); Mockito.verify(restErrorUtil).createRESTException(Mockito.anyInt(), Mockito.anyString(), Mockito.anyBoolean()); }