Skip to content

Commit

Permalink
[Kerberos] Fix to audit log authc_failed event once (#32220)
Browse files Browse the repository at this point in the history
The exception was being sent twice due to incorrect handling
of conditional statements causing multiple authentication_failed
events in audit logs.
  • Loading branch information
bizybot authored Jul 20, 2018
1 parent e12c883 commit 141cee2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public void validateTicket(final byte[] decodedToken, final Path keytabPath, fin
} catch (PrivilegedActionException pve) {
if (pve.getCause() instanceof LoginException) {
actionListener.onFailure((LoginException) pve.getCause());
}
if (pve.getCause() instanceof GSSException) {
} else if (pve.getCause() instanceof GSSException) {
actionListener.onFailure((GSSException) pve.getCause());
} else {
actionListener.onFailure(pve.getException());
}
actionListener.onFailure(pve.getException());
} finally {
privilegedLogoutNoThrow(loginContext);
privilegedDisposeNoThrow(gssContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

package org.elasticsearch.xpack.security.authc.kerberos;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.util.concurrent.UncategorizedExecutionException;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.TestEnvironment;
import org.elasticsearch.xpack.core.security.authc.kerberos.KerberosRealmSettings;
import org.elasticsearch.xpack.security.authc.kerberos.KerberosTicketValidator;
import org.ietf.jgss.GSSException;

import java.io.IOException;
Expand All @@ -25,6 +25,7 @@
import javax.security.auth.login.LoginException;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
Expand Down Expand Up @@ -57,10 +58,23 @@ public void testInvalidKerbTicketFailsValidation() throws Exception {

final Environment env = TestEnvironment.newEnvironment(globalSettings);
final Path keytabPath = env.configFile().resolve(KerberosRealmSettings.HTTP_SERVICE_KEYTAB_PATH.get(settings));
final PlainActionFuture<Tuple<String, String>> future = new PlainActionFuture<>();
kerberosTicketValidator.validateTicket(Base64.getDecoder().decode(base64KerbToken), keytabPath, true, future);
final GSSException gssException = expectThrows(GSSException.class, () -> unwrapExpectedExceptionFromFutureAndThrow(future));
assertThat(gssException.getMajor(), equalTo(GSSException.DEFECTIVE_TOKEN));
kerberosTicketValidator.validateTicket(Base64.getDecoder().decode(base64KerbToken), keytabPath, true,
new ActionListener<Tuple<String, String>>() {
boolean exceptionHandled = false;

@Override
public void onResponse(Tuple<String, String> response) {
fail("expected exception to be thrown of type GSSException");
}

@Override
public void onFailure(Exception e) {
assertThat(exceptionHandled, is(false));
assertThat(e, instanceOf(GSSException.class));
assertThat(((GSSException) e).getMajor(), equalTo(GSSException.DEFECTIVE_TOKEN));
exceptionHandled = true;
}
});
}

public void testWhenKeyTabWithInvalidContentFailsValidation()
Expand Down

0 comments on commit 141cee2

Please sign in to comment.