
# 访问HBase ThriftServer认证
#### 操作场景
HBase把Thrift结合起来可以向外部应用提供HBase服务。在HBase服务安装时可选部署ThriftServer实例，ThriftServer系统可访问HBase的用户，拥有HBase所有NameSpace和表的读、写、执行、创建和管理的权限。访问ThriftServer服务同样需要进行Kerberos认证。HBase实现了两套Thrift Server服务，此处"hbase-thrift-example"为ThriftServer实例服务的调用实现。
#### 操作步骤
1. 登录FusionInsight Manager，选择"集群 \> 服务 \> HBase \> 配置 \> 全部配置"，搜索并修改ThriftServer实例的配置参数"hbase.thrift.security.qop"。该参数值需与"hbase.rpc.protection"的值一一对应。保存配置，重启配置过期节点服务使更改的配置生效。
   
   ![](https://support.huaweicloud.com/devg-lts-mrs/public_sys-resources/note_3.0-zh-cn.png)
   "hbase.rpc.protection"与"hbase.thrift.security.qop"参数值的对应关系为：
   - **"privacy" - "auth-conf**"
   
   - **"authentication" - "auth**"
   
   - **"integrity" - "auth-int"**
    
   
   
2. 获取集群中安装ThriftServer对应实例的配置文件。 
   - 方法一：选择"集群 \> 服务 \> HBase \> 实例"，单击待操作的实例ThriftServer进入详情界面，获取配置文件"hdfs-site.xml"、"core-site.xml"、"hbase-site.xml"。
   
   - 方法二：通过[准备HBase应用开发和运行环境](https://support.huaweicloud.com/devg-lts-mrs/mrs_07_080006.html)中解压客户端文件的方法获取配置文件，需要在获取的"hbase-site.xml"中手动添加以下配置，其中"hbase.thrift.security.qop"的参数值与[1]保持一致。
     ```
     <property>
     <name>hbase.thrift.security.qop</name>
     <value>auth</value>
     </property>
     <property>
     <name>hbase.thrift.kerberos.principal</name>
     <value>thrift/hadoop.hadoop.com@HADOOP.COM</value>
     </property>
     <property>
     <name>hbase.thrift.keytab.file</name><value>/opt/huawei/Bigdata/FusionInsight_HD_8.1.2.2/install/FusionInsight-HBase-2.2.3/keytabs/HBase/thrift.keytab</value>
     </property>
     ```
     
   
   
   
   
 
#### 样例代码
- 代码认证
  以下代码在"hbase-thrift-example"样例工程的"com.huawei.bigdata.hbase.examples"包的"TestMain"类中。
  ```
  private static void init() throws IOException {
          // Default load from conf directory
          conf = HBaseConfiguration.create();
          String userdir = TestMain.class.getClassLoader().getResource("conf").getPath() + File.separator;[1]
          //In Linux environment
          //String userdir = System.getProperty("user.dir") + File.separator + "conf" + 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);
  }
  ```
  \[1\]**userdir**获取的是编译后资源路径下conf目录的路径。初始化配置用到的core-site.xml、hdfs-site.xml、hbase-site.xml文件和用于安全认证的用户凭证文件，需要放置到"src/main/resources/conf"的目录下。
  

- 安全登录
  请根据实际情况，修改"userName"为实际用户名，例如"developuser"。
  ```
  private static void login() throws IOException {
          if (User.isHBaseSecurityEnabled(conf)) {
              userName = " developuser ";
              //In Windows environment
              String userdir = TestMain.class.getClassLoader().getResource("conf").getPath() + File.separator;
              //In Linux environment
              //String userdir = System.getProperty("user.dir") + File.separator + "conf" + 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);
          }
  }
  ```
  
- 连接ThriftServer实例
  ```
  try {    
          test = new ThriftSample();    
          test.test("10.120.16.170", THRIFT_PORT, conf);[2]
      } catch (TException | IOException e) {
          LOG.error("Test thrift error", e);
      }
  ```
  MRS 3.6.0-LTS及之后版本：
  ```
  try {    
          test = new ThriftSample();    
          
          // Specify the Thrift server hostnames, separated by comma if multiple are there.
          String thriftServerHostNames = "xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx";
          test.test(thriftServerHostNames, THRIFT_PORT, conf);[2]
      } catch (TException | IOException e) {
          LOG.error("Test thrift error", e);
      }
  ```
  \[2\]test.test()传入参数为待访问的ThriftServer实例所在节点ip地址，需根据实际运行集群情况进行修改，建议部署多个ThriftServer增加业务可靠性，多个ThriftServer IP以逗号进行分隔，该节点IP需要配置到运行样例代码的本机hosts文件中。
  "THRIFT_PORT"为ThriftServer实例的配置参数"hbase.regionserver.thrift.port"对应的值。
  
 
