|
5 | 5 | */
|
6 | 6 | package org.elasticsearch.xpack.security.authc;
|
7 | 7 |
|
| 8 | +import org.elasticsearch.ElasticsearchException; |
8 | 9 | import org.elasticsearch.ElasticsearchSecurityException;
|
9 | 10 | import org.elasticsearch.Version;
|
10 | 11 | import org.elasticsearch.action.ActionListener;
|
|
54 | 55 | import org.elasticsearch.xpack.core.security.authc.support.TokensInvalidationResult;
|
55 | 56 | import org.elasticsearch.xpack.core.security.user.User;
|
56 | 57 | import org.elasticsearch.xpack.core.watcher.watch.ClockMock;
|
| 58 | +import org.elasticsearch.xpack.security.support.FeatureNotEnabledException; |
57 | 59 | import org.elasticsearch.xpack.security.support.SecurityIndexManager;
|
58 | 60 | import org.elasticsearch.xpack.security.test.SecurityMocks;
|
59 | 61 | import org.hamcrest.Matchers;
|
|
63 | 65 | import org.junit.BeforeClass;
|
64 | 66 |
|
65 | 67 | import javax.crypto.SecretKey;
|
66 |
| - |
67 | 68 | import java.io.IOException;
|
68 | 69 | import java.net.URLEncoder;
|
69 | 70 | import java.nio.charset.StandardCharsets;
|
|
79 | 80 | import static java.time.Clock.systemUTC;
|
80 | 81 | import static org.elasticsearch.repositories.blobstore.ESBlobStoreRepositoryIntegTestCase.randomBytes;
|
81 | 82 | import static org.elasticsearch.test.ClusterServiceUtils.setState;
|
| 83 | +import static org.elasticsearch.test.TestMatchers.throwableWithMessage; |
| 84 | +import static org.hamcrest.Matchers.contains; |
82 | 85 | import static org.hamcrest.Matchers.containsString;
|
83 | 86 | import static org.hamcrest.Matchers.equalTo;
|
| 87 | +import static org.hamcrest.Matchers.instanceOf; |
84 | 88 | import static org.hamcrest.Matchers.notNullValue;
|
85 | 89 | import static org.hamcrest.Matchers.nullValue;
|
86 | 90 | import static org.mockito.Matchers.any;
|
@@ -561,20 +565,23 @@ public void testTokenServiceDisabled() throws Exception {
|
561 | 565 | .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), false)
|
562 | 566 | .build(),
|
563 | 567 | Clock.systemUTC(), client, licenseState, securityContext, securityMainIndex, securityTokensIndex, clusterService);
|
564 |
| - IllegalStateException e = expectThrows(IllegalStateException.class, |
| 568 | + ElasticsearchException e = expectThrows(ElasticsearchException.class, |
565 | 569 | () -> tokenService.createOAuth2Tokens(null, null, null, true, null));
|
566 |
| - assertEquals("security tokens are not enabled", e.getMessage()); |
| 570 | + assertThat(e, throwableWithMessage("security tokens are not enabled")); |
| 571 | + assertThat(e, instanceOf(FeatureNotEnabledException.class)); |
| 572 | + // Client can check the metadata for this value, and depend on an exact string match: |
| 573 | + assertThat(e.getMetadata(FeatureNotEnabledException.DISABLED_FEATURE_METADATA), contains("security_tokens")); |
567 | 574 |
|
568 | 575 | PlainActionFuture<UserToken> future = new PlainActionFuture<>();
|
569 | 576 | tokenService.getAndValidateToken(null, future);
|
570 | 577 | assertNull(future.get());
|
571 | 578 |
|
572 |
| - e = expectThrows(IllegalStateException.class, () -> { |
573 |
| - PlainActionFuture<TokensInvalidationResult> invalidateFuture = new PlainActionFuture<>(); |
574 |
| - tokenService.invalidateAccessToken((String) null, invalidateFuture); |
575 |
| - invalidateFuture.actionGet(); |
576 |
| - }); |
577 |
| - assertEquals("security tokens are not enabled", e.getMessage()); |
| 579 | + PlainActionFuture<TokensInvalidationResult> invalidateFuture = new PlainActionFuture<>(); |
| 580 | + e = expectThrows(ElasticsearchException.class, () -> tokenService.invalidateAccessToken((String) null, invalidateFuture)); |
| 581 | + assertThat(e, throwableWithMessage("security tokens are not enabled")); |
| 582 | + assertThat(e, instanceOf(FeatureNotEnabledException.class)); |
| 583 | + // Client can check the metadata for this value, and depend on an exact string match: |
| 584 | + assertThat(e.getMetadata(FeatureNotEnabledException.DISABLED_FEATURE_METADATA), contains("security_tokens")); |
578 | 585 | }
|
579 | 586 |
|
580 | 587 | public void testBytesKeyEqualsHashCode() {
|
|
0 commit comments