2525import org .apache .commons .lang3 .StringUtils ;
2626import org .apache .hadoop .conf .Configuration ;
2727import org .apache .hadoop .hbase .HBaseConfiguration ;
28+ import org .apache .hadoop .hbase .exceptions .IllegalArgumentIOException ;
2829import org .apache .hadoop .security .UserGroupInformation ;
30+ import org .apache .hadoop .security .authentication .util .KerberosName ;
2931import org .slf4j .Logger ;
3032import org .slf4j .LoggerFactory ;
33+ import sun .security .krb5 .Config ;
34+ import sun .security .krb5 .KrbException ;
3135
3236import java .io .File ;
3337import java .io .IOException ;
@@ -51,38 +55,34 @@ public class HbaseConfigUtils {
5155 private final static String KEY_HBASE_SECURITY_AUTHENTICATION = "hbase.security.authentication" ;
5256 private final static String KEY_HBASE_SECURITY_AUTHORIZATION = "hbase.security.authorization" ;
5357 private final static String KEY_HBASE_MASTER_KERBEROS_PRINCIPAL = "hbase.master.kerberos.principal" ;
54- private final static String KEY_HBASE_MASTER_KEYTAB_FILE = "hbase.master.keytab.file" ;
55- private final static String KEY_HBASE_REGIONSERVER_KEYTAB_FILE = "hbase.regionserver.keytab.file" ;
5658 private final static String KEY_HBASE_REGIONSERVER_KERBEROS_PRINCIPAL = "hbase.regionserver.kerberos.principal" ;
5759
60+ public final static String KEY_HBASE_CLIENT_KEYTAB_FILE = "hbase.client.keytab.file" ;
61+ public final static String KEY_HBASE_CLIENT_KERBEROS_PRINCIPAL = "hbase.client.kerberos.principal" ;
62+
63+ public static final String KEY_HADOOP_SECURITY_AUTHENTICATION = "hadoop.security.authentication" ;
64+ public static final String KEY_HADOOP_SECURITY_AUTH_TO_LOCAL = "hadoop.security.auth_to_local" ;
65+ public static final String KEY_HADOOP_SECURITY_AUTHORIZATION = "hadoop.security.authorization" ;
66+
5867 // async side kerberos
5968 private final static String KEY_HBASE_SECURITY_AUTH_ENABLE = "hbase.security.auth.enable" ;
60- private final static String KEY_HBASE_SASL_CLIENTCONFIG = "hbase.sasl.clientconfig" ;
6169 public final static String KEY_HBASE_KERBEROS_REGIONSERVER_PRINCIPAL = "hbase.kerberos.regionserver.principal" ;
6270 public static final String KEY_KEY_TAB = "hbase.keytab" ;
6371 public static final String KEY_PRINCIPAL = "hbase.principal" ;
6472
6573 public final static String KEY_HBASE_ZOOKEEPER_QUORUM = "hbase.zookeeper.quorum" ;
6674 public final static String KEY_HBASE_ZOOKEEPER_ZNODE_QUORUM = "hbase.zookeeper.znode.parent" ;
6775
68-
76+ public static final String KEY_ZOOKEEPER_SASL_CLIENT = "zookeeper.sasl.client" ;
6977 private static final String KEY_JAVA_SECURITY_KRB5_CONF = "java.security.krb5.conf" ;
7078
7179 private static List <String > KEYS_KERBEROS_REQUIRED = Arrays .asList (
7280 KEY_HBASE_SECURITY_AUTHENTICATION ,
73- KEY_HBASE_MASTER_KERBEROS_PRINCIPAL ,
74- KEY_HBASE_MASTER_KEYTAB_FILE ,
75- KEY_HBASE_REGIONSERVER_KEYTAB_FILE ,
76- KEY_HBASE_REGIONSERVER_KERBEROS_PRINCIPAL
77- );
78-
79- private static List <String > ASYNC_KEYS_KERBEROS_REQUIRED = Arrays .asList (
80- KEY_HBASE_SECURITY_AUTH_ENABLE ,
81- KEY_HBASE_SASL_CLIENTCONFIG ,
8281 KEY_HBASE_KERBEROS_REGIONSERVER_PRINCIPAL ,
83- KEY_HBASE_SECURITY_AUTHENTICATION ,
84- KEY_KEY_TAB );
85-
82+ KEY_PRINCIPAL ,
83+ KEY_KEY_TAB ,
84+ KEY_JAVA_SECURITY_KRB5_CONF
85+ );
8686
8787 public static Configuration getConfig (Map <String , Object > hbaseConfigMap ) {
8888 Configuration hConfiguration = HBaseConfiguration .create ();
@@ -124,24 +124,11 @@ public static Configuration getHadoopConfiguration(Map<String, Object> hbaseConf
124124 throw new IllegalArgumentException (String .format ("Must provide [%s] when authentication is Kerberos" , key ));
125125 }
126126 }
127- loadKrb5Conf (hbaseConfigMap );
128-
129- Configuration conf = new Configuration ();
130- if (hbaseConfigMap == null ) {
131- return conf ;
132- }
133-
134- hbaseConfigMap .forEach ((key , val ) -> {
135- if (val != null ) {
136- conf .set (key , val .toString ());
137- }
138- });
139-
140- return conf ;
127+ return HBaseConfiguration .create ();
141128 }
142129
143130 public static String getPrincipal (Map <String , Object > hbaseConfigMap ) {
144- String principal = MapUtils .getString (hbaseConfigMap , KEY_HBASE_MASTER_KERBEROS_PRINCIPAL );
131+ String principal = MapUtils .getString (hbaseConfigMap , KEY_PRINCIPAL );
145132 if (StringUtils .isNotEmpty (principal )) {
146133 return principal ;
147134 }
@@ -150,14 +137,37 @@ public static String getPrincipal(Map<String, Object> hbaseConfigMap) {
150137 }
151138
152139 public static String getKeytab (Map <String , Object > hbaseConfigMap ) {
153- String keytab = MapUtils .getString (hbaseConfigMap , KEY_HBASE_MASTER_KEYTAB_FILE );
140+ String keytab = MapUtils .getString (hbaseConfigMap , KEY_KEY_TAB );
154141 if (StringUtils .isNotEmpty (keytab )) {
155142 return keytab ;
156143 }
157144
158145 throw new IllegalArgumentException ("" );
159146 }
160147
148+ public static void fillSyncKerberosConfig (org .apache .hadoop .conf .Configuration config , Map <String , Object > hbaseConfigMap ) throws IOException {
149+ if (StringUtils .isEmpty (MapUtils .getString (hbaseConfigMap , KEY_HBASE_KERBEROS_REGIONSERVER_PRINCIPAL ))) {
150+ throw new IllegalArgumentException ("Must provide regionserverPrincipal when authentication is Kerberos" );
151+ }
152+
153+ String regionserverPrincipal = MapUtils .getString (hbaseConfigMap , KEY_HBASE_KERBEROS_REGIONSERVER_PRINCIPAL );
154+ config .set (HbaseConfigUtils .KEY_HBASE_MASTER_KERBEROS_PRINCIPAL , regionserverPrincipal );
155+ config .set (HbaseConfigUtils .KEY_HBASE_REGIONSERVER_KERBEROS_PRINCIPAL , regionserverPrincipal );
156+ config .set (HbaseConfigUtils .KEY_HBASE_SECURITY_AUTHORIZATION , "true" );
157+ config .set (HbaseConfigUtils .KEY_HBASE_SECURITY_AUTHENTICATION , "kerberos" );
158+
159+ if (!StringUtils .isEmpty (MapUtils .getString (hbaseConfigMap , KEY_ZOOKEEPER_SASL_CLIENT ))) {
160+ System .setProperty (HbaseConfigUtils .KEY_ZOOKEEPER_SASL_CLIENT , MapUtils .getString (hbaseConfigMap , KEY_ZOOKEEPER_SASL_CLIENT ));
161+ }
162+
163+ String securityKrb5Conf = MapUtils .getString (hbaseConfigMap , KEY_JAVA_SECURITY_KRB5_CONF );
164+ if (!StringUtils .isEmpty (securityKrb5Conf )) {
165+ String krb5ConfPath = System .getProperty ("user.dir" ) + File .separator + securityKrb5Conf ;
166+ LOG .info ("krb5ConfPath:{}" , krb5ConfPath );
167+ System .setProperty (HbaseConfigUtils .KEY_JAVA_SECURITY_KRB5_CONF , krb5ConfPath );
168+ }
169+ }
170+
161171 public static void loadKrb5Conf (Map <String , Object > config ) {
162172 String krb5conf = MapUtils .getString (config , KEY_JAVA_SECURITY_KRB5_CONF );
163173 checkOpt (krb5conf , KEY_JAVA_SECURITY_KRB5_CONF );
@@ -190,4 +200,35 @@ public static UserGroupInformation loginAndReturnUGI(Configuration conf, String
190200
191201 return UserGroupInformation .loginUserFromKeytabAndReturnUGI (principal , keytab );
192202 }
203+
204+ public static UserGroupInformation loginAndReturnUGI2 (Configuration conf , String principal , String keytab ) throws IOException , KrbException {
205+ LOG .info ("loginAndReturnUGI principal {}" ,principal );
206+ LOG .info ("loginAndReturnUGI keytab {}" ,keytab );
207+ if (conf == null ) {
208+ throw new IllegalArgumentException ("kerberos conf can not be null" );
209+ }
210+
211+ if (org .apache .commons .lang .StringUtils .isEmpty (principal )) {
212+ throw new IllegalArgumentException ("principal can not be null" );
213+ }
214+
215+ if (org .apache .commons .lang .StringUtils .isEmpty (keytab )) {
216+ throw new IllegalArgumentException ("keytab can not be null" );
217+ }
218+
219+ if (!new File (keytab ).exists ()){
220+ throw new IllegalArgumentIOException ("keytab [" + keytab + "] not exist" );
221+ }
222+
223+ conf .set (KEY_HADOOP_SECURITY_AUTHENTICATION , "Kerberos" );
224+ //conf.set("hadoop.security.auth_to_local", "DEFAULT");
225+ conf .set (KEY_HADOOP_SECURITY_AUTH_TO_LOCAL , "RULE:[1:$1] RULE:[2:$1]" );
226+ conf .set (KEY_HADOOP_SECURITY_AUTHORIZATION , "true" );
227+
228+ Config .refresh ();
229+ KerberosName .resetDefaultRealm ();
230+ UserGroupInformation .setConfiguration (conf );
231+
232+ return UserGroupInformation .loginUserFromKeytabAndReturnUGI (principal , keytab );
233+ }
193234}
0 commit comments