|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | + |
| 20 | +package com.dtstack.flink.sql.authenticate; |
| 21 | + |
| 22 | +import org.apache.commons.collections.MapUtils; |
| 23 | +import org.apache.commons.lang.StringUtils; |
| 24 | +import org.apache.hadoop.conf.Configuration; |
| 25 | +import org.apache.hadoop.security.UserGroupInformation; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | +import sun.security.krb5.Config; |
| 29 | +import sun.security.krb5.internal.ktab.KeyTab; |
| 30 | +import sun.security.krb5.internal.ktab.KeyTabEntry; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.IOException; |
| 34 | +import java.net.InetAddress; |
| 35 | +import java.util.Locale; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author jiangbo |
| 40 | + * @date 2019/8/20 |
| 41 | + */ |
| 42 | +public class KerberosUtil { |
| 43 | + |
| 44 | + public static Logger LOG = LoggerFactory.getLogger(KerberosUtil.class); |
| 45 | + |
| 46 | + private static final String PRINCIPAL_SPLIT_REGEX = "/"; |
| 47 | + private static final String SP = File.separator; |
| 48 | + |
| 49 | + private static final String KEY_SFTP_CONF = "sftpConf"; |
| 50 | + private static final String KEY_REMOTE_DIR = "remoteDir"; |
| 51 | + private static final String KEY_USE_LOCAL_FILE = "useLocalFile"; |
| 52 | + private static final String KEY_JAVA_SECURITY_KRB5_CONF = "java.security.krb5.conf"; |
| 53 | + |
| 54 | + private static String LOCAL_DIR; |
| 55 | + |
| 56 | + static { |
| 57 | + String systemInfo = System.getProperty("os.name"); |
| 58 | + if(systemInfo.toLowerCase().startsWith("windows")){ |
| 59 | + LOCAL_DIR = System.getProperty("user.dir"); |
| 60 | + } else { |
| 61 | + LOCAL_DIR = "/tmp/flinksql/keytab"; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static UserGroupInformation loginAndReturnUGI(Configuration conf, String principal, String keytab) throws IOException { |
| 66 | + if (conf == null) { |
| 67 | + throw new IllegalArgumentException("kerberos conf can not be null"); |
| 68 | + } |
| 69 | + |
| 70 | + if (StringUtils.isEmpty(principal)) { |
| 71 | + throw new IllegalArgumentException("principal can not be null"); |
| 72 | + } |
| 73 | + |
| 74 | + if(StringUtils.isEmpty(keytab)){ |
| 75 | + throw new IllegalArgumentException("keytab can not be null"); |
| 76 | + } |
| 77 | + |
| 78 | + if(StringUtils.isNotEmpty(conf.get(KEY_JAVA_SECURITY_KRB5_CONF))){ |
| 79 | + reloadKrb5Conf(conf); |
| 80 | + } |
| 81 | + |
| 82 | + conf.set("hadoop.security.authentication", "Kerberos"); |
| 83 | + UserGroupInformation.setConfiguration(conf); |
| 84 | + |
| 85 | + LOG.info("login user:{} with keytab:{}", principal, keytab); |
| 86 | + return UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal, keytab); |
| 87 | + } |
| 88 | + |
| 89 | + private static void reloadKrb5Conf(Configuration conf){ |
| 90 | + String krb5File = conf.get(KEY_JAVA_SECURITY_KRB5_CONF); |
| 91 | + LOG.info("set krb5 file:{}", krb5File); |
| 92 | + System.setProperty(KEY_JAVA_SECURITY_KRB5_CONF, krb5File); |
| 93 | + |
| 94 | + try { |
| 95 | + if (!System.getProperty("java.vendor").contains("IBM")) { |
| 96 | + Config.refresh(); |
| 97 | + } |
| 98 | + } catch (Exception e){ |
| 99 | + LOG.warn("reload krb5 file:{} error:", krb5File, e); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public static void loadKrb5Conf(Map<String, Object> kerberosConfig, String jobId, String plugin){ |
| 104 | + String krb5FilePath = MapUtils.getString(kerberosConfig, KEY_JAVA_SECURITY_KRB5_CONF); |
| 105 | + if(StringUtils.isEmpty(krb5FilePath)){ |
| 106 | + LOG.info("krb5 file is empty,will use default file"); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + krb5FilePath = loadFile(kerberosConfig, krb5FilePath, jobId, plugin); |
| 111 | + kerberosConfig.put(KEY_JAVA_SECURITY_KRB5_CONF, krb5FilePath); |
| 112 | + } |
| 113 | + |
| 114 | + public static String loadFile(Map<String, Object> kerberosConfig, String filePath, String jobId, String plugin) { |
| 115 | + boolean useLocalFile = MapUtils.getBooleanValue(kerberosConfig, KEY_USE_LOCAL_FILE); |
| 116 | + if(useLocalFile){ |
| 117 | + LOG.info("will use local file:{}", filePath); |
| 118 | + checkFileExists(filePath); |
| 119 | + } else { |
| 120 | + if(filePath.contains(SP)){ |
| 121 | + filePath = filePath.substring(filePath.lastIndexOf(SP) + 1); |
| 122 | + } |
| 123 | + |
| 124 | + filePath = loadFromSFTP(kerberosConfig, filePath, jobId, plugin); |
| 125 | + } |
| 126 | + |
| 127 | + return filePath; |
| 128 | + } |
| 129 | + |
| 130 | + private static void checkFileExists(String keytab){ |
| 131 | + File file = new File(keytab); |
| 132 | + if (file.exists()){ |
| 133 | + if (file.isDirectory()) { |
| 134 | + throw new RuntimeException("keytab is a directory:" + keytab); |
| 135 | + } |
| 136 | + } else { |
| 137 | + throw new RuntimeException("keytab file not exists:" + keytab); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static String loadFromSFTP(Map<String, Object> config, String keytab, String jobId, String plugin){ |
| 142 | + String localDir = createLocalDir(jobId, plugin); |
| 143 | + String localPath = localDir + SP + keytab; |
| 144 | + |
| 145 | + SFTPHandler handler = null; |
| 146 | + try { |
| 147 | + handler = SFTPHandler.getInstance(MapUtils.getMap(config, KEY_SFTP_CONF)); |
| 148 | + |
| 149 | + String remoteDir = MapUtils.getString(config, KEY_REMOTE_DIR); |
| 150 | + String filePathOnSFTP = remoteDir + "/" + keytab; |
| 151 | + if(handler.isFileExist(filePathOnSFTP)){ |
| 152 | + handler.downloadFile(filePathOnSFTP, localPath); |
| 153 | + |
| 154 | + LOG.info("download file:{} to local:{}", filePathOnSFTP, localDir); |
| 155 | + return localPath; |
| 156 | + } else { |
| 157 | + String hostname = InetAddress.getLocalHost().getCanonicalHostName().toLowerCase(Locale.US); |
| 158 | + filePathOnSFTP = remoteDir + "/" + hostname + "/" + keytab; |
| 159 | + handler.downloadFile(filePathOnSFTP, localPath); |
| 160 | + |
| 161 | + LOG.info("download file:{} to local:{}", filePathOnSFTP, localDir); |
| 162 | + return localPath; |
| 163 | + } |
| 164 | + } catch (Exception e){ |
| 165 | + throw new RuntimeException(e); |
| 166 | + } finally { |
| 167 | + if (handler != null){ |
| 168 | + handler.close(); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public static String findPrincipalFromKeytab(String principal, String keytabFile) { |
| 174 | + String serverName = principal.split(PRINCIPAL_SPLIT_REGEX)[0]; |
| 175 | + |
| 176 | + KeyTab keyTab = KeyTab.getInstance(keytabFile); |
| 177 | + for (KeyTabEntry entry : keyTab.getEntries()) { |
| 178 | + String princ = entry.getService().getName(); |
| 179 | + if(princ.startsWith(serverName)){ |
| 180 | + LOG.info("parse principal:{} from keytab:{}", princ, keytabFile); |
| 181 | + return princ; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return principal; |
| 186 | + } |
| 187 | + |
| 188 | + public static void clear(String jobId){ |
| 189 | + File file = new File(LOCAL_DIR + SP + jobId); |
| 190 | + if (file.exists()){ |
| 191 | + boolean result = file.delete(); |
| 192 | + if (!result){ |
| 193 | + LOG.warn("Delete file failure:[{}]", LOCAL_DIR + SP + jobId); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + private static String createLocalDir(String jobId, String plugin){ |
| 199 | + String path = LOCAL_DIR + SP + jobId + SP + plugin; |
| 200 | + File file = new File(path); |
| 201 | + if (file.exists()){ |
| 202 | + return path; |
| 203 | + } |
| 204 | + |
| 205 | + boolean result = file.mkdirs(); |
| 206 | + if (!result){ |
| 207 | + LOG.warn("Create dir failure:{}", path); |
| 208 | + } |
| 209 | + |
| 210 | + LOG.info("create local dir:{}", path); |
| 211 | + return path; |
| 212 | + } |
| 213 | +} |
0 commit comments