Skip to content

Commit 55ae512

Browse files
authored
More realistic test scenario in Authc service test (#118519)
This changes `AuthenticationServiceTests.testInvalidToken` to configure the security (tokens) index correctly. Previously `defensiveCopy()` would return `null` which would cause `getTokenDocById` to throw an exception, which `decodeToken` would catch and ignore. But that is not a realistic scenario, and is testing by side-effect.
1 parent c1569b2 commit 55ae512

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
import java.util.function.Consumer;
125125

126126
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_PRIMARY_TERM;
127+
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
127128
import static org.elasticsearch.test.ActionListenerUtils.anyActionListener;
128129
import static org.elasticsearch.test.SecurityTestsUtils.assertAuthenticationException;
129130
import static org.elasticsearch.test.TestMatchers.throwableWithMessage;
@@ -1955,6 +1956,37 @@ public void testInvalidToken() throws Exception {
19551956
final User user = new User("_username", "r1");
19561957
when(firstRealm.token(threadContext)).thenReturn(token);
19571958
when(firstRealm.supports(token)).thenReturn(true);
1959+
1960+
when(securityIndex.defensiveCopy()).thenReturn(securityIndex);
1961+
// An invalid token might decode to something that looks like a UUID
1962+
// Randomise it being invalid because the index doesn't exist, or the document doesn't exist
1963+
if (randomBoolean()) {
1964+
when(securityIndex.isAvailable(any())).thenReturn(false);
1965+
when(securityIndex.getUnavailableReason(any())).thenReturn(new ElasticsearchException(getTestName()));
1966+
} else {
1967+
when(securityIndex.isAvailable(any())).thenReturn(true);
1968+
doAnswer(inv -> {
1969+
final GetRequest request = inv.getArgument(0);
1970+
final ActionListener<GetResponse> listener = inv.getArgument(1);
1971+
listener.onResponse(
1972+
new GetResponse(
1973+
new GetResult(
1974+
request.index(),
1975+
request.id(),
1976+
UNASSIGNED_SEQ_NO,
1977+
UNASSIGNED_PRIMARY_TERM,
1978+
0,
1979+
false,
1980+
null,
1981+
Map.of(),
1982+
Map.of()
1983+
)
1984+
)
1985+
);
1986+
return null;
1987+
}).when(client).get(any(GetRequest.class), any());
1988+
}
1989+
19581990
mockAuthenticate(firstRealm, token, user);
19591991
final int numBytes = randomIntBetween(TokenService.MINIMUM_BYTES, TokenService.MINIMUM_BYTES + 32);
19601992
final byte[] randomBytes = new byte[numBytes];

0 commit comments

Comments
 (0)