Search in sources :

Example 1 with RangerExportPolicyList

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

the class ServiceDBStore method writeJson.

private void writeJson(List<RangerPolicy> policies, String jsonFileName, HttpServletResponse response) throws JSONException, IOException {
    response.setContentType("text/json");
    response.setHeader("Content-Disposition", "attachment; filename=" + jsonFileName);
    ServletOutputStream out = null;
    RangerExportPolicyList rangerExportPolicyList = new RangerExportPolicyList();
    putMetaDataInfo(rangerExportPolicyList);
    rangerExportPolicyList.setPolicies(policies);
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(rangerExportPolicyList, RangerExportPolicyList.class);
    try {
        out = response.getOutputStream();
        response.setStatus(HttpServletResponse.SC_OK);
        IOUtils.write(json, out, "UTF-8");
    } catch (Exception e) {
        LOG.error("Error while exporting json file " + jsonFileName, e);
    } finally {
        try {
            if (out != null) {
                out.flush();
                out.close();
            }
        } catch (Exception ex) {
        }
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) GsonBuilder(com.google.gson.GsonBuilder) RangerExportPolicyList(org.apache.ranger.view.RangerExportPolicyList) Gson(com.google.gson.Gson) VXString(org.apache.ranger.view.VXString) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException)

Example 2 with RangerExportPolicyList

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

the class ServiceREST method getPoliciesInCsv.

@GET
@Path("/policies/csv")
@Produces("text/csv")
public void getPoliciesInCsv(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceREST.getPoliciesInCsv()");
    }
    RangerPerfTracer perf = null;
    SearchFilter filter = searchUtil.getSearchFilter(request, policyService.sortFields);
    try {
        if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.getPoliciesInCsv()");
        }
        List<RangerPolicy> policyLists = new ArrayList<RangerPolicy>();
        policyLists = getAllFilteredPolicyList(filter, request, policyLists);
        if (CollectionUtils.isNotEmpty(policyLists)) {
            for (RangerPolicy rangerPolicy : policyLists) {
                if (rangerPolicy != null) {
                    ensureAdminAndAuditAccess(rangerPolicy);
                }
            }
            svcStore.getPoliciesInCSV(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 = JsonUtilsV2.mapToJson(rangerExportPolicyList.getMetaDataInfo());
        List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>();
        XXTrxLog xxTrxLog = new XXTrxLog();
        xxTrxLog.setAction("EXPORT CSV");
        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) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with RangerExportPolicyList

use of org.apache.ranger.view.RangerExportPolicyList 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);
                }
            }
            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 = JsonUtilsV2.mapToJson(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) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with RangerExportPolicyList

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

the class ServiceREST method processPolicyInputJsonForMetaData.

private RangerExportPolicyList processPolicyInputJsonForMetaData(InputStream uploadedInputStream, RangerExportPolicyList rangerExportPolicyList) throws Exception {
    String policiesString = IOUtils.toString(uploadedInputStream);
    policiesString = policiesString.trim();
    if (StringUtils.isNotEmpty(policiesString)) {
        rangerExportPolicyList = JsonUtilsV2.jsonToObj(policiesString, RangerExportPolicyList.class);
    } else {
        LOG.error("Provided json file is empty!!");
        throw restErrorUtil.createRESTException("Provided json file is empty!!");
    }
    return rangerExportPolicyList;
}
Also used : RangerExportPolicyList(org.apache.ranger.view.RangerExportPolicyList) VXString(org.apache.ranger.view.VXString)

Example 5 with RangerExportPolicyList

use of org.apache.ranger.view.RangerExportPolicyList 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);
    requestParamsValidation(filter);
    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 = JsonUtilsV2.mapToJson(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) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

RangerExportPolicyList (org.apache.ranger.view.RangerExportPolicyList)7 VXString (org.apache.ranger.view.VXString)7 ArrayList (java.util.ArrayList)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 XXTrxLog (org.apache.ranger.entity.XXTrxLog)5 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)5 RangerPerfTracer (org.apache.ranger.plugin.util.RangerPerfTracer)5 SearchFilter (org.apache.ranger.plugin.util.SearchFilter)4 IOException (java.io.IOException)3 GET (javax.ws.rs.GET)3 Gson (com.google.gson.Gson)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 LinkedHashMap (java.util.LinkedHashMap)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 GsonBuilder (com.google.gson.GsonBuilder)1 UnknownHostException (java.net.UnknownHostException)1