更新时间:2023-06-20 GMT+08:00
分享

HBase服务数据读写示例安全认证(多集群互信场景)

场景说明

当不同的多个Manager系统下安全模式的集群需要互相访问对方的资源时,管理员可以设置互信的系统,使外部系统的用户可以在本系统中使用。每个系统用户安全使用的范围定义为“域”,不同的Manager系统需要定义唯一的域名。跨Manager访问实际上就是用户跨域使用。集群配置互信具体操作步骤请参考集群互信管理章节。

多集群互信场景下,以符合跨域访问的用户身份,使用从其中一个manager系统中获取到的用于Kerberos安全认证的keytab文件和principal文件,以及多个Manager系统各自的客户端配置文件,可实现一次认证登录后访问调用多集群的HBase服务。

以下代码在hbase-example样例工程的“com.huawei.bigdata.hbase.examples”包的“TestMultipleLogin”类中。

  • 代码认证
            List<String> confDirectorys = new ArrayList<>();
            List<Configuration> confs = new LinkedList<>();
            try {
                // conf directory
                confDirectorys.add("hadoopDomain");[1]
                confDirectorys.add("hadoop1Domain");[2]
     
                for (String confDir : confDirectorys) {
                    confs.add(init(confDir));[3]
                }
     
                login(confs.get(0), confDirectorys.get(0));[4]
            } catch (IOException e) {
                LOG.error("Failed to login because ", e);
                return;
            }

    [1]此处hadoopDomain为保存用户凭证和其中一个集群的配置文件目录名称,该目录相对路径为hbase-example/src/main/resources/hadoopDomain,可根据需要进行变更。

    [2]此处hadoop1Domain为保存另一个集群配置文件的目录名称,该目录相对路径为hbase-example/src/main/resources/hadoop1Domain,可根据需要进行变更。

    [3]依次初始化conf对象。

    [4]进行登录认证。

  • 初始化配置
    private static Configuration init(String confDirectoryName) throws IOException {
            // Default load from conf directory
            Configuration conf = HBaseConfiguration.create();
            //In Windows environment
            String userdir = TestMain.class.getClassLoader().getResource(confDirectoryName).getPath() + File.separator;
            //In Linux environment
            //String userdir = System.getProperty("user.dir") + File.separator + confDirectoryName + File.separator;
            conf.addResource(new Path(userdir + "core-site.xml"), false);
            conf.addResource(new Path(userdir + "hdfs-site.xml"), false);
            conf.addResource(new Path(userdir + "hbase-site.xml"), false);
            return conf;
    }

前提条件

已获取样例工程运行所需的配置文件及认证文件,详细操作请参见准备连接集群配置文件

配置安全登录

请根据实际情况,在hbase-example样例工程的“com.huawei.bigdata.hbase.examples”包的“TestMultipleLogin”类中修改“userName”为实际用户名,例如“developuser”。

private static void login(Configuration conf, String confDir) throwsIOException {      

        if (User.isHBaseSecurityEnabled(conf)) {
           userName = " developuser ";

           //In Windows environment
           String userdir = TestMain.class.getClassLoader().getResource(confDir).getPath() + File.separator;
           //In Linux environment
           //String userdir = System.getProperty("user.dir") + File.separator + confDir + File.separator;

           userKeytabFile = userdir + "user.keytab";
           krb5File = userdir + "krb5.conf";
  
           /*  

            * if need to connect zk, please provide jaas info about zk. of course,
            * you can do it as below:
            * System.setProperty("java.security.auth.login.config",confDirPath +
            * "jaas.conf"); but the demo can help you more : Note: if this process
            * will connect more than one zk cluster, the demo may be not proper. you
            * can contact us for more help
            */

            LoginUtil.setJaasConf(ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME, userName,userKeytabFile);
            LoginUtil.login(userName, userKeytabFile, krb5File, conf);
        }
}
分享:

    相关文档

    相关产品