Skip to content

Commit c1785ff

Browse files
committed
Add test
1 parent 1b83d56 commit c1785ff

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

server/src/main/java/org/elasticsearch/watcher/FileWatcher.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public FileWatcher(Path path, boolean checkFileContents) {
5757
rootFileObserver = new FileObserver(path);
5858
}
5959

60+
// For testing
61+
public Path getPath() {
62+
return path;
63+
}
64+
6065
/**
6166
* Clears any state with the FileWatcher, making all files show up as new
6267
*/

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationReloaderTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.elasticsearch.test.http.MockWebServer;
3838
import org.elasticsearch.threadpool.TestThreadPool;
3939
import org.elasticsearch.threadpool.ThreadPool;
40+
import org.elasticsearch.watcher.FileWatcher;
41+
import org.elasticsearch.watcher.ResourceWatcher;
4042
import org.elasticsearch.watcher.ResourceWatcherService;
4143
import org.junit.After;
4244
import org.junit.Before;
@@ -66,7 +68,9 @@
6668
import java.security.cert.CertificateException;
6769
import java.util.Collection;
6870
import java.util.Collections;
71+
import java.util.HashSet;
6972
import java.util.List;
73+
import java.util.Set;
7074
import java.util.concurrent.CountDownLatch;
7175
import java.util.concurrent.CyclicBarrier;
7276
import java.util.concurrent.TimeUnit;
@@ -79,6 +83,7 @@
7983
import javax.net.ssl.SSLSocket;
8084

8185
import static org.elasticsearch.test.TestMatchers.throwableWithMessage;
86+
import static org.hamcrest.Matchers.containsInAnyOrder;
8287
import static org.hamcrest.Matchers.containsString;
8388
import static org.hamcrest.Matchers.sameInstance;
8489

@@ -559,6 +564,38 @@ public void testFailureToReadFileDoesntFail() throws Exception {
559564
}
560565
}
561566

567+
/**
568+
* Due to exclusive access entitlements
569+
* (see {@link org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.FileData#exclusive}),
570+
* it is not safe to monitor a directory or any files that are not an explicit part of this SSL configuration.
571+
*/
572+
public void testReloaderOnlyWatchesSpecifiedFiles() throws Exception {
573+
final Set<Path> watchedPaths = new HashSet<>();
574+
final ResourceWatcherService mockResourceWatcher = Mockito.mock(ResourceWatcherService.class);
575+
Mockito.when(mockResourceWatcher.add(Mockito.any(ResourceWatcher.class), Mockito.any(ResourceWatcherService.Frequency.class)))
576+
.then(inv -> {
577+
final FileWatcher fileWatcher = asInstanceOf(FileWatcher.class, inv.getArguments()[0]);
578+
watchedPaths.add(fileWatcher.getPath());
579+
return null;
580+
});
581+
582+
final Path tempDir = createTempDir();
583+
final Path clientCertPath = tempDir.resolve("testclient.crt");
584+
Settings settings = baseKeystoreSettings(tempDir, null).putList(
585+
"xpack.security.transport.ssl.certificate_authorities",
586+
clientCertPath.toString()
587+
).put("path.home", createTempDir()).build();
588+
589+
final Environment env = newEnvironment(settings);
590+
final Collection<SslConfiguration> configurations = SSLService.getSSLConfigurations(env).values();
591+
new SSLConfigurationReloader(ignore -> {}, mockResourceWatcher, configurations);
592+
593+
assertThat(
594+
watchedPaths,
595+
containsInAnyOrder(tempDir.resolve("testclient.pem"), tempDir.resolve("testclient.crt"), tempDir.resolve("testclientcert.crt"))
596+
);
597+
}
598+
562599
private Settings.Builder baseKeystoreSettings(Path tempDir, MockSecureSettings secureSettings) throws IOException {
563600
final Path keyPath = tempDir.resolve("testclient.pem");
564601
final Path certPath = tempDir.resolve("testclientcert.crt"); // testclient.crt filename already used in #testPEMTrustReloadException

0 commit comments

Comments
 (0)