Search in sources :

Example 56 with RangerPolicy

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

the class ServiceREST method getPolicyByGuid.

private RangerPolicy getPolicyByGuid(String guid) {
    RangerPolicy ret = null;
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceREST.getPolicyByGuid(" + guid + ")");
    }
    SearchFilter filter = new SearchFilter();
    filter.setParam(SearchFilter.GUID, guid);
    List<RangerPolicy> policies = getPolicies(filter);
    if (CollectionUtils.isNotEmpty(policies)) {
        ret = policies.get(0);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceREST.getPolicyByGuid(" + guid + ")" + ret);
    }
    return ret;
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) SearchFilter(org.apache.ranger.plugin.util.SearchFilter)

Example 57 with RangerPolicy

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

the class ServiceREST method getPoliciesInJson.

@GET
@Path("/policies/exportJson")
@Produces("text/json")
public void getPoliciesInJson(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("checkPoliciesExists") Boolean checkPoliciesExists) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceREST.getPoliciesInJson()");
    }
    RangerPerfTracer perf = null;
    SearchFilter filter = searchUtil.getSearchFilter(request, policyService.sortFields);
    try {
        if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.getPoliciesInJson()");
        }
        if (checkPoliciesExists == null) {
            checkPoliciesExists = false;
        }
        List<RangerPolicy> policyLists = new ArrayList<RangerPolicy>();
        policyLists = getAllFilteredPolicyList(filter, request, policyLists);
        if (CollectionUtils.isNotEmpty(policyLists)) {
            for (RangerPolicy rangerPolicy : policyLists) {
                if (rangerPolicy != null) {
                    ensureAdminAndAuditAccess(rangerPolicy);
                }
            }
            bizUtil.blockAuditorRoleUser();
            svcStore.getPoliciesInJson(policyLists, response);
        } else {
            checkPoliciesExists = true;
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
            LOG.error("There is no Policy to Export!!");
        }
        if (!checkPoliciesExists) {
            RangerExportPolicyList rangerExportPolicyList = new RangerExportPolicyList();
            svcStore.putMetaDataInfo(rangerExportPolicyList);
            String metaDataInfo = new ObjectMapper().writeValueAsString(rangerExportPolicyList.getMetaDataInfo());
            List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>();
            XXTrxLog xxTrxLog = new XXTrxLog();
            xxTrxLog.setAction("EXPORT JSON");
            xxTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
            xxTrxLog.setPreviousValue(metaDataInfo);
            trxLogList.add(xxTrxLog);
            bizUtil.createTrxLog(trxLogList);
        }
    } catch (WebApplicationException excp) {
        throw excp;
    } catch (Throwable excp) {
        LOG.error("Error while exporting policy file!!", excp);
        throw restErrorUtil.createRESTException(excp.getMessage());
    } finally {
        RangerPerfTracer.log(perf);
    }
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) WebApplicationException(javax.ws.rs.WebApplicationException) RangerPerfTracer(org.apache.ranger.plugin.util.RangerPerfTracer) RangerExportPolicyList(org.apache.ranger.view.RangerExportPolicyList) ArrayList(java.util.ArrayList) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) VXString(org.apache.ranger.view.VXString) XXTrxLog(org.apache.ranger.entity.XXTrxLog) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 58 with RangerPolicy

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

the class ServiceREST method deletePoliciesProvidedInServiceMap.

private void deletePoliciesProvidedInServiceMap(List<String> sourceServices, List<String> destinationServices) {
    int totalDeletedPilicies = 0;
    if (CollectionUtils.isNotEmpty(sourceServices) && CollectionUtils.isNotEmpty(destinationServices)) {
        RangerPolicyValidator validator = validatorFactory.getPolicyValidator(svcStore);
        for (int i = 0; i < sourceServices.size(); i++) {
            if (!destinationServices.get(i).isEmpty()) {
                final RangerPolicyList servicePolicies = getServicePolicies(destinationServices.get(i), new SearchFilter());
                if (servicePolicies != null) {
                    List<RangerPolicy> rangerPolicyList = servicePolicies.getPolicies();
                    if (CollectionUtils.isNotEmpty(rangerPolicyList)) {
                        for (RangerPolicy rangerPolicy : rangerPolicyList) {
                            if (rangerPolicy != null) {
                                try {
                                    validator.validate(rangerPolicy.getId(), Action.DELETE);
                                    ensureAdminAccess(rangerPolicy);
                                    bizUtil.blockAuditorRoleUser();
                                    svcStore.deletePolicy(rangerPolicy);
                                    totalDeletedPilicies = totalDeletedPilicies + 1;
                                    if (LOG.isDebugEnabled()) {
                                        LOG.debug("Policy " + rangerPolicy.getName() + " deleted successfully.");
                                        LOG.debug("TotalDeletedPilicies: " + totalDeletedPilicies);
                                    }
                                } catch (Throwable excp) {
                                    LOG.error("deletePolicy(" + rangerPolicy.getId() + ") failed", excp);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Total Deleted Policy : " + totalDeletedPilicies);
    }
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) RangerPolicyList(org.apache.ranger.view.RangerPolicyList) RangerPolicyValidator(org.apache.ranger.plugin.model.validation.RangerPolicyValidator)

Example 59 with RangerPolicy

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

the class ServiceREST method filterServicePolicies.

private ServicePolicies filterServicePolicies(ServicePolicies servicePolicies) {
    ServicePolicies ret = null;
    boolean containsDisabledResourcePolicies = false;
    boolean containsDisabledTagPolicies = false;
    if (servicePolicies != null) {
        List<RangerPolicy> policies = null;
        policies = servicePolicies.getPolicies();
        if (CollectionUtils.isNotEmpty(policies)) {
            for (RangerPolicy policy : policies) {
                if (!policy.getIsEnabled()) {
                    containsDisabledResourcePolicies = true;
                    break;
                }
            }
        }
        if (servicePolicies.getTagPolicies() != null) {
            policies = servicePolicies.getTagPolicies().getPolicies();
            if (CollectionUtils.isNotEmpty(policies)) {
                for (RangerPolicy policy : policies) {
                    if (!policy.getIsEnabled()) {
                        containsDisabledTagPolicies = true;
                        break;
                    }
                }
            }
        }
        if (!containsDisabledResourcePolicies && !containsDisabledTagPolicies) {
            ret = servicePolicies;
        } else {
            ret = new ServicePolicies();
            ret.setServiceDef(servicePolicies.getServiceDef());
            ret.setServiceId(servicePolicies.getServiceId());
            ret.setServiceName(servicePolicies.getServiceName());
            ret.setPolicyVersion(servicePolicies.getPolicyVersion());
            ret.setPolicyUpdateTime(servicePolicies.getPolicyUpdateTime());
            ret.setPolicies(servicePolicies.getPolicies());
            ret.setTagPolicies(servicePolicies.getTagPolicies());
            if (containsDisabledResourcePolicies) {
                List<RangerPolicy> filteredPolicies = new ArrayList<RangerPolicy>();
                for (RangerPolicy policy : servicePolicies.getPolicies()) {
                    if (policy.getIsEnabled()) {
                        filteredPolicies.add(policy);
                    }
                }
                ret.setPolicies(filteredPolicies);
            }
            if (containsDisabledTagPolicies) {
                ServicePolicies.TagPolicies tagPolicies = new ServicePolicies.TagPolicies();
                tagPolicies.setServiceDef(servicePolicies.getTagPolicies().getServiceDef());
                tagPolicies.setServiceId(servicePolicies.getTagPolicies().getServiceId());
                tagPolicies.setServiceName(servicePolicies.getTagPolicies().getServiceName());
                tagPolicies.setPolicyVersion(servicePolicies.getTagPolicies().getPolicyVersion());
                tagPolicies.setPolicyUpdateTime(servicePolicies.getTagPolicies().getPolicyUpdateTime());
                List<RangerPolicy> filteredPolicies = new ArrayList<RangerPolicy>();
                for (RangerPolicy policy : servicePolicies.getTagPolicies().getPolicies()) {
                    if (policy.getIsEnabled()) {
                        filteredPolicies.add(policy);
                    }
                }
                tagPolicies.setPolicies(filteredPolicies);
                ret.setTagPolicies(tagPolicies);
            }
        }
    }
    return ret;
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) ServicePolicies(org.apache.ranger.plugin.util.ServicePolicies) ArrayList(java.util.ArrayList)

Example 60 with RangerPolicy

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

the class ServiceREST method getPoliciesInExcel.

@GET
@Path("/policies/downloadExcel")
@Produces("application/ms-excel")
public void getPoliciesInExcel(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceREST.getPoliciesInExcel()");
    }
    RangerPerfTracer perf = null;
    SearchFilter filter = searchUtil.getSearchFilter(request, policyService.sortFields);
    try {
        if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.getPoliciesInExcel()");
        }
        List<RangerPolicy> policyLists = new ArrayList<RangerPolicy>();
        policyLists = getAllFilteredPolicyList(filter, request, policyLists);
        if (CollectionUtils.isNotEmpty(policyLists)) {
            for (RangerPolicy rangerPolicy : policyLists) {
                if (rangerPolicy != null) {
                    ensureAdminAndAuditAccess(rangerPolicy);
                }
            }
            bizUtil.blockAuditorRoleUser();
            svcStore.getPoliciesInExcel(policyLists, response);
        } else {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
            LOG.error("No policies found to download!");
        }
        RangerExportPolicyList rangerExportPolicyList = new RangerExportPolicyList();
        svcStore.putMetaDataInfo(rangerExportPolicyList);
        String metaDataInfo = new ObjectMapper().writeValueAsString(rangerExportPolicyList.getMetaDataInfo());
        List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>();
        XXTrxLog xxTrxLog = new XXTrxLog();
        xxTrxLog.setAction("EXPORT EXCEL");
        xxTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
        xxTrxLog.setPreviousValue(metaDataInfo);
        trxLogList.add(xxTrxLog);
        bizUtil.createTrxLog(trxLogList);
    } catch (WebApplicationException excp) {
        throw excp;
    } catch (Throwable excp) {
        LOG.error("Error while downloading policy report", excp);
        throw restErrorUtil.createRESTException(excp.getMessage());
    } finally {
        RangerPerfTracer.log(perf);
    }
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) WebApplicationException(javax.ws.rs.WebApplicationException) RangerPerfTracer(org.apache.ranger.plugin.util.RangerPerfTracer) RangerExportPolicyList(org.apache.ranger.view.RangerExportPolicyList) ArrayList(java.util.ArrayList) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) VXString(org.apache.ranger.view.VXString) XXTrxLog(org.apache.ranger.entity.XXTrxLog) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)196 ArrayList (java.util.ArrayList)78 Test (org.junit.Test)73 RangerService (org.apache.ranger.plugin.model.RangerService)52 VXString (org.apache.ranger.view.VXString)48 HashMap (java.util.HashMap)38 RangerPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem)36 RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)33 SearchFilter (org.apache.ranger.plugin.util.SearchFilter)30 WebApplicationException (javax.ws.rs.WebApplicationException)29 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)27 RangerPolicyItemAccess (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess)26 Path (javax.ws.rs.Path)23 Produces (javax.ws.rs.Produces)22 RangerPerfTracer (org.apache.ranger.plugin.util.RangerPerfTracer)20 Date (java.util.Date)19 IOException (java.io.IOException)18 XXService (org.apache.ranger.entity.XXService)18 ServicePolicies (org.apache.ranger.plugin.util.ServicePolicies)16 RangerPolicyList (org.apache.ranger.view.RangerPolicyList)15