Search in sources :

Example 81 with RangerService

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

the class ServiceUtil method toRangerService.

public RangerService toRangerService(VXAsset asset) {
    if (asset == null) {
        return null;
    }
    RangerService ret = new RangerService();
    dataObjectToRangerObject(asset, ret);
    ret.setType(toServiceType(asset.getAssetType()));
    ret.setName(asset.getName());
    ret.setDescription(asset.getDescription());
    ret.setIsEnabled(asset.getActiveStatus() == RangerCommonEnums.STATUS_ENABLED);
    ret.setConfigs(jsonUtil.jsonToMap(asset.getConfig()));
    return ret;
}
Also used : RangerService(org.apache.ranger.plugin.model.RangerService)

Example 82 with RangerService

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

the class ServiceUtil method toGrantRevokeRequest.

public GrantRevokeRequest toGrantRevokeRequest(VXPolicy vXPolicy) {
    String serviceType = null;
    RangerService service = null;
    GrantRevokeRequest ret = new GrantRevokeRequest();
    if (vXPolicy != null) {
        String serviceName = vXPolicy.getRepositoryName();
        try {
            service = svcStore.getServiceByName(serviceName);
        } catch (Exception e) {
            LOG.error(HttpServletResponse.SC_BAD_REQUEST + "No Service Found for ServiceName:" + serviceName);
            throw restErrorUtil.createRESTException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage() + serviceName, true);
        }
        if (service != null) {
            serviceType = service.getType();
        } else {
            LOG.error(HttpServletResponse.SC_BAD_REQUEST + "No Service Found for ServiceName" + serviceName);
            throw restErrorUtil.createRESTException(HttpServletResponse.SC_BAD_REQUEST, "No Service Found for ServiceName" + serviceName, true);
        }
        if (vXPolicy.getGrantor() != null) {
            ret.setGrantor(vXPolicy.getGrantor());
        }
        ret.setEnableAudit(Boolean.TRUE);
        ret.setIsRecursive(Boolean.FALSE);
        ret.setReplaceExistingPermissions(toBooleanReplacePerm(vXPolicy.isReplacePerm()));
        Integer assetType = toAssetType(serviceType);
        if (assetType == RangerCommonEnums.ASSET_HIVE) {
            String database = StringUtils.isEmpty(vXPolicy.getDatabases()) ? "*" : vXPolicy.getDatabases();
            String table = getTableOrUdf(vXPolicy);
            String column = StringUtils.isEmpty(vXPolicy.getColumns()) ? "*" : vXPolicy.getColumns();
            Map<String, String> mapResource = new HashMap<String, String>();
            mapResource.put("database", database);
            mapResource.put("table", table);
            mapResource.put("column", column);
            ret.setResource(mapResource);
        } else if (assetType == RangerCommonEnums.ASSET_HBASE) {
            String tableName = vXPolicy.getTables();
            tableName = StringUtil.isEmpty(tableName) ? "*" : tableName;
            String colFamily = vXPolicy.getColumnFamilies();
            colFamily = StringUtil.isEmpty(colFamily) ? "*" : colFamily;
            String qualifier = vXPolicy.getColumns();
            qualifier = StringUtil.isEmpty(qualifier) ? "*" : qualifier;
            Map<String, String> mapResource = new HashMap<String, String>();
            mapResource.put("table", tableName);
            mapResource.put("column-family", colFamily);
            mapResource.put("column", qualifier);
        }
        List<VXPermObj> vXPermObjList = vXPolicy.getPermMapList();
        if (vXPermObjList != null) {
            for (VXPermObj vXPermObj : vXPermObjList) {
                boolean delegatedAdmin = false;
                if (vXPermObj.getUserList() != null) {
                    for (String user : vXPermObj.getUserList()) {
                        if (user.contains(getUserName(user))) {
                            ret.getUsers().add(user);
                        }
                    }
                }
                if (vXPermObj.getGroupList() != null) {
                    for (String group : vXPermObj.getGroupList()) {
                        if (group.contains(getGroupName(group))) {
                            ret.getGroups().add(group);
                        }
                    }
                }
                if (vXPermObj.getPermList() != null) {
                    for (String perm : vXPermObj.getPermList()) {
                        if (AppConstants.getEnumFor_XAPermType(perm) != 0) {
                            if ("Admin".equalsIgnoreCase(perm)) {
                                delegatedAdmin = true;
                                if (assetType != null && assetType.intValue() != RangerCommonEnums.ASSET_HBASE) {
                                    continue;
                                }
                            }
                            ret.getAccessTypes().add(perm);
                        }
                    }
                }
                if (delegatedAdmin) {
                    ret.setDelegateAdmin(Boolean.TRUE);
                } else {
                    ret.setDelegateAdmin(Boolean.FALSE);
                }
            }
        }
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) RangerService(org.apache.ranger.plugin.model.RangerService) VXPermObj(org.apache.ranger.view.VXPermObj) GrantRevokeRequest(org.apache.ranger.plugin.util.GrantRevokeRequest) VXAuditMap(org.apache.ranger.view.VXAuditMap) VXPermMap(org.apache.ranger.view.VXPermMap) HashMap(java.util.HashMap) Map(java.util.Map) RangerServiceNotFoundException(org.apache.ranger.plugin.util.RangerServiceNotFoundException) InvalidNameException(javax.naming.InvalidNameException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 83 with RangerService

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

the class ServiceDBStore method deletePolicy.

@Override
public void deletePolicy(Long policyId) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.deletePolicy(" + policyId + ")");
    }
    RangerPolicy policy = getPolicy(policyId);
    if (policy == null) {
        throw new Exception("no policy exists with ID=" + policyId);
    }
    String policyName = policy.getName();
    RangerService service = getServiceByName(policy.getService());
    if (service == null) {
        throw new Exception("service does not exist - name='" + policy.getService());
    }
    Long version = policy.getVersion();
    if (version == null) {
        version = Long.valueOf(1);
        LOG.info("Found Version Value: `null`, so setting value of version to 1, While updating object, version should not be null.");
    } else {
        version = Long.valueOf(version.longValue() + 1);
    }
    policy.setVersion(version);
    List<XXTrxLog> trxLogList = policyService.getTransactionLog(policy, RangerPolicyService.OPERATION_DELETE_CONTEXT);
    deleteExistingPolicyItems(policy);
    deleteExistingPolicyResources(policy);
    deleteExistingPolicyLabel(policy);
    policyService.delete(policy);
    handlePolicyUpdate(service, true);
    dataHistService.createObjectDataHistory(policy, RangerDataHistService.ACTION_DELETE);
    bizUtil.createTrxLog(trxLogList);
    LOG.info("Policy Deleted Successfully. PolicyName : " + policyName);
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) VXString(org.apache.ranger.view.VXString) RangerService(org.apache.ranger.plugin.model.RangerService) XXTrxLog(org.apache.ranger.entity.XXTrxLog) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException)

Example 84 with RangerService

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

the class ServiceDBStore method updateServiceWithCustomProperty.

private void updateServiceWithCustomProperty() {
    LOG.info("Adding custom properties to services");
    SearchFilter filter = new SearchFilter();
    try {
        List<RangerService> lstRangerService = getServices(filter);
        for (RangerService rangerService : lstRangerService) {
            String serviceUser = PropertiesUtil.getProperty("ranger.plugins." + rangerService.getType() + ".serviceuser");
            if (!StringUtils.isEmpty(serviceUser)) {
                boolean chkServiceUpdate = false;
                LOG.debug("customproperty = " + rangerService.getConfigs().get(ServiceREST.Allowed_User_List_For_Download) + " for service = " + rangerService.getName());
                if (!rangerService.getConfigs().containsKey(ServiceREST.Allowed_User_List_For_Download)) {
                    rangerService.getConfigs().put(ServiceREST.Allowed_User_List_For_Download, serviceUser);
                    chkServiceUpdate = true;
                }
                if ((!rangerService.getConfigs().containsKey(ServiceREST.Allowed_User_List_For_Grant_Revoke)) && ("hbase".equalsIgnoreCase(rangerService.getType()) || "hive".equalsIgnoreCase(rangerService.getType()))) {
                    rangerService.getConfigs().put(ServiceREST.Allowed_User_List_For_Grant_Revoke, serviceUser);
                    chkServiceUpdate = true;
                }
                if (!rangerService.getConfigs().containsKey(TagREST.Allowed_User_List_For_Tag_Download)) {
                    rangerService.getConfigs().put(TagREST.Allowed_User_List_For_Tag_Download, serviceUser);
                    chkServiceUpdate = true;
                }
                if (chkServiceUpdate) {
                    updateService(rangerService, null);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Updated service " + rangerService.getName() + " with custom properties in secure environment");
                    }
                }
            }
        }
    } catch (Throwable e) {
        LOG.fatal("updateServiceWithCustomProperty failed with exception : " + e.getMessage());
    }
}
Also used : SearchFilter(org.apache.ranger.plugin.util.SearchFilter) RangerService(org.apache.ranger.plugin.model.RangerService) VXString(org.apache.ranger.view.VXString)

Example 85 with RangerService

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

the class ServiceDBStore method deletePolicy.

public void deletePolicy(RangerPolicy policy) throws Exception {
    if (policy == null) {
        return;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.deletePolicy(" + policy.getId() + ")");
    }
    RangerService service = getServiceByName(policy.getService());
    if (service == null) {
        throw new Exception("service does not exist - name='" + policy.getService());
    }
    Long version = policy.getVersion();
    if (version == null) {
        version = Long.valueOf(1);
        LOG.info("Found Version Value: `null`, so setting value of version to 1, While updating object, version should not be null.");
    } else {
        version = Long.valueOf(version.longValue() + 1);
    }
    policy.setVersion(version);
    List<XXTrxLog> trxLogList = policyService.getTransactionLog(policy, RangerPolicyService.OPERATION_DELETE_CONTEXT);
    deleteExistingPolicyItemsNative(policy);
    deleteExistingPolicyResourcesNative(policy);
    deleteExistingPolicyLabelNative(policy);
    daoMgr.getXXPolicy().deletePolicyIDReference("id", policy.getId());
    handlePolicyUpdate(service, true);
    dataHistService.createObjectDataHistory(policy, RangerDataHistService.ACTION_DELETE);
    bizUtil.createTrxLog(trxLogList);
}
Also used : RangerService(org.apache.ranger.plugin.model.RangerService) XXTrxLog(org.apache.ranger.entity.XXTrxLog) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException)

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