Search in sources :

Example 1 with XXServiceDef

use of org.apache.ranger.entity.XXServiceDef in project ranger by apache.

the class ServiceDBStore method writeCSVForPolicyItems.

private void writeCSVForPolicyItems(RangerPolicy policy, RangerPolicyItem policyItem, RangerDataMaskPolicyItem dataMaskPolicyItem, RangerRowFilterPolicyItem rowFilterPolicyItem, StringBuilder csvBuffer, String policyConditionType) {
    if (LOG.isDebugEnabled()) {
        // To avoid PMD violation
        LOG.debug("policyConditionType:[" + policyConditionType + "]");
    }
    final String COMMA_DELIMITER = "|";
    final String LINE_SEPARATOR = "\n";
    List<String> groups = new ArrayList<String>();
    List<String> users = new ArrayList<String>();
    String groupNames = "";
    String userNames = "";
    String policyLabelName = "";
    String accessType = "";
    String policyStatus = "";
    String policyType = "";
    Boolean delegateAdmin = false;
    String isRecursive = "";
    String isExcludes = "";
    String serviceName = "";
    String description = "";
    Boolean isAuditEnabled = true;
    String isExcludesValue = "";
    String maskingInfo = "";
    List<RangerPolicyItemAccess> accesses = new ArrayList<RangerPolicyItemAccess>();
    List<RangerPolicyItemCondition> conditionsList = new ArrayList<RangerPolicyItemCondition>();
    String conditionKeyValue = "";
    String resValue = "";
    String resourceKeyVal = "";
    String isRecursiveValue = "";
    String resKey = "";
    String ServiceType = "";
    String filterExpr = "";
    String policyName = "";
    List<String> policyLabels = new ArrayList<String>();
    String policyConditionTypeValue = "";
    serviceName = policy.getService();
    description = policy.getDescription();
    isAuditEnabled = policy.getIsAuditEnabled();
    policyLabels = policy.getPolicyLabels();
    StringBuffer sb = new StringBuffer();
    StringBuffer sbIsRecursive = new StringBuffer();
    StringBuffer sbIsExcludes = new StringBuffer();
    Map<String, RangerPolicyResource> resources = policy.getResources();
    RangerPolicyItemDataMaskInfo dataMaskInfo = new RangerPolicyItemDataMaskInfo();
    RangerPolicyItemRowFilterInfo filterInfo = new RangerPolicyItemRowFilterInfo();
    policyName = policy.getName();
    policyName = policyName.replace("|", "");
    if (resources != null) {
        for (Entry<String, RangerPolicyResource> resource : resources.entrySet()) {
            resKey = resource.getKey();
            RangerPolicyResource policyResource = resource.getValue();
            List<String> resvalueList = policyResource.getValues();
            isExcludes = policyResource.getIsExcludes().toString();
            isRecursive = policyResource.getIsRecursive().toString();
            resValue = resvalueList.toString();
            sb = sb.append(resourceKeyVal).append(" ").append(resKey).append("=").append(resValue);
            sbIsExcludes = sbIsExcludes.append(resourceKeyVal).append(" ").append(resKey).append("=[").append(isExcludes).append("]");
            sbIsRecursive = sbIsRecursive.append(resourceKeyVal).append(" ").append(resKey).append("=[").append(isRecursive).append("]");
        }
        isExcludesValue = sbIsExcludes.toString();
        isExcludesValue = isExcludesValue.substring(1);
        isRecursiveValue = sbIsRecursive.toString();
        isRecursiveValue = isRecursiveValue.substring(1);
        resourceKeyVal = sb.toString();
        resourceKeyVal = resourceKeyVal.substring(1);
        if (policyItem != null && dataMaskPolicyItem == null && rowFilterPolicyItem == null) {
            groups = policyItem.getGroups();
            users = policyItem.getUsers();
            accesses = policyItem.getAccesses();
            delegateAdmin = policyItem.getDelegateAdmin();
            conditionsList = policyItem.getConditions();
        } else if (dataMaskPolicyItem != null && policyItem == null && rowFilterPolicyItem == null) {
            groups = dataMaskPolicyItem.getGroups();
            users = dataMaskPolicyItem.getUsers();
            accesses = dataMaskPolicyItem.getAccesses();
            delegateAdmin = dataMaskPolicyItem.getDelegateAdmin();
            conditionsList = dataMaskPolicyItem.getConditions();
            dataMaskInfo = dataMaskPolicyItem.getDataMaskInfo();
            String dataMaskType = dataMaskInfo.getDataMaskType();
            String conditionExpr = dataMaskInfo.getConditionExpr();
            String valueExpr = dataMaskInfo.getValueExpr();
            maskingInfo = "dataMasktype=[" + dataMaskType + "]";
            if (conditionExpr != null && !conditionExpr.isEmpty() && valueExpr != null && !valueExpr.isEmpty()) {
                maskingInfo = maskingInfo + "; conditionExpr=[" + conditionExpr + "]";
            }
        } else if (rowFilterPolicyItem != null && policyItem == null && dataMaskPolicyItem == null) {
            groups = rowFilterPolicyItem.getGroups();
            users = rowFilterPolicyItem.getUsers();
            accesses = rowFilterPolicyItem.getAccesses();
            delegateAdmin = rowFilterPolicyItem.getDelegateAdmin();
            conditionsList = rowFilterPolicyItem.getConditions();
            filterInfo = rowFilterPolicyItem.getRowFilterInfo();
            filterExpr = filterInfo.getFilterExpr();
        }
        if (CollectionUtils.isNotEmpty(accesses)) {
            for (RangerPolicyItemAccess access : accesses) {
                accessType = accessType + access.getType().replace("#", "").replace("|", "") + "#";
            }
            accessType = accessType.substring(0, accessType.lastIndexOf("#"));
        }
        if (CollectionUtils.isNotEmpty(groups)) {
            for (String group : groups) {
                group = group.replace("|", "");
                group = group.replace("#", "");
                groupNames = groupNames + group + "#";
            }
            groupNames = groupNames.substring(0, groupNames.lastIndexOf("#"));
        }
        if (CollectionUtils.isNotEmpty(users)) {
            for (String user : users) {
                user = user.replace("|", "");
                user = user.replace("#", "");
                userNames = userNames + user + "#";
            }
            userNames = userNames.substring(0, userNames.lastIndexOf("#"));
        }
        String conditionValue = "";
        for (RangerPolicyItemCondition conditions : conditionsList) {
            String conditionType = conditions.getType();
            List<String> conditionList = conditions.getValues();
            conditionValue = conditionList.toString();
            conditionKeyValue = conditionType + "=" + conditionValue;
        }
        XXService xxservice = daoMgr.getXXService().findByName(policy.getService());
        if (xxservice != null) {
            Long ServiceId = xxservice.getType();
            XXServiceDef xxservDef = daoMgr.getXXServiceDef().getById(ServiceId);
            if (xxservDef != null) {
                ServiceType = xxservDef.getName();
            }
        }
    }
    if (policyConditionType != null) {
        policyConditionTypeValue = policyConditionType;
    }
    if (policyConditionType == null && ServiceType.equalsIgnoreCase("tag")) {
        policyConditionTypeValue = POLICY_ALLOW_INCLUDE;
    } else if (policyConditionType == null) {
        policyConditionTypeValue = "";
    }
    if (policy.getIsEnabled()) {
        policyStatus = "Enabled";
    } else {
        policyStatus = "Disabled";
    }
    int policyTypeInt = policy.getPolicyType();
    switch(policyTypeInt) {
        case RangerPolicy.POLICY_TYPE_ACCESS:
            policyType = POLICY_TYPE_ACCESS;
            break;
        case RangerPolicy.POLICY_TYPE_DATAMASK:
            policyType = POLICY_TYPE_DATAMASK;
            break;
        case RangerPolicy.POLICY_TYPE_ROWFILTER:
            policyType = POLICY_TYPE_ROWFILTER;
            break;
    }
    if (CollectionUtils.isNotEmpty(policyLabels)) {
        for (String policyLabel : policyLabels) {
            policyLabel = policyLabel.replace("|", "");
            policyLabel = policyLabel.replace("#", "");
            policyLabelName = policyLabelName + policyLabel + "#";
        }
        policyLabelName = policyLabelName.substring(0, policyLabelName.lastIndexOf("#"));
    }
    csvBuffer.append(policy.getId());
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(policyName);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(resourceKeyVal);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(groupNames);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(userNames);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(accessType.trim());
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(ServiceType);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(policyStatus);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(policyType);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(delegateAdmin.toString().toUpperCase());
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(isRecursiveValue);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(isExcludesValue);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(serviceName);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(description);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(isAuditEnabled.toString().toUpperCase());
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(conditionKeyValue.trim());
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(policyConditionTypeValue);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(maskingInfo);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(filterExpr);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(policyLabelName);
    csvBuffer.append(COMMA_DELIMITER);
    csvBuffer.append(LINE_SEPARATOR);
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) ArrayList(java.util.ArrayList) VXString(org.apache.ranger.view.VXString) RangerPolicyItemAccess(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess) RangerPolicyItemRowFilterInfo(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemRowFilterInfo) RangerPolicyItemCondition(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition) XXService(org.apache.ranger.entity.XXService) RangerPolicyItemDataMaskInfo(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemDataMaskInfo)

Example 2 with XXServiceDef

use of org.apache.ranger.entity.XXServiceDef in project ranger by apache.

the class ServiceDBStore method getServiceDefByName.

@Override
public RangerServiceDef getServiceDefByName(String name) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.getServiceDefByName(" + name + ")");
    }
    RangerServiceDef ret = null;
    XXServiceDef xServiceDef = daoMgr.getXXServiceDef().findByName(name);
    if (xServiceDef != null) {
        ret = serviceDefService.getPopulatedViewObject(xServiceDef);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("== ServiceDBStore.getServiceDefByName(" + name + "): " + ret);
    }
    return ret;
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef)

Example 3 with XXServiceDef

use of org.apache.ranger.entity.XXServiceDef in project ranger by apache.

the class ServiceDBStore method writeCSV.

private StringBuilder writeCSV(List<RangerPolicy> policies, String cSVFileName, HttpServletResponse response) {
    response.setContentType("text/csv");
    final String LINE_SEPARATOR = "\n";
    final String FILE_HEADER = "ID|Name|Resources|Groups|Users|Accesses|Service Type|Status|Policy Type|Delegate Admin|isRecursive|" + "isExcludes|Service Name|Description|isAuditEnabled|Policy Conditions|Policy Condition Type|Masking Options|Row Filter Expr|Policy Label Name";
    StringBuilder csvBuffer = new StringBuilder();
    csvBuffer.append(FILE_HEADER);
    csvBuffer.append(LINE_SEPARATOR);
    if (!CollectionUtils.isEmpty(policies)) {
        for (RangerPolicy policy : policies) {
            List<RangerPolicyItem> policyItems = policy.getPolicyItems();
            List<RangerRowFilterPolicyItem> rowFilterPolicyItems = policy.getRowFilterPolicyItems();
            List<RangerDataMaskPolicyItem> dataMaskPolicyItems = policy.getDataMaskPolicyItems();
            List<RangerPolicyItem> allowExceptions = policy.getAllowExceptions();
            List<RangerPolicyItem> denyExceptions = policy.getDenyExceptions();
            List<RangerPolicyItem> denyPolicyItems = policy.getDenyPolicyItems();
            XXService xxservice = daoMgr.getXXService().findByName(policy.getService());
            String serviceType = "";
            if (xxservice != null) {
                Long ServiceId = xxservice.getType();
                XXServiceDef xxservDef = daoMgr.getXXServiceDef().getById(ServiceId);
                if (xxservDef != null) {
                    serviceType = xxservDef.getName();
                }
            }
            if (CollectionUtils.isNotEmpty(policyItems)) {
                for (RangerPolicyItem policyItem : policyItems) {
                    writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_ALLOW_INCLUDE);
                }
            } else if (CollectionUtils.isNotEmpty(dataMaskPolicyItems)) {
                for (RangerDataMaskPolicyItem dataMaskPolicyItem : dataMaskPolicyItems) {
                    writeCSVForPolicyItems(policy, null, dataMaskPolicyItem, null, csvBuffer, null);
                }
            } else if (CollectionUtils.isNotEmpty(rowFilterPolicyItems)) {
                for (RangerRowFilterPolicyItem rowFilterPolicyItem : rowFilterPolicyItems) {
                    writeCSVForPolicyItems(policy, null, null, rowFilterPolicyItem, csvBuffer, null);
                }
            } else if (serviceType.equalsIgnoreCase(EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME)) {
                if (CollectionUtils.isEmpty(policyItems)) {
                    RangerPolicyItem policyItem = new RangerPolicyItem();
                    writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_ALLOW_INCLUDE);
                }
            } else if (CollectionUtils.isEmpty(policyItems)) {
                RangerPolicyItem policyItem = new RangerPolicyItem();
                writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_ALLOW_INCLUDE);
            }
            if (CollectionUtils.isNotEmpty(allowExceptions)) {
                for (RangerPolicyItem policyItem : allowExceptions) {
                    writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_ALLOW_EXCLUDE);
                }
            }
            if (CollectionUtils.isNotEmpty(denyExceptions)) {
                for (RangerPolicyItem policyItem : denyExceptions) {
                    writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_DENY_EXCLUDE);
                }
            }
            if (CollectionUtils.isNotEmpty(denyPolicyItems)) {
                for (RangerPolicyItem policyItem : denyPolicyItems) {
                    writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_DENY_INCLUDE);
                }
            }
        }
    }
    response.setHeader("Content-Disposition", "attachment; filename=" + cSVFileName);
    response.setStatus(HttpServletResponse.SC_OK);
    return csvBuffer;
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerDataMaskPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerDataMaskPolicyItem) RangerRowFilterPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerRowFilterPolicyItem) VXString(org.apache.ranger.view.VXString) RangerPolicyItem(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem) XXService(org.apache.ranger.entity.XXService)

Example 4 with XXServiceDef

use of org.apache.ranger.entity.XXServiceDef in project ranger by apache.

the class TagDBStore method createResourceForServiceResource.

private void createResourceForServiceResource(Long resourceId, RangerServiceResource serviceResource) {
    String serviceName = serviceResource.getServiceName();
    XXService xService = daoManager.getXXService().findByName(serviceName);
    if (xService == null) {
        throw errorUtil.createRESTException("No Service found with name: " + serviceName, MessageEnums.ERROR_CREATING_OBJECT);
    }
    XXServiceDef xServiceDef = daoManager.getXXServiceDef().getById(xService.getType());
    if (xServiceDef == null) {
        throw errorUtil.createRESTException("No Service-Def found with ID: " + xService.getType(), MessageEnums.ERROR_CREATING_OBJECT);
    }
    Map<String, RangerPolicy.RangerPolicyResource> resElements = serviceResource.getResourceElements();
    for (Map.Entry<String, RangerPolicyResource> resElement : resElements.entrySet()) {
        XXResourceDef xResDef = daoManager.getXXResourceDef().findByNameAndServiceDefId(resElement.getKey(), xServiceDef.getId());
        if (xResDef == null) {
            LOG.error("TagDBStore.createResource: ResourceType is not valid [" + resElement.getKey() + "]");
            throw errorUtil.createRESTException("Resource Type is not valid [" + resElement.getKey() + "]", MessageEnums.DATA_NOT_FOUND);
        }
        RangerPolicyResource policyRes = resElement.getValue();
        XXServiceResourceElement resourceElement = new XXServiceResourceElement();
        resourceElement.setIsExcludes(policyRes.getIsExcludes());
        resourceElement.setIsRecursive(policyRes.getIsRecursive());
        resourceElement.setResDefId(xResDef.getId());
        resourceElement.setResourceId(resourceId);
        resourceElement = rangerAuditFields.populateAuditFieldsForCreate(resourceElement);
        resourceElement = daoManager.getXXServiceResourceElement().create(resourceElement);
        int sortOrder = 1;
        for (String resVal : policyRes.getValues()) {
            XXServiceResourceElementValue resourceElementValue = new XXServiceResourceElementValue();
            resourceElementValue.setResElementId(resourceElement.getId());
            resourceElementValue.setValue(resVal);
            resourceElementValue.setSortOrder(sortOrder);
            resourceElementValue = rangerAuditFields.populateAuditFieldsForCreate(resourceElementValue);
            resourceElementValue = daoManager.getXXServiceResourceElementValue().create(resourceElementValue);
            sortOrder++;
        }
    }
}
Also used : XXServiceResourceElementValue(org.apache.ranger.entity.XXServiceResourceElementValue) XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) XXService(org.apache.ranger.entity.XXService) HashMap(java.util.HashMap) XXTagResourceMap(org.apache.ranger.entity.XXTagResourceMap) Map(java.util.Map) XXResourceDef(org.apache.ranger.entity.XXResourceDef) XXServiceResourceElement(org.apache.ranger.entity.XXServiceResourceElement)

Example 5 with XXServiceDef

use of org.apache.ranger.entity.XXServiceDef in project ranger by apache.

the class TestRangerServiceDefServiceBase method test1MapViewToEntityBean.

@Test
public void test1MapViewToEntityBean() {
    RangerServiceDef rangerServiceDef = rangerServiceDef();
    XXServiceDef serviceDef = serviceDef();
    int operationContext = 1;
    XXServiceDef dbServiceDef = rangerServiceDefService.mapViewToEntityBean(rangerServiceDef, serviceDef, operationContext);
    Assert.assertNotNull(dbServiceDef);
    Assert.assertEquals(dbServiceDef, serviceDef);
    Assert.assertEquals(dbServiceDef.getDescription(), serviceDef.getDescription());
    Assert.assertEquals(dbServiceDef.getGuid(), serviceDef.getGuid());
    Assert.assertEquals(dbServiceDef.getName(), serviceDef.getName());
    Assert.assertEquals(dbServiceDef.getAddedByUserId(), serviceDef.getAddedByUserId());
    Assert.assertEquals(dbServiceDef.getId(), serviceDef.getId());
    Assert.assertEquals(dbServiceDef.getVersion(), serviceDef.getVersion());
    Assert.assertEquals(dbServiceDef.getImplclassname(), serviceDef.getImplclassname());
    Assert.assertEquals(dbServiceDef.getUpdatedByUserId(), serviceDef.getUpdatedByUserId());
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) Test(org.junit.Test)

Aggregations

XXServiceDef (org.apache.ranger.entity.XXServiceDef)79 Test (org.junit.Test)38 XXService (org.apache.ranger.entity.XXService)34 RangerService (org.apache.ranger.plugin.model.RangerService)26 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)25 VXString (org.apache.ranger.view.VXString)22 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)21 WebApplicationException (javax.ws.rs.WebApplicationException)20 ArrayList (java.util.ArrayList)14 Date (java.util.Date)14 XXServiceDao (org.apache.ranger.db.XXServiceDao)13 Path (javax.ws.rs.Path)11 Produces (javax.ws.rs.Produces)11 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)11 RangerPerfTracer (org.apache.ranger.plugin.util.RangerPerfTracer)9 RangerAccessTypeDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef)8 RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 IOException (java.io.IOException)6 XXPortalUser (org.apache.ranger.entity.XXPortalUser)6