Revert "HADOOP-12897. KerberosAuthenticator.authenticate to include URL on IO failures. Contributed by Ajay Kumar."

This reverts commit 16b2cad8e8f5215325c51eb582f58640a386b06b.
This commit is contained in:
Xiao Chen 2018-02-14 10:24:49 -08:00
parent 36c9dda07f
commit 2f3415a4d6
2 changed files with 27 additions and 82 deletions

View File

@ -13,8 +13,6 @@
*/ */
package org.apache.hadoop.security.authentication.client; package org.apache.hadoop.security.authentication.client;
import com.google.common.annotations.VisibleForTesting;
import java.lang.reflect.Constructor;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.hadoop.security.authentication.server.HttpConstants; import org.apache.hadoop.security.authentication.server.HttpConstants;
import org.apache.hadoop.security.authentication.util.AuthToken; import org.apache.hadoop.security.authentication.util.AuthToken;
@ -179,62 +177,38 @@ public void setConnectionConfigurator(ConnectionConfigurator configurator) {
*/ */
@Override @Override
public void authenticate(URL url, AuthenticatedURL.Token token) public void authenticate(URL url, AuthenticatedURL.Token token)
throws IOException, AuthenticationException { throws IOException, AuthenticationException {
if (!token.isSet()) { if (!token.isSet()) {
this.url = url; this.url = url;
base64 = new Base64(0); base64 = new Base64(0);
try { HttpURLConnection conn = token.openConnection(url, connConfigurator);
HttpURLConnection conn = token.openConnection(url, connConfigurator); conn.setRequestMethod(AUTH_HTTP_METHOD);
conn.setRequestMethod(AUTH_HTTP_METHOD); conn.connect();
conn.connect();
boolean needFallback = false; boolean needFallback = false;
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
LOG.debug("JDK performed authentication on our behalf."); LOG.debug("JDK performed authentication on our behalf.");
// If the JDK already did the SPNEGO back-and-forth for // If the JDK already did the SPNEGO back-and-forth for
// us, just pull out the token. // us, just pull out the token.
AuthenticatedURL.extractToken(conn, token); AuthenticatedURL.extractToken(conn, token);
if (isTokenKerberos(token)) { if (isTokenKerberos(token)) {
return; return;
}
needFallback = true;
} }
if (!needFallback && isNegotiate(conn)) { needFallback = true;
LOG.debug("Performing our own SPNEGO sequence."); }
doSpnegoSequence(token); if (!needFallback && isNegotiate(conn)) {
} else { LOG.debug("Performing our own SPNEGO sequence.");
LOG.debug("Using fallback authenticator sequence."); doSpnegoSequence(token);
Authenticator auth = getFallBackAuthenticator(); } else {
// Make sure that the fall back authenticator have the same LOG.debug("Using fallback authenticator sequence.");
// ConnectionConfigurator, since the method might be overridden. Authenticator auth = getFallBackAuthenticator();
// Otherwise the fall back authenticator might not have the // Make sure that the fall back authenticator have the same
// information to make the connection (e.g., SSL certificates) // ConnectionConfigurator, since the method might be overridden.
auth.setConnectionConfigurator(connConfigurator); // Otherwise the fall back authenticator might not have the information
auth.authenticate(url, token); // to make the connection (e.g., SSL certificates)
} auth.setConnectionConfigurator(connConfigurator);
} catch (IOException ex){ auth.authenticate(url, token);
throw wrapExceptionWithMessage(ex,
"Error while authenticating with endpoint: " + url);
} catch (AuthenticationException ex){
throw wrapExceptionWithMessage(ex,
"Error while authenticating with endpoint: " + url);
} }
}
}
@VisibleForTesting
static <T extends Exception> T wrapExceptionWithMessage(
T exception, String msg) {
Class<? extends Throwable> exceptionClass = exception.getClass();
try {
Constructor<? extends Throwable> ctor = exceptionClass
.getConstructor(String.class);
Throwable t = ctor.newInstance(msg);
return (T) (t.initCause(exception));
} catch (Throwable e) {
LOG.debug("Unable to wrap exception of type {}, it has "
+ "no (String) constructor.", exceptionClass, e);
return exception;
} }
} }

View File

@ -20,9 +20,6 @@
import static org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler.KEYTAB; import static org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler.KEYTAB;
import static org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler.NAME_RULES; import static org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler.NAME_RULES;
import java.io.IOException;
import java.nio.charset.CharacterCodingException;
import javax.security.sasl.AuthenticationException;
import org.apache.hadoop.minikdc.KerberosSecurityTestcase; import org.apache.hadoop.minikdc.KerberosSecurityTestcase;
import org.apache.hadoop.security.authentication.KerberosTestUtils; import org.apache.hadoop.security.authentication.KerberosTestUtils;
import org.apache.hadoop.security.authentication.server.AuthenticationFilter; import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
@ -221,30 +218,4 @@ public Void call() throws Exception {
}); });
} }
@Test(timeout = 60000)
public void testWrapExceptionWithMessage() {
IOException ex;
ex = new IOException("Induced exception");
ex = KerberosAuthenticator.wrapExceptionWithMessage(ex, "Error while "
+ "authenticating with endpoint: localhost");
Assert.assertEquals("Induced exception", ex.getCause().getMessage());
Assert.assertEquals("Error while authenticating with endpoint: localhost",
ex.getMessage());
ex = new AuthenticationException("Auth exception");
ex = KerberosAuthenticator.wrapExceptionWithMessage(ex, "Error while "
+ "authenticating with endpoint: localhost");
Assert.assertEquals("Auth exception", ex.getCause().getMessage());
Assert.assertEquals("Error while authenticating with endpoint: localhost",
ex.getMessage());
// Test for Exception with no (String) constructor
// redirect the LOG to and check log message
ex = new CharacterCodingException();
Exception ex2 = KerberosAuthenticator.wrapExceptionWithMessage(ex,
"Error while authenticating with endpoint: localhost");
Assert.assertTrue(ex instanceof CharacterCodingException);
Assert.assertTrue(ex.equals(ex2));
}
} }