Skip to content

Commit bb5b590

Browse files
authored
[TEST] Manually trigger resource watching (#34893)
SSLTrustRestrictionsTests.testRestrictionsAreReloaded checks that the SSL trust configuration is automatically updated reapplied if the underlying "trust_restrictions.yml" file is modified. Since the default resource watcher frequency is 5seconds, it could take 10 second to run that test (as it waits for 2 reloaded). Previously this test set that frequency to a very low value (3ms) so that the elapsed time for the test would be reduced. However this caused other problems, including that the resource watcher would frequently run while the cluster was shutting down and files were being cleaned up. This change resets that watch frequency back to its default (5s) and then manually calls the "notifyNow" method on the resource watcher whenever the restrictions file is modified, so that the SSL trust configuration is reloaded at exactly the right time. Resolves: #34502
1 parent dc5bfe3 commit bb5b590

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/ssl/SSLTrustRestrictionsTests.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import org.elasticsearch.common.unit.TimeValue;
1414
import org.elasticsearch.env.TestEnvironment;
1515
import org.elasticsearch.test.ESIntegTestCase;
16+
import org.elasticsearch.test.InternalTestCluster;
1617
import org.elasticsearch.test.SecurityIntegTestCase;
1718
import org.elasticsearch.test.junit.annotations.TestLogging;
1819
import org.elasticsearch.transport.Transport;
20+
import org.elasticsearch.watcher.ResourceWatcherService;
1921
import org.elasticsearch.xpack.core.ssl.CertParsingUtils;
2022
import org.elasticsearch.xpack.core.ssl.PemUtils;
2123
import org.elasticsearch.xpack.core.ssl.RestrictedTrustManager;
@@ -50,7 +52,6 @@
5052
@TestLogging("org.elasticsearch.xpack.ssl.RestrictedTrustManager:DEBUG")
5153
public class SSLTrustRestrictionsTests extends SecurityIntegTestCase {
5254

53-
private static final int RESOURCE_RELOAD_MILLIS = 3;
5455
private static final TimeValue MAX_WAIT_RELOAD = TimeValue.timeValueSeconds(1);
5556

5657
private static Path configPath;
@@ -129,7 +130,6 @@ public Settings nodeSettings(int nodeOrdinal) {
129130

130131
writeRestrictions("*.trusted");
131132
builder.put("xpack.ssl.trust_restrictions.path", restrictionsPath);
132-
builder.put("resource.reload.interval.high", RESOURCE_RELOAD_MILLIS + "ms");
133133

134134
return builder.build();
135135
}
@@ -145,6 +145,7 @@ private void writeRestrictions(String trustedPattern) {
145145
} catch (IOException e) {
146146
throw new ElasticsearchException("failed to write restrictions", e);
147147
}
148+
runResourceWatcher();
148149
}
149150

150151
@Override
@@ -203,6 +204,23 @@ public void testRestrictionsAreReloaded() throws Exception {
203204
}, MAX_WAIT_RELOAD.millis(), TimeUnit.MILLISECONDS);
204205
}
205206

207+
/**
208+
* Force the file watch to be updated.
209+
* Ideally we'd just left the service do its thing, but that means waiting for 5sec
210+
* We can drop the 5s down, but then we run into resource contention issues.
211+
* This method just tells the {@link ResourceWatcherService} to run its check at a time that suits the tests. In all other respects
212+
* it works just like normal - the usual file checks apply for detecting it as "changed", and only the previously configured files
213+
* are checked.
214+
*/
215+
private void runResourceWatcher() {
216+
final InternalTestCluster cluster = internalCluster();
217+
if (cluster.size() > 0) {
218+
final ResourceWatcherService service = cluster.getInstance(ResourceWatcherService.class);
219+
logger.info("Triggering a reload of watched resources");
220+
service.notifyNow(ResourceWatcherService.Frequency.HIGH);
221+
}
222+
}
223+
206224
private void tryConnect(CertificateInfo certificate) throws Exception {
207225
Settings settings = Settings.builder()
208226
.put("path.home", createTempDir())

0 commit comments

Comments
 (0)