Skip to content

Commit 5432ccf

Browse files
authored
Merge branch 'apache:trunk' into YARN-11320
2 parents 3bb86c2 + a65d244 commit 5432ccf

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
import org.apache.hadoop.util.ProtoUtil;
124124
import org.apache.hadoop.util.StringUtils;
125125
import org.apache.hadoop.util.Time;
126+
import java.util.concurrent.atomic.AtomicBoolean;
126127
import org.apache.hadoop.tracing.Span;
127128
import org.apache.hadoop.tracing.SpanContext;
128129
import org.apache.hadoop.tracing.TraceScope;
@@ -153,6 +154,13 @@ public abstract class Server {
153154
private ExceptionsHandler exceptionsHandler = new ExceptionsHandler();
154155
private Tracer tracer;
155156
private AlignmentContext alignmentContext;
157+
158+
/**
159+
* Allow server to do force Kerberos re-login once after failure irrespective
160+
* of the last login time.
161+
*/
162+
private final AtomicBoolean canTryForceLogin = new AtomicBoolean(true);
163+
156164
/**
157165
* Logical name of the server used in metrics and monitor.
158166
*/
@@ -2206,7 +2214,23 @@ private void saslProcess(RpcSaslProto saslMessage)
22062214
AUDITLOG.warn(AUTH_FAILED_FOR + this.toString() + ":"
22072215
+ attemptingUser + " (" + e.getLocalizedMessage()
22082216
+ ") with true cause: (" + tce.getLocalizedMessage() + ")");
2209-
throw tce;
2217+
if (!UserGroupInformation.getLoginUser().isLoginSuccess()) {
2218+
doKerberosRelogin();
2219+
try {
2220+
// try processing message again
2221+
LOG.debug("Reprocessing sasl message for {}:{} after re-login",
2222+
this.toString(), attemptingUser);
2223+
saslResponse = processSaslMessage(saslMessage);
2224+
AUDITLOG.info("Retry {}{}:{} after failure", AUTH_SUCCESSFUL_FOR,
2225+
this.toString(), attemptingUser);
2226+
canTryForceLogin.set(true);
2227+
} catch (IOException exp) {
2228+
tce = (IOException) getTrueCause(e);
2229+
throw tce;
2230+
}
2231+
} else {
2232+
throw tce;
2233+
}
22102234
}
22112235

22122236
if (saslServer != null && saslServer.isComplete()) {
@@ -3322,6 +3346,26 @@ protected Server(String bindAddress, int port,
33223346
metricsUpdaterInterval, metricsUpdaterInterval, TimeUnit.MILLISECONDS);
33233347
}
33243348

3349+
private synchronized void doKerberosRelogin() throws IOException {
3350+
if(UserGroupInformation.getLoginUser().isLoginSuccess()){
3351+
return;
3352+
}
3353+
LOG.warn("Initiating re-login from IPC Server");
3354+
if (canTryForceLogin.compareAndSet(true, false)) {
3355+
if (UserGroupInformation.isLoginKeytabBased()) {
3356+
UserGroupInformation.getLoginUser().forceReloginFromKeytab();
3357+
} else if (UserGroupInformation.isLoginTicketBased()) {
3358+
UserGroupInformation.getLoginUser().forceReloginFromTicketCache();
3359+
}
3360+
} else {
3361+
if (UserGroupInformation.isLoginKeytabBased()) {
3362+
UserGroupInformation.getLoginUser().reloginFromKeytab();
3363+
} else if (UserGroupInformation.isLoginTicketBased()) {
3364+
UserGroupInformation.getLoginUser().reloginFromTicketCache();
3365+
}
3366+
}
3367+
}
3368+
33253369
public synchronized void addAuxiliaryListener(int auxiliaryPort)
33263370
throws IOException {
33273371
if (auxiliaryListenerMap == null) {

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/UserGroupInformation.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,18 @@ private void setLogin(LoginContext login) {
529529
user.setLogin(login);
530530
}
531531

532+
/** This method checks for a successful Kerberos login
533+
* and returns true by default if it is not using Kerberos.
534+
*
535+
* @return true on successful login
536+
*/
537+
public boolean isLoginSuccess() {
538+
LoginContext login = user.getLogin();
539+
return (login instanceof HadoopLoginContext)
540+
? ((HadoopLoginContext) login).isLoginSuccess()
541+
: true;
542+
}
543+
532544
/**
533545
* Set the last login time for logged in user
534546
* @param loginTime the number of milliseconds since the beginning of time
@@ -1276,6 +1288,23 @@ private void reloginFromKeytab(boolean checkTGT, boolean ignoreLastLoginTime)
12761288
relogin(login, ignoreLastLoginTime);
12771289
}
12781290

1291+
/**
1292+
* Force re-Login a user in from the ticket cache irrespective of the last
1293+
* login time. This method assumes that login had happened already. The
1294+
* Subject field of this UserGroupInformation object is updated to have the
1295+
* new credentials.
1296+
*
1297+
* @throws IOException
1298+
* raised on errors performing I/O.
1299+
* @throws KerberosAuthException
1300+
* on a failure
1301+
*/
1302+
@InterfaceAudience.Public
1303+
@InterfaceStability.Evolving
1304+
public void forceReloginFromTicketCache() throws IOException {
1305+
reloginFromTicketCache(true);
1306+
}
1307+
12791308
/**
12801309
* Re-Login a user in from the ticket cache. This
12811310
* method assumes that login had happened already.
@@ -1287,14 +1316,19 @@ private void reloginFromKeytab(boolean checkTGT, boolean ignoreLastLoginTime)
12871316
@InterfaceAudience.Public
12881317
@InterfaceStability.Evolving
12891318
public void reloginFromTicketCache() throws IOException {
1319+
reloginFromTicketCache(false);
1320+
}
1321+
1322+
private void reloginFromTicketCache(boolean ignoreLastLoginTime)
1323+
throws IOException {
12901324
if (!shouldRelogin() || !isFromTicket()) {
12911325
return;
12921326
}
12931327
HadoopLoginContext login = getLogin();
12941328
if (login == null) {
12951329
throw new KerberosAuthException(MUST_FIRST_LOGIN);
12961330
}
1297-
relogin(login, false);
1331+
relogin(login, ignoreLastLoginTime);
12981332
}
12991333

13001334
private void relogin(HadoopLoginContext login, boolean ignoreLastLoginTime)
@@ -2083,6 +2117,11 @@ private static class HadoopLoginContext extends LoginContext {
20832117
this.conf = conf;
20842118
}
20852119

2120+
/** Get the login status. */
2121+
public boolean isLoginSuccess() {
2122+
return isLoggedIn.get();
2123+
}
2124+
20862125
String getAppName() {
20872126
return appName;
20882127
}

0 commit comments

Comments
 (0)