Search in sources :

Example 91 with RangerService

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

the class ServiceDBStore method updatePolicy.

@Override
public RangerPolicy updatePolicy(RangerPolicy policy) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.updatePolicy(" + policy + ")");
    }
    XXPolicy xxExisting = daoMgr.getXXPolicy().getById(policy.getId());
    RangerPolicy existing = policyService.getPopulatedViewObject(xxExisting);
    if (existing == null) {
        throw new Exception("no policy exists with ID=" + policy.getId());
    }
    RangerService service = getServiceByName(policy.getService());
    if (service == null) {
        throw new Exception("service does not exist - name=" + policy.getService());
    }
    XXServiceDef xServiceDef = daoMgr.getXXServiceDef().findByName(service.getType());
    if (xServiceDef == null) {
        throw new Exception("service-def does not exist - name=" + service.getType());
    }
    if (!StringUtils.equalsIgnoreCase(existing.getService(), policy.getService())) {
        throw new Exception("policy id=" + policy.getId() + " already exists in service " + existing.getService() + ". It can not be moved to service " + policy.getService());
    }
    boolean renamed = !StringUtils.equalsIgnoreCase(policy.getName(), existing.getName());
    if (renamed) {
        XXPolicy newNamePolicy = daoMgr.getXXPolicy().findByNameAndServiceId(policy.getName(), service.getId());
        if (newNamePolicy != null) {
            throw new Exception("another policy already exists with name '" + policy.getName() + "'. ID=" + newNamePolicy.getId());
        }
    }
    Map<String, RangerPolicyResource> newResources = policy.getResources();
    List<RangerPolicyItem> policyItems = policy.getPolicyItems();
    List<RangerPolicyItem> denyPolicyItems = policy.getDenyPolicyItems();
    List<RangerPolicyItem> allowExceptions = policy.getAllowExceptions();
    List<RangerPolicyItem> denyExceptions = policy.getDenyExceptions();
    List<RangerDataMaskPolicyItem> dataMaskPolicyItems = policy.getDataMaskPolicyItems();
    List<RangerRowFilterPolicyItem> rowFilterItems = policy.getRowFilterPolicyItems();
    List<String> policyLabels = policy.getPolicyLabels();
    policy.setCreateTime(xxExisting.getCreateTime());
    policy.setGuid(xxExisting.getGuid());
    policy.setVersion(xxExisting.getVersion());
    List<XXTrxLog> trxLogList = policyService.getTransactionLog(policy, xxExisting, RangerPolicyService.OPERATION_UPDATE_CONTEXT);
    updatePolicySignature(policy);
    boolean isTagVersionUpdateNeeded = false;
    if (EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME.equals(service.getType())) {
        isTagVersionUpdateNeeded = existing.getIsEnabled() ? !policy.getIsEnabled() : policy.getIsEnabled();
        isTagVersionUpdateNeeded = isTagVersionUpdateNeeded || !StringUtils.equals(existing.getResourceSignature(), policy.getResourceSignature());
    }
    policy = policyService.update(policy);
    XXPolicy newUpdPolicy = daoMgr.getXXPolicy().getById(policy.getId());
    deleteExistingPolicyResources(policy);
    deleteExistingPolicyItems(policy);
    deleteExistingPolicyLabel(policy);
    createNewResourcesForPolicy(policy, newUpdPolicy, newResources);
    createNewPolicyItemsForPolicy(policy, newUpdPolicy, policyItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_ALLOW);
    createNewPolicyItemsForPolicy(policy, newUpdPolicy, denyPolicyItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_DENY);
    createNewPolicyItemsForPolicy(policy, newUpdPolicy, allowExceptions, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_ALLOW_EXCEPTIONS);
    createNewPolicyItemsForPolicy(policy, newUpdPolicy, denyExceptions, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_DENY_EXCEPTIONS);
    createNewDataMaskPolicyItemsForPolicy(policy, newUpdPolicy, dataMaskPolicyItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_DATAMASK);
    createNewRowFilterPolicyItemsForPolicy(policy, newUpdPolicy, rowFilterItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_ROWFILTER);
    createNewLabelsForPolicy(newUpdPolicy, policyLabels);
    handlePolicyUpdate(service, isTagVersionUpdateNeeded);
    RangerPolicy updPolicy = policyService.getPopulatedViewObject(newUpdPolicy);
    dataHistService.createObjectDataHistory(updPolicy, RangerDataHistService.ACTION_UPDATE);
    bizUtil.createTrxLog(trxLogList);
    return updPolicy;
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) RangerRowFilterPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerRowFilterPolicyItem) VXString(org.apache.ranger.view.VXString) XXTrxLog(org.apache.ranger.entity.XXTrxLog) XXPolicy(org.apache.ranger.entity.XXPolicy) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerDataMaskPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerDataMaskPolicyItem) RangerService(org.apache.ranger.plugin.model.RangerService)

Example 92 with RangerService

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

the class ServiceDBStore method createPolicy.

@Override
public RangerPolicy createPolicy(RangerPolicy policy) throws Exception {
    RangerService service = getServiceByName(policy.getService());
    if (service == null) {
        throw new Exception("service does not exist - name=" + policy.getService());
    }
    XXServiceDef xServiceDef = daoMgr.getXXServiceDef().findByName(service.getType());
    if (xServiceDef == null) {
        throw new Exception("service-def does not exist - name=" + service.getType());
    }
    XXPolicy existing = daoMgr.getXXPolicy().findByNameAndServiceId(policy.getName(), service.getId());
    if (existing != null) {
        throw new Exception("policy already exists: ServiceName=" + policy.getService() + "; PolicyName=" + policy.getName() + ". ID=" + existing.getId());
    }
    Map<String, RangerPolicyResource> resources = policy.getResources();
    List<RangerPolicyItem> policyItems = policy.getPolicyItems();
    List<RangerPolicyItem> denyPolicyItems = policy.getDenyPolicyItems();
    List<RangerPolicyItem> allowExceptions = policy.getAllowExceptions();
    List<RangerPolicyItem> denyExceptions = policy.getDenyExceptions();
    List<RangerDataMaskPolicyItem> dataMaskItems = policy.getDataMaskPolicyItems();
    List<RangerRowFilterPolicyItem> rowFilterItems = policy.getRowFilterPolicyItems();
    List<String> policyLabels = policy.getPolicyLabels();
    policy.setVersion(Long.valueOf(1));
    updatePolicySignature(policy);
    if (populateExistingBaseFields) {
        assignedIdPolicyService.setPopulateExistingBaseFields(true);
        daoMgr.getXXPolicy().setIdentityInsert(true);
        policy = assignedIdPolicyService.create(policy);
        daoMgr.getXXPolicy().setIdentityInsert(false);
        daoMgr.getXXPolicy().updateSequence();
        assignedIdPolicyService.setPopulateExistingBaseFields(false);
    } else {
        policy = policyService.create(policy);
    }
    XXPolicy xCreatedPolicy = daoMgr.getXXPolicy().getById(policy.getId());
    createNewResourcesForPolicy(policy, xCreatedPolicy, resources);
    createNewPolicyItemsForPolicy(policy, xCreatedPolicy, policyItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_ALLOW);
    createNewPolicyItemsForPolicy(policy, xCreatedPolicy, denyPolicyItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_DENY);
    createNewPolicyItemsForPolicy(policy, xCreatedPolicy, allowExceptions, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_ALLOW_EXCEPTIONS);
    createNewPolicyItemsForPolicy(policy, xCreatedPolicy, denyExceptions, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_DENY_EXCEPTIONS);
    createNewDataMaskPolicyItemsForPolicy(policy, xCreatedPolicy, dataMaskItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_DATAMASK);
    createNewRowFilterPolicyItemsForPolicy(policy, xCreatedPolicy, rowFilterItems, xServiceDef, RangerPolicyItemEvaluator.POLICY_ITEM_TYPE_ROWFILTER);
    createNewLabelsForPolicy(xCreatedPolicy, policyLabels);
    handlePolicyUpdate(service, true);
    RangerPolicy createdPolicy = policyService.getPopulatedViewObject(xCreatedPolicy);
    dataHistService.createObjectDataHistory(createdPolicy, RangerDataHistService.ACTION_CREATE);
    List<XXTrxLog> trxLogList = policyService.getTransactionLog(createdPolicy, RangerPolicyService.OPERATION_CREATE_CONTEXT);
    bizUtil.createTrxLog(trxLogList);
    return createdPolicy;
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) RangerRowFilterPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerRowFilterPolicyItem) VXString(org.apache.ranger.view.VXString) XXTrxLog(org.apache.ranger.entity.XXTrxLog) XXPolicy(org.apache.ranger.entity.XXPolicy) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerDataMaskPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerDataMaskPolicyItem) RangerService(org.apache.ranger.plugin.model.RangerService)

Example 93 with RangerService

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

the class MetricUtil method metricCalculation.

private void metricCalculation(String caseValue) {
    logger.info("Metric Type : " + caseValue);
    try {
        SearchCriteria searchCriteria = new SearchCriteria();
        searchCriteria.setStartIndex(0);
        searchCriteria.setMaxRows(100);
        searchCriteria.setGetCount(true);
        searchCriteria.setSortType("asc");
        switch(caseValue.toLowerCase()) {
            case "usergroup":
                try {
                    VXGroupList vxGroupList = xUserMgr.searchXGroups(searchCriteria);
                    long groupCount = vxGroupList.getTotalCount();
                    ArrayList<String> userKeyAdminRoleCount = new ArrayList<String>();
                    userKeyAdminRoleCount.add(RangerConstants.ROLE_SYS_ADMIN);
                    long userSysAdminCount = getUserCountBasedOnUserRole(userKeyAdminRoleCount);
                    ArrayList<String> userRoleListKeyRoleAdmin = new ArrayList<String>();
                    userRoleListKeyRoleAdmin.add(RangerConstants.ROLE_KEY_ADMIN);
                    long userKeyAdminCount = getUserCountBasedOnUserRole(userRoleListKeyRoleAdmin);
                    ArrayList<String> userRoleListUser = new ArrayList<String>();
                    userRoleListUser.add(RangerConstants.ROLE_USER);
                    long userRoleCount = getUserCountBasedOnUserRole(userRoleListUser);
                    long userTotalCount = userSysAdminCount + userKeyAdminCount + userRoleCount;
                    VXMetricUserGroupCount metricUserGroupCount = new VXMetricUserGroupCount();
                    metricUserGroupCount.setUserCountOfUserRole(userRoleCount);
                    metricUserGroupCount.setUserCountOfKeyAdminRole(userKeyAdminCount);
                    metricUserGroupCount.setUserCountOfSysAdminRole(userSysAdminCount);
                    metricUserGroupCount.setUserTotalCount(userTotalCount);
                    metricUserGroupCount.setGroupCount(groupCount);
                    Gson gson = new GsonBuilder().create();
                    final String jsonUserGroupCount = gson.toJson(metricUserGroupCount);
                    System.out.println(jsonUserGroupCount);
                } catch (Exception e) {
                    logger.error("Error calculating Metric for usergroup : " + e.getMessage());
                }
                break;
            case "audits":
                try {
                    int clientTimeOffsetInMinute = RestUtil.getClientTimeOffset();
                    String defaultDateFormat = "MM/dd/yyyy";
                    DateFormat formatter = new SimpleDateFormat(defaultDateFormat);
                    VXMetricAuditDetailsCount auditObj = new VXMetricAuditDetailsCount();
                    DateUtil dateUtilTwoDays = new DateUtil();
                    Date startDateUtilTwoDays = dateUtilTwoDays.getDateFromNow(-2);
                    Date dStart2 = restErrorUtil.parseDate(formatter.format(startDateUtilTwoDays), "Invalid value for startDate", MessageEnums.INVALID_INPUT_DATA, null, "startDate", defaultDateFormat);
                    Date endDateTwoDays = MiscUtil.getUTCDate();
                    Date dEnd2 = restErrorUtil.parseDate(formatter.format(endDateTwoDays), "Invalid value for endDate", MessageEnums.INVALID_INPUT_DATA, null, "endDate", defaultDateFormat);
                    dEnd2 = dateUtilTwoDays.getDateFromGivenDate(dEnd2, 0, 23, 59, 59);
                    dEnd2 = dateUtilTwoDays.addTimeOffset(dEnd2, clientTimeOffsetInMinute);
                    VXMetricServiceCount deniedCountObj = getAuditsCount(0, dStart2, dEnd2);
                    auditObj.setDenialEventsCountTwoDays(deniedCountObj);
                    VXMetricServiceCount allowedCountObj = getAuditsCount(1, dStart2, dEnd2);
                    auditObj.setAccessEventsCountTwoDays(allowedCountObj);
                    long totalAuditsCountTwoDays = deniedCountObj.getTotalCount() + allowedCountObj.getTotalCount();
                    auditObj.setSolrIndexCountTwoDays(totalAuditsCountTwoDays);
                    DateUtil dateUtilWeek = new DateUtil();
                    Date startDateUtilWeek = dateUtilWeek.getDateFromNow(-7);
                    Date dStart7 = restErrorUtil.parseDate(formatter.format(startDateUtilWeek), "Invalid value for startDate", MessageEnums.INVALID_INPUT_DATA, null, "startDate", defaultDateFormat);
                    Date endDateWeek = MiscUtil.getUTCDate();
                    DateUtil dateUtilweek = new DateUtil();
                    Date dEnd7 = restErrorUtil.parseDate(formatter.format(endDateWeek), "Invalid value for endDate", MessageEnums.INVALID_INPUT_DATA, null, "endDate", defaultDateFormat);
                    dEnd7 = dateUtilweek.getDateFromGivenDate(dEnd7, 0, 23, 59, 59);
                    dEnd7 = dateUtilweek.addTimeOffset(dEnd7, clientTimeOffsetInMinute);
                    VXMetricServiceCount deniedCountObjWeek = getAuditsCount(0, dStart7, dEnd7);
                    auditObj.setDenialEventsCountWeek(deniedCountObjWeek);
                    VXMetricServiceCount allowedCountObjWeek = getAuditsCount(1, dStart7, dEnd7);
                    auditObj.setAccessEventsCountWeek(allowedCountObjWeek);
                    long totalAuditsCountWeek = deniedCountObjWeek.getTotalCount() + allowedCountObjWeek.getTotalCount();
                    auditObj.setSolrIndexCountWeek(totalAuditsCountWeek);
                    Gson gson = new GsonBuilder().create();
                    final String jsonAudit = gson.toJson(auditObj);
                    System.out.println(jsonAudit);
                } catch (Exception e) {
                    logger.error("Error calculating Metric for audits : " + e.getMessage());
                }
                break;
            case "services":
                try {
                    SearchFilter serviceFilter = new SearchFilter();
                    serviceFilter.setMaxRows(200);
                    serviceFilter.setStartIndex(0);
                    serviceFilter.setGetCount(true);
                    serviceFilter.setSortBy("serviceId");
                    serviceFilter.setSortType("asc");
                    VXMetricServiceCount vXMetricServiceCount = new VXMetricServiceCount();
                    PList<RangerService> paginatedSvcs = svcStore.getPaginatedServices(serviceFilter);
                    long totalServiceCount = paginatedSvcs.getTotalCount();
                    List<RangerService> rangerServiceList = paginatedSvcs.getList();
                    Map<String, Long> services = new HashMap<String, Long>();
                    for (Object rangerService : rangerServiceList) {
                        RangerService RangerServiceObj = (RangerService) rangerService;
                        String serviceName = RangerServiceObj.getType();
                        if (!(services.containsKey(serviceName))) {
                            serviceFilter.setParam("serviceType", serviceName);
                            PList<RangerService> paginatedSvcscount = svcStore.getPaginatedServices(serviceFilter);
                            services.put(serviceName, paginatedSvcscount.getTotalCount());
                        }
                    }
                    vXMetricServiceCount.setServiceBasedCountList(services);
                    vXMetricServiceCount.setTotalCount(totalServiceCount);
                    Gson gson = new GsonBuilder().create();
                    final String jsonServices = gson.toJson(vXMetricServiceCount);
                    System.out.println(jsonServices);
                } catch (Exception e) {
                    logger.error("Error calculating Metric for services : " + e.getMessage());
                }
                break;
            case "policies":
                try {
                    SearchFilter policyFilter = new SearchFilter();
                    policyFilter.setMaxRows(200);
                    policyFilter.setStartIndex(0);
                    policyFilter.setGetCount(true);
                    policyFilter.setSortBy("serviceId");
                    policyFilter.setSortType("asc");
                    VXMetricPolicyCount vXMetricPolicyCount = new VXMetricPolicyCount();
                    PList<RangerPolicy> paginatedSvcsList = svcStore.getPaginatedPolicies(policyFilter);
                    vXMetricPolicyCount.setTotalCount(paginatedSvcsList.getTotalCount());
                    Map<String, VXMetricServiceCount> servicesWithPolicy = new HashMap<String, VXMetricServiceCount>();
                    for (int k = 2; k >= 0; k--) {
                        String serviceType = String.valueOf(k);
                        VXMetricServiceCount vXMetricServiceCount = getVXMetricServiceCount(serviceType);
                        if (k == 2) {
                            servicesWithPolicy.put("rowFilteringPolicies", vXMetricServiceCount);
                        } else if (k == 1) {
                            servicesWithPolicy.put("maskingPolicies", vXMetricServiceCount);
                        } else if (k == 0) {
                            servicesWithPolicy.put("resourcePolicy", vXMetricServiceCount);
                        }
                    }
                    boolean tagFlag = false;
                    if (tagFlag == false) {
                        policyFilter.setParam("serviceType", "tag");
                        PList<RangerPolicy> policiestype = svcStore.getPaginatedPolicies(policyFilter);
                        Map<String, Long> tagMap = new HashMap<String, Long>();
                        long tagCount = policiestype.getTotalCount();
                        tagMap.put("tag", tagCount);
                        VXMetricServiceCount vXMetricServiceCount = new VXMetricServiceCount();
                        vXMetricServiceCount.setServiceBasedCountList(tagMap);
                        vXMetricServiceCount.setTotalCount(tagCount);
                        servicesWithPolicy.put("tagBasedPolicies", vXMetricServiceCount);
                        tagFlag = true;
                    }
                    vXMetricPolicyCount.setPolicyCountList(servicesWithPolicy);
                    Gson gson = new GsonBuilder().create();
                    final String jsonPolicies = gson.toJson(vXMetricPolicyCount);
                    System.out.println(jsonPolicies);
                } catch (Exception e) {
                    logger.error("Error calculating Metric for policies : " + e.getMessage());
                }
                break;
            case "database":
                try {
                    int dbFlavor = RangerBizUtil.getDBFlavor();
                    String dbFlavourType = "Unknow ";
                    if (dbFlavor == AppConstants.DB_FLAVOR_MYSQL) {
                        dbFlavourType = "MYSQL ";
                    } else if (dbFlavor == AppConstants.DB_FLAVOR_ORACLE) {
                        dbFlavourType = "ORACLE ";
                    } else if (dbFlavor == AppConstants.DB_FLAVOR_POSTGRES) {
                        dbFlavourType = "POSTGRES ";
                    } else if (dbFlavor == AppConstants.DB_FLAVOR_SQLANYWHERE) {
                        dbFlavourType = "SQLANYWHERE ";
                    } else if (dbFlavor == AppConstants.DB_FLAVOR_SQLSERVER) {
                        dbFlavourType = "SQLSERVER ";
                    }
                    String dbDetail = dbFlavourType + xaBizUtil.getDBVersion();
                    Gson gson = new GsonBuilder().create();
                    final String jsonDBDetail = gson.toJson(dbDetail);
                    System.out.println(jsonDBDetail);
                } catch (Exception e) {
                    logger.error("Error calculating Metric for database : " + e.getMessage());
                }
                break;
            case "contextenrichers":
                try {
                    SearchFilter filter = new SearchFilter();
                    filter.setStartIndex(0);
                    VXMetricContextEnricher serviceWithContextEnrichers = new VXMetricContextEnricher();
                    PList<RangerServiceDef> paginatedSvcDefs = svcStore.getPaginatedServiceDefs(filter);
                    List<RangerServiceDef> repoTypeList = paginatedSvcDefs.getList();
                    if (repoTypeList != null) {
                        for (RangerServiceDef repoType : repoTypeList) {
                            RangerServiceDef rangerServiceDefObj = (RangerServiceDef) repoType;
                            String name = rangerServiceDefObj.getName();
                            List<RangerContextEnricherDef> contextEnrichers = rangerServiceDefObj.getContextEnrichers();
                            if (contextEnrichers != null && !contextEnrichers.isEmpty()) {
                                serviceWithContextEnrichers.setServiceName(name);
                                serviceWithContextEnrichers.setTotalCount(contextEnrichers.size());
                            }
                        }
                    }
                    Gson gson = new GsonBuilder().create();
                    final String jsonContextEnrichers = gson.toJson(serviceWithContextEnrichers);
                    System.out.println(jsonContextEnrichers);
                } catch (Exception e) {
                    logger.error("Error calculating Metric for contextenrichers : " + e.getMessage());
                }
                break;
            case "denyconditions":
                try {
                    SearchFilter policyFilter1 = new SearchFilter();
                    policyFilter1.setMaxRows(200);
                    policyFilter1.setStartIndex(0);
                    policyFilter1.setGetCount(true);
                    policyFilter1.setSortBy("serviceId");
                    policyFilter1.setSortType("asc");
                    int denyCount = 0;
                    Map<String, Integer> denyconditionsonMap = new HashMap<String, Integer>();
                    PList<RangerServiceDef> paginatedSvcDefs = svcStore.getPaginatedServiceDefs(policyFilter1);
                    if (paginatedSvcDefs != null) {
                        List<RangerServiceDef> rangerServiceDefs = paginatedSvcDefs.getList();
                        if (rangerServiceDefs != null && !rangerServiceDefs.isEmpty()) {
                            for (RangerServiceDef rangerServiceDef : rangerServiceDefs) {
                                if (rangerServiceDef != null) {
                                    String serviceDef = rangerServiceDef.getName();
                                    if (!StringUtils.isEmpty(serviceDef)) {
                                        policyFilter1.setParam("serviceType", serviceDef);
                                        PList<RangerPolicy> policiesList = svcStore.getPaginatedPolicies(policyFilter1);
                                        if (policiesList != null && policiesList.getListSize() > 0) {
                                            int policyListCount = policiesList.getListSize();
                                            if (policyListCount > 0 && policiesList.getList() != null) {
                                                List<RangerPolicy> policies = policiesList.getList();
                                                for (RangerPolicy policy : policies) {
                                                    if (policy != null) {
                                                        List<RangerPolicyItem> policyItem = policy.getDenyPolicyItems();
                                                        if (policyItem != null && !policyItem.isEmpty()) {
                                                            if (denyconditionsonMap.get(serviceDef) != null) {
                                                                denyCount = denyconditionsonMap.get(serviceDef) + denyCount + policyItem.size();
                                                            } else {
                                                                denyCount = denyCount + policyItem.size();
                                                            }
                                                        }
                                                        List<RangerPolicyItem> policyItemExclude = policy.getDenyExceptions();
                                                        if (policyItemExclude != null && !policyItemExclude.isEmpty()) {
                                                            if (denyconditionsonMap.get(serviceDef) != null) {
                                                                denyCount = denyconditionsonMap.get(serviceDef) + denyCount + policyItemExclude.size();
                                                            } else {
                                                                denyCount = denyCount + policyItemExclude.size();
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        policyFilter1.removeParam("serviceType");
                                    }
                                    denyconditionsonMap.put(serviceDef, denyCount);
                                    denyCount = 0;
                                }
                            }
                        }
                    }
                    Gson gson = new GsonBuilder().create();
                    String jsonContextDenyCondtionOn = gson.toJson(denyconditionsonMap);
                    System.out.println(jsonContextDenyCondtionOn);
                } catch (Exception e) {
                    logger.error("Error calculating Metric for denyconditions : " + e.getMessage());
                }
                break;
            default:
                System.out.println("type: Incorrect Arguments usage : -type policies | audits | usergroup | services | database | contextenrichers | denyconditions");
                logger.info("Please enter the valid arguments for Metric Calculation");
                break;
        }
    } catch (Exception e) {
        logger.error("Error calculating Metric : " + e.getMessage());
    }
}
Also used : VXMetricAuditDetailsCount(org.apache.ranger.view.VXMetricAuditDetailsCount) HashMap(java.util.HashMap) DateUtil(org.apache.ranger.common.DateUtil) VXGroupList(org.apache.ranger.view.VXGroupList) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) VXMetricPolicyCount(org.apache.ranger.view.VXMetricPolicyCount) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) VXMetricUserGroupCount(org.apache.ranger.view.VXMetricUserGroupCount) RangerService(org.apache.ranger.plugin.model.RangerService) GsonBuilder(com.google.gson.GsonBuilder) VXMetricContextEnricher(org.apache.ranger.view.VXMetricContextEnricher) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem) SearchCriteria(org.apache.ranger.common.SearchCriteria) Date(java.util.Date) RangerContextEnricherDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerContextEnricherDef) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) VXMetricServiceCount(org.apache.ranger.view.VXMetricServiceCount) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) SimpleDateFormat(java.text.SimpleDateFormat)

Example 94 with RangerService

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

the class MetricUtil method getVXMetricServiceCount.

private VXMetricServiceCount getVXMetricServiceCount(String serviceType) throws Exception {
    SearchFilter policyFilter1 = new SearchFilter();
    policyFilter1.setMaxRows(200);
    policyFilter1.setStartIndex(0);
    policyFilter1.setGetCount(true);
    policyFilter1.setSortBy("serviceId");
    policyFilter1.setSortType("asc");
    policyFilter1.setParam("policyType", serviceType);
    PList<RangerPolicy> policies = svcStore.getPaginatedPolicies(policyFilter1);
    PList<RangerService> paginatedSvcsSevice = svcStore.getPaginatedServices(policyFilter1);
    List<RangerService> rangerServiceList = paginatedSvcsSevice.getList();
    Map<String, Long> servicesforPolicyType = new HashMap<String, Long>();
    long tagCount = 0;
    for (Object rangerService : rangerServiceList) {
        RangerService rangerServiceObj = (RangerService) rangerService;
        String serviceName = rangerServiceObj.getType();
        if (!(servicesforPolicyType.containsKey(serviceName))) {
            policyFilter1.setParam("serviceType", serviceName);
            PList<RangerPolicy> policiestype = svcStore.getPaginatedPolicies(policyFilter1);
            long count = policiestype.getTotalCount();
            if (count != 0) {
                if (!"tag".equalsIgnoreCase(serviceName)) {
                    servicesforPolicyType.put(serviceName, count);
                } else {
                    tagCount = count;
                }
            }
        }
    }
    VXMetricServiceCount vXMetricServiceCount = new VXMetricServiceCount();
    vXMetricServiceCount.setServiceBasedCountList(servicesforPolicyType);
    long totalCountOfPolicyType = policies.getTotalCount() - tagCount;
    vXMetricServiceCount.setTotalCount(totalCountOfPolicyType);
    return vXMetricServiceCount;
}
Also used : HashMap(java.util.HashMap) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) VXMetricServiceCount(org.apache.ranger.view.VXMetricServiceCount) RangerService(org.apache.ranger.plugin.model.RangerService)

Example 95 with RangerService

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

the class AssetREST method getXResource.

@GET
@Path("/resources/{id}")
@Produces({ "application/xml", "application/json" })
public VXResource getXResource(@PathParam("id") Long id) {
    if (logger.isDebugEnabled()) {
        logger.debug("==> AssetREST.getXResource(" + id + ")");
    }
    RangerPolicy policy = null;
    RangerService service = null;
    policy = serviceREST.getPolicy(id);
    if (policy != null) {
        service = serviceREST.getServiceByName(policy.getService());
    }
    VXResource ret = serviceUtil.toVXResource(policy, service);
    if (logger.isDebugEnabled()) {
        logger.debug("<== AssetREST.getXResource(" + id + "): " + ret);
    }
    return ret;
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerService(org.apache.ranger.plugin.model.RangerService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

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