You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hbase.apache.org by Gaurav Thakur <ga...@pb.com> on 2014/05/08 12:06:20 UTC

Java Client : Secure Hbase

I`m looking for an example to connect to secure hbase using java.

Any Pointers will be helpful.

I tried to do it as follows but I`m unsuccessfull.

System.setProperty(CommonConstants.KRB_REALM, krbRealmValue);
              System.setProperty(CommonConstants.KRB_KDC, krbKdcValue);
              System.setProperty(CommonConstants.KRB_DEBUG, krbDebug);
hbaseConf.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION, AUTH_KRB);
              hbaseConf.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHORIZATION, AUTHORIZATION);
              hbaseConf.set(CommonConfigurationKeysPublic.FS_AUTOMATIC_CLOSE_KEY, AUTO_CLOSE);
              hbaseConf.set(CommonConfigurationKeysPublic.FS_DEFAULT_NAME_KEY, defaultFS);
              //hbaseConf.set("dfs.namenode.kerberos.principal", nnPrincipal);
              hbaseConf.set("hbase.zookeeper.quorum", ConfigUtil.getProperty(CommonConstants.HBASE_CONF, "hbase.host"));
              hbaseConf.set("hbase.zookeeper.property.clientPort", ConfigUtil.getProperty(CommonConstants.HBASE_CONF, "hbase.port"));
              Configuration conf = HBaseConfiguration.create(hbaseConf);
              conf.set("hbase.connection.timeout", "1200");
              //conf.set("zookeeper.znode.parent", "/hbase-secure");
              conf.set("hbase.master", "gauravt-namenode.pbi.global.pvt:60000");
              conf.set("hbase.client.retries.number", Integer.toString(0));
              conf.set("zookeeper.session.timeout", Integer.toString(0));
               conf.set("hbase.rpc.engine", "org.apache.hadoop.hbase.ipc.SecureRpcEngine");
               conf.set("zookeeper.recovery.retry", Integer.toString(0));
              conf.set("hbase.security.authentication", AUTH_KRB);
              conf.set("hbase.security.authorization", AUTHORIZATION);
               conf.set("hbase.master.kerberos.principal", "hbaseprincipal");
               conf.set("hbase.regionserver.kerberos.principal", "hbaseprincipal");
              hbaseConf = conf;
UserGroupInformation.setConfiguration(hbaseConf);
              userGroupInformation = UserGroupInformation.loginUserFromKeytabAndReturnUGI(domainUser, keytabFilePath);
              UserGroupInformation.setLoginUser(userGroupInformation);

              HBaseAdmin admins = new HBaseAdmin(hbaseConf)

                     if(admins.isTableAvailable("ambarismoketest")) {
                           System.out.println("Table is available");
                     };

                     HConnection connection = HConnectionManager.createConnection(client.getConfig());

                     HTableInterface table = connection.getTable("ambarismoketest");

                     byte [] family = Bytes.toBytes("fammily");

                     byte [] col01 = Bytes.toBytes("col01");

                     Scan scan = new Scan();
                     scan.addColumn(family, col01);

                     ResultScanner rs = table.getScanner(scan);

                     for (Result r = rs.next(); r != null; r = rs.next()) {
                byte[] valueObj = r.getValue(family, col01);
                String value = new String(valueObj);
                System.out.println(value);
            }

              System.out.println(table.get(new Get(null)));

              This fail at the time of trying to create a connection.

Thanks
gaurav



________________________________


Re: Java Client : Secure Hbase

Posted by anil gupta <an...@gmail.com>.
Hey Gaurav,

You need to have keytab and principal. Additionally, the configuration
files of cluster should be present in classpath of your app.

Please find below the sample code to create a configuration object for a
secure cluster:

/**
   * @param keytab location of the keytab file
   * @param principal Kerberos principal
   * @return Configuration that can be used to create HConnection or HTable
for a secure HBase cluster.
   */
  public static Configuration getHBaseConfiguration(String keytab, String
principal) {
    // create the type of object we will return from this function

    Configuration conf = HBaseConfiguration.create();

    // security
    conf.set("hbase.myclient.keytab", keytab);
    conf.set("hbase.myclient.principal", principal);
    UserGroupInformation.setConfiguration(conf);
    try {
      User.login(conf, "hbase.myclient.keytab", "hbase.myclient.principal",
null);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return conf;
  }


On Thu, May 8, 2014 at 3:06 AM, Gaurav Thakur <ga...@pb.com> wrote:

> I`m looking for an example to connect to secure hbase using java.
>
> Any Pointers will be helpful.
>
> I tried to do it as follows but I`m unsuccessfull.
>
> System.setProperty(CommonConstants.KRB_REALM, krbRealmValue);
>               System.setProperty(CommonConstants.KRB_KDC, krbKdcValue);
>               System.setProperty(CommonConstants.KRB_DEBUG, krbDebug);
> hbaseConf.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION,
> AUTH_KRB);
>
> hbaseConf.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHORIZATION,
> AUTHORIZATION);
>
> hbaseConf.set(CommonConfigurationKeysPublic.FS_AUTOMATIC_CLOSE_KEY,
> AUTO_CLOSE);
>
> hbaseConf.set(CommonConfigurationKeysPublic.FS_DEFAULT_NAME_KEY, defaultFS);
>               //hbaseConf.set("dfs.namenode.kerberos.principal",
> nnPrincipal);
>               hbaseConf.set("hbase.zookeeper.quorum",
> ConfigUtil.getProperty(CommonConstants.HBASE_CONF, "hbase.host"));
>               hbaseConf.set("hbase.zookeeper.property.clientPort",
> ConfigUtil.getProperty(CommonConstants.HBASE_CONF, "hbase.port"));
>               Configuration conf = HBaseConfiguration.create(hbaseConf);
>               conf.set("hbase.connection.timeout", "1200");
>               //conf.set("zookeeper.znode.parent", "/hbase-secure");
>               conf.set("hbase.master",
> "gauravt-namenode.pbi.global.pvt:60000");
>               conf.set("hbase.client.retries.number", Integer.toString(0));
>               conf.set("zookeeper.session.timeout", Integer.toString(0));
>                conf.set("hbase.rpc.engine",
> "org.apache.hadoop.hbase.ipc.SecureRpcEngine");
>                conf.set("zookeeper.recovery.retry", Integer.toString(0));
>               conf.set("hbase.security.authentication", AUTH_KRB);
>               conf.set("hbase.security.authorization", AUTHORIZATION);
>                conf.set("hbase.master.kerberos.principal",
> "hbaseprincipal");
>                conf.set("hbase.regionserver.kerberos.principal",
> "hbaseprincipal");
>               hbaseConf = conf;
> UserGroupInformation.setConfiguration(hbaseConf);
>               userGroupInformation =
> UserGroupInformation.loginUserFromKeytabAndReturnUGI(domainUser,
> keytabFilePath);
>               UserGroupInformation.setLoginUser(userGroupInformation);
>
>               HBaseAdmin admins = new HBaseAdmin(hbaseConf)
>
>                      if(admins.isTableAvailable("ambarismoketest")) {
>                            System.out.println("Table is available");
>                      };
>
>                      HConnection connection =
> HConnectionManager.createConnection(client.getConfig());
>
>                      HTableInterface table =
> connection.getTable("ambarismoketest");
>
>                      byte [] family = Bytes.toBytes("fammily");
>
>                      byte [] col01 = Bytes.toBytes("col01");
>
>                      Scan scan = new Scan();
>                      scan.addColumn(family, col01);
>
>                      ResultScanner rs = table.getScanner(scan);
>
>                      for (Result r = rs.next(); r != null; r = rs.next()) {
>                 byte[] valueObj = r.getValue(family, col01);
>                 String value = new String(valueObj);
>                 System.out.println(value);
>             }
>
>               System.out.println(table.get(new Get(null)));
>
>               This fail at the time of trying to create a connection.
>
> Thanks
> gaurav
>
>
>
> ________________________________
>
>


-- 
Thanks & Regards,
Anil Gupta