Search in sources :

Example 31 with ServicePolicies

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

the class RangerServicePoliciesCache method getServicePolicies.

public ServicePolicies getServicePolicies(String serviceName, Long serviceId, ServiceStore serviceStore) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerServicePoliciesCache.getServicePolicies(" + serviceName + ", " + serviceId + ")");
    }
    ServicePolicies ret = null;
    if (StringUtils.isNotBlank(serviceName) && serviceId != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("useServicePoliciesCache=" + useServicePoliciesCache);
        }
        ServicePolicies servicePolicies = null;
        if (!useServicePoliciesCache) {
            if (serviceStore != null) {
                try {
                    servicePolicies = serviceStore.getServicePolicies(serviceName);
                } catch (Exception exception) {
                    LOG.error("getServicePolicies(" + serviceName + "): failed to get latest policies from service-store", exception);
                }
            } else {
                LOG.error("getServicePolicies(" + serviceName + "): failed to get latest policies as service-store is null!");
            }
        } else {
            ServicePoliciesWrapper servicePoliciesWrapper = null;
            synchronized (this) {
                servicePoliciesWrapper = servicePoliciesMap.get(serviceName);
                if (servicePoliciesWrapper != null) {
                    if (!serviceId.equals(servicePoliciesWrapper.getServiceId())) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Service [" + serviceName + "] changed service-id from " + servicePoliciesWrapper.getServiceId() + " to " + serviceId);
                            LOG.debug("Recreating servicePoliciesWrapper for serviceName [" + serviceName + "]");
                        }
                        servicePoliciesMap.remove(serviceName);
                        servicePoliciesWrapper = null;
                    }
                }
                if (servicePoliciesWrapper == null) {
                    servicePoliciesWrapper = new ServicePoliciesWrapper(serviceId);
                    servicePoliciesMap.put(serviceName, servicePoliciesWrapper);
                }
            }
            if (serviceStore != null) {
                boolean refreshed = servicePoliciesWrapper.getLatestOrCached(serviceName, serviceStore);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("getLatestOrCached returned " + refreshed);
                }
            } else {
                LOG.error("getServicePolicies(" + serviceName + "): failed to get latest policies as service-store is null!");
            }
            servicePolicies = servicePoliciesWrapper.getServicePolicies();
        }
        ret = servicePolicies;
    } else {
        LOG.error("getServicePolicies() failed to get policies as serviceName is null or blank and/or serviceId is null!");
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerServicePoliciesCache.getServicePolicies(" + serviceName + ", " + serviceId + "): count=" + ((ret == null || ret.getPolicies() == null) ? 0 : ret.getPolicies().size()));
    }
    return ret;
}
Also used : ServicePolicies(org.apache.ranger.plugin.util.ServicePolicies)

Aggregations

ServicePolicies (org.apache.ranger.plugin.util.ServicePolicies)31 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)16 ArrayList (java.util.ArrayList)13 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)10 RangerPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem)8 RangerPolicyItemAccess (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess)8 RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)8 VXString (org.apache.ranger.view.VXString)7 IOException (java.io.IOException)5 Date (java.util.Date)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 UnknownHostException (java.net.UnknownHostException)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 AccessPolicy (org.apache.nifi.authorization.AccessPolicy)3 XXService (org.apache.ranger.entity.XXService)3 RangerService (org.apache.ranger.plugin.model.RangerService)3 RangerPolicyEngineImpl (org.apache.ranger.plugin.policyengine.RangerPolicyEngineImpl)3