1818
1919package com .dtstack .flink .sql .side .hbase .utils ;
2020
21- import com .dtstack .flink .sql .side .hbase .table .HbaseSideTableInfo ;
22- import com .dtstack .flink .sql .util .AuthUtil ;
2321import org .apache .commons .collections .MapUtils ;
2422import org .apache .commons .lang3 .StringUtils ;
2523import org .apache .hadoop .conf .Configuration ;
3331import java .io .FileWriter ;
3432import java .io .IOException ;
3533import java .util .Arrays ;
36- import java .util .HashMap ;
3734import java .util .List ;
3835import java .util .Map ;
3936import java .util .UUID ;
@@ -50,42 +47,157 @@ public class HbaseConfigUtils {
5047
5148 private static final Logger LOG = LoggerFactory .getLogger (HbaseConfigUtils .class );
5249 // sync side kerberos
53- public final static String KEY_HBASE_SECURITY_AUTHENTICATION = "hbase.security.authentication" ;
54- public final static String KEY_HBASE_SECURITY_AUTHORIZATION = "hbase.security.authorization" ;
55- public final static String KEY_HBASE_MASTER_KEYTAB_FILE = "hbase.master.keytab.file" ;
56- public final static String KEY_HBASE_MASTER_KERBEROS_PRINCIPAL = "hbase.master.kerberos.principal" ;
57- public final static String KEY_HBASE_REGIONSERVER_KEYTAB_FILE = "hbase.regionserver.keytab.file" ;
58- public final static String KEY_HBASE_REGIONSERVER_KERBEROS_PRINCIPAL = "hbase.regionserver.kerberos.principal" ;
50+ private final static String AUTHENTICATION_TYPE = "Kerberos" ;
51+ private final static String KEY_HBASE_SECURITY_AUTHENTICATION = "hbase.security.authentication" ;
52+ private final static String KEY_HBASE_SECURITY_AUTHORIZATION = "hbase.security.authorization" ;
53+ 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" ;
56+ private final static String KEY_HBASE_REGIONSERVER_KERBEROS_PRINCIPAL = "hbase.regionserver.kerberos.principal" ;
5957
6058 // async side kerberos
61- public final static String KEY_HBASE_SECURITY_AUTH_ENABLE = "hbase.security.auth.enable" ;
62- public final static String KEY_HBASE_SASL_CLIENTCONFIG = "hbase.sasl.clientconfig" ;
63- public final static String KEY_HBASE_KERBEROS_REGIONSERVER_PRINCIPAL = "hbase.kerberos.regionserver.principal" ;
59+ 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" ;
61+ private final static String KEY_HBASE_KERBEROS_REGIONSERVER_PRINCIPAL = "hbase.kerberos.regionserver.principal" ;
62+ private static final String KEY_KEY_TAB = "hbase.keytab" ;
63+ private static final String KEY_PRINCIPAL = "hbase.principal" ;
6464
6565 public final static String KEY_HBASE_ZOOKEEPER_QUORUM = "hbase.zookeeper.quorum" ;
66- public final static String KEY_HBASE_ZOOKEEPER_ZNODE_QUORUM_SYNC = "zookeeper.znode.parent" ;
67- public final static String KEY_HBASE_ZOOKEEPER_ZNODE_QUORUM_ASYNC = "hbase.zookeeper.znode.parent" ;
66+ public final static String KEY_HBASE_ZOOKEEPER_ZNODE_QUORUM = "hbase.zookeeper.znode.parent" ;
6867
6968
70- public static final String KEY_JAVA_SECURITY_KRB5_CONF = "java.security.krb5.conf" ;
71- public static final String KEY_ZOOKEEPER_SASL_CLIENT = "zookeeper.sasl.client" ;
72-
69+ private static final String KEY_JAVA_SECURITY_KRB5_CONF = "java.security.krb5.conf" ;
7370 public static final String KEY_JAVA_SECURITY_AUTH_LOGIN_CONF = "java.security.auth.login.config" ;
7471
7572
76- public static AuthUtil .JAASConfig buildJaasConfig (HbaseSideTableInfo hbaseSideTableInfo ) {
77- String keytabPath = System .getProperty ("user.dir" ) + File .separator + hbaseSideTableInfo .getRegionserverKeytabFile ();
78- Map <String , String > loginModuleOptions = new HashMap <>();
79- loginModuleOptions .put ("useKeyTab" , "true" );
80- loginModuleOptions .put ("useTicketCache" , "false" );
81- loginModuleOptions .put ("keyTab" , "\" " + keytabPath + "\" " );
82- loginModuleOptions .put ("principal" , "\" " + hbaseSideTableInfo .getJaasPrincipal () + "\" " );
83- return AuthUtil .JAASConfig .builder ().setEntryName ("Client" )
84- .setLoginModule ("com.sun.security.auth.module.Krb5LoginModule" )
85- .setLoginModuleFlag ("required" ).setLoginModuleOptions (loginModuleOptions ).build ();
73+ private static final String SP = File .separator ;
74+ private static final String KEY_KRB5_CONF = "krb5.conf" ;
75+
76+
77+ private static List <String > KEYS_KERBEROS_REQUIRED = Arrays .asList (
78+ KEY_HBASE_SECURITY_AUTHENTICATION ,
79+ KEY_HBASE_MASTER_KERBEROS_PRINCIPAL ,
80+ KEY_HBASE_MASTER_KEYTAB_FILE ,
81+ KEY_HBASE_REGIONSERVER_KEYTAB_FILE ,
82+ KEY_HBASE_REGIONSERVER_KERBEROS_PRINCIPAL
83+ );
84+
85+ private static List <String > ASYNC_KEYS_KERBEROS_REQUIRED = Arrays .asList (
86+ KEY_HBASE_SECURITY_AUTH_ENABLE ,
87+ KEY_HBASE_SASL_CLIENTCONFIG ,
88+ KEY_HBASE_KERBEROS_REGIONSERVER_PRINCIPAL ,
89+ KEY_HBASE_SECURITY_AUTHENTICATION ,
90+ KEY_KEY_TAB );
91+
92+
93+ public static Configuration getConfig (Map <String , Object > hbaseConfigMap ) {
94+ Configuration hConfiguration = HBaseConfiguration .create ();
95+
96+ for (Map .Entry <String , Object > entry : hbaseConfigMap .entrySet ()) {
97+ if (entry .getValue () != null && !(entry .getValue () instanceof Map )) {
98+ hConfiguration .set (entry .getKey (), entry .getValue ().toString ());
99+ }
100+ }
101+ return hConfiguration ;
102+ }
103+
104+ public static boolean openKerberos (Map <String , Object > hbaseConfigMap ) {
105+ if (!MapUtils .getBooleanValue (hbaseConfigMap , KEY_HBASE_SECURITY_AUTHORIZATION )) {
106+ return false ;
107+ }
108+ return AUTHENTICATION_TYPE .equalsIgnoreCase (MapUtils .getString (hbaseConfigMap , KEY_HBASE_SECURITY_AUTHENTICATION ));
109+ }
110+
111+ public static boolean asyncOpenKerberos (Map <String , Object > hbaseConfigMap ) {
112+ if (!MapUtils .getBooleanValue (hbaseConfigMap , KEY_HBASE_SECURITY_AUTH_ENABLE )) {
113+ return false ;
114+ }
115+ return AUTHENTICATION_TYPE .equalsIgnoreCase (MapUtils .getString (hbaseConfigMap , KEY_HBASE_SECURITY_AUTHENTICATION ));
86116 }
87117
88118
119+
120+
121+ public static Configuration getHadoopConfiguration (Map <String , Object > hbaseConfigMap ) {
122+ for (String key : KEYS_KERBEROS_REQUIRED ) {
123+ if (StringUtils .isEmpty (MapUtils .getString (hbaseConfigMap , key ))) {
124+ throw new IllegalArgumentException (String .format ("Must provide [%s] when authentication is Kerberos" , key ));
125+ }
126+ }
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 ;
141+ }
142+
143+ public static String getPrincipal (Map <String , Object > hbaseConfigMap ) {
144+ String principal = MapUtils .getString (hbaseConfigMap , KEY_HBASE_MASTER_KERBEROS_PRINCIPAL );
145+ if (StringUtils .isNotEmpty (principal )) {
146+ return principal ;
147+ }
148+
149+ throw new IllegalArgumentException ("" );
150+ }
151+
152+ public static String getKeytab (Map <String , Object > hbaseConfigMap ) {
153+ String keytab = MapUtils .getString (hbaseConfigMap , KEY_HBASE_MASTER_KEYTAB_FILE );
154+ if (StringUtils .isNotEmpty (keytab )) {
155+ return keytab ;
156+ }
157+
158+ throw new IllegalArgumentException ("" );
159+ }
160+
161+ public static void loadKrb5Conf (Map <String , Object > kerberosConfig ) {
162+ String krb5FilePath = MapUtils .getString (kerberosConfig , KEY_JAVA_SECURITY_KRB5_CONF );
163+ if (!org .apache .commons .lang .StringUtils .isEmpty (krb5FilePath )) {
164+ System .setProperty (KEY_JAVA_SECURITY_KRB5_CONF , krb5FilePath );;
165+ }
166+ }
167+
168+ public static String creatJassFile (String configStr ) throws IOException {
169+ String fileName = System .getProperty ("user.dir" );
170+ File krbConf = new File (fileName );
171+ File temp = File .createTempFile ("JAAS" , ".conf" , krbConf );
172+ temp .deleteOnExit ();
173+ BufferedWriter out = new BufferedWriter (new FileWriter (temp , false ));
174+ out .write (configStr + "\n " );
175+ out .close ();
176+ return temp .getAbsolutePath ();
177+ }
178+
179+ public static String buildJaasStr (Map <String , Object > kerberosConfig ) {
180+ for (String key : ASYNC_KEYS_KERBEROS_REQUIRED ) {
181+ if (StringUtils .isEmpty (MapUtils .getString (kerberosConfig , key ))) {
182+ throw new IllegalArgumentException (String .format ("Must provide [%s] when authentication is Kerberos" , key ));
183+ }
184+ }
185+
186+ String keyTab = MapUtils .getString (kerberosConfig , KEY_KEY_TAB );
187+ String principal = MapUtils .getString (kerberosConfig , KEY_PRINCIPAL );
188+
189+ StringBuilder jaasSB = new StringBuilder ("Client {\n " +
190+ " com.sun.security.auth.module.Krb5LoginModule required\n " +
191+ " useKeyTab=true\n " +
192+ " useTicketCache=false\n " );
193+ jaasSB .append (" keyTab=\" " ).append (keyTab ).append ("\" " ).append ("\n " );
194+ jaasSB .append (" principal=\" " ).append (principal ).append ("\" " ).append (";\n " );
195+ jaasSB .append ("};" );
196+ return jaasSB .toString ();
197+ }
198+
199+
200+
89201 public static UserGroupInformation loginAndReturnUGI (Configuration conf , String principal , String keytab ) throws IOException {
90202 if (conf == null ) {
91203 throw new IllegalArgumentException ("kerberos conf can not be null" );
@@ -104,5 +216,4 @@ public static UserGroupInformation loginAndReturnUGI(Configuration conf, String
104216
105217 return UserGroupInformation .loginUserFromKeytabAndReturnUGI (principal , keytab );
106218 }
107-
108219}
0 commit comments