Search in sources :

Example 6 with RangerExportPolicyList

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

the class ServiceREST method importPoliciesFromFile.

@POST
@Path("/policies/importPoliciesFromFile")
@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON })
@Produces({ "application/json", "application/xml" })
@PreAuthorize("@rangerPreAuthSecurityHandler.isAdminOrKeyAdminRole()")
public void importPoliciesFromFile(@Context HttpServletRequest request, @FormDataParam("servicesMapJson") InputStream serviceMapStream, @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @QueryParam("isOverride") Boolean isOverride) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceREST.importPoliciesFromFile()");
    }
    RangerPerfTracer perf = null;
    String metaDataInfo = null;
    List<XXTrxLog> trxLogListError = new ArrayList<XXTrxLog>();
    XXTrxLog xxTrxLogError = new XXTrxLog();
    try {
        if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.importPoliciesFromFile()");
        }
        List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>();
        XXTrxLog xxTrxLog = new XXTrxLog();
        xxTrxLog.setAction("IMPORT START");
        xxTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
        xxTrxLog.setPreviousValue("IMPORT START");
        trxLogList.add(xxTrxLog);
        bizUtil.createTrxLog(trxLogList);
        if (isOverride == null) {
            isOverride = false;
        }
        List<String> serviceNameList = new ArrayList<String>();
        String serviceType = null;
        List<String> serviceTypeList = null;
        SearchFilter filter = searchUtil.getSearchFilter(request, policyService.sortFields);
        if (StringUtils.isNotEmpty(request.getParameter(PARAM_SERVICE_TYPE))) {
            serviceType = request.getParameter(PARAM_SERVICE_TYPE);
        }
        if (StringUtils.isNotEmpty(serviceType)) {
            serviceTypeList = new ArrayList<String>(Arrays.asList(serviceType.split(",")));
        }
        List<RangerService> rangerServiceList = null;
        List<RangerService> rangerServiceLists = new ArrayList<RangerService>();
        if (CollectionUtils.isNotEmpty(serviceTypeList)) {
            for (String s : serviceTypeList) {
                filter.removeParam(PARAM_SERVICE_TYPE);
                filter.setParam(PARAM_SERVICE_TYPE, s.trim());
                rangerServiceList = getServices(filter);
                rangerServiceLists.addAll(rangerServiceList);
            }
        }
        if (!CollectionUtils.sizeIsEmpty(rangerServiceLists)) {
            for (RangerService rService : rangerServiceLists) {
                if (StringUtils.isNotEmpty(rService.getName())) {
                    serviceNameList.add(rService.getName());
                }
            }
        }
        Map<String, String> servicesMappingMap = new LinkedHashMap<String, String>();
        List<String> sourceServices = new ArrayList<String>();
        List<String> destinationServices = new ArrayList<String>();
        if (serviceMapStream != null) {
            servicesMappingMap = svcStore.getServiceMap(serviceMapStream);
        }
        if (!CollectionUtils.sizeIsEmpty(servicesMappingMap)) {
            for (Entry<String, String> map : servicesMappingMap.entrySet()) {
                String sourceServiceName = null;
                String destinationServiceName = null;
                if (StringUtils.isNotEmpty(map.getKey().trim()) && StringUtils.isNotEmpty(map.getValue().trim())) {
                    sourceServiceName = map.getKey().trim();
                    destinationServiceName = map.getValue().trim();
                } else {
                    LOG.error("Source service or destonation service name is not provided!!");
                    throw restErrorUtil.createRESTException("Source service or destonation service name is not provided!!");
                }
                if (StringUtils.isNotEmpty(sourceServiceName) && StringUtils.isNotEmpty(destinationServiceName)) {
                    sourceServices.add(sourceServiceName);
                    destinationServices.add(destinationServiceName);
                }
            }
        }
        String fileName = fileDetail.getFileName();
        int totalPolicyCreate = 0;
        Map<String, RangerPolicy> policiesMap = new LinkedHashMap<String, RangerPolicy>();
        List<String> dataFileSourceServices = new ArrayList<String>();
        if (fileName.endsWith("json")) {
            try {
                RangerExportPolicyList rangerExportPolicyList = null;
                List<RangerPolicy> policies = null;
                Gson gson = new Gson();
                String policiesString = IOUtils.toString(uploadedInputStream);
                policiesString = policiesString.trim();
                if (StringUtils.isNotEmpty(policiesString)) {
                    gson.fromJson(policiesString, RangerExportPolicyList.class);
                    rangerExportPolicyList = new ObjectMapper().readValue(policiesString, RangerExportPolicyList.class);
                } else {
                    LOG.error("Provided json file is empty!!");
                    throw restErrorUtil.createRESTException("Provided json file is empty!!");
                }
                if (rangerExportPolicyList != null && !CollectionUtils.sizeIsEmpty(rangerExportPolicyList.getMetaDataInfo())) {
                    metaDataInfo = new ObjectMapper().writeValueAsString(rangerExportPolicyList.getMetaDataInfo());
                } else {
                    LOG.info("metadata info is not provided!!");
                }
                if (rangerExportPolicyList != null && !CollectionUtils.sizeIsEmpty(rangerExportPolicyList.getPolicies())) {
                    policies = rangerExportPolicyList.getPolicies();
                } else {
                    LOG.error("Provided json file does not contain any policy!!");
                    throw restErrorUtil.createRESTException("Provided json file does not contain any policy!!");
                }
                if (CollectionUtils.sizeIsEmpty(servicesMappingMap) && isOverride) {
                    if (policies != null && !CollectionUtils.sizeIsEmpty(policies)) {
                        for (RangerPolicy policyInJson : policies) {
                            if (policyInJson != null) {
                                if (StringUtils.isNotEmpty(policyInJson.getService().trim())) {
                                    String serviceName = policyInJson.getService().trim();
                                    if (CollectionUtils.isNotEmpty(serviceNameList) && serviceNameList.contains(serviceName)) {
                                        sourceServices.add(serviceName);
                                        destinationServices.add(serviceName);
                                    } else if (CollectionUtils.isEmpty(serviceNameList)) {
                                        sourceServices.add(serviceName);
                                        destinationServices.add(serviceName);
                                    }
                                } else {
                                    LOG.error("Service Name or Policy Name is not provided!!");
                                    throw restErrorUtil.createRESTException("Service Name or Policy Name is not provided!!");
                                }
                            }
                        }
                    }
                } else if (!CollectionUtils.sizeIsEmpty(servicesMappingMap)) {
                    if (policies != null && !CollectionUtils.sizeIsEmpty(policies)) {
                        for (RangerPolicy policyInJson : policies) {
                            if (policyInJson != null) {
                                if (StringUtils.isNotEmpty(policyInJson.getService().trim())) {
                                    dataFileSourceServices.add(policyInJson.getService().trim());
                                } else {
                                    LOG.error("Service Name or Policy Name is not provided!!");
                                    throw restErrorUtil.createRESTException("Service Name or Policy Name is not provided!!");
                                }
                            }
                        }
                        if (!dataFileSourceServices.containsAll(sourceServices)) {
                            LOG.error("Json File does not contain sepcified source service name.");
                            throw restErrorUtil.createRESTException("Json File does not contain sepcified source service name.");
                        }
                    }
                }
                String updateIfExists = request.getParameter(PARAM_UPDATE_IF_EXISTS);
                String polResource = request.getParameter(SearchFilter.POL_RESOURCE);
                if (updateIfExists == null || updateIfExists.isEmpty()) {
                    updateIfExists = "false";
                } else if (updateIfExists.equalsIgnoreCase("true")) {
                    isOverride = false;
                }
                if (isOverride && "false".equalsIgnoreCase(updateIfExists) && StringUtils.isEmpty(polResource)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Deleting Policy from provided services in servicesMapJson file...");
                    }
                    if (CollectionUtils.isNotEmpty(sourceServices) && CollectionUtils.isNotEmpty(destinationServices)) {
                        deletePoliciesProvidedInServiceMap(sourceServices, destinationServices);
                    }
                }
                if ("true".equalsIgnoreCase(updateIfExists) && StringUtils.isNotEmpty(polResource)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Deleting Policy from provided services in servicesMapJson file for specific resource...");
                    }
                    if (CollectionUtils.isNotEmpty(sourceServices) && CollectionUtils.isNotEmpty(destinationServices)) {
                        deletePoliciesForResource(sourceServices, destinationServices, polResource, request, policies);
                    }
                }
                if (policies != null && !CollectionUtils.sizeIsEmpty(policies)) {
                    for (RangerPolicy policyInJson : policies) {
                        if (policyInJson != null) {
                            policiesMap = svcStore.createPolicyMap(servicesMappingMap, sourceServices, destinationServices, policyInJson, policiesMap);
                        }
                    }
                }
                if (!CollectionUtils.sizeIsEmpty(policiesMap.entrySet())) {
                    for (Entry<String, RangerPolicy> entry : policiesMap.entrySet()) {
                        RangerPolicy policy = entry.getValue();
                        if (policy != null) {
                            if (!CollectionUtils.isEmpty(serviceNameList)) {
                                for (String service : serviceNameList) {
                                    if (StringUtils.isNotEmpty(service.trim()) && StringUtils.isNotEmpty(policy.getService().trim())) {
                                        if (policy.getService().trim().equalsIgnoreCase(service.trim())) {
                                            if (updateIfExists != null && !updateIfExists.isEmpty()) {
                                                request.setAttribute(PARAM_SERVICE_NAME, policy.getService());
                                                request.setAttribute(PARAM_POLICY_NAME, policy.getName());
                                            }
                                            createPolicy(policy, request);
                                            totalPolicyCreate = totalPolicyCreate + 1;
                                            if (LOG.isDebugEnabled()) {
                                                LOG.debug("Policy " + policy.getName() + " created successfully.");
                                            }
                                            break;
                                        }
                                    } else {
                                        LOG.error("Service Name or Policy Name is not provided!!");
                                        throw restErrorUtil.createRESTException("Service Name or Policy Name is not provided!!");
                                    }
                                }
                            } else {
                                if (updateIfExists != null && !updateIfExists.isEmpty()) {
                                    request.setAttribute(PARAM_SERVICE_NAME, policy.getService());
                                    request.setAttribute(PARAM_POLICY_NAME, policy.getName());
                                }
                                createPolicy(policy, request);
                                totalPolicyCreate = totalPolicyCreate + 1;
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("Policy " + policy.getName() + " created successfully.");
                                }
                            }
                        }
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Total Policy Created From Json file : " + totalPolicyCreate);
                    }
                    if (!(totalPolicyCreate > 0)) {
                        LOG.error("zero policy is created from provided data file!!");
                        throw restErrorUtil.createRESTException("zero policy is created from provided data file!!");
                    }
                }
            } catch (IOException e) {
                LOG.error(e.getMessage());
                throw restErrorUtil.createRESTException(e.getMessage());
            }
        } else {
            LOG.error("Provided file format is not supported!!");
            throw restErrorUtil.createRESTException("Provided file format is not supported!!");
        }
    } catch (JsonSyntaxException ex) {
        LOG.error("Provided json file is not valid!!", ex);
        xxTrxLogError.setAction("IMPORT ERROR");
        xxTrxLogError.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
        if (StringUtils.isNotEmpty(metaDataInfo)) {
            xxTrxLogError.setPreviousValue(metaDataInfo);
        }
        trxLogListError.add(xxTrxLogError);
        bizUtil.createTrxLog(trxLogListError);
        throw restErrorUtil.createRESTException(ex.getMessage());
    } catch (WebApplicationException excp) {
        LOG.error("Error while importing policy from file!!", excp);
        xxTrxLogError.setAction("IMPORT ERROR");
        xxTrxLogError.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
        if (StringUtils.isNotEmpty(metaDataInfo)) {
            xxTrxLogError.setPreviousValue(metaDataInfo);
        }
        trxLogListError.add(xxTrxLogError);
        bizUtil.createTrxLog(trxLogListError);
        throw excp;
    } catch (Throwable excp) {
        LOG.error("Error while importing policy from file!!", excp);
        xxTrxLogError.setAction("IMPORT ERROR");
        xxTrxLogError.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
        if (StringUtils.isNotEmpty(metaDataInfo)) {
            xxTrxLogError.setPreviousValue(metaDataInfo);
        }
        trxLogListError.add(xxTrxLogError);
        bizUtil.createTrxLog(trxLogListError);
        throw restErrorUtil.createRESTException(excp.getMessage());
    } finally {
        RangerPerfTracer.log(perf);
        List<XXTrxLog> trxLogListEnd = new ArrayList<XXTrxLog>();
        XXTrxLog xxTrxLogEnd = new XXTrxLog();
        xxTrxLogEnd.setAction("IMPORT END");
        xxTrxLogEnd.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
        if (StringUtils.isNotEmpty(metaDataInfo)) {
            xxTrxLogEnd.setPreviousValue(metaDataInfo);
        }
        trxLogListEnd.add(xxTrxLogEnd);
        bizUtil.createTrxLog(trxLogListEnd);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== ServiceREST.importPoliciesFromFile()");
        }
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RangerExportPolicyList(org.apache.ranger.view.RangerExportPolicyList) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) VXString(org.apache.ranger.view.VXString) LinkedHashMap(java.util.LinkedHashMap) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerService(org.apache.ranger.plugin.model.RangerService) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) RangerPerfTracer(org.apache.ranger.plugin.util.RangerPerfTracer) XXTrxLog(org.apache.ranger.entity.XXTrxLog) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 7 with RangerExportPolicyList

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

the class ServiceREST method importPoliciesFromFile.

@POST
@Path("/policies/importPoliciesFromFile")
@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON })
@Produces({ "application/json", "application/xml" })
@PreAuthorize("@rangerPreAuthSecurityHandler.isAdminOrKeyAdminRole()")
public void importPoliciesFromFile(@Context HttpServletRequest request, @FormDataParam("servicesMapJson") InputStream serviceMapStream, @FormDataParam("zoneMapJson") InputStream zoneMapStream, @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @QueryParam("isOverride") Boolean isOverride, @QueryParam("importType") String importType) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceREST.importPoliciesFromFile()");
    }
    RangerAdminOpContext opContext = new RangerAdminOpContext();
    opContext.setBulkModeContext(true);
    RangerContextHolder.setOpContext(opContext);
    RangerPerfTracer perf = null;
    String metaDataInfo = null;
    List<XXTrxLog> trxLogListError = new ArrayList<XXTrxLog>();
    XXTrxLog xxTrxLogError = new XXTrxLog();
    request.setAttribute(PARAM_IMPORT_IN_PROGRESS, true);
    try {
        if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "ServiceREST.importPoliciesFromFile()");
        }
        List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>();
        XXTrxLog xxTrxLog = new XXTrxLog();
        xxTrxLog.setAction("IMPORT START");
        xxTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
        xxTrxLog.setPreviousValue("IMPORT START");
        trxLogList.add(xxTrxLog);
        bizUtil.createTrxLog(trxLogList);
        if (isOverride == null) {
            isOverride = false;
        }
        List<String> serviceNameList = new ArrayList<String>();
        getServiceNameList(request, serviceNameList);
        Map<String, String> servicesMappingMap = new LinkedHashMap<String, String>();
        List<String> sourceServices = new ArrayList<String>();
        List<String> destinationServices = new ArrayList<String>();
        Map<String, String> zoneMappingMap = new LinkedHashMap<String, String>();
        List<String> sourceZones = new ArrayList<String>();
        List<String> destinationZones = new ArrayList<String>();
        if (zoneMapStream != null) {
            zoneMappingMap = svcStore.getMapFromInputStream(zoneMapStream);
            processZoneMapping(zoneMappingMap, sourceZones, destinationZones);
        }
        if (serviceMapStream != null) {
            servicesMappingMap = svcStore.getMapFromInputStream(serviceMapStream);
            processServiceMapping(servicesMappingMap, sourceServices, destinationServices);
        }
        String fileName = fileDetail.getFileName();
        int totalPolicyCreate = 0;
        String zoneNameInJson = null;
        Map<String, RangerPolicy> policiesMap = new LinkedHashMap<String, RangerPolicy>();
        List<String> dataFileSourceServices = new ArrayList<String>();
        if (fileName.endsWith("json")) {
            try {
                RangerExportPolicyList rangerExportPolicyList = null;
                List<RangerPolicy> policies = null;
                rangerExportPolicyList = processPolicyInputJsonForMetaData(uploadedInputStream, rangerExportPolicyList);
                if (rangerExportPolicyList != null && !CollectionUtils.sizeIsEmpty(rangerExportPolicyList.getMetaDataInfo())) {
                    metaDataInfo = JsonUtilsV2.mapToJson(rangerExportPolicyList.getMetaDataInfo());
                } else {
                    LOG.info("metadata info is not provided!!");
                }
                policies = getPoliciesFromProvidedJson(rangerExportPolicyList);
                int i = 0;
                if (CollectionUtils.sizeIsEmpty(servicesMappingMap) && isOverride) {
                    if (policies != null && !CollectionUtils.sizeIsEmpty(policies)) {
                        for (RangerPolicy policyInJson : policies) {
                            if (policyInJson != null) {
                                if (i == 0 && StringUtils.isNotBlank(policyInJson.getZoneName())) {
                                    zoneNameInJson = policyInJson.getZoneName().trim();
                                }
                                if (StringUtils.isNotEmpty(policyInJson.getService().trim())) {
                                    String serviceName = policyInJson.getService().trim();
                                    if (CollectionUtils.isNotEmpty(serviceNameList) && serviceNameList.contains(serviceName) && !sourceServices.contains(serviceName) && !destinationServices.contains(serviceName)) {
                                        sourceServices.add(serviceName);
                                        destinationServices.add(serviceName);
                                    } else if (CollectionUtils.isEmpty(serviceNameList) && !sourceServices.contains(serviceName) && !destinationServices.contains(serviceName)) {
                                        sourceServices.add(serviceName);
                                        destinationServices.add(serviceName);
                                    }
                                } else {
                                    LOG.error("Service Name or Policy Name is not provided!!");
                                    throw restErrorUtil.createRESTException("Service Name or Policy Name is not provided!!");
                                }
                            }
                            i++;
                        }
                    }
                } else if (!CollectionUtils.sizeIsEmpty(servicesMappingMap)) {
                    if (policies != null && !CollectionUtils.sizeIsEmpty(policies)) {
                        i = 0;
                        for (RangerPolicy policyInJson : policies) {
                            if (policyInJson != null) {
                                if (i == 0 && StringUtils.isNotBlank(policyInJson.getZoneName())) {
                                    zoneNameInJson = policyInJson.getZoneName().trim();
                                }
                                if (StringUtils.isNotEmpty(policyInJson.getService().trim())) {
                                    dataFileSourceServices.add(policyInJson.getService().trim());
                                } else {
                                    LOG.error("Service Name or Policy Name is not provided!!");
                                    throw restErrorUtil.createRESTException("Service Name or Policy Name is not provided!!");
                                }
                                i++;
                            }
                        }
                        if (!dataFileSourceServices.containsAll(sourceServices)) {
                            LOG.error("Json File does not contain sepcified source service name.");
                            throw restErrorUtil.createRESTException("Json File does not contain sepcified source service name.");
                        }
                    }
                }
                boolean deleteIfExists = ("true".equalsIgnoreCase(StringUtils.trimToEmpty(request.getParameter(PARAM_DELETE_IF_EXISTS)))) ? true : false;
                boolean updateIfExists = ("true".equalsIgnoreCase(StringUtils.trimToEmpty(request.getParameter(PARAM_UPDATE_IF_EXISTS)))) ? true : false;
                String polResource = request.getParameter(SearchFilter.POL_RESOURCE);
                if (updateIfExists) {
                    isOverride = false;
                }
                String destinationZoneName = getDestinationZoneName(destinationZones, zoneNameInJson);
                if (isOverride && !updateIfExists && StringUtils.isEmpty(polResource)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Deleting Policy from provided services in servicesMapJson file...");
                    }
                    if (CollectionUtils.isNotEmpty(sourceServices) && CollectionUtils.isNotEmpty(destinationServices)) {
                        // In order to delete Zone specific policies from service
                        deletePoliciesProvidedInServiceMap(sourceServices, destinationServices, destinationZoneName);
                    }
                } else if (updateIfExists && StringUtils.isNotEmpty(polResource)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Deleting Policy from provided services in servicesMapJson file for specific resource...");
                    }
                    if (CollectionUtils.isNotEmpty(sourceServices) && CollectionUtils.isNotEmpty(destinationServices)) {
                        // In order to delete Zone specific policies from service
                        deletePoliciesForResource(sourceServices, destinationServices, request, policies, destinationZoneName);
                    }
                }
                if (policies != null && !CollectionUtils.sizeIsEmpty(policies)) {
                    for (RangerPolicy policyInJson : policies) {
                        if (policyInJson != null) {
                            if (StringUtils.isNotBlank(destinationZoneName)) {
                                boolean isZoneServiceExistAtDestination = validateDestZoneServiceMapping(destinationZoneName, policyInJson, servicesMappingMap);
                                if (!isZoneServiceExistAtDestination) {
                                    LOG.warn("provided service of policy in File is not associated with zone");
                                    continue;
                                }
                            }
                            policiesMap = svcStore.createPolicyMap(zoneMappingMap, sourceZones, destinationZoneName, servicesMappingMap, sourceServices, destinationServices, policyInJson, // zone Info is also sent for creating policy map
                            policiesMap);
                        }
                    }
                    if (deleteIfExists) {
                        // deleting target policies if already exist
                        deleteExactMatchPolicyForResource(policies, request.getRemoteUser(), destinationZoneName);
                    }
                }
                totalPolicyCreate = createPolicesBasedOnPolicyMap(request, policiesMap, serviceNameList, updateIfExists, totalPolicyCreate);
                if (!(totalPolicyCreate > 0)) {
                    LOG.error("zero policy is created from provided data file!!");
                    throw restErrorUtil.createRESTException("zero policy is created from provided data file!!");
                }
            } catch (IOException e) {
                LOG.error(e.getMessage());
                throw restErrorUtil.createRESTException(e.getMessage());
            }
        } else {
            LOG.error("Provided file format is not supported!!");
            throw restErrorUtil.createRESTException("Provided file format is not supported!!");
        }
    } catch (JsonSyntaxException ex) {
        LOG.error("Provided json file is not valid!!", ex);
        xxTrxLogError.setAction("IMPORT ERROR");
        xxTrxLogError.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
        if (StringUtils.isNotEmpty(metaDataInfo)) {
            xxTrxLogError.setPreviousValue(metaDataInfo);
        }
        trxLogListError.add(xxTrxLogError);
        bizUtil.createTrxLog(trxLogListError);
        throw restErrorUtil.createRESTException(ex.getMessage());
    } catch (WebApplicationException excp) {
        LOG.error("Error while importing policy from file!!", excp);
        xxTrxLogError.setAction("IMPORT ERROR");
        xxTrxLogError.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
        if (StringUtils.isNotEmpty(metaDataInfo)) {
            xxTrxLogError.setPreviousValue(metaDataInfo);
        }
        trxLogListError.add(xxTrxLogError);
        bizUtil.createTrxLog(trxLogListError);
        throw excp;
    } catch (Throwable excp) {
        LOG.error("Error while importing policy from file!!", excp);
        xxTrxLogError.setAction("IMPORT ERROR");
        xxTrxLogError.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
        if (StringUtils.isNotEmpty(metaDataInfo)) {
            xxTrxLogError.setPreviousValue(metaDataInfo);
        }
        trxLogListError.add(xxTrxLogError);
        bizUtil.createTrxLog(trxLogListError);
        throw restErrorUtil.createRESTException(excp.getMessage());
    } finally {
        RangerPerfTracer.log(perf);
        List<XXTrxLog> trxLogListEnd = new ArrayList<XXTrxLog>();
        XXTrxLog xxTrxLogEnd = new XXTrxLog();
        xxTrxLogEnd.setAction("IMPORT END");
        xxTrxLogEnd.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_POLICY);
        if (StringUtils.isNotEmpty(metaDataInfo)) {
            xxTrxLogEnd.setPreviousValue(metaDataInfo);
        }
        trxLogListEnd.add(xxTrxLogEnd);
        bizUtil.createTrxLog(trxLogListEnd);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== ServiceREST.importPoliciesFromFile()");
        }
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RangerPerfTracer(org.apache.ranger.plugin.util.RangerPerfTracer) RangerExportPolicyList(org.apache.ranger.view.RangerExportPolicyList) ArrayList(java.util.ArrayList) RangerAdminOpContext(org.apache.ranger.security.context.RangerAdminOpContext) VXString(org.apache.ranger.view.VXString) XXTrxLog(org.apache.ranger.entity.XXTrxLog) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) JsonSyntaxException(com.google.gson.JsonSyntaxException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

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