Search in sources :

Example 6 with RangerPolicyList

use of org.apache.ranger.view.RangerPolicyList 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 7 with RangerPolicyList

use of org.apache.ranger.view.RangerPolicyList in project ranger by apache.

the class ServiceREST method deletePoliciesForResource.

private void deletePoliciesForResource(List<String> sourceServices, List<String> destinationServices, String resource, HttpServletRequest request, List<RangerPolicy> exportPolicies) {
    int totalDeletedPilicies = 0;
    if (CollectionUtils.isNotEmpty(sourceServices) && CollectionUtils.isNotEmpty(destinationServices)) {
        Set<String> exportedPolicyNames = new HashSet<String>();
        if (CollectionUtils.isNotEmpty(exportPolicies)) {
            for (RangerPolicy rangerPolicy : exportPolicies) {
                if (rangerPolicy != null) {
                    exportedPolicyNames.add(rangerPolicy.getName());
                }
            }
        }
        for (int i = 0; i < sourceServices.size(); i++) {
            if (!destinationServices.get(i).isEmpty()) {
                RangerPolicyList servicePolicies = null;
                servicePolicies = getServicePoliciesByName(destinationServices.get(i), request);
                if (servicePolicies != null) {
                    List<RangerPolicy> rangerPolicyList = servicePolicies.getPolicies();
                    if (CollectionUtils.isNotEmpty(rangerPolicyList)) {
                        for (RangerPolicy rangerPolicy : rangerPolicyList) {
                            if (rangerPolicy != null) {
                                Map<String, RangerPolicy.RangerPolicyResource> rangerPolicyResourceMap = rangerPolicy.getResources();
                                if (rangerPolicyResourceMap != null) {
                                    RangerPolicy.RangerPolicyResource rangerPolicyResource = null;
                                    if (rangerPolicyResourceMap.containsKey("path")) {
                                        rangerPolicyResource = rangerPolicyResourceMap.get("path");
                                    } else if (rangerPolicyResourceMap.containsKey("database")) {
                                        rangerPolicyResource = rangerPolicyResourceMap.get("database");
                                    }
                                    if (rangerPolicyResource != null) {
                                        if (CollectionUtils.isNotEmpty(rangerPolicyResource.getValues()) && rangerPolicyResource.getValues().size() > 1) {
                                            continue;
                                        }
                                    }
                                }
                                if (rangerPolicy.getId() != null) {
                                    if (!exportedPolicyNames.contains(rangerPolicy.getName())) {
                                        deletePolicy(rangerPolicy.getId());
                                        if (LOG.isDebugEnabled()) {
                                            LOG.debug("Policy " + rangerPolicy.getName() + " deleted successfully.");
                                        }
                                        totalDeletedPilicies = totalDeletedPilicies + 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) VXString(org.apache.ranger.view.VXString) RangerPolicyList(org.apache.ranger.view.RangerPolicyList) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) HashSet(java.util.HashSet)

Example 8 with RangerPolicyList

use of org.apache.ranger.view.RangerPolicyList in project ranger by apache.

the class ServiceREST method toRangerPolicyList.

private RangerPolicyList toRangerPolicyList(PList<RangerPolicy> policyList) {
    RangerPolicyList ret = new RangerPolicyList();
    if (policyList != null) {
        ret.setPolicies(policyList.getList());
        ret.setPageSize(policyList.getPageSize());
        ret.setResultSize(policyList.getResultSize());
        ret.setStartIndex(policyList.getStartIndex());
        ret.setTotalCount(policyList.getTotalCount());
        ret.setSortBy(policyList.getSortBy());
        ret.setSortType(policyList.getSortType());
    }
    return ret;
}
Also used : RangerPolicyList(org.apache.ranger.view.RangerPolicyList)

Example 9 with RangerPolicyList

use of org.apache.ranger.view.RangerPolicyList in project ranger by apache.

the class ServiceREST method toRangerPolicyList.

private RangerPolicyList toRangerPolicyList(List<RangerPolicy> policyList, SearchFilter filter) {
    RangerPolicyList ret = new RangerPolicyList();
    if (CollectionUtils.isNotEmpty(policyList)) {
        int totalCount = policyList.size();
        int startIndex = filter.getStartIndex();
        int pageSize = filter.getMaxRows();
        int toIndex = Math.min(startIndex + pageSize, totalCount);
        String sortType = filter.getSortType();
        String sortBy = filter.getSortBy();
        List<RangerPolicy> retList = new ArrayList<RangerPolicy>();
        for (int i = startIndex; i < toIndex; i++) {
            retList.add(policyList.get(i));
        }
        ret.setPolicies(retList);
        ret.setPageSize(pageSize);
        ret.setResultSize(retList.size());
        ret.setStartIndex(startIndex);
        ret.setTotalCount(totalCount);
        ret.setSortBy(sortBy);
        ret.setSortType(sortType);
    }
    return ret;
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) ArrayList(java.util.ArrayList) VXString(org.apache.ranger.view.VXString) RangerPolicyList(org.apache.ranger.view.RangerPolicyList)

Example 10 with RangerPolicyList

use of org.apache.ranger.view.RangerPolicyList in project ranger by apache.

the class ServiceDBStore method getPaginatedPolicies.

public PList<RangerPolicy> getPaginatedPolicies(SearchFilter filter) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.getPaginatedPolicies(+ " + filter + ")");
    }
    RangerPolicyList policyList = searchRangerPolicies(filter);
    if (LOG.isDebugEnabled()) {
        LOG.debug("before filter: count=" + policyList.getListSize());
    }
    predicateUtil.applyFilter(policyList.getPolicies(), filter);
    if (LOG.isDebugEnabled()) {
        LOG.debug("after filter: count=" + policyList.getListSize());
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceDBStore.getPaginatedPolicies(" + filter + "): count=" + policyList.getListSize());
    }
    return new PList<RangerPolicy>(policyList.getPolicies(), policyList.getStartIndex(), policyList.getPageSize(), policyList.getTotalCount(), policyList.getResultSize(), policyList.getSortType(), policyList.getSortBy());
}
Also used : PList(org.apache.ranger.plugin.store.PList) RangerPolicyList(org.apache.ranger.view.RangerPolicyList)

Aggregations

RangerPolicyList (org.apache.ranger.view.RangerPolicyList)18 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)14 SearchFilter (org.apache.ranger.plugin.util.SearchFilter)10 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 VXString (org.apache.ranger.view.VXString)4 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 HashSet (java.util.HashSet)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 PList (org.apache.ranger.plugin.store.PList)2 RangerPerfTracer (org.apache.ranger.plugin.util.RangerPerfTracer)2 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 XXPolicy (org.apache.ranger.entity.XXPolicy)1