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:
1 parent
c4850d0764
commit
a02e4f181b
5 files changed
+174
-78
No files matched your search
+87
-60
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user