Ranger基本使用

Ranger是一个用于Hadoop平台的安全框架,提供集中的服务启用、权限管理和全方位的数据安全访问控制。它支持通过UI或RESTAPI进行安全管理,实现细粒度的授权,包括基于角色和属性的授权,并对Hadoop组件的安全行为进行审计。Ranger的核心是Web应用程序,包含策略管理、审计日志和报告功能。示例代码展示了如何使用Ranger的API进行用户管理和权限设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ranger是用来在Hadoop平台上进行监控,启用服务,以及全方位数据安全访问管理的安全框架。

目标

  • 允许用户使用UI或者REST API对所有和安全相关的任务进行集中化管理
  • 允许用户使用一个管理工具对操作Hadoop体系中的组件和工具的行为进行细粒度的授权
  • 支持Hadoop体系中各个组件的授权认证标准
  • 增强了对不同业务场景需求的授权方法支持,例如基于角色的授权或者基于属性的授权
  • 支持对Hadoop组件所有涉及安全的审计行为的集中化管理

工作原理

Ranger的核心是web应用程序,也成为RangerAdmin模块,此模块由管理策略,审计日志和报告等三部分组成。

Java开发的Ranger工具类

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fline.aic.db.util.HttpClientUtil;
import org.apache.commons.lang.StringUtils;

public class RangerUtil {
	private static String rangerUrl = "rangeUrl";
	private static String rangerUserName = "rangeUserName";
	private static String rangerPassword = "rangePassword";
	private static String hiveService="active_hive";
	
	
	static {
		Properties p = new Properties();
		InputStream fis = RangerUtil.class.getClassLoader()
				.getResourceAsStream("applicationContext.properties");
		if (fis == null) {
			
		} else {
			try {
				p.load(fis);
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		rangerUrl=p.getProperty("multi.rangerUrl");
		rangerUserName=p.getProperty("multi.rangerUserName");
		rangerPassword=p.getProperty("multi.rangerPassword");
		hiveService=p.getProperty("multi.rangerHiveService");
		
		
	}

	/**
	 * 根据用户名获取信息
	 * @param userName
	 * @return
	 */
	public static String getUser(String userName) {
		String result = HttpClientUtil.doGet(rangerUrl+"service/xusers/users/userName/"+userName,rangerUserName,rangerPassword);
		return result;
	}

	/**
	 * 创建用户
	 * @param userName
	 * @param password
	 * @return
	 */
	public static String createUser(String userName,String password) {
		StringBuilder sb = new StringBuilder();
		
		sb.append("{");
		sb.append("\"name\":\""+userName+"\",");
		sb.append("\"password\":\""+password+"\",");
		sb.append("\"userRoleList\":[\"ROLE_USER\"]");
		sb.append("}");
		
		String result = HttpClientUtil.doPostJson(rangerUrl+"service/xusers/secure/users", sb.toString(),rangerUserName,rangerPassword);
		return result;
	}

	/**
	 * hive库的所有权授权给用户
	 * @param userName
	 * @param hivedb
	 * @return
	 * @throws IOException
	 */
	public static String createHivePolice(String userName,String hivedb) throws IOException {
		String policeName=hivedb+"_"+userName+"_owner";
		String policeDescription="Policy for "+hivedb+" - database, table, column";
		StringBuilder sb = new StringBuilder();

		sb.append("{");
		sb.append(" \"isEnabled\": true," +
				"    \"version\": 1," +
				"    \"service\": \""+hiveService+"\"," +
				"    \"name\": \""+policeName+"\"," +
				"    \"policyType\": 0," +
				"    \"policyPriority\": 0," +
				"    \"description\": \""+policeDescription+"\"," +
				"    \"isAuditEnabled\": true," +
				"    \"resources\": {" +
				"        \"database\": {" +
				"            \"values\": [" +
				"                \""+hivedb+"\"" +
				"            ]," +
				"            \"isExcludes\": false," +
				"            \"isRecursive\": false" +
				"        }," +
				"        \"column\": {" +
				"            \"values\": [" +
				"                \"*\"" +
				"            ]," +
				"            \"isExcludes\": false," +
				"            \"isRecursive\": false" +
				"        }," +
				"        \"table\": {" +
				"            \"values\": [" +
				"                \"*\"" +
				"            ]," +
				"            \"isExcludes\": false," +
				"            \"isRecursive\": false" +
				"        }" +
				"    }," +
				"    \"policyItems\": [" +
				"        {" +
				"            \"accesses\": [" +
				"                {" +
				"                    \"type\": \"select\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"update\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"create\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"drop\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"alter\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"index\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"lock\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"all\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"read\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"write\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"repladmin\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"serviceadmin\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"tempudfadmin\"," +
				"                    \"isAllowed\": true" +
				"                }" +
				"            ]," +
				"            \"users\": [" +
				"                \""+userName+"\"" +
				"            ]," +
				"            \"groups\": []," +
				"            \"conditions\": []," +
				"            \"delegateAdmin\": true" +
				"        }" +
				"    ]," +
				"    \"denyPolicyItems\": []," +
				"    \"allowExceptions\": []," +
				"    \"denyExceptions\": []," +
				"    \"dataMaskPolicyItems\": []," +
				"    \"rowFilterPolicyItems\": []," +
				"    \"options\": {}," +
				"    \"validitySchedules\": []," +
				"    \"policyLabels\": [" +
				"        \"\"" +
				"    ]");
		sb.append("}");
		String result = HttpClientUtil.doPostJson(rangerUrl+"service/plugins/policies", sb.toString(),rangerUserName,rangerPassword);
		return result;
	}

	/**
	 * 获取serviceName下的全部有效的策略
	 */
	public static String getAllPolicies(){
		String result = HttpClientUtil.doGet(rangerUrl+"service/plugins/policies/download/"+hiveService,rangerUserName,rangerPassword);
		return result;
	}


	/**
	 * 将某库中的某表的只读授权给用户
	 * @param userName
	 * @param hivedb
	 * @param tableName
	 * @return
	 * @throws IOException
	 */
	public static String updateAuthUserTable(String userName,String hivedb,String tableName) throws IOException {
		String policeName =hivedb+"_authorize_"+userName;

		String isExistPolice = getPoliciesByName(policeName);

		String result = "";

		if("Not found".equals(isExistPolice) || StringUtils.isEmpty(isExistPolice)){
			result = createAuthUserTable(userName,hivedb,tableName);
		}else{
			JSONObject obj = JSONObject.parseObject(isExistPolice);
			//Police ID
			long id = obj.getLong("id");

			JSONObject resources = obj.getJSONObject("resources");
			JSONObject table = resources.getJSONObject("table");

			JSONArray values = table.getJSONArray("values");

			//已经存在了该表的授权
			if(values.contains(tableName)){
				return isExistPolice;
			}else{
				values.add(tableName);
				table.put("values",values);

				result = updatePoliciesById(id,obj.toJSONString());
			}
		}
		return result;
	}

	/**
	 * 删除授权
	 * @param userName
	 * @param hivedb
	 * @param tableName
	 * @return
	 * @throws IOException
	 */
	public static String removeAuthUserTable(String userName,String hivedb,String tableName) throws IOException {
		String policeName =hivedb+"_authorize_"+userName;

		String isExistPolice = getPoliciesByName(policeName);

		String result = "";

		if(!"Not found".equals(isExistPolice) && StringUtils.isNotEmpty(isExistPolice)){
			JSONObject obj = JSONObject.parseObject(isExistPolice);
			//Police ID
			long id = obj.getLong("id");

			JSONObject resources = obj.getJSONObject("resources");
			JSONObject table = resources.getJSONObject("table");

			JSONArray values = table.getJSONArray("values");
			System.out.println("授权表==>"+values);
			//已经存在了该表的授权
			if(values.contains(tableName)){
				values.remove(tableName);
				//如果删除没有了其他表的授权则直接删除该规则
				if(values==null ||values.size()==0){
					System.out.println("执行删除操作");
					deletePoliciesById(id);
				}else {
					table.put("values", values);
					System.out.println("执行更新操作");
					result = updatePoliciesById(id, obj.toJSONString());
				}
			}
		}
		return result;
	}

	/**
	 * 将某库中的某表的只读授权给用户
	 * @param userName
	 * @param hivedb
	 * @param tableName
	 * @return
	 * @throws IOException
	 */
	public static String createAuthUserTable(String userName,String hivedb,String tableName) throws IOException {
		String policeName=hivedb+"_authorize_"+userName;
		String policeDescription="Policy for some tables in"+hivedb+" To "+userName;
		StringBuilder sb = new StringBuilder();

		sb.append("{");
		sb.append(" \"isEnabled\": true," +
				"    \"version\": 1," +
				"    \"service\": \""+hiveService+"\"," +
				"    \"name\": \""+policeName+"\"," +
				"    \"policyType\": 0," +
				"    \"policyPriority\": 0," +
				"    \"description\": \""+policeDescription+"\"," +
				"    \"isAuditEnabled\": true," +
				"    \"resources\": {" +
				"        \"database\": {" +
				"            \"values\": [" +
				"                \""+hivedb+"\"" +
				"            ]," +
				"            \"isExcludes\": false," +
				"            \"isRecursive\": false" +
				"        }," +
				"        \"column\": {" +
				"            \"values\": [" +
				"                \"*\"" +
				"            ]," +
				"            \"isExcludes\": false," +
				"            \"isRecursive\": false" +
				"        }," +
				"        \"table\": {" +
				"            \"values\": [" +
				"               \""+tableName+"\"" +
				"            ]," +
				"            \"isExcludes\": false," +
				"            \"isRecursive\": false" +
				"        }" +
				"    }," +
				"    \"policyItems\": [" +
				"        {" +
				"            \"accesses\": [" +
				"                {" +
				"                    \"type\": \"select\"," +
				"                    \"isAllowed\": true" +
				"                }," +
				"                {" +
				"                    \"type\": \"read\"," +
				"                    \"isAllowed\": true" +
				"                }" +
				"            ]," +
				"            \"users\": [" +
				"                \""+userName+"\"" +
				"            ]," +
				"            \"groups\": []," +
				"            \"conditions\": []," +
				"            \"delegateAdmin\": true" +
				"        }" +
				"    ]," +
				"    \"denyPolicyItems\": []," +
				"    \"allowExceptions\": []," +
				"    \"denyExceptions\": []," +
				"    \"dataMaskPolicyItems\": []," +
				"    \"rowFilterPolicyItems\": []," +
				"    \"options\": {}," +
				"    \"validitySchedules\": []," +
				"    \"policyLabels\": [" +
				"        \"\"" +
				"    ]");
		sb.append("}");
		String result = HttpClientUtil.doPostJson(rangerUrl+"service/plugins/policies", sb.toString(),rangerUserName,rangerPassword);
		return result;
	}

	/**
	 * 根据PoliciesName获取策略
	 * @param policeName
	 */
	public static String getPoliciesByName(String policeName){
		String result = HttpClientUtil.doGet(rangerUrl+"service/public/v2/api/service/"+hiveService+"/policy/"+policeName,rangerUserName,rangerPassword);
		return result;
	}


	/**
	 * 根据策略ID删除
	 * @param id
	 */
	public static String deletePoliciesById(long id){
		String result = HttpClientUtil.doDelete(rangerUrl+"service/public/v2/api/policy/"+id,rangerUserName,rangerPassword);
		return result;
	}

	/**
	 * 根据策略ID更新
	 * @param id
	 * @param jsonStr
	 */
	public static String updatePoliciesById(long id,String jsonStr){
		String result = HttpClientUtil.doPut(rangerUrl+"service/public/v2/api/policy/"+id,jsonStr,rangerUserName,rangerPassword);
		return result;
	}

}
### Hadoop核心组件与Ranger的安全管理及授权集成 Hadoop生态系统中的安全管理是一个复杂而重要的领域,Cloudera Distribution Including Apache Hadoop (CDH) 提供了一套全面的工具来支持这一需求[^1]。Apache Ranger 是一个广泛使用的框架,用于集中化策略管理,旨在为大数据平台提供细粒度的访问控制。 #### 集成概述 Hadoop 的核心组件(如 HDFS、YARN 和 Hive)可以通过配置与 Apache Ranger 进行集成,从而实现统一的安全管理和授权功能。这种集成为管理员提供了更强大的能力去定义和执行复杂的权限规则。以下是具体的技术细节: - **HDFS 集成**: 当 HDFS 与 Ranger 集成时,可以基于文件路径设置精细到目录或文件级别的访问权限。通过 Ranger 插件,所有的读写操作都会被拦截并验证是否符合预设的安全策略[^4]。 ```bash hdfs dfs -mkdir /secure_folder ranger-admin --add-policy /secure_folder user=alice access=read-write ``` - **Hive 集成**: 对于数据仓库层面上的需求,Ranger 支持针对表、列甚至 SQL 查询语句本身的访问控制。这使得企业能够保护敏感字段不被未经授权的人查看[^2]。 - **YARN 集成**: YARN 中的任务调度也可以受到 Ranger 的监管,允许或者拒绝特定用户提交作业的权利。这对于多租户环境下的资源隔离尤为重要[^3]。 #### 技术实施要点 为了成功完成上述提到的各种类型的集成工作,通常需要遵循以下几个方面的要求: - 安装相应的 Ranger 插件版本匹配目标服务; - 修改相关服务的核心配置文件以启用插件加载机制; - 创建必要的 Kerberos 主体和服务票据以便建立可信的身份认证链路; 这些步骤共同构成了从基础架构层面保障整个系统的安全性屏障的一部分。 ```xml <property> <name>ranger.hdfs.plugin.enabled</name> <value>true</value> </property> ``` 以上 XML 片段展示了如何修改 `core-site.xml` 来激活 HDFS 上的 Ranger 插件实例。 ### 结论 综上所述,借助 Apache Ranger 实现对 Hadoop 各大子项目的深入管控不仅增强了整体解决方案的价值主张,同时也满足了现代企业在数字化转型过程中对于隐私保护日益增长的关注程度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值