A: A3 secrets loader reads token files with redaction and permission checks

This commit is contained in:
ari committed 2026-10-19 22:31:29 -04:00
1 parent bcac6c76e6
commit 7d10153677
4 files changed
+390 -18

No files matched your search

+35 -18
View File
@@ -79,30 +79,30 @@ fn validate_peer(
peer.name
)));
}
// Only peers this process will call need a readable file. A disabled
// entry can name a credential that is not on disk yet.
// 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 {
match std::fs::metadata(&peer.token_file) {
Ok(meta) if meta.is_file() => {}
Ok(_) => {
return Err(ConfigError::Invalid(format!(
"peer {}: token_file {} is not a file",
peer.name,
peer.token_file.display()
)))
}
Err(err) => {
return Err(ConfigError::Invalid(format!(
"peer {}: token_file {} is not readable: {err}",
peer.name,
peer.token_file.display()
)))
}
if let Err(err) = crate::peers::secrets::load_token(peer) {
return Err(token_file_error(peer, err));
}
}
Ok(())
}
fn token_file_error(peer: &PeerConfig, err: crate::peers::PeerError) -> ConfigError {
let reason = match err {
crate::peers::PeerError::TokenFile(reason) => reason,
_ => "could not be read".into(),
};
ConfigError::Invalid(format!(
"peer {}: token_file {}: {reason}",
peer.name,
peer.token_file.display()
))
}
fn validate_url(
section_enabled: bool,
peer: &PeerConfig,
@@ -414,6 +414,21 @@ mod tests {
assert!(err.contains("beta"));
}
#[test]
fn enabled_peer_rejects_an_empty_token_file() {
let dir = TempDir::new();
let mut beta = peer("beta", "http://127.0.0.1:1984");
beta.token_file = dir.write_token("");
let cfg = PeersConfig {
enabled: true,
peer: vec![beta],
..PeersConfig::default()
};
let err = message(validate(&cfg).unwrap_err());
assert!(err.contains("peer beta"), "{err}");
assert!(err.contains("empty"), "{err}");
}
#[test]
fn enabled_with_no_peers_warns_and_is_not_an_error() {
let cfg = PeersConfig {
@@ -449,6 +464,8 @@ mod tests {
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
}
}