A: open peer token files only from serve and peer check

`Config::load` now runs structural checks only (names, URLs, CIDRs, paths,
timeouts) and never touches the host, so `token create|list`, `peer list`,
`peer check` and `--print-config` work where the credential is absent, e.g.
outside the unit. `Config::validate_token_readiness` opens the enabled peers'
files and runs at the top of `serve`; `peer check` checks the probed peer.
Also canonicalise IPv4-mapped IPv6 literals against the deny list and stop
putting the host in the plain-http error while the warning omits it.
This commit is contained in:
ari committed 2026-10-19 22:31:29 -04:00
1 parent c4850d0764
commit a02e4f181b
5 files changed
+174 -78

No files matched your search

+10
View File
@@ -318,6 +318,16 @@ impl Config {
Ok(())
}
/// Checks that need the host rather than the config text: the peer token
/// files this process will open.
///
/// `load` runs only the structural checks, so CLI commands that never
/// contact a peer (`token`, `peer list`, `--print-config`) work where the
/// credential is absent; `serve` calls this before it starts anything.
pub fn validate_token_readiness(&self) -> Result<(), ConfigError> {
crate::peers::config::validate_tokens(&self.peers)
}
/// Effective config as TOML, with OTLP header values redacted.
pub fn to_toml_redacted(&self) -> Result<String, toml::ser::Error> {
let mut c = self.clone();
+5
View File
@@ -120,6 +120,11 @@ async fn serve(
config_path: Option<PathBuf>,
overrides: CliOverrides,
) -> Result<()> {
// Peer token files are read here, not in `Config::load`: token and
// diagnostics commands must work without the credential.
config
.validate_token_readiness()
.context("validating peer token files")?;
// Install SIGHUP handling before anything else: the default action would
// terminate the process, with or without TLS.
let hup = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup())
+15 -18
View File
@@ -1,7 +1,8 @@
//! `slinuxm peer list|check`, the read-only diagnostic CLI.
//!
//! Both subcommands work from configuration and the external token files only;
//! they never touch the database, start loops or print a token value.
//! Both subcommands work from configuration and (for `check`) the probed
//! peer's token file only; they never touch the database, start loops or print
//! a token value. `peer list` reads no token file at all.
use std::sync::Arc;
use std::time::Duration;
@@ -10,6 +11,7 @@ use anyhow::{bail, Result};
use clap::Subcommand;
use crate::config::Config;
use crate::peers::config;
use crate::peers::health::check_once;
use crate::peers::transport::PeerTransport;
use crate::peers::types::{ApiStatus, AuthState, PeerConfig, PeersConfig, ServerStatus};
@@ -31,7 +33,9 @@ pub async fn run(cmd: PeerCmd, config: &Config) -> Result<()> {
PeerCmd::Check { name } => {
// Resolve the name before the transport exists, so a typo reads
// as an unknown peer rather than as a missing transport.
configured_peer(&config.peers, &name)?;
let peer = configured_peer(&config.peers, &name)?;
// Only this peer's credential is needed to probe it.
config::validate_peer_token(peer).map_err(|err| anyhow::anyhow!("{err}"))?;
let transport = real_transport()?;
print!(
"{}",
@@ -139,6 +143,8 @@ fn class(error: Option<&str>) -> &str {
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
use crate::peers::error::PeerError;
use crate::peers::testing::{fake_peers_config, FakeTransport};
@@ -151,27 +157,18 @@ mod tests {
#[test]
fn list_is_sorted_and_shows_paths_not_contents() {
let path = std::env::temp_dir().join(format!(
"slinuxm-peer-cli-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.subsec_nanos()
));
std::fs::write(&path, SECRET).unwrap();
// `list_report` takes a config and never opens the file: a path that
// does not exist must still render, and no content can appear.
let missing = PathBuf::from("/run/credentials/skyg-linux-tracker.service/peer-gamma");
let mut cfg = config(&["gamma", "beta"]);
cfg.peer[0].token_file = path.clone();
cfg.peer[0].token_file = missing.clone();
cfg.peer[1].enabled = false;
assert!(!missing.exists(), "fixture must not exist");
let report = list_report(&cfg);
let _ = std::fs::remove_file(&path);
assert!(report.contains("NAME"));
assert!(report.contains("beta"));
assert!(report.contains("gamma"));
assert!(report.contains("http://beta:1984"));
assert!(report.contains(&path.display().to_string()));
assert!(report.contains(&missing.display().to_string()));
assert!(report.find("beta").unwrap() < report.find("gamma").unwrap());
assert!(report.contains("gamma"), "{report}");
assert!(report.contains(" no "), "disabled peer is marked: {report}");
assert!(!report.contains(SECRET));
}
+87 -60
View File
@@ -1,8 +1,10 @@
//! Validation for the `[peers]` section.
//!
//! Structural checks run even when the feature is off, so a bad entry is not
//! silent until someone flips `enabled`. Token files are opened only for
//! peers that would actually be contacted.
//! Structural checks run on every load, even when the feature is off, so a
//! bad entry is not silent until someone flips `enabled`. They never touch
//! the host: token management, `peer list` and `--print-config` must work
//! where the credential is not in place yet. Opening token files is a
//! separate check that only the processes contacting peers run.
use std::collections::BTreeSet;
use std::net::IpAddr;
@@ -13,6 +15,7 @@ use url::Url;
use crate::config::ConfigError;
use crate::peers::types::{PeerConfig, PeersConfig, MAX_PEERS};
/// Structural checks: names, URLs, CIDRs, paths and timeouts. No file reads.
pub fn validate(cfg: &PeersConfig) -> Result<(), ConfigError> {
timeout_below("tcp", cfg.tcp_timeout_secs, cfg.tcp_interval_secs)?;
timeout_below("api", cfg.api_timeout_secs, cfg.api_interval_secs)?;
@@ -39,6 +42,30 @@ pub fn validate(cfg: &PeersConfig) -> Result<(), ConfigError> {
Ok(())
}
/// Opens the token file of every peer this process would contact.
///
/// Run at startup (`serve`) and by `peer check`, never from `Config::load`:
/// a credential installed after the service starts, or a shell that cannot
/// read `$CREDENTIALS_DIRECTORY`, must not block token or config commands.
pub fn validate_tokens(cfg: &PeersConfig) -> Result<(), ConfigError> {
if !cfg.enabled {
return Ok(());
}
cfg.peer
.iter()
.filter(|peer| peer.enabled)
.try_for_each(validate_peer_token)
}
/// Same check for one peer, whether or not the section is enabled.
pub fn validate_peer_token(peer: &PeerConfig) -> Result<(), ConfigError> {
// The value is dropped here; every use reads the file again so rotation
// needs no restart and the config never retains it.
crate::peers::secrets::load_token(peer)
.map(drop)
.map_err(|err| token_file_error(peer, err))
}
fn timeout_below(kind: &str, timeout: u64, interval: u64) -> Result<(), ConfigError> {
if interval == 0 || timeout == 0 || timeout >= interval {
return Err(ConfigError::Invalid(format!(
@@ -79,15 +106,6 @@ fn validate_peer(
peer.name
)));
}
// Only peers this process will call need a usable token. A disabled
// entry can name a credential that is not on disk yet. The value is
// dropped here; later checks read the file again so rotation needs no
// restart and the config never retains it.
if section_enabled && peer.enabled {
if let Err(err) = crate::peers::secrets::load_token(peer) {
return Err(token_file_error(peer, err));
}
}
Ok(())
}
@@ -135,9 +153,12 @@ fn validate_url(
peer.name
)));
}
let host = url.host_str().ok_or_else(|| {
ConfigError::Invalid(format!("peer {}: url is missing a host", peer.name))
})?;
if url.host_str().is_none() {
return Err(ConfigError::Invalid(format!(
"peer {}: url is missing a host",
peer.name
)));
}
// A name is not an address yet, so it cannot be matched here.
if let Some(ip) = host_ip(url) {
if deny.iter().any(|net| net.contains(&ip)) {
@@ -150,7 +171,7 @@ fn validate_url(
if url.scheme() == "http" && !is_loopback(url) {
if !peer.allow_plain_http {
return Err(ConfigError::Invalid(format!(
"peer {}: plain http to non-loopback host {host:?} requires allow_plain_http = true",
"peer {}: plain http to a non-loopback host requires allow_plain_http = true",
peer.name
)));
}
@@ -165,9 +186,11 @@ fn validate_url(
}
fn host_ip(url: &Url) -> Option<IpAddr> {
// An IPv4-mapped IPv6 literal (`[::ffff:169.254.169.254]`) must be
// matched against the IPv4 deny list, so canonicalise before comparing.
match url.host()? {
url::Host::Ipv4(ip) => Some(IpAddr::V4(ip)),
url::Host::Ipv6(ip) => Some(IpAddr::V6(ip)),
url::Host::Ipv6(ip) => Some(IpAddr::V6(ip).to_canonical()),
url::Host::Domain(_) => None,
}
}
@@ -176,7 +199,7 @@ fn is_loopback(url: &Url) -> bool {
match url.host() {
Some(url::Host::Domain(host)) => host.eq_ignore_ascii_case("localhost"),
Some(url::Host::Ipv4(ip)) => ip.is_loopback(),
Some(url::Host::Ipv6(ip)) => ip.is_loopback(),
Some(url::Host::Ipv6(ip)) => IpAddr::V6(ip).to_canonical().is_loopback(),
None => false,
}
}
@@ -186,7 +209,7 @@ mod tests {
use std::path::PathBuf;
use super::*;
use crate::peers::testing::capture::logged;
use crate::peers::testing::{capture::logged, TempDir};
use crate::peers::types::PeerName;
fn peer(name: &str, url: &str) -> PeerConfig {
@@ -312,7 +335,7 @@ mod tests {
assert!(err.contains("allow_plain_http"));
assert!(!err.contains("token"));
let dir = TempDir::new();
let dir = TempDir::new("peer-cfg");
let mut allowed = peer("beta", "http://10.1.2.3:1984");
allowed.allow_plain_http = true;
allowed.token_file = dir.write_token("pending\n");
@@ -348,29 +371,63 @@ mod tests {
}
#[test]
fn token_file_must_be_absolute_and_readable_only_when_used() {
fn ipv4_mapped_ipv6_literals_match_the_v4_deny_list() {
let mut beta = peer("beta", "http://[::ffff:169.254.169.254]:80");
beta.allow_plain_http = true;
let cfg = PeersConfig {
peer: vec![beta],
..PeersConfig::default()
};
let err = message(validate(&cfg).unwrap_err());
assert!(err.contains("deny_cidrs"), "{err}");
// Same for loopback: a mapped literal is loopback, so plain http is
// allowed without the flag.
let cfg = disabled_section(vec![peer("beta", "http://[::ffff:127.0.0.1]:1984")]);
validate(&cfg).unwrap();
}
#[test]
fn token_file_must_be_absolute_but_is_not_opened_by_structural_checks() {
let mut relative = peer("beta", "https://beta:1984");
relative.token_file = PathBuf::from("peer-beta");
let err = message(validate(&disabled_section(vec![relative])).unwrap_err());
assert!(err.contains("beta"));
assert!(err.contains("absolute"));
// A missing credential is not a config error: token management,
// `peer list` and `--print-config` load this config successfully.
let cfg = PeersConfig {
enabled: true,
peer: vec![peer("beta", "https://beta:1984")],
..PeersConfig::default()
};
validate(&cfg).unwrap();
}
#[test]
fn token_readiness_is_checked_only_for_peers_that_are_contacted() {
let mut cfg = PeersConfig {
enabled: true,
peer: vec![peer("beta", "https://beta:1984")],
..PeersConfig::default()
};
let err = message(validate(&cfg).unwrap_err());
assert!(err.contains("peer beta"));
assert!(err.contains("not readable"));
let err = message(validate_tokens(&cfg).unwrap_err());
assert!(err.contains("peer beta"), "{err}");
assert!(err.contains("not readable"), "{err}");
cfg.peer[0].enabled = false;
validate(&cfg).unwrap();
validate_tokens(&cfg).unwrap();
let dir = TempDir::new();
cfg.enabled = false;
cfg.peer[0].enabled = true;
validate_tokens(&cfg).unwrap();
let dir = TempDir::new("peer-cfg");
cfg.enabled = true;
cfg.peer[0].token_file = dir.write_token("pending\n");
validate(&cfg).unwrap();
validate_tokens(&cfg).unwrap();
validate_peer_token(&cfg.peer[0]).unwrap();
}
#[test]
@@ -383,8 +440,8 @@ mod tests {
}
#[test]
fn enabled_peer_rejects_an_empty_token_file() {
let dir = TempDir::new();
fn token_readiness_rejects_an_empty_token_file() {
let dir = TempDir::new("peer-cfg");
let mut beta = peer("beta", "http://127.0.0.1:1984");
beta.token_file = dir.write_token("");
let cfg = PeersConfig {
@@ -392,7 +449,7 @@ mod tests {
peer: vec![beta],
..PeersConfig::default()
};
let err = message(validate(&cfg).unwrap_err());
let err = message(validate_tokens(&cfg).unwrap_err());
assert!(err.contains("peer beta"), "{err}");
assert!(err.contains("empty"), "{err}");
}
@@ -413,34 +470,4 @@ mod tests {
assert!(is_loopback(&Url::parse("http://127.0.0.2:1984").unwrap()));
assert!(!is_loopback(&Url::parse("http://10.0.0.1:1984").unwrap()));
}
/// Removes the directory on drop, including when an assertion panics.
struct TempDir(PathBuf);
impl TempDir {
fn new() -> Self {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.subsec_nanos();
let path = std::env::temp_dir()
.join(format!("slinuxm-peer-cfg-{}-{nanos}", std::process::id()));
std::fs::create_dir(&path).unwrap();
Self(path)
}
fn write_token(&self, contents: &str) -> PathBuf {
let path = self.0.join("token");
std::fs::write(&path, contents).unwrap();
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
path
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
}
+57
View File
@@ -332,6 +332,63 @@ pub fn fake_state_with_peers(config: Config, reg: Arc<PeerRegistry>) -> AppState
}
}
/// Scratch directory removed on drop, unique per call even across threads.
pub struct TempDir {
path: PathBuf,
}
impl TempDir {
pub fn new(prefix: &str) -> Self {
use std::sync::atomic::{AtomicU64, Ordering};
static SEQ: AtomicU64 = AtomicU64::new(0);
loop {
let seq = SEQ.fetch_add(1, Ordering::Relaxed);
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.subsec_nanos())
.unwrap_or(0);
let path = std::env::temp_dir().join(format!(
"slinuxm-{prefix}-{}-{seq}-{nanos}",
std::process::id()
));
match std::fs::create_dir(&path) {
Ok(()) => return Self { path },
// Another process reused the name; take the next one.
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(err) => panic!("creating {}: {err}", path.display()),
}
}
}
pub fn path(&self) -> &std::path::Path {
&self.path
}
/// Write `name` (parents created) and set its mode.
pub fn write(&self, name: &str, contents: &[u8], mode: u32) -> PathBuf {
use std::os::unix::fs::PermissionsExt;
let path = self.path.join(name);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).expect("creating test dirs");
}
std::fs::write(&path, contents).expect("writing test file");
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(mode))
.expect("setting test file mode");
path
}
/// A private (0600) token file named `token`.
pub fn write_token(&self, contents: &str) -> PathBuf {
self.write("token", contents.as_bytes(), 0o600)
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.path);
}
}
#[cfg(test)]
mod tests {
use super::*;