Skip to content

Commit 141cee2

Browse files
authored
[Kerberos] Fix to audit log authc_failed event once (#32220)
The exception was being sent twice due to incorrect handling of conditional statements causing multiple authentication_failed events in audit logs.
1 parent e12c883 commit 141cee2

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTicketValidator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ public void validateTicket(final byte[] decodedToken, final Path keytabPath, fin
9696
} catch (PrivilegedActionException pve) {
9797
if (pve.getCause() instanceof LoginException) {
9898
actionListener.onFailure((LoginException) pve.getCause());
99-
}
100-
if (pve.getCause() instanceof GSSException) {
99+
} else if (pve.getCause() instanceof GSSException) {
101100
actionListener.onFailure((GSSException) pve.getCause());
101+
} else {
102+
actionListener.onFailure(pve.getException());
102103
}
103-
actionListener.onFailure(pve.getException());
104104
} finally {
105105
privilegedLogoutNoThrow(loginContext);
106106
privilegedDisposeNoThrow(gssContext);

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTicketValidatorTests.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

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

9+
import org.elasticsearch.action.ActionListener;
910
import org.elasticsearch.action.support.PlainActionFuture;
1011
import org.elasticsearch.common.collect.Tuple;
1112
import org.elasticsearch.common.settings.SecureString;
1213
import org.elasticsearch.common.util.concurrent.UncategorizedExecutionException;
1314
import org.elasticsearch.env.Environment;
1415
import org.elasticsearch.env.TestEnvironment;
1516
import org.elasticsearch.xpack.core.security.authc.kerberos.KerberosRealmSettings;
16-
import org.elasticsearch.xpack.security.authc.kerberos.KerberosTicketValidator;
1717
import org.ietf.jgss.GSSException;
1818

1919
import java.io.IOException;
@@ -25,6 +25,7 @@
2525
import javax.security.auth.login.LoginException;
2626

2727
import static org.hamcrest.Matchers.equalTo;
28+
import static org.hamcrest.Matchers.instanceOf;
2829
import static org.hamcrest.Matchers.is;
2930
import static org.hamcrest.Matchers.notNullValue;
3031
import static org.hamcrest.Matchers.nullValue;
@@ -57,10 +58,23 @@ public void testInvalidKerbTicketFailsValidation() throws Exception {
5758

5859
final Environment env = TestEnvironment.newEnvironment(globalSettings);
5960
final Path keytabPath = env.configFile().resolve(KerberosRealmSettings.HTTP_SERVICE_KEYTAB_PATH.get(settings));
60-
final PlainActionFuture<Tuple<String, String>> future = new PlainActionFuture<>();
61-
kerberosTicketValidator.validateTicket(Base64.getDecoder().decode(base64KerbToken), keytabPath, true, future);
62-
final GSSException gssException = expectThrows(GSSException.class, () -> unwrapExpectedExceptionFromFutureAndThrow(future));
63-
assertThat(gssException.getMajor(), equalTo(GSSException.DEFECTIVE_TOKEN));
61+
kerberosTicketValidator.validateTicket(Base64.getDecoder().decode(base64KerbToken), keytabPath, true,
62+
new ActionListener<Tuple<String, String>>() {
63+
boolean exceptionHandled = false;
64+
65+
@Override
66+
public void onResponse(Tuple<String, String> response) {
67+
fail("expected exception to be thrown of type GSSException");
68+
}
69+
70+
@Override
71+
public void onFailure(Exception e) {
72+
assertThat(exceptionHandled, is(false));
73+
assertThat(e, instanceOf(GSSException.class));
74+
assertThat(((GSSException) e).getMajor(), equalTo(GSSException.DEFECTIVE_TOKEN));
75+
exceptionHandled = true;
76+
}
77+
});
6478
}
6579

6680
public void testWhenKeyTabWithInvalidContentFailsValidation()

0 commit comments

Comments
 (0)