|
37 | 37 | import org.apache.hadoop.hbase.client.Put; |
38 | 38 | import org.apache.hadoop.hbase.client.Table; |
39 | 39 | import org.apache.hadoop.hbase.util.Bytes; |
| 40 | +import org.apache.hadoop.security.UserGroupInformation; |
40 | 41 | import org.slf4j.Logger; |
41 | 42 | import org.slf4j.LoggerFactory; |
42 | 43 |
|
| 44 | +import java.io.File; |
43 | 45 | import java.io.IOException; |
| 46 | +import java.security.PrivilegedAction; |
44 | 47 | import java.util.List; |
45 | 48 | import java.util.Map; |
46 | 49 | import java.util.Set; |
@@ -91,6 +94,29 @@ public void open(int taskNumber, int numTasks) throws IOException { |
91 | 94 | LOG.warn("---open---"); |
92 | 95 | conn = ConnectionFactory.createConnection(conf); |
93 | 96 | table = conn.getTable(TableName.valueOf(tableName)); |
| 97 | + if (kerberosAuthEnable) { |
| 98 | + conf.set(HbaseConfigUtils.KEY_HBASE_ZOOKEEPER_QUORUM, host); |
| 99 | + conf.set(HbaseConfigUtils.KEY_HBASE_ZOOKEEPER_ZNODE_QUORUM, zkParent); |
| 100 | + fillSyncKerberosConfig(conf, regionserverKeytabFile, regionserverPrincipal, zookeeperSaslClient, securityKrb5Conf); |
| 101 | + |
| 102 | + UserGroupInformation userGroupInformation = HbaseConfigUtils.loginAndReturnUGI(conf, regionserverPrincipal, regionserverKeytabFile); |
| 103 | + org.apache.hadoop.conf.Configuration finalConf = conf; |
| 104 | + conn = userGroupInformation.doAs(new PrivilegedAction<Connection>() { |
| 105 | + @Override |
| 106 | + public Connection run() { |
| 107 | + try { |
| 108 | + return ConnectionFactory.createConnection(finalConf); |
| 109 | + } catch (IOException e) { |
| 110 | + LOG.error("Get connection fail with config:{}", finalConf); |
| 111 | + throw new RuntimeException(e); |
| 112 | + } |
| 113 | + } |
| 114 | + }); |
| 115 | + } else { |
| 116 | + conf.set(HbaseConfigUtils.KEY_HBASE_ZOOKEEPER_QUORUM, host); |
| 117 | + conf.set(HbaseConfigUtils.KEY_HBASE_ZOOKEEPER_ZNODE_QUORUM, zkParent); |
| 118 | + conn = ConnectionFactory.createConnection(conf); |
| 119 | + } |
94 | 120 | LOG.warn("---open end(get table from hbase) ---"); |
95 | 121 | initMetric(); |
96 | 122 | } |
@@ -311,5 +337,35 @@ public HbaseOutputFormat finish() { |
311 | 337 |
|
312 | 338 | } |
313 | 339 |
|
| 340 | + private void fillSyncKerberosConfig( org.apache.hadoop.conf.Configuration config, String regionserverKeytabFile, String regionserverPrincipal, |
| 341 | + String zookeeperSaslClient, String securityKrb5Conf) throws IOException { |
| 342 | + if (StringUtils.isEmpty(regionserverKeytabFile)) { |
| 343 | + throw new IllegalArgumentException("Must provide regionserverKeytabFile when authentication is Kerberos"); |
| 344 | + } |
| 345 | + String regionserverKeytabFilePath = System.getProperty("user.dir") + File.separator + regionserverKeytabFile; |
| 346 | + LOG.info("regionserverKeytabFilePath:{}",regionserverKeytabFilePath); |
| 347 | + config.set(HbaseConfigUtils.KEY_HBASE_MASTER_KEYTAB_FILE, regionserverKeytabFilePath); |
| 348 | + config.set(HbaseConfigUtils.KEY_HBASE_REGIONSERVER_KEYTAB_FILE, regionserverKeytabFilePath); |
| 349 | + |
| 350 | + if (StringUtils.isEmpty(regionserverPrincipal)) { |
| 351 | + throw new IllegalArgumentException("Must provide regionserverPrincipal when authentication is Kerberos"); |
| 352 | + } |
| 353 | + config.set(HbaseConfigUtils.KEY_HBASE_MASTER_KERBEROS_PRINCIPAL, regionserverPrincipal); |
| 354 | + config.set(HbaseConfigUtils.KEY_HBASE_REGIONSERVER_KERBEROS_PRINCIPAL, regionserverPrincipal); |
| 355 | + config.set(HbaseConfigUtils.KEY_HBASE_SECURITY_AUTHORIZATION, "true"); |
| 356 | + config.set(HbaseConfigUtils.KEY_HBASE_SECURITY_AUTHENTICATION, "kerberos"); |
| 357 | + |
| 358 | + |
| 359 | + if (!StringUtils.isEmpty(zookeeperSaslClient)) { |
| 360 | + System.setProperty(HbaseConfigUtils.KEY_ZOOKEEPER_SASL_CLIENT, zookeeperSaslClient); |
| 361 | + } |
| 362 | + |
| 363 | + if (!StringUtils.isEmpty(securityKrb5Conf)) { |
| 364 | + String krb5ConfPath = System.getProperty("user.dir") + File.separator + securityKrb5Conf; |
| 365 | + LOG.info("krb5ConfPath:{}", krb5ConfPath); |
| 366 | + System.setProperty(HbaseConfigUtils.KEY_JAVA_SECURITY_KRB5_CONF, krb5ConfPath); |
| 367 | + } |
| 368 | + } |
| 369 | + |
314 | 370 |
|
315 | 371 | } |
0 commit comments