[control] Fix round 2: Caddy rereads its files on every load and the served certificate is checked, Caddy trials get a deadline outside control (caddy-guard, caddy-revert), the watch under Caddy judges upstreams as edge does, a reload of Caddy during the watch is noticed and device changes wait for the apply, stricter root key scan, IPv4-mapped trusted proxies
This commit is contained in:
@@ -218,9 +218,11 @@ fn bundle_password() -> Result<String, PkiFail> {
|
||||
|
||||
/// Private keys in `dir` that are not control's own: control refuses to run with one in its
|
||||
/// state directory (ROADMAP §1.2: the root is never on the server). Found by content (any
|
||||
/// `PRIVATE KEY` PEM block, plain or encrypted) and by name (`*root*key*`, `*root*.pem` with
|
||||
/// a key); the only keys allowed are `pki/intermediate.key`, `pki/pending-*.key` and
|
||||
/// `pki/devices/*.key`, which control writes encrypted itself.
|
||||
/// `PRIVATE KEY` PEM block, plain or encrypted) and by name (`*root*key*`); the only keys
|
||||
/// allowed are `pki/intermediate.key`, `pki/pending-*.key` and `pki/devices/*.key`, and those
|
||||
/// must hold exactly one key in control's own encrypted armour, nothing else. A symbolic link
|
||||
/// is never followed and never skipped: it is reported. Git's object store (`*.git/objects`,
|
||||
/// compressed history) is not read.
|
||||
pub fn root_key_files(dir: &Path) -> Vec<PathBuf> {
|
||||
let allowed = |p: &Path| -> bool {
|
||||
let Ok(rel) = p.strip_prefix(dir) else {
|
||||
@@ -242,6 +244,12 @@ pub fn root_key_files(dir: &Path) -> Vec<PathBuf> {
|
||||
_ => false,
|
||||
}
|
||||
};
|
||||
let own_armour = |b: &[u8]| -> bool {
|
||||
let t = String::from_utf8_lossy(b);
|
||||
let begins = t.matches("-----BEGIN ").count();
|
||||
let ours = t.matches(OWN_BEGIN).count();
|
||||
begins == 1 && ours == 1 && t.matches(KEY_MARK).count() == 2
|
||||
};
|
||||
let mut found = Vec::new();
|
||||
let mut stack = vec![(dir.to_path_buf(), 0)];
|
||||
while let Some((d, depth)) = stack.pop() {
|
||||
@@ -250,21 +258,41 @@ pub fn root_key_files(dir: &Path) -> Vec<PathBuf> {
|
||||
};
|
||||
for e in rd.flatten() {
|
||||
let p = e.path();
|
||||
let Ok(meta) = e.metadata() else { continue };
|
||||
// symlink_metadata: a link is looked at, not followed.
|
||||
let Ok(meta) = std::fs::symlink_metadata(&p) else {
|
||||
continue;
|
||||
};
|
||||
if meta.file_type().is_symlink() {
|
||||
found.push(p);
|
||||
continue;
|
||||
}
|
||||
if meta.is_dir() {
|
||||
if depth < 8 {
|
||||
let git_objects =
|
||||
e.file_name() == "objects" && d.extension().is_some_and(|x| x == "git");
|
||||
if depth < 8 && !git_objects {
|
||||
stack.push((p, depth + 1));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if !meta.is_file() || allowed(&p) {
|
||||
if !meta.is_file() {
|
||||
continue;
|
||||
}
|
||||
let read = || {
|
||||
(meta.len() <= 1 << 20)
|
||||
.then(|| std::fs::read(&p).ok())
|
||||
.flatten()
|
||||
};
|
||||
if allowed(&p) {
|
||||
// Control's own key files: one key, in control's encrypted armour.
|
||||
if !read().is_some_and(|b| own_armour(&b)) {
|
||||
found.push(p);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let name = e.file_name().to_string_lossy().to_ascii_lowercase();
|
||||
let by_name = name.contains("root") && name.contains("key");
|
||||
let by_content = meta.len() <= 1 << 20
|
||||
&& std::fs::read(&p)
|
||||
.is_ok_and(|b| b.windows(KEY_MARK.len()).any(|w| w == KEY_MARK.as_bytes()));
|
||||
let by_content =
|
||||
read().is_some_and(|b| b.windows(KEY_MARK.len()).any(|w| w == KEY_MARK.as_bytes()));
|
||||
if by_name || by_content {
|
||||
found.push(p);
|
||||
}
|
||||
@@ -275,6 +303,7 @@ pub fn root_key_files(dir: &Path) -> Vec<PathBuf> {
|
||||
|
||||
// Split so that this file does not match the pre-commit hook itself.
|
||||
const KEY_MARK: &str = concat!("PRIVATE ", "KEY-----");
|
||||
const OWN_BEGIN: &str = concat!("-----BEGIN IANITOR ENCRYPTED ", "PRIVATE KEY-----");
|
||||
|
||||
impl Pki {
|
||||
/// Open the CA state. The root certificate must be a CA and self-signed; the password
|
||||
@@ -1182,3 +1211,45 @@ pub fn cli_wait(state_dir: &Path, rid: &str, since: u64) -> Result<String, Strin
|
||||
std::thread::sleep(std::time::Duration::from_millis(250));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod root_key_tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn allowed_paths_hold_only_our_armour_links_are_reported_and_git_objects_are_not_read() {
|
||||
let dir = std::env::temp_dir().join(format!("ianitor-rk-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(dir.join("pki/devices")).unwrap();
|
||||
std::fs::create_dir_all(dir.join("routes.git/objects/ab")).unwrap();
|
||||
let key = pki::generate_key().unwrap();
|
||||
let sealed = pki::encrypt_key_with(&key, b"pw", 1000).unwrap();
|
||||
let plain = key.serialize_pem();
|
||||
std::fs::write(dir.join("pki/intermediate.key"), &sealed).unwrap();
|
||||
std::fs::write(dir.join("pki/devices/d1.key"), &sealed).unwrap();
|
||||
// Git's store is not read (a blob could hold anything, compressed or not).
|
||||
std::fs::write(dir.join("routes.git/objects/ab/cdef"), &plain).unwrap();
|
||||
assert!(
|
||||
root_key_files(&dir).is_empty(),
|
||||
"{:?}",
|
||||
root_key_files(&dir)
|
||||
);
|
||||
// A plain key, or a second block, in an allowed place is not ours.
|
||||
std::fs::write(dir.join("pki/pending-x.key"), &plain).unwrap();
|
||||
std::fs::write(dir.join("pki/devices/d2.key"), format!("{sealed}{plain}")).unwrap();
|
||||
#[cfg(unix)]
|
||||
std::os::unix::fs::symlink("/nonexistent/domi-root.key", dir.join("pki/devices/d3.key"))
|
||||
.unwrap();
|
||||
let found = root_key_files(&dir);
|
||||
assert!(found.contains(&dir.join("pki/pending-x.key")), "{found:?}");
|
||||
assert!(found.contains(&dir.join("pki/devices/d2.key")), "{found:?}");
|
||||
#[cfg(unix)]
|
||||
assert!(found.contains(&dir.join("pki/devices/d3.key")), "{found:?}");
|
||||
assert!(
|
||||
!found.contains(&dir.join("pki/intermediate.key")),
|
||||
"{found:?}"
|
||||
);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user