|
18 | 18 |
|
19 | 19 | package com.dtstack.flink.sql.side.hbase.utils; |
20 | 20 |
|
| 21 | +import com.dtstack.flink.sql.util.DtFileUtils; |
| 22 | +import com.google.common.base.Preconditions; |
| 23 | +import com.google.common.base.Strings; |
21 | 24 | import org.apache.commons.collections.MapUtils; |
22 | 25 | import org.apache.commons.lang3.StringUtils; |
23 | 26 | import org.apache.hadoop.conf.Configuration; |
|
26 | 29 | import org.slf4j.Logger; |
27 | 30 | import org.slf4j.LoggerFactory; |
28 | 31 |
|
29 | | -import java.io.BufferedWriter; |
30 | 32 | import java.io.File; |
31 | | -import java.io.FileWriter; |
32 | 33 | import java.io.IOException; |
33 | 34 | import java.util.Arrays; |
34 | 35 | import java.util.List; |
@@ -57,21 +58,15 @@ public class HbaseConfigUtils { |
57 | 58 | // async side kerberos |
58 | 59 | private final static String KEY_HBASE_SECURITY_AUTH_ENABLE = "hbase.security.auth.enable"; |
59 | 60 | private final static String KEY_HBASE_SASL_CLIENTCONFIG = "hbase.sasl.clientconfig"; |
60 | | - private final static String KEY_HBASE_KERBEROS_REGIONSERVER_PRINCIPAL = "hbase.kerberos.regionserver.principal"; |
| 61 | + public final static String KEY_HBASE_KERBEROS_REGIONSERVER_PRINCIPAL = "hbase.kerberos.regionserver.principal"; |
61 | 62 | public static final String KEY_KEY_TAB = "hbase.keytab"; |
62 | | - private static final String KEY_PRINCIPAL = "hbase.principal"; |
| 63 | + public static final String KEY_PRINCIPAL = "hbase.principal"; |
63 | 64 |
|
64 | 65 | public final static String KEY_HBASE_ZOOKEEPER_QUORUM = "hbase.zookeeper.quorum"; |
65 | 66 | public final static String KEY_HBASE_ZOOKEEPER_ZNODE_QUORUM = "hbase.zookeeper.znode.parent"; |
66 | 67 |
|
67 | 68 |
|
68 | 69 | private static final String KEY_JAVA_SECURITY_KRB5_CONF = "java.security.krb5.conf"; |
69 | | - public static final String KEY_JAVA_SECURITY_AUTH_LOGIN_CONF = "java.security.auth.login.config"; |
70 | | - |
71 | | - |
72 | | - private static final String SP = File.separator; |
73 | | - private static final String KEY_KRB5_CONF = "krb5.conf"; |
74 | | - |
75 | 70 |
|
76 | 71 | private static List<String> KEYS_KERBEROS_REQUIRED = Arrays.asList( |
77 | 72 | KEY_HBASE_SECURITY_AUTHENTICATION, |
@@ -100,23 +95,29 @@ public static Configuration getConfig(Map<String, Object> hbaseConfigMap) { |
100 | 95 | return hConfiguration; |
101 | 96 | } |
102 | 97 |
|
103 | | - public static boolean openKerberos(Map<String, Object> hbaseConfigMap) { |
104 | | - if (!MapUtils.getBooleanValue(hbaseConfigMap, KEY_HBASE_SECURITY_AUTHORIZATION)) { |
105 | | - return false; |
| 98 | + public static boolean isEnableKerberos(Map<String, Object> hbaseConfigMap) { |
| 99 | + boolean hasAuthorization = AUTHENTICATION_TYPE.equalsIgnoreCase( |
| 100 | + MapUtils.getString(hbaseConfigMap, KEY_HBASE_SECURITY_AUTHORIZATION) |
| 101 | + ); |
| 102 | + boolean hasAuthentication = AUTHENTICATION_TYPE.equalsIgnoreCase( |
| 103 | + MapUtils.getString(hbaseConfigMap, KEY_HBASE_SECURITY_AUTHENTICATION) |
| 104 | + ); |
| 105 | + boolean hasAuthEnable = MapUtils.getBooleanValue(hbaseConfigMap, KEY_HBASE_SECURITY_AUTH_ENABLE); |
| 106 | + |
| 107 | + if(hasAuthentication || hasAuthorization || hasAuthEnable) { |
| 108 | + LOG.info("Enable kerberos for hbase."); |
| 109 | + setKerberosConf(hbaseConfigMap); |
| 110 | + return true; |
106 | 111 | } |
107 | | - return AUTHENTICATION_TYPE.equalsIgnoreCase(MapUtils.getString(hbaseConfigMap, KEY_HBASE_SECURITY_AUTHENTICATION)); |
| 112 | + return false; |
108 | 113 | } |
109 | 114 |
|
110 | | - public static boolean asyncOpenKerberos(Map<String, Object> hbaseConfigMap) { |
111 | | - if (!MapUtils.getBooleanValue(hbaseConfigMap, KEY_HBASE_SECURITY_AUTH_ENABLE)) { |
112 | | - return false; |
113 | | - } |
114 | | - return AUTHENTICATION_TYPE.equalsIgnoreCase(MapUtils.getString(hbaseConfigMap, KEY_HBASE_SECURITY_AUTHENTICATION)); |
| 115 | + private static void setKerberosConf(Map<String,Object> hbaseConfigMap) { |
| 116 | + hbaseConfigMap.put(KEY_HBASE_SECURITY_AUTHORIZATION, AUTHENTICATION_TYPE); |
| 117 | + hbaseConfigMap.put(KEY_HBASE_SECURITY_AUTHENTICATION, AUTHENTICATION_TYPE); |
| 118 | + hbaseConfigMap.put(KEY_HBASE_SECURITY_AUTH_ENABLE, true); |
115 | 119 | } |
116 | 120 |
|
117 | | - |
118 | | - |
119 | | - |
120 | 121 | public static Configuration getHadoopConfiguration(Map<String, Object> hbaseConfigMap) { |
121 | 122 | for (String key : KEYS_KERBEROS_REQUIRED) { |
122 | 123 | if (StringUtils.isEmpty(MapUtils.getString(hbaseConfigMap, key))) { |
@@ -157,46 +158,20 @@ public static String getKeytab(Map<String, Object> hbaseConfigMap) { |
157 | 158 | throw new IllegalArgumentException(""); |
158 | 159 | } |
159 | 160 |
|
160 | | - public static void loadKrb5Conf(Map<String, Object> kerberosConfig) { |
161 | | - String krb5FilePath = System.getProperty("user.dir") + File.separator + MapUtils.getString(kerberosConfig, KEY_JAVA_SECURITY_KRB5_CONF); |
162 | | - if (!org.apache.commons.lang.StringUtils.isEmpty(krb5FilePath)) { |
163 | | - System.setProperty(KEY_JAVA_SECURITY_KRB5_CONF, krb5FilePath);; |
164 | | - } |
165 | | - } |
166 | | - |
167 | | - public static String creatJassFile(String configStr) throws IOException { |
168 | | - String fileName = System.getProperty("user.dir"); |
169 | | - File krbConf = new File(fileName); |
170 | | - File temp = File.createTempFile("JAAS", ".conf", krbConf); |
171 | | - temp.deleteOnExit(); |
172 | | - BufferedWriter out = new BufferedWriter(new FileWriter(temp, false)); |
173 | | - out.write(configStr + "\n"); |
174 | | - out.close(); |
175 | | - return temp.getAbsolutePath(); |
| 161 | + public static void loadKrb5Conf(Map<String, Object> config) { |
| 162 | + String krb5conf = MapUtils.getString(config, KEY_JAVA_SECURITY_KRB5_CONF); |
| 163 | + checkOpt(krb5conf, KEY_JAVA_SECURITY_KRB5_CONF); |
| 164 | + String krb5FilePath = System.getProperty("user.dir") + File.separator + MapUtils.getString(config, KEY_JAVA_SECURITY_KRB5_CONF); |
| 165 | + DtFileUtils.checkExists(krb5FilePath); |
| 166 | + System.setProperty(KEY_JAVA_SECURITY_KRB5_CONF, krb5FilePath); |
| 167 | + LOG.info("{} is set to {}", KEY_JAVA_SECURITY_KRB5_CONF, krb5FilePath); |
176 | 168 | } |
177 | 169 |
|
178 | | - public static String buildJaasStr(Map<String, Object> kerberosConfig) { |
179 | | - for (String key : ASYNC_KEYS_KERBEROS_REQUIRED) { |
180 | | - if (StringUtils.isEmpty(MapUtils.getString(kerberosConfig, key))) { |
181 | | - throw new IllegalArgumentException(String.format("Must provide [%s] when authentication is Kerberos", key)); |
182 | | - } |
183 | | - } |
184 | | - |
185 | | - String keyTab = System.getProperty("user.dir") + File.separator + MapUtils.getString(kerberosConfig, KEY_KEY_TAB); |
186 | | - String principal = MapUtils.getString(kerberosConfig, KEY_PRINCIPAL); |
187 | | - |
188 | | - StringBuilder jaasSB = new StringBuilder("Client {\n" + |
189 | | - " com.sun.security.auth.module.Krb5LoginModule required\n" + |
190 | | - " useKeyTab=true\n" + |
191 | | - " useTicketCache=false\n"); |
192 | | - jaasSB.append(" keyTab=\"").append(keyTab).append("\"").append("\n"); |
193 | | - jaasSB.append(" principal=\"").append(principal).append("\"").append(";\n"); |
194 | | - jaasSB.append("};"); |
195 | | - return jaasSB.toString(); |
| 170 | + // TODO 日后改造可以下沉到Core模块 |
| 171 | + public static void checkOpt(String opt, String key) { |
| 172 | + Preconditions.checkState(!Strings.isNullOrEmpty(opt), "%s must be set!", key); |
196 | 173 | } |
197 | 174 |
|
198 | | - |
199 | | - |
200 | 175 | public static UserGroupInformation loginAndReturnUGI(Configuration conf, String principal, String keytab) throws IOException { |
201 | 176 | if (conf == null) { |
202 | 177 | throw new IllegalArgumentException("kerberos conf can not be null"); |
|
0 commit comments