Java Code Examples for org.apache.ranger.plugin.util.ServicePolicies#getPolicyDeltas()

The following examples show how to use org.apache.ranger.plugin.util.ServicePolicies#getPolicyDeltas() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServiceREST.java    From ranger with Apache License 2.0 4 votes vote down vote up
@GET
@Path("/policies/download/{serviceName}")
@Produces({ "application/json", "application/xml" })
public ServicePolicies getServicePoliciesIfUpdated(
		@PathParam("serviceName") String serviceName,
		@QueryParam("lastKnownVersion") Long lastKnownVersion,
		@DefaultValue("0") @QueryParam("lastActivationTime") Long lastActivationTime,
		@QueryParam("pluginId") String pluginId,
		@DefaultValue("") @QueryParam("clusterName") String clusterName,
		@DefaultValue("") @QueryParam("zoneName") String zoneName,
		@DefaultValue("false") @QueryParam("supportsPolicyDeltas") Boolean supportsPolicyDeltas,
		@DefaultValue("") @QueryParam("pluginCapabilities") String pluginCapabilities,
		@Context HttpServletRequest request) throws Exception {
	if (LOG.isDebugEnabled()) {
		LOG.debug("==> ServiceREST.getServicePoliciesIfUpdated("
				+ serviceName + ", " + lastKnownVersion + ", "
				+ lastActivationTime + ", " + pluginId + ", "
				+ clusterName + ", " + supportsPolicyDeltas + ")");
	}

	ServicePolicies ret      = null;
	int             httpCode = HttpServletResponse.SC_OK;
	String          logMsg   = null;
	RangerPerfTracer perf    = null;
	Long downloadedVersion   = null;
	boolean isValid          = false;

	try {
		bizUtil.failUnauthenticatedIfNotAllowed();

		isValid = serviceUtil.isValidateHttpsAuthentication(serviceName, request);
	} catch (WebApplicationException webException) {
		httpCode = webException.getResponse().getStatus();
		logMsg = webException.getResponse().getEntity().toString();
	} catch (Exception e) {
		httpCode = HttpServletResponse.SC_BAD_REQUEST;
		logMsg = e.getMessage();
	}
	if (isValid) {
		if (lastKnownVersion == null) {
			lastKnownVersion = Long.valueOf(-1);
		}

		try {
			if(RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
				perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.getServicePoliciesIfUpdated(serviceName=" + serviceName + ",lastKnownVersion=" + lastKnownVersion + ",lastActivationTime=" + lastActivationTime + ")");
			}
			ServicePolicies servicePolicies = svcStore.getServicePoliciesIfUpdated(serviceName, lastKnownVersion, !supportsPolicyDeltas);

			if (servicePolicies == null) {
				downloadedVersion = lastKnownVersion;
				httpCode = HttpServletResponse.SC_NOT_MODIFIED;
				logMsg = "No change since last update";
			} else {
				Map<String, RangerSecurityZone.RangerSecurityZoneService> securityZones = zoneStore.getSecurityZonesForService(serviceName);
				ServicePolicies updatedServicePolicies = servicePolicies;
				if (MapUtils.isNotEmpty(securityZones)) {
					updatedServicePolicies = RangerPolicyAdminCache.getUpdatedServicePoliciesForZones(servicePolicies, securityZones);
					patchAssociatedTagServiceInSecurityZoneInfos(updatedServicePolicies);
				}
				downloadedVersion = updatedServicePolicies.getPolicyVersion();
				if (lastKnownVersion == -1L || !supportsPolicyDeltas) {
					ret = filterServicePolicies(updatedServicePolicies);
				} else {
					ret = updatedServicePolicies;
				}
				ret.setServiceConfig(svcStore.getServiceConfigForPlugin(ret.getServiceId()));
				httpCode = HttpServletResponse.SC_OK;
				logMsg = "Returning " + (ret.getPolicies() != null ? ret.getPolicies().size() : (ret.getPolicyDeltas() != null ? ret.getPolicyDeltas().size() : 0)) + " policies. Policy version=" + ret.getPolicyVersion();
			}
		} catch (Throwable excp) {
			LOG.error("getServicePoliciesIfUpdated(" + serviceName + ", " + lastKnownVersion + ", " + lastActivationTime + ") failed", excp);

			httpCode = HttpServletResponse.SC_BAD_REQUEST;
			logMsg = excp.getMessage();
		} finally {
			createPolicyDownloadAudit(serviceName, lastKnownVersion, pluginId, httpCode, clusterName, zoneName, request);
			RangerPerfTracer.log(perf);
		}
	}
	assetMgr.createPluginInfo(serviceName, pluginId, request, RangerPluginInfo.ENTITY_TYPE_POLICIES, 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("<== ServiceREST.getServicePoliciesIfUpdated(" + serviceName + ", " + lastKnownVersion + ", " + lastActivationTime + ", " + pluginId + ", " + clusterName + ", " + supportsPolicyDeltas + "): count=" + ((ret == null || ret.getPolicies() == null) ? 0 : ret.getPolicies().size()));
	}

	return ret;
}
 
Example 2
Source File: RangerServicePoliciesCache.java    From ranger with Apache License 2.0 4 votes vote down vote up
ServicePolicies getLatestOrCached(String serviceName, ServiceStore serviceStore, Long lastKnownVersion, boolean needsBackwardCompatibility) throws Exception {
	if (LOG.isDebugEnabled()) {
		LOG.debug("==> RangerServicePoliciesCache.getLatestOrCached(lastKnownVersion=" + lastKnownVersion + ", " + needsBackwardCompatibility + ")");
	}
	ServicePolicies ret        = null;
	boolean         lockResult = false;

	try {
		final boolean isCacheReloadedByDQEvent;

		lockResult = lock.tryLock(waitTimeInSeconds, TimeUnit.SECONDS);

		if (lockResult) {
			isCacheReloadedByDQEvent = getLatest(serviceName, serviceStore, lastKnownVersion);

			if (isCacheReloadedByDQEvent) {
				if (LOG.isDebugEnabled()) {
					LOG.debug("ServicePolicies cache was completely loaded from database because of a disqualifying event - such as service-definition change!");
				}
			}

			if (needsBackwardCompatibility || isCacheReloadedByDQEvent
				|| lastKnownVersion == -1L || lastKnownVersion.equals(servicePolicies.getPolicyVersion())) {
				// Looking for all policies, or Some disqualifying change encountered
				if (LOG.isDebugEnabled()) {
					LOG.debug("All policies were requested, returning cached ServicePolicies");
				}
				ret = this.servicePolicies;
			} else {
				boolean         isDeltaCacheReinitialized = false;
				ServicePolicies servicePoliciesForDeltas  = this.deltaCache != null ? this.deltaCache.getServicePolicyDeltasFromVersion(lastKnownVersion) : null;

				if (servicePoliciesForDeltas == null) {
					servicePoliciesForDeltas  = serviceStore.getServicePolicyDeltas(serviceName, lastKnownVersion);
					isDeltaCacheReinitialized = true;
				}
				if (servicePoliciesForDeltas != null && servicePoliciesForDeltas.getPolicyDeltas() != null) {
					if (LOG.isDebugEnabled()) {
						LOG.debug("Deltas were requested. Returning deltas from lastKnownVersion:[" + lastKnownVersion + "]");
					}
					if (isDeltaCacheReinitialized) {
						this.deltaCache = new ServicePolicyDeltasCache(lastKnownVersion, servicePoliciesForDeltas);
					}
					ret = servicePoliciesForDeltas;
				} else {
					LOG.warn("Deltas were requested for service:[" + serviceName + "], but could not get them!! lastKnownVersion:[" + lastKnownVersion + "]; Returning cached ServicePolicies:[" + (servicePolicies != null ? servicePolicies.getPolicyVersion() : -1L) + "]");

					this.deltaCache = null;
					ret = this.servicePolicies;
				}
			}
		} else {
			if (LOG.isDebugEnabled()) {
				LOG.debug("Could not get lock in [" + waitTimeInSeconds + "] seconds, returning cached ServicePolicies");
			}
			ret = this.servicePolicies;
		}
	} catch (InterruptedException exception) {
		LOG.error("getLatestOrCached:lock got interrupted..", exception);
	} finally {
		if (lockResult) {
			lock.unlock();
		}
	}
	if (LOG.isTraceEnabled()) {
		LOG.trace("RangerServicePoliciesCache.getLatestOrCached - Returns ServicePolicies:[" + ret +"]");
	}

	if (LOG.isDebugEnabled()) {
		LOG.debug("<== RangerServicePoliciesCache.getLatestOrCached(lastKnownVersion=" + lastKnownVersion + ", " + needsBackwardCompatibility + ") : " + ret);
	}
	return ret;
}