Search in sources :

Example 16 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class TagDBStore method getServiceTagsIfUpdated.

@Override
public ServiceTags getServiceTagsIfUpdated(String serviceName, Long lastKnownVersion) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> TagDBStore.getServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + ")");
    }
    ServiceTags ret = null;
    XXService xxService = daoManager.getXXService().findByName(serviceName);
    if (xxService == null) {
        LOG.error("Requested Service not found. serviceName=" + serviceName);
        throw restErrorUtil.createRESTException(HttpServletResponse.SC_NOT_FOUND, RangerServiceNotFoundException.buildExceptionMsg(serviceName), false);
    }
    XXServiceVersionInfo serviceVersionInfoDbObj = daoManager.getXXServiceVersionInfo().findByServiceName(serviceName);
    if (serviceVersionInfoDbObj == null) {
        LOG.warn("serviceVersionInfo does not exist. name=" + serviceName);
    }
    if (lastKnownVersion == null || serviceVersionInfoDbObj == null || serviceVersionInfoDbObj.getTagVersion() == null || !lastKnownVersion.equals(serviceVersionInfoDbObj.getTagVersion())) {
        ret = RangerServiceTagsCache.getInstance().getServiceTags(serviceName, xxService.getId(), this);
    }
    if (ret != null && lastKnownVersion != null && lastKnownVersion.equals(ret.getTagVersion())) {
        // ServiceTags are not changed
        ret = null;
    }
    if (LOG.isDebugEnabled()) {
        RangerServiceTagsCache.getInstance().dump();
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== TagDBStore.getServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + "): count=" + ((ret == null || ret.getTags() == null) ? 0 : ret.getTags().size()));
    }
    return ret;
}
Also used : ServiceTags(org.apache.ranger.plugin.util.ServiceTags) XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo)

Example 17 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class TagREST method getServiceTagsIfUpdated.

// This API is typically used by plug-in to get selected tagged resources from RangerAdmin
@GET
@Path(TagRESTConstants.TAGS_DOWNLOAD + "{serviceName}")
@Produces({ "application/json", "application/xml" })
public 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, @Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> TagREST.getServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + ", " + lastActivationTime + ", " + pluginId + ")");
    }
    ServiceTags ret = null;
    int httpCode = HttpServletResponse.SC_OK;
    String logMsg = null;
    Long downloadedVersion = null;
    try {
        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();
        }
    } 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.getServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + ", " + lastActivationTime + ", " + pluginId + ")");
    }
    return ret;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 18 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class TagAdminRESTSink method doUpload.

private ServiceTags doUpload(ServiceTags serviceTags) throws Exception {
    if (isKerberized) {
        try {
            UserGroupInformation userGroupInformation = UserGroupInformation.getLoginUser();
            if (userGroupInformation != null) {
                try {
                    userGroupInformation.checkTGTAndReloginFromKeytab();
                } catch (IOException ioe) {
                    LOG.error("Error renewing TGT and relogin", ioe);
                    userGroupInformation = null;
                }
            }
            if (userGroupInformation != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Using Principal = " + userGroupInformation.getUserName());
                }
                final ServiceTags serviceTag = serviceTags;
                ServiceTags ret = userGroupInformation.doAs(new PrivilegedAction<ServiceTags>() {

                    @Override
                    public ServiceTags run() {
                        try {
                            return uploadServiceTags(serviceTag);
                        } catch (Exception e) {
                            LOG.error("Upload of service-tags failed with message ", e);
                        }
                        return null;
                    }
                });
                return ret;
            } else {
                LOG.error("Failed to get UserGroupInformation.getLoginUser()");
                // This will cause retries !!!
                return null;
            }
        } catch (Exception e) {
            LOG.error("Upload of service-tags failed with message ", e);
        }
        return null;
    } else {
        return uploadServiceTags(serviceTags);
    }
}
Also used : ServiceTags(org.apache.ranger.plugin.util.ServiceTags) IOException(java.io.IOException) IOException(java.io.IOException) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 19 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class AtlasNotificationMapper method buildServiceTagsForEntityDeleteNotification.

@SuppressWarnings("unchecked")
private static ServiceTags buildServiceTagsForEntityDeleteNotification(RangerAtlasEntityWithTags entityWithTags) {
    final ServiceTags ret;
    RangerAtlasEntity entity = entityWithTags.getEntity();
    String guid = entity.getGuid();
    if (StringUtils.isNotBlank(guid)) {
        ret = new ServiceTags();
        RangerServiceResource serviceResource = new RangerServiceResource();
        serviceResource.setGuid(guid);
        ret.getServiceResources().add(serviceResource);
    } else {
        ret = buildServiceTags(entityWithTags, null);
        if (ret != null) {
            // tag-definitions should NOT be deleted as part of service-resource delete
            ret.setTagDefinitions(MapUtils.EMPTY_MAP);
            // Ranger deletes tags associated with deleted service-resource
            ret.setTags(MapUtils.EMPTY_MAP);
        }
    }
    if (ret != null) {
        ret.setOp(ServiceTags.OP_DELETE);
    }
    return ret;
}
Also used : RangerAtlasEntity(org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) ServiceTags(org.apache.ranger.plugin.util.ServiceTags)

Example 20 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class AtlasRESTTagSource method synchUp.

public void synchUp() {
    List<RangerAtlasEntityWithTags> rangerAtlasEntities = getAtlasActiveEntities();
    if (CollectionUtils.isNotEmpty(rangerAtlasEntities)) {
        if (LOG.isDebugEnabled()) {
            for (RangerAtlasEntityWithTags element : rangerAtlasEntities) {
                LOG.debug(element);
            }
        }
        Map<String, ServiceTags> serviceTagsMap = AtlasNotificationMapper.processAtlasEntities(rangerAtlasEntities);
        if (MapUtils.isNotEmpty(serviceTagsMap)) {
            for (Map.Entry<String, ServiceTags> entry : serviceTagsMap.entrySet()) {
                if (LOG.isDebugEnabled()) {
                    Gson gsonBuilder = new GsonBuilder().setDateFormat("yyyyMMdd-HH:mm:ss.SSS-Z").setPrettyPrinting().create();
                    String serviceTagsString = gsonBuilder.toJson(entry.getValue());
                    LOG.debug("serviceTags=" + serviceTagsString);
                }
                updateSink(entry.getValue());
            }
        }
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) Gson(com.google.gson.Gson) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ServiceTags (org.apache.ranger.plugin.util.ServiceTags)25 WebApplicationException (javax.ws.rs.WebApplicationException)10 XXService (org.apache.ranger.entity.XXService)9 Test (org.junit.Test)8 ExpectedException (org.junit.rules.ExpectedException)8 XXServiceDef (org.apache.ranger.entity.XXServiceDef)7 RangerService (org.apache.ranger.plugin.model.RangerService)7 XXServiceDao (org.apache.ranger.db.XXServiceDao)6 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)6 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 RangerAtlasEntity (org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity)3 Gson (com.google.gson.Gson)2 IOException (java.io.IOException)2 Map (java.util.Map)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)2 XXServiceVersionInfo (org.apache.ranger.entity.XXServiceVersionInfo)2