use std::path::PathBuf;
pub use quietquic_proto::config::{ClientConfigFile, ClientEntry, ConfigError, Psk, ServerSecrets};
pub trait SecretSource {
fn load(&self) -> Result<ServerSecrets, ConfigError>;
}
pub struct FileSource {
path: PathBuf,
}
impl FileSource {
pub fn new(path: impl Into<PathBuf>) -> Self {
Self { path: path.into() }
}
}
impl SecretSource for FileSource {
fn load(&self) -> Result<ServerSecrets, ConfigError> {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Ok(meta) = std::fs::metadata(&self.path) {
let mode = meta.permissions().mode() & 0o777;
if mode & 0o077 != 0 {
tracing::warn!(
path = ?self.path,
mode = format!("{:o}", mode),
"secret file is group/world accessible; recommend chmod 600"
);
}
}
}
let text = std::fs::read_to_string(&self.path)?;
Ok(toml::from_str(&text)?)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
#[test]
fn file_source_loads() {
let dir = std::env::temp_dir().join(format!("sq-test-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("keys.toml");
let mut f = std::fs::File::create(&path).unwrap();
f.write_all(
br#"
listen = "127.0.0.1:4443"
[[clients]]
client_id = "a"
psk = "0000000000000000000000000000000000000000000000000000000000000002"
"#,
)
.unwrap();
let secrets = FileSource::new(&path).load().unwrap();
assert_eq!(secrets.clients[0].psk.as_bytes()[31], 2);
}
}