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

+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));
}