Skip to content

Commit b3d69ae

Browse files
authored
[Test] Use InvalidateTokenRequest factory methods (#64120)
In the HLRC, InvalidateTokenRequest has 4 factory methods to simplify the construction of requests that use only 1 search criteria. This change replaces direct uses of the constructor (within tests) with the relevant factory methods. This is helpful because the constructor takes 4 String arguments, so it is easy to accidentally transpose them and have the test perform the wrong invalidation action (as was the case in testExpiredTokensDeletedAfterExpiration where a refreshToken was passed in the first argument, instead of the second).
1 parent 7b10d87 commit b3d69ae

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/TokenAuthIntegTests.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void testExpiredTokensDeletedAfterExpiration() throws Exception {
147147
Instant created = Instant.now();
148148

149149
InvalidateTokenResponse invalidateResponse = restClient.security().invalidateToken(
150-
new InvalidateTokenRequest(accessToken, null, null, null), SECURITY_REQUEST_OPTIONS);
150+
InvalidateTokenRequest.accessToken(accessToken), SECURITY_REQUEST_OPTIONS);
151151
assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(1));
152152
assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
153153
assertThat(invalidateResponse.getErrors(), empty());
@@ -174,7 +174,7 @@ public void testExpiredTokensDeletedAfterExpiration() throws Exception {
174174
if (deleteTriggered.compareAndSet(false, true)) {
175175
// invalidate a invalid token... doesn't matter that it is bad... we just want this action to trigger the deletion
176176
InvalidateTokenResponse invalidateResponseTwo = restClient.security()
177-
.invalidateToken(new InvalidateTokenRequest("fooobar", null, null, null),
177+
.invalidateToken(InvalidateTokenRequest.accessToken("fooobar"),
178178
SECURITY_REQUEST_OPTIONS);
179179
assertThat(invalidateResponseTwo.getInvalidatedTokens(), equalTo(0));
180180
assertThat(invalidateResponseTwo.getPreviouslyInvalidatedTokens(), equalTo(0));
@@ -189,12 +189,13 @@ public void testExpiredTokensDeletedAfterExpiration() throws Exception {
189189

190190
// Now the documents are deleted, try to invalidate the access token and refresh token again
191191
InvalidateTokenResponse invalidateAccessTokenResponse = restClient.security().invalidateToken(
192-
new InvalidateTokenRequest(accessToken, null, null, null), SECURITY_REQUEST_OPTIONS);
192+
InvalidateTokenRequest.accessToken(accessToken), SECURITY_REQUEST_OPTIONS);
193193
assertThat(invalidateAccessTokenResponse.getInvalidatedTokens(), equalTo(0));
194194
assertThat(invalidateAccessTokenResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
195195
assertThat(invalidateAccessTokenResponse.getErrors(), empty());
196+
196197
InvalidateTokenResponse invalidateRefreshTokenResponse = restClient.security().invalidateToken(
197-
new InvalidateTokenRequest(refreshToken, null, null, null), SECURITY_REQUEST_OPTIONS);
198+
InvalidateTokenRequest.refreshToken(refreshToken), SECURITY_REQUEST_OPTIONS);
198199
assertThat(invalidateRefreshTokenResponse.getInvalidatedTokens(), equalTo(0));
199200
assertThat(invalidateRefreshTokenResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
200201
assertThat(invalidateRefreshTokenResponse.getErrors(), empty());
@@ -208,7 +209,7 @@ public void testInvalidateAllTokensForUser() throws Exception {
208209
SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS);
209210
}
210211
InvalidateTokenResponse invalidateResponse = restClient.security().invalidateToken(
211-
new InvalidateTokenRequest(null, null, null, SecuritySettingsSource.TEST_USER_NAME),
212+
InvalidateTokenRequest.userTokens(SecuritySettingsSource.TEST_USER_NAME),
212213
RequestOptions.DEFAULT.toBuilder().addHeader("Authorization",
213214
UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER,
214215
SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)).build());
@@ -225,7 +226,7 @@ public void testInvalidateAllTokensForRealm() throws Exception {
225226
SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS);
226227
}
227228
InvalidateTokenResponse invalidateResponse = restClient.security().invalidateToken(
228-
new InvalidateTokenRequest(null, null, "file", null),
229+
InvalidateTokenRequest.realmTokens("file"),
229230
RequestOptions.DEFAULT.toBuilder().addHeader("Authorization",
230231
UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER,
231232
SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)).build());
@@ -242,7 +243,7 @@ public void testInvalidateAllTokensForRealmThatHasNone() throws IOException {
242243
SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS);
243244
}
244245
InvalidateTokenResponse invalidateResponse = restClient.security().invalidateToken(
245-
new InvalidateTokenRequest(null, null, "saml", null),
246+
InvalidateTokenRequest.realmTokens("saml"),
246247
RequestOptions.DEFAULT.toBuilder().addHeader("Authorization",
247248
UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER,
248249
SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)).build());
@@ -257,12 +258,12 @@ public void testInvalidateMultipleTimes() throws IOException {
257258
SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS);
258259

259260
InvalidateTokenResponse invalidateResponse = restClient.security()
260-
.invalidateToken(new InvalidateTokenRequest(response.getAccessToken(), null, null, null), SECURITY_REQUEST_OPTIONS);
261+
.invalidateToken(InvalidateTokenRequest.accessToken(response.getAccessToken()), SECURITY_REQUEST_OPTIONS);
261262
assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(1));
262263
assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
263264
assertThat(invalidateResponse.getErrors(), empty());
264265
InvalidateTokenResponse invalidateAgainResponse = restClient.security()
265-
.invalidateToken(new InvalidateTokenRequest(response.getAccessToken(), null, null, null), SECURITY_REQUEST_OPTIONS);
266+
.invalidateToken(InvalidateTokenRequest.accessToken(response.getAccessToken()), SECURITY_REQUEST_OPTIONS);
266267
assertThat(invalidateAgainResponse.getInvalidatedTokens(), equalTo(0));
267268
assertThat(invalidateAgainResponse.getPreviouslyInvalidatedTokens(), equalTo(1));
268269
assertThat(invalidateAgainResponse.getErrors(), empty());
@@ -272,37 +273,36 @@ public void testInvalidateNotValidAccessTokens() throws Exception {
272273
final RestHighLevelClient restClient = new TestRestHighLevelClient();
273274
// Perform a request to invalidate a token, before the tokens index is created
274275
ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> restClient.security()
275-
.invalidateToken(new InvalidateTokenRequest(generateAccessToken(Version.CURRENT), null, null, null),
276+
.invalidateToken(InvalidateTokenRequest.accessToken(generateAccessToken(Version.CURRENT)),
276277
SECURITY_REQUEST_OPTIONS));
277278
assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
278279
// Create a token to trigger index creation
279280
restClient.security().createToken(CreateTokenRequest.passwordGrant(
280281
SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS);
281282
InvalidateTokenResponse invalidateResponse = restClient.security()
282-
.invalidateToken(new InvalidateTokenRequest("!this_is_not_a_base64_string_and_we_should_fail_decoding_it", null, null, null),
283+
.invalidateToken(InvalidateTokenRequest.accessToken("!this_is_not_a_base64_string_and_we_should_fail_decoding_it"),
283284
SECURITY_REQUEST_OPTIONS);
284285
assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0));
285286
assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
286287
assertThat(invalidateResponse.getErrors(), empty());
287288

288289
invalidateResponse = restClient.security()
289-
.invalidateToken(new InvalidateTokenRequest("10we+might+assume+this+is+valid+old+token", null, null,
290-
null), SECURITY_REQUEST_OPTIONS);
290+
.invalidateToken(InvalidateTokenRequest.accessToken("10we+might+assume+this+is+valid+old+token"), SECURITY_REQUEST_OPTIONS);
291291
assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0));
292292
assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
293293
assertThat(invalidateResponse.getErrors(), empty());
294294

295295
invalidateResponse = restClient.security()
296-
.invalidateToken(new InvalidateTokenRequest(generateInvalidShortAccessToken(Version.CURRENT), null, null,
297-
null), SECURITY_REQUEST_OPTIONS);
296+
.invalidateToken(InvalidateTokenRequest.accessToken(generateInvalidShortAccessToken(Version.CURRENT)),
297+
SECURITY_REQUEST_OPTIONS);
298298
assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0));
299299
assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
300300
assertThat(invalidateResponse.getErrors(), empty());
301301

302302
// Generate a token that could be a valid token string for the version we are on, and should decode fine, but is not found in our
303303
// tokens index
304304
invalidateResponse = restClient.security()
305-
.invalidateToken(new InvalidateTokenRequest(generateAccessToken(Version.CURRENT), null, null, null),
305+
.invalidateToken(InvalidateTokenRequest.accessToken(generateAccessToken(Version.CURRENT)),
306306
SECURITY_REQUEST_OPTIONS);
307307
assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0));
308308
assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
@@ -313,30 +313,29 @@ public void testInvalidateNotValidRefreshTokens() throws Exception {
313313
final RestHighLevelClient restClient = new TestRestHighLevelClient();
314314
// Perform a request to invalidate a refresh token, before the tokens index is created
315315
ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> restClient.security()
316-
.invalidateToken(new InvalidateTokenRequest(null,
317-
TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID()), null, null),
316+
.invalidateToken(InvalidateTokenRequest.refreshToken(
317+
TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID())),
318318
SECURITY_REQUEST_OPTIONS));
319319
assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
320320
// Create a token to trigger index creation
321321
restClient.security().createToken(CreateTokenRequest.passwordGrant(
322322
SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS);
323323
InvalidateTokenResponse invalidateResponse = restClient.security()
324-
.invalidateToken(new InvalidateTokenRequest(null, "!this_is_not_a_base64_string_and_we_should_fail_decoding_it", null, null),
324+
.invalidateToken(InvalidateTokenRequest.refreshToken("!this_is_not_a_base64_string_and_we_should_fail_decoding_it"),
325325
SECURITY_REQUEST_OPTIONS);
326326
assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0));
327327
assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
328328
assertThat(invalidateResponse.getErrors(), empty());
329329

330330
invalidateResponse = restClient.security()
331-
.invalidateToken(new InvalidateTokenRequest(null, "10we+might+assume+this+is+valid+old+token", null,
332-
null), SECURITY_REQUEST_OPTIONS);
331+
.invalidateToken(InvalidateTokenRequest.refreshToken("10we+might+assume+this+is+valid+old+token"), SECURITY_REQUEST_OPTIONS);
333332
assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0));
334333
assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
335334
assertThat(invalidateResponse.getErrors(), empty());
336335

337336
invalidateResponse = restClient.security()
338-
.invalidateToken(new InvalidateTokenRequest(null,
339-
TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, randomAlphaOfLength(32)), null, null),
337+
.invalidateToken(InvalidateTokenRequest.refreshToken(
338+
TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, randomAlphaOfLength(32))),
340339
SECURITY_REQUEST_OPTIONS);
341340
assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0));
342341
assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
@@ -345,8 +344,8 @@ public void testInvalidateNotValidRefreshTokens() throws Exception {
345344
// Generate a token that could be a valid token string for the version we are on, and should decode fine, but is not found in our
346345
// tokens index
347346
invalidateResponse = restClient.security()
348-
.invalidateToken(new InvalidateTokenRequest(null,
349-
TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID()), null, null),
347+
.invalidateToken(InvalidateTokenRequest.refreshToken(
348+
TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID())),
350349
SECURITY_REQUEST_OPTIONS);
351350
assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0));
352351
assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
@@ -377,7 +376,7 @@ public void testRefreshingInvalidatedToken() throws IOException {
377376
SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS);
378377
assertNotNull(createTokenResponse.getRefreshToken());
379378
InvalidateTokenResponse invalidateResponse = restClient.security()
380-
.invalidateToken(new InvalidateTokenRequest(null, createTokenResponse.getRefreshToken(), null, null), SECURITY_REQUEST_OPTIONS);
379+
.invalidateToken(InvalidateTokenRequest.refreshToken(createTokenResponse.getRefreshToken()), SECURITY_REQUEST_OPTIONS);
381380
assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(1));
382381
assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0));
383382
assertThat(invalidateResponse.getErrors(), empty());

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthDelegationIntegTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public void testTokenInvalidate() throws Exception {
212212
assertThat(authnRealm.getType(), is("pki"));
213213
assertThat(resp.getAuthenticationType(), is("token"));
214214
// invalidate
215-
InvalidateTokenRequest invalidateRequest = new InvalidateTokenRequest(token, null, null, null);
215+
InvalidateTokenRequest invalidateRequest = InvalidateTokenRequest.accessToken(token);
216216
optionsBuilder = RequestOptions.DEFAULT.toBuilder();
217217
optionsBuilder.addHeader("Authorization",
218218
basicAuthHeaderValue(delegateeUsername, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING));

0 commit comments

Comments
 (0)