Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/
package org.elasticsearch.xpack.core.security;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.Version;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
Expand All @@ -17,6 +17,7 @@
import org.elasticsearch.xpack.core.security.user.User;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.Objects;
import java.util.function.Consumer;
Expand Down Expand Up @@ -53,10 +54,8 @@ public Authentication getAuthentication() {
try {
return Authentication.readFromContext(threadContext);
} catch (IOException e) {
// TODO: this seems bogus, the only way to get an ioexception here is from a corrupt or tampered
// auth header, which should be be audited?
logger.error("failed to read authentication", e);
return null;
throw new UncheckedIOException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.Authentication.AuthenticationType;
import org.elasticsearch.xpack.core.security.authc.Authentication.RealmRef;
import org.elasticsearch.xpack.core.security.authc.AuthenticationField;
import org.elasticsearch.xpack.core.security.user.SystemUser;
import org.elasticsearch.xpack.core.security.user.User;
import org.junit.Before;

import java.io.EOFException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.concurrent.atomic.AtomicReference;

import static org.hamcrest.Matchers.instanceOf;

public class SecurityContextTests extends ESTestCase {

private Settings settings;
Expand Down Expand Up @@ -51,6 +56,14 @@ public void testGetAuthenticationAndUser() throws IOException {
assertEquals(user, securityContext.getUser());
}

public void testGetAuthenticationDoesNotSwallowIOException() {
threadContext.putHeader(AuthenticationField.AUTHENTICATION_KEY, ""); // an intentionally corrupt header
final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext);
final UncheckedIOException e = expectThrows(UncheckedIOException.class, securityContext::getAuthentication);
assertNotNull(e.getCause());
assertThat(e.getCause(), instanceOf(EOFException.class));
}

public void testSetUser() {
final User user = new User("test");
assertNull(securityContext.getAuthentication());
Expand Down