|
123 | 123 | import static org.hamcrest.Matchers.equalTo;
|
124 | 124 | import static org.hamcrest.Matchers.instanceOf;
|
125 | 125 | import static org.hamcrest.Matchers.is;
|
| 126 | +import static org.hamcrest.Matchers.lessThanOrEqualTo; |
126 | 127 | import static org.hamcrest.Matchers.not;
|
127 | 128 | import static org.hamcrest.Matchers.notNullValue;
|
128 | 129 | import static org.hamcrest.Matchers.nullValue;
|
@@ -778,39 +779,57 @@ public void testApiKeyCacheWillNotTraceLogOnEvictionDueToCacheTtl() throws Illeg
|
778 | 779 | }
|
779 | 780 | }
|
780 | 781 |
|
781 |
| - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/74586") |
782 | 782 | public void testApiKeyAuthCacheWillLogWarningOnPossibleThrashing() throws Exception {
|
783 | 783 | ApiKeyService service = createApiKeyService(
|
784 |
| - Settings.builder().put("xpack.security.authc.api_key.cache.max_keys", 1).build()); |
| 784 | + Settings.builder().put("xpack.security.authc.api_key.cache.max_keys", 2).build()); |
785 | 785 | final Cache<String, ListenableFuture<CachedApiKeyHashResult>> apiKeyAuthCache = service.getApiKeyAuthCache();
|
786 | 786 |
|
787 | 787 | // Fill the cache
|
788 |
| - final String apiKeyId = randomAlphaOfLength(22); |
789 |
| - apiKeyAuthCache.put(apiKeyId, new ListenableFuture<>()); |
| 788 | + apiKeyAuthCache.put(randomAlphaOfLength(20), new ListenableFuture<>()); |
| 789 | + apiKeyAuthCache.put(randomAlphaOfLength(21), new ListenableFuture<>()); |
790 | 790 | final Logger logger = LogManager.getLogger(ApiKeyService.class);
|
791 |
| - Loggers.setLevel(logger, Level.WARN); |
| 791 | + Loggers.setLevel(logger, Level.TRACE); |
792 | 792 | final MockLogAppender appender = new MockLogAppender();
|
793 | 793 | Loggers.addAppender(logger, appender);
|
794 | 794 | appender.start();
|
795 | 795 |
|
796 | 796 | try {
|
797 | 797 | // Prepare the warning logging to trigger
|
798 | 798 | service.getEvictionCounter().add(4500);
|
| 799 | + final long thrashingCheckIntervalInSeconds = 300L; |
| 800 | + final long secondsToNanoSeconds = 1_000_000_000L; |
| 801 | + // Calculate the last thrashing check time to ensure that the elapsed time is longer than the |
| 802 | + // thrashing checking interval (300 seconds). Also add another 10 seconds to counter any |
| 803 | + // test flakiness. |
| 804 | + final long lastCheckedAt = System.nanoTime() - (thrashingCheckIntervalInSeconds + 10L) * secondsToNanoSeconds; |
| 805 | + service.getLastEvictionCheckedAt().set(lastCheckedAt); |
799 | 806 | // Ensure the counter is updated
|
800 | 807 | assertBusy(() -> assertThat(service.getEvictionCounter().longValue() >= 4500, is(true)));
|
| 808 | + appender.addExpectation(new MockLogAppender.SeenEventExpectation( |
| 809 | + "evict", ApiKeyService.class.getName(), Level.TRACE, |
| 810 | + "API key with ID [*] was evicted from the authentication cache*" |
| 811 | + )); |
801 | 812 | appender.addExpectation(new MockLogAppender.SeenEventExpectation(
|
802 | 813 | "thrashing", ApiKeyService.class.getName(), Level.WARN,
|
803 | 814 | "Possible thrashing for API key authentication cache,*"
|
804 | 815 | ));
|
805 |
| - apiKeyAuthCache.put(randomAlphaOfLength(23), new ListenableFuture<>()); |
| 816 | + apiKeyAuthCache.put(randomAlphaOfLength(22), new ListenableFuture<>()); |
806 | 817 | appender.assertAllExpectationsMatched();
|
807 | 818 |
|
| 819 | + // Counter and timer should be reset |
| 820 | + assertThat(service.getLastEvictionCheckedAt().get(), lessThanOrEqualTo(System.nanoTime())); |
| 821 | + assertBusy(() -> assertThat(service.getEvictionCounter().longValue(), equalTo(0L))); |
| 822 | + |
808 | 823 | // Will not log warning again for the next eviction because of throttling
|
| 824 | + appender.addExpectation(new MockLogAppender.SeenEventExpectation( |
| 825 | + "evict-again", ApiKeyService.class.getName(), Level.TRACE, |
| 826 | + "API key with ID [*] was evicted from the authentication cache*" |
| 827 | + )); |
809 | 828 | appender.addExpectation(new MockLogAppender.UnseenEventExpectation(
|
810 | 829 | "throttling", ApiKeyService.class.getName(), Level.WARN,
|
811 | 830 | "Possible thrashing for API key authentication cache,*"
|
812 | 831 | ));
|
813 |
| - apiKeyAuthCache.put(randomAlphaOfLength(24), new ListenableFuture<>()); |
| 832 | + apiKeyAuthCache.put(randomAlphaOfLength(23), new ListenableFuture<>()); |
814 | 833 | appender.assertAllExpectationsMatched();
|
815 | 834 | } finally {
|
816 | 835 | appender.stop();
|
|
0 commit comments