moq_native/watch.rs
1//! Watch on-disk files (TLS certs/keys) and get notified when they're rotated.
2
3use std::path::{Path, PathBuf};
4
5use notify::Watcher;
6use tokio::sync::mpsc;
7
8/// Watches a set of files and resolves whenever something changes in their
9/// directories.
10///
11/// Reacting to the filesystem (rather than a SIGHUP/SIGUSR1) is what lets
12/// cert-manager, Kubernetes secret mounts, and `mv`-into-place rotate files with
13/// no extra signalling: they rewrite the file and the watcher fires.
14///
15/// Watches each file's *parent directory*, not the file itself. Editors,
16/// cert-manager, and K8s secret mounts replace files by atomic rename or symlink
17/// swap, which changes the inode (and, for the K8s `..data` symlink, fires on the
18/// directory without ever naming the file), so a watch set directly on the path
19/// would be missed.
20pub struct FileWatcher {
21 // Holds the OS watcher alive; dropping it stops events.
22 _watcher: notify::RecommendedWatcher,
23 events: mpsc::Receiver<()>,
24}
25
26impl FileWatcher {
27 /// Start watching the parent directories of `paths`. Errors if the OS watcher
28 /// can't be created or a directory can't be watched (e.g. the inotify
29 /// instance/watch limit is hit). `notify` already falls back to a built-in
30 /// poll watcher on platforms without a native backend, so there's no manual
31 /// polling here.
32 pub fn new(paths: &[PathBuf]) -> notify::Result<Self> {
33 // A capacity-1 channel of unit wakeups coalesces the burst of raw events
34 // notify emits per change (and any unrelated churn in the directory): a
35 // full buffer already has a pending wakeup, so extra sends are dropped.
36 let (tx, rx) = mpsc::channel(1);
37 let mut watcher = notify::recommended_watcher(move |res: notify::Result<notify::Event>| {
38 let send = match res {
39 Ok(event) => is_reload_trigger(&event.kind),
40 // A watcher error (e.g. inotify queue overflow) may mean we missed a
41 // real change, so reload to be safe.
42 Err(_) => true,
43 };
44 if send {
45 let _ = tx.try_send(());
46 }
47 })?;
48
49 // Watch each distinct parent directory once. A bare filename like
50 // `cert.pem` has an empty-string parent (`Some("")`, not `None`), which the
51 // OS watcher rejects with "No path was found", so map that to the current
52 // directory.
53 let mut dirs: Vec<&Path> = paths
54 .iter()
55 .filter_map(|p| p.parent())
56 .map(|p| if p.as_os_str().is_empty() { Path::new(".") } else { p })
57 .collect();
58 dirs.sort_unstable();
59 dirs.dedup();
60 for dir in dirs {
61 watcher.watch(dir, notify::RecursiveMode::NonRecursive)?;
62 }
63
64 Ok(Self {
65 _watcher: watcher,
66 events: rx,
67 })
68 }
69
70 /// Resolve once the OS reports activity in a watched directory. The caller
71 /// reloads on return; reloads are idempotent, so the coarse "something
72 /// changed" granularity at worst costs an occasional redundant reload.
73 pub async fn changed(&mut self) {
74 // The sender lives inside `_watcher`, which we hold for `&mut self`, so the
75 // channel can't be closed here.
76 self.events
77 .recv()
78 .await
79 .expect("file watcher channel closed unexpectedly");
80 }
81}
82
83/// Whether a raw notify event reflects a real change that should trigger a reload.
84///
85/// The reload path opens and reads the watched files, and notify's inotify backend
86/// reports IN_OPEN/IN_ACCESS for those reads. Treating them as changes makes a reload
87/// re-trigger itself in a tight loop (a ~400/sec storm that starved TLS handshakes in
88/// production), so we react only to events that can mean new cert bytes: a create, a
89/// modify/rename, or a finished write (IN_CLOSE_WRITE).
90fn is_reload_trigger(kind: ¬ify::EventKind) -> bool {
91 use notify::EventKind;
92 use notify::event::{AccessKind, AccessMode};
93 match kind {
94 // A finished write is the only access event that signals new content. The
95 // reload opens and reads these files itself (and other processes may read them
96 // too), so open/read/close-without-write must be ignored or it loops forever.
97 EventKind::Access(AccessKind::Close(AccessMode::Write)) => true,
98 EventKind::Access(_) => false,
99 // Rotations arrive as a create or a modify/rename: cert-manager and
100 // mv-into-place rename over the file, the K8s `..data` symlink swap fires a
101 // directory rename, and in-place rewrites modify the data.
102 EventKind::Create(_) | EventKind::Modify(_) => true,
103 // A bare removal leaves nothing to load (wait for the replacement's create),
104 // and Any/Other are unclassified noise we don't act on.
105 _ => false,
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 // A bare filename has a `Some("")` parent; watching "" is rejected by the OS
114 // watcher, so `new` must fall back to the current directory rather than error.
115 #[test]
116 fn bare_filename_watches_current_dir() {
117 FileWatcher::new(&[PathBuf::from("cert.pem"), PathBuf::from("key.pem")])
118 .expect("bare filenames should watch the current directory");
119 }
120
121 // The reload reads its own files; reads and bare removals must not re-trigger it.
122 #[test]
123 fn ignored_events_do_not_trigger_reload() {
124 use notify::EventKind;
125 use notify::event::{AccessKind, AccessMode, RemoveKind};
126 assert!(!is_reload_trigger(&EventKind::Access(AccessKind::Read)));
127 assert!(!is_reload_trigger(&EventKind::Access(AccessKind::Open(
128 AccessMode::Read
129 ))));
130 assert!(!is_reload_trigger(&EventKind::Access(AccessKind::Open(
131 AccessMode::Any
132 ))));
133 assert!(!is_reload_trigger(&EventKind::Access(AccessKind::Close(
134 AccessMode::Read
135 ))));
136 assert!(!is_reload_trigger(&EventKind::Remove(RemoveKind::Any)));
137 }
138
139 // A finished write, a create, and a modify/rename are real rotations.
140 #[test]
141 fn writes_and_rotations_trigger_reload() {
142 use notify::EventKind;
143 use notify::event::{AccessKind, AccessMode, CreateKind, ModifyKind};
144 assert!(is_reload_trigger(&EventKind::Access(AccessKind::Close(
145 AccessMode::Write
146 ))));
147 assert!(is_reload_trigger(&EventKind::Create(CreateKind::Any)));
148 assert!(is_reload_trigger(&EventKind::Modify(ModifyKind::Any)));
149 }
150}