fix(relay): the authority list follows its file's content, not its file time
A file time is only as fine as the operating system's clock tick (about 15 ms on Windows), so a second edit within one tick kept the same time and reload() saw no change: an operator's quick second edit was ignored. The list now remembers the text it last read (good or bad) and reloads when the text differs; the file is capped at 64 KiB and read at each 30 s housekeeping tick. A bad file is still refused and never replaces a good list. The tests no longer force the change, and the dns test says what happened if it fails. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
dfd1c2c914
commit
227b692202
2 files changed
+72
-16
No files matched your search
@@ -64,7 +64,6 @@
|
|||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
use std::time::SystemTime;
|
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
@@ -72,6 +71,9 @@ use crate::names::Name;
|
|||||||
use crate::proto::{Authority, Binding};
|
use crate::proto::{Authority, Binding};
|
||||||
|
|
||||||
const MAX_AUTHORITIES: usize = 16;
|
const MAX_AUTHORITIES: usize = 16;
|
||||||
|
/// A list file larger than this is refused unread: sixteen entries need a
|
||||||
|
/// few kilobytes.
|
||||||
|
const MAX_FILE_BYTES: u64 = 64 * 1024;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
@@ -127,7 +129,9 @@ struct Listed {
|
|||||||
pub struct AuthorityList {
|
pub struct AuthorityList {
|
||||||
current: RwLock<Arc<Vec<Listed>>>,
|
current: RwLock<Arc<Vec<Listed>>>,
|
||||||
source: Option<PathBuf>,
|
source: Option<PathBuf>,
|
||||||
seen: std::sync::Mutex<Option<SystemTime>>,
|
/// The text of the file as last read, good or bad: a change is a
|
||||||
|
/// difference in content, never in a file time (see [`AuthorityList::reload`]).
|
||||||
|
seen: std::sync::Mutex<Option<String>>,
|
||||||
bindings: Option<Arc<dyn BindingSource>>,
|
bindings: Option<Arc<dyn BindingSource>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,6 +203,15 @@ fn parse(text: &str) -> Result<Vec<Listed>, ListError> {
|
|||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_file(path: &std::path::Path) -> Result<String, ListError> {
|
||||||
|
if std::fs::metadata(path)?.len() > MAX_FILE_BYTES {
|
||||||
|
return Err(ListError::Invalid(format!(
|
||||||
|
"the file is larger than {MAX_FILE_BYTES} bytes"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
Ok(std::fs::read_to_string(path)?)
|
||||||
|
}
|
||||||
|
|
||||||
impl AuthorityList {
|
impl AuthorityList {
|
||||||
/// A list from TOML text.
|
/// A list from TOML text.
|
||||||
pub fn from_toml(text: &str) -> Result<Self, ListError> {
|
pub fn from_toml(text: &str) -> Result<Self, ListError> {
|
||||||
@@ -212,13 +225,12 @@ impl AuthorityList {
|
|||||||
|
|
||||||
/// A list read from a file, and read again by [`AuthorityList::reload`].
|
/// A list read from a file, and read again by [`AuthorityList::reload`].
|
||||||
pub fn from_file(path: PathBuf) -> Result<Self, ListError> {
|
pub fn from_file(path: PathBuf) -> Result<Self, ListError> {
|
||||||
let text = std::fs::read_to_string(&path)?;
|
let text = read_file(&path)?;
|
||||||
let list = parse(&text)?;
|
let list = parse(&text)?;
|
||||||
let seen = std::fs::metadata(&path).and_then(|m| m.modified()).ok();
|
|
||||||
Ok(AuthorityList {
|
Ok(AuthorityList {
|
||||||
current: RwLock::new(Arc::new(list)),
|
current: RwLock::new(Arc::new(list)),
|
||||||
source: Some(path),
|
source: Some(path),
|
||||||
seen: std::sync::Mutex::new(seen),
|
seen: std::sync::Mutex::new(Some(text)),
|
||||||
bindings: None,
|
bindings: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -240,21 +252,30 @@ impl AuthorityList {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read the file again if it changed. A bad file changes nothing.
|
/// Read the file again and replace the list if its content changed. A
|
||||||
/// Returns whether the list was replaced.
|
/// bad file changes nothing. Returns whether the list was replaced.
|
||||||
|
///
|
||||||
|
/// A change is found by comparing the text with the text last read,
|
||||||
|
/// never by file time: a file time is as coarse as the operating
|
||||||
|
/// system's clock tick (on Windows, about 15 ms), so a second edit
|
||||||
|
/// within one tick keeps the same time and a time check would skip it.
|
||||||
|
/// The file is small (at most 64 KiB) and the relay's housekeeping
|
||||||
|
/// calls this every 30 seconds, so reading it each time costs nothing.
|
||||||
pub fn reload(&self) -> Result<bool, ListError> {
|
pub fn reload(&self) -> Result<bool, ListError> {
|
||||||
let Some(path) = &self.source else {
|
let Some(path) = &self.source else {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
};
|
};
|
||||||
let modified = std::fs::metadata(path).and_then(|m| m.modified()).ok();
|
let text = read_file(path)?;
|
||||||
{
|
{
|
||||||
let mut seen = self.seen.lock().unwrap_or_else(|p| p.into_inner());
|
let mut seen = self.seen.lock().unwrap_or_else(|p| p.into_inner());
|
||||||
if *seen == modified && modified.is_some() {
|
if seen.as_deref() == Some(text.as_str()) {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
*seen = modified;
|
// Remembered before it is parsed, so a file that stays bad is
|
||||||
|
// refused once, not on every check; the next edit is read again.
|
||||||
|
*seen = Some(text.clone());
|
||||||
}
|
}
|
||||||
let list = parse(&std::fs::read_to_string(path)?)?;
|
let list = parse(&text)?;
|
||||||
*self.current.write().unwrap_or_else(|p| p.into_inner()) = Arc::new(list);
|
*self.current.write().unwrap_or_else(|p| p.into_inner()) = Arc::new(list);
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
@@ -404,23 +425,55 @@ needs_binding = true
|
|||||||
assert_eq!(l.caa_issuers(), ["letsencrypt.org", "sectigo.com"]);
|
assert_eq!(l.caa_issuers(), ["letsencrypt.org", "sectigo.com"]);
|
||||||
let flipped = "[[authority]]\nid='zerossl'\ndirectory='https://acme.zerossl.com/v2/DV90'\ncaa_domain='sectigo.com'\nneeds_binding=true\n[[authority]]\nid='letsencrypt'\ndirectory='https://acme-v02.api.letsencrypt.org/directory'\ncaa_domain='letsencrypt.org'\n";
|
let flipped = "[[authority]]\nid='zerossl'\ndirectory='https://acme.zerossl.com/v2/DV90'\ncaa_domain='sectigo.com'\nneeds_binding=true\n[[authority]]\nid='letsencrypt'\ndirectory='https://acme-v02.api.letsencrypt.org/directory'\ncaa_domain='letsencrypt.org'\n";
|
||||||
std::fs::write(&path, flipped).unwrap();
|
std::fs::write(&path, flipped).unwrap();
|
||||||
// Same-second edits may not move the mtime: force it to look new.
|
|
||||||
*l.seen.lock().unwrap() = None;
|
|
||||||
assert!(l.reload().unwrap());
|
assert!(l.reload().unwrap());
|
||||||
assert_eq!(l.for_app(&name())[0].id, "zerossl");
|
assert_eq!(l.for_app(&name())[0].id, "zerossl");
|
||||||
assert_eq!(l.caa_issuers(), ["sectigo.com", "letsencrypt.org"]);
|
assert_eq!(l.caa_issuers(), ["sectigo.com", "letsencrypt.org"]);
|
||||||
std::fs::write(&path, "not toml {{{").unwrap();
|
std::fs::write(&path, "not toml {{{").unwrap();
|
||||||
*l.seen.lock().unwrap() = None;
|
|
||||||
assert!(l.reload().is_err());
|
assert!(l.reload().is_err());
|
||||||
assert_eq!(l.for_app(&name())[0].id, "zerossl", "the good list stands");
|
assert_eq!(l.for_app(&name())[0].id, "zerossl", "the good list stands");
|
||||||
// A file that gains a binding secret is a bad file too.
|
// A file that gains a binding secret is a bad file too.
|
||||||
let secret = format!("{flipped}binding_kid='k'\nbinding_hmac_key='a2V5'\n");
|
let secret = format!("{flipped}binding_kid='k'\nbinding_hmac_key='a2V5'\n");
|
||||||
std::fs::write(&path, secret).unwrap();
|
std::fs::write(&path, secret).unwrap();
|
||||||
*l.seen.lock().unwrap() = None;
|
|
||||||
assert!(l.reload().is_err());
|
assert!(l.reload().is_err());
|
||||||
assert_eq!(l.for_app(&name())[0].id, "zerossl", "the good list stands");
|
assert_eq!(l.for_app(&name())[0].id, "zerossl", "the good list stands");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_second_edit_of_the_same_length_at_once_is_not_missed() {
|
||||||
|
// Nothing here moves a file time or waits: two edits in one clock
|
||||||
|
// tick keep the same time, and the same length, and still change
|
||||||
|
// the list.
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let path = dir.path().join("authorities.toml");
|
||||||
|
let with = |ca: &str| {
|
||||||
|
format!("[[authority]]\nid='a'\ndirectory='https://x/d'\ncaa_domain='{ca}.example'\n")
|
||||||
|
};
|
||||||
|
std::fs::write(&path, with("one")).unwrap();
|
||||||
|
let l = AuthorityList::from_file(path.clone()).unwrap();
|
||||||
|
assert!(!l.reload().unwrap(), "unchanged: nothing replaced");
|
||||||
|
for (n, ca) in ["two", "tre", "for", "one"].into_iter().enumerate() {
|
||||||
|
std::fs::write(&path, with(ca)).unwrap();
|
||||||
|
assert!(l.reload().unwrap(), "edit {n} ({ca}) was missed");
|
||||||
|
assert_eq!(l.caa_issuers(), [format!("{ca}.example")]);
|
||||||
|
assert!(!l.reload().unwrap(), "edit {n} read twice");
|
||||||
|
}
|
||||||
|
// A bad file is refused once and the good list stands; fixing it back
|
||||||
|
// to what the list already is changes nothing served but is read.
|
||||||
|
std::fs::write(&path, "garbage {{{").unwrap();
|
||||||
|
assert!(l.reload().is_err());
|
||||||
|
assert!(
|
||||||
|
!l.reload().unwrap(),
|
||||||
|
"the same bad file is not refused twice"
|
||||||
|
);
|
||||||
|
assert_eq!(l.caa_issuers(), ["one.example"]);
|
||||||
|
std::fs::write(&path, with("one")).unwrap();
|
||||||
|
assert!(l.reload().unwrap());
|
||||||
|
// A file too large to be a list is refused unread.
|
||||||
|
std::fs::write(&path, " ".repeat(70 * 1024)).unwrap();
|
||||||
|
assert!(l.reload().is_err());
|
||||||
|
assert_eq!(l.caa_issuers(), ["one.example"]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_binding_source_is_asked_only_for_authorities_that_need_one() {
|
fn a_binding_source_is_asked_only_for_authorities_that_need_one() {
|
||||||
let l = AuthorityList::from_toml(TWO)
|
let l = AuthorityList::from_toml(TWO)
|
||||||
|
|||||||
@@ -768,7 +768,10 @@ mod tests {
|
|||||||
"{one}[[authority]]\nid='b'\ndirectory='https://y/d'\ncaa_domain='ca-two.example'\n"
|
"{one}[[authority]]\nid='b'\ndirectory='https://y/d'\ncaa_domain='ca-two.example'\n"
|
||||||
);
|
);
|
||||||
std::fs::write(&path, two).unwrap();
|
std::fs::write(&path, two).unwrap();
|
||||||
assert!(list.reload().unwrap());
|
assert!(
|
||||||
|
list.reload().unwrap(),
|
||||||
|
"the list file was rewritten and the reload saw no change"
|
||||||
|
);
|
||||||
assert_eq!(caa(&svc).len(), 2);
|
assert_eq!(caa(&svc).len(), 2);
|
||||||
// No authority listed: no CAA record, which restricts nobody.
|
// No authority listed: no CAA record, which restricts nobody.
|
||||||
let none = DnsService::new(
|
let none = DnsService::new(
|
||||||
|
|||||||
Reference in new issue
Block a user