|
@@ -24,6 +24,8 @@ import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_TOKEN_FI
|
|
import static org.apache.hadoop.security.UGIExceptionMessages.*;
|
|
import static org.apache.hadoop.security.UGIExceptionMessages.*;
|
|
import static org.apache.hadoop.util.PlatformName.IBM_JAVA;
|
|
import static org.apache.hadoop.util.PlatformName.IBM_JAVA;
|
|
|
|
|
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
|
|
+
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
@@ -45,6 +47,7 @@ import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
+import javax.security.auth.DestroyFailedException;
|
|
import javax.security.auth.Subject;
|
|
import javax.security.auth.Subject;
|
|
import javax.security.auth.callback.CallbackHandler;
|
|
import javax.security.auth.callback.CallbackHandler;
|
|
import javax.security.auth.kerberos.KerberosPrincipal;
|
|
import javax.security.auth.kerberos.KerberosPrincipal;
|
|
@@ -76,8 +79,6 @@ import org.apache.hadoop.security.token.TokenIdentifier;
|
|
import org.apache.hadoop.util.Shell;
|
|
import org.apache.hadoop.util.Shell;
|
|
import org.apache.hadoop.util.StringUtils;
|
|
import org.apache.hadoop.util.StringUtils;
|
|
import org.apache.hadoop.util.Time;
|
|
import org.apache.hadoop.util.Time;
|
|
-
|
|
|
|
-import com.google.common.annotations.VisibleForTesting;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
@@ -1165,10 +1166,41 @@ public class UserGroupInformation {
|
|
reloginFromKeytab();
|
|
reloginFromKeytab();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // if the first kerberos ticket is not TGT, then remove and destroy it since
|
|
|
|
+ // the kerberos library of jdk always use the first kerberos ticket as TGT.
|
|
|
|
+ // See HADOOP-13433 for more details.
|
|
|
|
+ @VisibleForTesting
|
|
|
|
+ void fixKerberosTicketOrder() {
|
|
|
|
+ Set<Object> creds = getSubject().getPrivateCredentials();
|
|
|
|
+ synchronized (creds) {
|
|
|
|
+ for (Iterator<Object> iter = creds.iterator(); iter.hasNext();) {
|
|
|
|
+ Object cred = iter.next();
|
|
|
|
+ if (cred instanceof KerberosTicket) {
|
|
|
|
+ KerberosTicket ticket = (KerberosTicket) cred;
|
|
|
|
+ if (!ticket.getServer().getName().startsWith("krbtgt")) {
|
|
|
|
+ LOG.warn(
|
|
|
|
+ "The first kerberos ticket is not TGT"
|
|
|
|
+ + "(the server principal is {}), remove and destroy it.",
|
|
|
|
+ ticket.getServer());
|
|
|
|
+ iter.remove();
|
|
|
|
+ try {
|
|
|
|
+ ticket.destroy();
|
|
|
|
+ } catch (DestroyFailedException e) {
|
|
|
|
+ LOG.warn("destroy ticket failed", e);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ LOG.warn("Warning, no kerberos ticket found while attempting to renew ticket");
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Re-Login a user in from a keytab file. Loads a user identity from a keytab
|
|
* Re-Login a user in from a keytab file. Loads a user identity from a keytab
|
|
* file and logs them in. They become the currently logged-in user. This
|
|
* file and logs them in. They become the currently logged-in user. This
|
|
- * method assumes that {@link #loginUserFromKeytab(String, String)} had
|
|
|
|
|
|
+ * method assumes that {@link #loginUserFromKeytab(String, String)} had
|
|
* happened already.
|
|
* happened already.
|
|
* The Subject field of this UserGroupInformation object is updated to have
|
|
* The Subject field of this UserGroupInformation object is updated to have
|
|
* the new credentials.
|
|
* the new credentials.
|
|
@@ -1178,11 +1210,12 @@ public class UserGroupInformation {
|
|
@InterfaceAudience.Public
|
|
@InterfaceAudience.Public
|
|
@InterfaceStability.Evolving
|
|
@InterfaceStability.Evolving
|
|
public synchronized void reloginFromKeytab() throws IOException {
|
|
public synchronized void reloginFromKeytab() throws IOException {
|
|
- if (!isSecurityEnabled() ||
|
|
|
|
- user.getAuthenticationMethod() != AuthenticationMethod.KERBEROS ||
|
|
|
|
- !isKeytab)
|
|
|
|
|
|
+ if (!isSecurityEnabled()
|
|
|
|
+ || user.getAuthenticationMethod() != AuthenticationMethod.KERBEROS
|
|
|
|
+ || !isKeytab) {
|
|
return;
|
|
return;
|
|
-
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
long now = Time.now();
|
|
long now = Time.now();
|
|
if (!shouldRenewImmediatelyForTests && !hasSufficientTimeElapsed(now)) {
|
|
if (!shouldRenewImmediatelyForTests && !hasSufficientTimeElapsed(now)) {
|
|
return;
|
|
return;
|
|
@@ -1194,12 +1227,12 @@ public class UserGroupInformation {
|
|
now < getRefreshTime(tgt)) {
|
|
now < getRefreshTime(tgt)) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
LoginContext login = getLogin();
|
|
LoginContext login = getLogin();
|
|
if (login == null || keytabFile == null) {
|
|
if (login == null || keytabFile == null) {
|
|
throw new KerberosAuthException(MUST_FIRST_LOGIN_FROM_KEYTAB);
|
|
throw new KerberosAuthException(MUST_FIRST_LOGIN_FROM_KEYTAB);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
long start = 0;
|
|
long start = 0;
|
|
// register most recent relogin attempt
|
|
// register most recent relogin attempt
|
|
user.setLastLogin(now);
|
|
user.setLastLogin(now);
|
|
@@ -1222,6 +1255,7 @@ public class UserGroupInformation {
|
|
}
|
|
}
|
|
start = Time.now();
|
|
start = Time.now();
|
|
login.login();
|
|
login.login();
|
|
|
|
+ fixKerberosTicketOrder();
|
|
metrics.loginSuccess.add(Time.now() - start);
|
|
metrics.loginSuccess.add(Time.now() - start);
|
|
setLogin(login);
|
|
setLogin(login);
|
|
}
|
|
}
|
|
@@ -1233,7 +1267,7 @@ public class UserGroupInformation {
|
|
kae.setPrincipal(keytabPrincipal);
|
|
kae.setPrincipal(keytabPrincipal);
|
|
kae.setKeytabFile(keytabFile);
|
|
kae.setKeytabFile(keytabFile);
|
|
throw kae;
|
|
throw kae;
|
|
- }
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1247,10 +1281,11 @@ public class UserGroupInformation {
|
|
@InterfaceAudience.Public
|
|
@InterfaceAudience.Public
|
|
@InterfaceStability.Evolving
|
|
@InterfaceStability.Evolving
|
|
public synchronized void reloginFromTicketCache() throws IOException {
|
|
public synchronized void reloginFromTicketCache() throws IOException {
|
|
- if (!isSecurityEnabled() ||
|
|
|
|
- user.getAuthenticationMethod() != AuthenticationMethod.KERBEROS ||
|
|
|
|
- !isKrbTkt)
|
|
|
|
|
|
+ if (!isSecurityEnabled()
|
|
|
|
+ || user.getAuthenticationMethod() != AuthenticationMethod.KERBEROS
|
|
|
|
+ || !isKrbTkt) {
|
|
return;
|
|
return;
|
|
|
|
+ }
|
|
LoginContext login = getLogin();
|
|
LoginContext login = getLogin();
|
|
if (login == null) {
|
|
if (login == null) {
|
|
throw new KerberosAuthException(MUST_FIRST_LOGIN);
|
|
throw new KerberosAuthException(MUST_FIRST_LOGIN);
|
|
@@ -1278,15 +1313,15 @@ public class UserGroupInformation {
|
|
LOG.debug("Initiating re-login for " + getUserName());
|
|
LOG.debug("Initiating re-login for " + getUserName());
|
|
}
|
|
}
|
|
login.login();
|
|
login.login();
|
|
|
|
+ fixKerberosTicketOrder();
|
|
setLogin(login);
|
|
setLogin(login);
|
|
} catch (LoginException le) {
|
|
} catch (LoginException le) {
|
|
KerberosAuthException kae = new KerberosAuthException(LOGIN_FAILURE, le);
|
|
KerberosAuthException kae = new KerberosAuthException(LOGIN_FAILURE, le);
|
|
kae.setUser(getUserName());
|
|
kae.setUser(getUserName());
|
|
throw kae;
|
|
throw kae;
|
|
- }
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Log a user in from a keytab file. Loads a user identity from a keytab
|
|
* Log a user in from a keytab file. Loads a user identity from a keytab
|
|
* file and login them in. This new user does not affect the currently
|
|
* file and login them in. This new user does not affect the currently
|