Skip to content

Commit 26ca379

Browse files
tmaciejewskisnicoll
authored andcommitted
Add support for symlinks in FileWatcher
This commit allows using symlinks for SSL certificate hot reloading. See gh-43586
1 parent 0035569 commit 26ca379

File tree

2 files changed

+24
-1
lines changed
  • spring-boot-project/spring-boot-autoconfigure/src

2 files changed

+24
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/FileWatcher.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,25 @@ void watch(Set<Path> paths, Runnable action) {
8686
this.thread = new WatcherThread();
8787
this.thread.start();
8888
}
89-
this.thread.register(new Registration(paths, action));
89+
this.thread.register(new Registration(resolveSymlinks(paths), action));
9090
}
9191
catch (IOException ex) {
9292
throw new UncheckedIOException("Failed to register paths for watching: " + paths, ex);
9393
}
9494
}
9595
}
9696

97+
private Set<Path> resolveSymlinks(Set<Path> paths) throws IOException {
98+
Set<Path> result = new HashSet<>();
99+
for (Path path : paths) {
100+
result.add(path);
101+
if (Files.isSymbolicLink(path)) {
102+
result.add(Files.readSymbolicLink(path));
103+
}
104+
}
105+
return result;
106+
}
107+
97108
@Override
98109
public void close() throws IOException {
99110
synchronized (this.lock) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ssl/FileWatcherTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ void shouldWatchFile(@TempDir Path tempDir) throws Exception {
9494
callback.expectChanges();
9595
}
9696

97+
@Test
98+
void shouldFollowSymlink(@TempDir Path tempDir) throws Exception {
99+
Path realFile = tempDir.resolve("realFile.txt");
100+
Path symLink = tempDir.resolve("symlink.txt");
101+
Files.createFile(realFile);
102+
Files.createSymbolicLink(symLink, realFile);
103+
WaitingCallback callback = new WaitingCallback();
104+
this.fileWatcher.watch(Set.of(symLink), callback);
105+
Files.writeString(realFile, "Some content");
106+
callback.expectChanges();
107+
}
108+
97109
@Test
98110
void shouldIgnoreNotWatchedFiles(@TempDir Path tempDir) throws Exception {
99111
Path watchedFile = tempDir.resolve("watched.txt");

0 commit comments

Comments
 (0)