A: A1 peers config section, env overrides and print-config redaction
This commit is contained in:
1 parent
12c3a40708
commit
40d18c2253
3 files changed
+211
No files matched your search
@@ -2,7 +2,9 @@
|
||||
//! sources: the peer-less default, the ping route, and registry reads.
|
||||
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
use std::time::Duration;
|
||||
|
||||
use axum::body::{to_bytes, Body};
|
||||
@@ -41,6 +43,103 @@ fn status(name: &str, up: bool) -> PeerStatus {
|
||||
}
|
||||
}
|
||||
|
||||
const SECRET: &str = "slinuxm_PEER_TOKEN_SHOULD_NOT_LEAK";
|
||||
|
||||
struct TempDir(PathBuf);
|
||||
|
||||
impl TempDir {
|
||||
fn new() -> Self {
|
||||
let nanos = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.subsec_nanos();
|
||||
let dir =
|
||||
std::env::temp_dir().join(format!("slinuxm-peers-{}-{nanos}", std::process::id()));
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
Self(dir)
|
||||
}
|
||||
|
||||
fn path(&self) -> &Path {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TempDir {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn write_private_token(path: &Path, contents: &str) {
|
||||
std::fs::write(path, contents).unwrap();
|
||||
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600)).unwrap();
|
||||
}
|
||||
|
||||
fn slinuxm(config: &Path, args: &[&str]) -> std::process::Output {
|
||||
Command::new(env!("CARGO_BIN_EXE_slinuxm"))
|
||||
.arg("--config")
|
||||
.arg(config)
|
||||
.args(args)
|
||||
.env_clear()
|
||||
.env("PATH", std::env::var_os("PATH").unwrap_or_default())
|
||||
.output()
|
||||
.expect("run slinuxm")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn print_config_shows_peer_paths_and_not_token_contents() {
|
||||
let dir = TempDir::new();
|
||||
let token = dir.path().join("peer.token");
|
||||
write_private_token(&token, SECRET);
|
||||
let cfg = dir.path().join("config.toml");
|
||||
std::fs::write(
|
||||
&cfg,
|
||||
format!(
|
||||
"\
|
||||
[peers]\n\
|
||||
enabled = true\n\
|
||||
tcp_interval_secs = 20\n\
|
||||
tcp_timeout_secs = 4\n\
|
||||
\n\
|
||||
[[peers.peer]]\n\
|
||||
name = \"beta\"\n\
|
||||
url = \"http://127.0.0.1:1984\"\n\
|
||||
token_file = \"{}\"\n\
|
||||
",
|
||||
token.display()
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let out = slinuxm(&cfg, &["--print-config"]);
|
||||
let stdout = String::from_utf8(out.stdout).unwrap();
|
||||
let stderr = String::from_utf8(out.stderr).unwrap();
|
||||
assert!(
|
||||
out.status.success(),
|
||||
"print-config failed: {stderr}\n{stdout}"
|
||||
);
|
||||
assert!(stdout.contains("[peers]"));
|
||||
assert!(stdout.contains("enabled = true"));
|
||||
assert!(stdout.contains(&token.display().to_string()));
|
||||
assert!(!stdout.contains("SHOULD_NOT_LEAK"), "{stdout}");
|
||||
assert!(!stderr.contains("SHOULD_NOT_LEAK"), "{stderr}");
|
||||
|
||||
let bare = dir.path().join("empty.toml");
|
||||
std::fs::write(&bare, "").unwrap();
|
||||
let out = slinuxm(&bare, &["--print-config"]);
|
||||
let stdout = String::from_utf8(out.stdout).unwrap();
|
||||
assert!(
|
||||
out.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&out.stderr)
|
||||
);
|
||||
assert!(stdout.contains("[peers]\n"));
|
||||
assert!(stdout.contains("enabled = false\n"));
|
||||
assert!(stdout.contains("port = 1984\n"));
|
||||
assert!(stdout.contains("peer = []\n"));
|
||||
assert!(!stdout.contains("SHOULD_NOT_LEAK"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn peerless_default_is_off_and_unchanged() {
|
||||
let config = Config::default();
|
||||
|
||||
Reference in new issue
Block a user