|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.hadoop.security; |
| 20 | + |
| 21 | +import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod; |
| 22 | +import org.apache.hadoop.security.authentication.util.KerberosName; |
| 23 | +import org.apache.hadoop.security.authentication.util.KerberosUtil; |
| 24 | +import org.apache.hadoop.util.StringUtils; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import javax.security.auth.Subject; |
| 29 | +import javax.security.auth.login.AppConfigurationEntry; |
| 30 | +import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag; |
| 31 | +import javax.security.auth.login.LoginContext; |
| 32 | +import javax.security.auth.login.LoginException; |
| 33 | + |
| 34 | +import java.io.File; |
| 35 | +import java.io.IOException; |
| 36 | +import java.security.Principal; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.HashSet; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Set; |
| 41 | + |
| 42 | +public class SecureClientLogin { |
| 43 | + private static final Logger LOG = LoggerFactory.getLogger(SecureClientLogin.class); |
| 44 | + |
| 45 | + public static final String HOSTNAME_PATTERN = "_HOST"; |
| 46 | + |
| 47 | + private SecureClientLogin() { |
| 48 | + // to block instantiation |
| 49 | + } |
| 50 | + |
| 51 | + public static synchronized Subject loginUserFromKeytab(String user, String path) throws IOException { |
| 52 | + try { |
| 53 | + Subject subject = new Subject(); |
| 54 | + SecureClientLoginConfiguration loginConf = new SecureClientLoginConfiguration(true, user, path); |
| 55 | + LoginContext login = new LoginContext("hadoop-keytab-kerberos", subject, null, loginConf); |
| 56 | + |
| 57 | + subject.getPrincipals().add(new User(user, AuthenticationMethod.KERBEROS, login)); |
| 58 | + |
| 59 | + login.login(); |
| 60 | + |
| 61 | + return login.getSubject(); |
| 62 | + } catch (LoginException le) { |
| 63 | + throw new IOException("Login failure for " + user + " from keytab " + path, le); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public static synchronized Subject loginUserFromKeytab(String user, String path, String nameRules) throws IOException { |
| 68 | + try { |
| 69 | + Subject subject = new Subject(); |
| 70 | + SecureClientLoginConfiguration loginConf = new SecureClientLoginConfiguration(true, user, path); |
| 71 | + LoginContext login = new LoginContext("hadoop-keytab-kerberos", subject, null, loginConf); |
| 72 | + |
| 73 | + KerberosName.setRules(nameRules); |
| 74 | + |
| 75 | + subject.getPrincipals().add(new User(user, AuthenticationMethod.KERBEROS, login)); |
| 76 | + |
| 77 | + login.login(); |
| 78 | + |
| 79 | + return login.getSubject(); |
| 80 | + } catch (LoginException le) { |
| 81 | + throw new IOException("Login failure for " + user + " from keytab " + path, le); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public static synchronized Subject loginUserWithPassword(String user, String password) throws IOException { |
| 86 | + try { |
| 87 | + Subject subject = new Subject(); |
| 88 | + SecureClientLoginConfiguration loginConf = new SecureClientLoginConfiguration(false, user, password); |
| 89 | + LoginContext login = new LoginContext("hadoop-keytab-kerberos", subject, null, loginConf); |
| 90 | + |
| 91 | + subject.getPrincipals().add(new User(user, AuthenticationMethod.KERBEROS, login)); |
| 92 | + |
| 93 | + login.login(); |
| 94 | + |
| 95 | + return login.getSubject(); |
| 96 | + } catch (LoginException le) { |
| 97 | + throw new IOException("Login failure for " + user + " using password ****", le); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static synchronized Subject login(String user) throws IOException { |
| 102 | + Subject subject = new Subject(); |
| 103 | + |
| 104 | + subject.getPrincipals().add(new User(user)); |
| 105 | + |
| 106 | + return subject; |
| 107 | + } |
| 108 | + |
| 109 | + public static Set<Principal> getUserPrincipals(Subject aSubject) { |
| 110 | + if (aSubject != null) { |
| 111 | + Set<User> list = aSubject.getPrincipals(User.class); |
| 112 | + |
| 113 | + return list != null ? new HashSet<>(list) : null; |
| 114 | + } else { |
| 115 | + return null; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public static Principal createUserPrincipal(String aLoginName) { |
| 120 | + return new User(aLoginName); |
| 121 | + } |
| 122 | + |
| 123 | + public static boolean isKerberosCredentialExists(String principal, String keytabPath) { |
| 124 | + boolean isValid = false; |
| 125 | + |
| 126 | + if (keytabPath != null && !keytabPath.isEmpty()) { |
| 127 | + File keytabFile = new File(keytabPath); |
| 128 | + |
| 129 | + if (!keytabFile.exists()) { |
| 130 | + LOG.warn("{} doesn't exist.", keytabPath); |
| 131 | + } else if (!keytabFile.canRead()) { |
| 132 | + LOG.warn("Unable to read {}. Please check the file access permissions for user", keytabPath); |
| 133 | + } else { |
| 134 | + isValid = true; |
| 135 | + } |
| 136 | + } else { |
| 137 | + LOG.warn("Can't find keyTab Path : {}", keytabPath); |
| 138 | + } |
| 139 | + if (!(principal != null && !principal.isEmpty() && isValid)) { |
| 140 | + isValid = false; |
| 141 | + |
| 142 | + LOG.warn("Can't find principal : {}", principal); |
| 143 | + } |
| 144 | + |
| 145 | + return isValid; |
| 146 | + } |
| 147 | + |
| 148 | + public static String getPrincipal(String principalConfig, String hostName) throws IOException { |
| 149 | + String[] components = getComponents(principalConfig); |
| 150 | + |
| 151 | + if (components == null || components.length != 3 || !HOSTNAME_PATTERN.equals(components[1])) { |
| 152 | + return principalConfig; |
| 153 | + } else { |
| 154 | + if (hostName == null) { |
| 155 | + throw new IOException("Can't replace " + HOSTNAME_PATTERN + " pattern since client ranger.service.host is null"); |
| 156 | + } |
| 157 | + |
| 158 | + return replacePattern(components, hostName); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private static String[] getComponents(String principalConfig) { |
| 163 | + if (principalConfig == null) { |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + return principalConfig.split("[/@]"); |
| 168 | + } |
| 169 | + |
| 170 | + private static String replacePattern(String[] components, String hostname) throws IOException { |
| 171 | + String fqdn = hostname; |
| 172 | + |
| 173 | + if (org.apache.commons.lang3.StringUtils.isEmpty(fqdn) || "0.0.0.0".equals(fqdn)) { |
| 174 | + fqdn = java.net.InetAddress.getLocalHost().getCanonicalHostName(); |
| 175 | + } |
| 176 | + |
| 177 | + return components[0] + "/" + StringUtils.toLowerCase(fqdn) + "@" + components[2]; |
| 178 | + } |
| 179 | + |
| 180 | + static class SecureClientLoginConfiguration extends javax.security.auth.login.Configuration { |
| 181 | + private final Map<String, String> kerberosOptions = new HashMap<>(); |
| 182 | + private boolean usePassword; |
| 183 | + |
| 184 | + public SecureClientLoginConfiguration(boolean useKeyTab, String principal, String credential) { |
| 185 | + kerberosOptions.put("principal", principal); |
| 186 | + kerberosOptions.put("debug", "false"); |
| 187 | + |
| 188 | + if (useKeyTab) { |
| 189 | + kerberosOptions.put("useKeyTab", "true"); |
| 190 | + kerberosOptions.put("keyTab", credential); |
| 191 | + kerberosOptions.put("doNotPrompt", "true"); |
| 192 | + } else { |
| 193 | + usePassword = true; |
| 194 | + |
| 195 | + kerberosOptions.put("useKeyTab", "false"); |
| 196 | + kerberosOptions.put(KrbPasswordSaverLoginModule.USERNAME_PARAM, principal); |
| 197 | + kerberosOptions.put(KrbPasswordSaverLoginModule.PASSWORD_PARAM, credential); |
| 198 | + kerberosOptions.put("doNotPrompt", "false"); |
| 199 | + kerberosOptions.put("useFirstPass", "true"); |
| 200 | + kerberosOptions.put("tryFirstPass", "false"); |
| 201 | + } |
| 202 | + |
| 203 | + kerberosOptions.put("storeKey", "true"); |
| 204 | + kerberosOptions.put("refreshKrb5Config", "true"); |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public AppConfigurationEntry[] getAppConfigurationEntry(String appName) { |
| 209 | + AppConfigurationEntry keytabKerberosLogin = new AppConfigurationEntry(KerberosUtil.getKrb5LoginModuleName(), LoginModuleControlFlag.REQUIRED, kerberosOptions); |
| 210 | + |
| 211 | + if (usePassword) { |
| 212 | + AppConfigurationEntry kerberosPwdSaver = new AppConfigurationEntry(KrbPasswordSaverLoginModule.class.getName(), LoginModuleControlFlag.REQUIRED, kerberosOptions); |
| 213 | + |
| 214 | + return new AppConfigurationEntry[] {kerberosPwdSaver, keytabKerberosLogin}; |
| 215 | + } else { |
| 216 | + return new AppConfigurationEntry[] {keytabKerberosLogin}; |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments