Search in sources :

Example 66 with RangerService

use of org.apache.ranger.plugin.model.RangerService in project ranger by apache.

the class ServiceREST method validateResourcePoliciesRequest.

private String validateResourcePoliciesRequest(String serviceDefName, String serviceName, HttpServletRequest request, List<RangerService> services, Map<String, Object> resource) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceREST.validatePoliciesForResourceRequest(service-type=" + serviceDefName + ", service-name=" + serviceName + ")");
    }
    final String ret;
    if (MapUtils.isNotEmpty(request.getParameterMap())) {
        for (Map.Entry<String, String[]> e : request.getParameterMap().entrySet()) {
            String name = e.getKey();
            String[] values = e.getValue();
            if (!StringUtils.isEmpty(name) && !ArrayUtils.isEmpty(values) && name.startsWith(SearchFilter.RESOURCE_PREFIX)) {
                resource.put(name.substring(SearchFilter.RESOURCE_PREFIX.length()), values[0]);
            }
        }
    }
    if (MapUtils.isEmpty(resource)) {
        ret = "No resource specified";
    } else {
        RangerServiceDef serviceDef = null;
        try {
            serviceDef = svcStore.getServiceDefByName(serviceDefName);
        } catch (Exception e) {
            LOG.error("Invalid service-type:[" + serviceDefName + "]", e);
        }
        if (serviceDef == null) {
            ret = "Invalid service-type:[" + serviceDefName + "]";
        } else {
            Set<String> resourceDefNames = resource.keySet();
            RangerServiceDefHelper serviceDefHelper = new RangerServiceDefHelper(serviceDef);
            Set<List<RangerServiceDef.RangerResourceDef>> resourceHierarchies = serviceDefHelper.getResourceHierarchies(RangerPolicy.POLICY_TYPE_ACCESS, resourceDefNames);
            if (CollectionUtils.isEmpty(resourceHierarchies)) {
                ret = "Invalid resource specified: resource-names:" + resourceDefNames + " are not part of any valid resource hierarchy for service-type:[" + serviceDefName + "]";
            } else {
                if (StringUtils.isNotBlank(serviceName)) {
                    RangerService service = null;
                    try {
                        service = svcStore.getServiceByName(serviceName);
                    } catch (Exception e) {
                        LOG.error("Invalid service-name:[" + serviceName + "]");
                    }
                    if (service == null || !StringUtils.equals(service.getType(), serviceDefName)) {
                        ret = "Invalid service-name:[" + serviceName + "] or service-name is not of service-type:[" + serviceDefName + "]";
                    } else {
                        services.add(service);
                        ret = StringUtils.EMPTY;
                    }
                } else {
                    SearchFilter filter = new SearchFilter();
                    filter.setParam(SearchFilter.SERVICE_TYPE, serviceDefName);
                    List<RangerService> serviceList = null;
                    try {
                        serviceList = svcStore.getServices(filter);
                    } catch (Exception e) {
                        LOG.error("Cannot find service of service-type:[" + serviceDefName + "]");
                    }
                    if (CollectionUtils.isEmpty(serviceList) || serviceList.size() != 1) {
                        ret = "Either 0 or more than 1 services found for service-type :[" + serviceDefName + "]";
                    } else {
                        services.add(serviceList.get(0));
                        ret = StringUtils.EMPTY;
                    }
                }
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceREST.validatePoliciesForResourceRequest(service-type=" + serviceDefName + ", service-name=" + serviceName + ") : " + ret);
    }
    return ret;
}
Also used : SearchFilter(org.apache.ranger.plugin.util.SearchFilter) VXString(org.apache.ranger.view.VXString) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) RangerServiceDefHelper(org.apache.ranger.plugin.model.validation.RangerServiceDefHelper) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) RangerPluginInfoList(org.apache.ranger.view.RangerPluginInfoList) RangerServiceList(org.apache.ranger.view.RangerServiceList) ArrayList(java.util.ArrayList) VXPolicyLabelList(org.apache.ranger.view.VXPolicyLabelList) List(java.util.List) RangerExportPolicyList(org.apache.ranger.view.RangerExportPolicyList) RangerPolicyList(org.apache.ranger.view.RangerPolicyList) RangerServiceDefList(org.apache.ranger.view.RangerServiceDefList) RangerAPIList(org.apache.ranger.security.context.RangerAPIList) PList(org.apache.ranger.plugin.store.PList) RangerService(org.apache.ranger.plugin.model.RangerService) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 67 with RangerService

use of org.apache.ranger.plugin.model.RangerService in project ranger by apache.

the class ServiceREST method getServiceByName.

@GET
@Path("/services/name/{name}")
@Produces({ "application/json", "application/xml" })
@PreAuthorize("@rangerPreAuthSecurityHandler.isAPIAccessible(\"" + RangerAPIList.GET_SERVICE_BY_NAME + "\")")
public RangerService getServiceByName(@PathParam("name") String name) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceREST.getServiceByName(" + name + ")");
    }
    RangerService ret = null;
    RangerPerfTracer perf = null;
    try {
        if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.getService(serviceName=" + name + ")");
        }
        ret = svcStore.getServiceByName(name);
    } catch (WebApplicationException excp) {
        throw excp;
    } catch (Throwable excp) {
        LOG.error("getServiceByName(" + name + ") failed", excp);
        throw restErrorUtil.createRESTException(excp.getMessage());
    } finally {
        RangerPerfTracer.log(perf);
    }
    if (ret == null) {
        throw restErrorUtil.createRESTException(HttpServletResponse.SC_NOT_FOUND, "Not found", true);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceREST.getServiceByName(" + name + "): " + ret);
    }
    return ret;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RangerPerfTracer(org.apache.ranger.plugin.util.RangerPerfTracer) RangerService(org.apache.ranger.plugin.model.RangerService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 68 with RangerService

use of org.apache.ranger.plugin.model.RangerService in project ranger by apache.

the class ServiceDBStore method getServices.

@Override
public List<RangerService> getServices(SearchFilter filter) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.getServices()");
    }
    RangerServiceList serviceList = svcService.searchRangerServices(filter);
    predicateUtil.applyFilter(serviceList.getServices(), filter);
    List<RangerService> ret = serviceList.getServices();
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceDBStore.getServices()");
    }
    return ret;
}
Also used : RangerServiceList(org.apache.ranger.view.RangerServiceList) RangerService(org.apache.ranger.plugin.model.RangerService)

Example 69 with RangerService

use of org.apache.ranger.plugin.model.RangerService in project ranger by apache.

the class ServiceDBStore method deleteService.

@Override
public void deleteService(Long id) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.deleteService(" + id + ")");
    }
    RangerService service = getService(id);
    if (service == null) {
        throw new Exception("no service exists with ID=" + id);
    }
    List<XXPolicy> policies = daoMgr.getXXPolicy().findByServiceId(service.getId());
    RangerPolicy rangerPolicy = null;
    for (XXPolicy policy : policies) {
        LOG.info("Deleting Policy, policyName: " + policy.getName());
        rangerPolicy = getPolicy(policy.getId());
        deletePolicy(rangerPolicy);
    }
    XXServiceConfigMapDao configDao = daoMgr.getXXServiceConfigMap();
    List<XXServiceConfigMap> configs = configDao.findByServiceId(service.getId());
    for (XXServiceConfigMap configMap : configs) {
        configDao.remove(configMap);
    }
    Long version = service.getVersion();
    if (version == null) {
        version = Long.valueOf(1);
        LOG.info("Found Version Value: `null`, so setting value of version to 1, While updating object, version should not be null.");
    } else {
        version = Long.valueOf(version.longValue() + 1);
    }
    service.setVersion(version);
    svcService.delete(service);
    dataHistService.createObjectDataHistory(service, RangerDataHistService.ACTION_DELETE);
    List<XXTrxLog> trxLogList = svcService.getTransactionLog(service, RangerServiceService.OPERATION_DELETE_CONTEXT);
    bizUtil.createTrxLog(trxLogList);
}
Also used : XXServiceConfigMap(org.apache.ranger.entity.XXServiceConfigMap) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerService(org.apache.ranger.plugin.model.RangerService) XXTrxLog(org.apache.ranger.entity.XXTrxLog) XXPolicy(org.apache.ranger.entity.XXPolicy) XXServiceConfigMapDao(org.apache.ranger.db.XXServiceConfigMapDao) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException)

Example 70 with RangerService

use of org.apache.ranger.plugin.model.RangerService in project ranger by apache.

the class TestServiceDBStore method test37getPaginatedServicePolicies.

@Test
public void test37getPaginatedServicePolicies() throws Exception {
    SearchFilter filter = new SearchFilter();
    filter.setParam(SearchFilter.POLICY_NAME, "policyName");
    filter.setParam(SearchFilter.SERVICE_NAME, "serviceName");
    RangerService rangerService = rangerService();
    XXService xService = xService();
    XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class);
    Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao);
    Mockito.when(xServiceDao.getById(Id)).thenReturn(xService);
    // PList<RangerPolicy> dbRangerPolicyList =
    serviceDBStore.getPaginatedServicePolicies(rangerService.getId(), filter);
    Mockito.verify(daoManager).getXXService();
}
Also used : SearchFilter(org.apache.ranger.plugin.util.SearchFilter) RangerService(org.apache.ranger.plugin.model.RangerService) Test(org.junit.Test)

Aggregations

RangerService (org.apache.ranger.plugin.model.RangerService)163 Test (org.junit.Test)85 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)52 ArrayList (java.util.ArrayList)39 WebApplicationException (javax.ws.rs.WebApplicationException)30 XXServiceDef (org.apache.ranger.entity.XXServiceDef)26 SearchFilter (org.apache.ranger.plugin.util.SearchFilter)24 VXString (org.apache.ranger.view.VXString)24 XXService (org.apache.ranger.entity.XXService)23 Path (javax.ws.rs.Path)22 Produces (javax.ws.rs.Produces)21 Date (java.util.Date)20 HashMap (java.util.HashMap)16 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)15 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)14 IOException (java.io.IOException)12 GET (javax.ws.rs.GET)12 XXTrxLog (org.apache.ranger.entity.XXTrxLog)12 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)12