Add brokerd's configuration
Implemented crates/brokerd/src/config.rs: typed Paths, Sockets, Approvals and Config with serde(deny_unknown_fields, default) on every struct, hand-written ConfigError (Read/Parse) with Display and std::error::Error, and the parse/load/ broker_socket/admin_socket/audit_dir/state_dir methods. Added serde, serde_json and toml to crates/brokerd/Cargo.toml, registered pub mod config; in lib.rs, added brokerd to the serde and serde_json Used-by cells in docs/dependencies.md, and copied the given test and six fixtures byte-identical. 7 config tests pass; make gate prints gate: ok. Implemented-By: OpenCode session (model recorded in docs/implementer-log.md)
This commit is contained in:
@@ -10,3 +10,6 @@ workspace = true
|
||||
|
||||
[dependencies]
|
||||
proto.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
toml.workspace = true
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
//! `brokerd` configuration: read `brokerd.toml` into a typed `Config`.
|
||||
//!
|
||||
//! This is our own format, so unknown keys are errors in every table: a
|
||||
//! misspelt key that silently fell back to its default would be a setting the
|
||||
//! owner believes is set and is not.
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||
#[serde(deny_unknown_fields, default)]
|
||||
pub struct Paths {
|
||||
pub home: PathBuf,
|
||||
pub grants: PathBuf,
|
||||
}
|
||||
|
||||
impl Default for Paths {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
home: std::env::var_os("BOXMAKER_HOME")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| PathBuf::from("/var/lib/boxmaker")),
|
||||
grants: PathBuf::from("/etc/boxmaker/grants"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An empty path means "the default under `home`".
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Default)]
|
||||
#[serde(deny_unknown_fields, default)]
|
||||
pub struct Sockets {
|
||||
pub broker: PathBuf,
|
||||
pub admin: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||
#[serde(deny_unknown_fields, default)]
|
||||
pub struct Approvals {
|
||||
pub ttl_ms: u64,
|
||||
}
|
||||
|
||||
impl Default for Approvals {
|
||||
fn default() -> Self {
|
||||
Self { ttl_ms: 900_000 }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Default)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Config {
|
||||
#[serde(default)]
|
||||
pub paths: Paths,
|
||||
#[serde(default)]
|
||||
pub sockets: Sockets,
|
||||
#[serde(default)]
|
||||
pub approvals: Approvals,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ConfigError {
|
||||
Read(PathBuf, std::io::Error),
|
||||
Parse(PathBuf, toml::de::Error),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ConfigError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
ConfigError::Read(path, err) => write!(f, "{}: {err}", path.display()),
|
||||
ConfigError::Parse(path, err) => write!(f, "{}: {err}", path.display()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ConfigError {}
|
||||
|
||||
impl Config {
|
||||
pub fn parse(text: &str) -> Result<Config, toml::de::Error> {
|
||||
toml::from_str(text)
|
||||
}
|
||||
pub fn load(path: &Path) -> Result<Config, ConfigError> {
|
||||
let text =
|
||||
std::fs::read_to_string(path).map_err(|e| ConfigError::Read(path.to_path_buf(), e))?;
|
||||
let config: Config =
|
||||
toml::from_str(&text).map_err(|e| ConfigError::Parse(path.to_path_buf(), e))?;
|
||||
Ok(config)
|
||||
}
|
||||
pub fn broker_socket(&self) -> PathBuf {
|
||||
if self.sockets.broker.as_os_str().is_empty() {
|
||||
self.paths.home.join("run/loop-broker/broker.sock")
|
||||
} else {
|
||||
self.sockets.broker.clone()
|
||||
}
|
||||
}
|
||||
pub fn admin_socket(&self) -> PathBuf {
|
||||
if self.sockets.admin.as_os_str().is_empty() {
|
||||
self.paths.home.join("run/owner-broker/admin.sock")
|
||||
} else {
|
||||
self.sockets.admin.clone()
|
||||
}
|
||||
}
|
||||
pub fn audit_dir(&self) -> PathBuf {
|
||||
self.paths.home.join("audit")
|
||||
}
|
||||
pub fn state_dir(&self) -> PathBuf {
|
||||
self.paths.home.join("broker/sessions")
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
//! The broker: the only role that holds authority.
|
||||
|
||||
pub mod config;
|
||||
pub mod policy;
|
||||
pub mod runner;
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
//! Tests for `brokerd`'s configuration. Do not edit: these define the required behaviour.
|
||||
|
||||
use brokerd::config::{Approvals, Config, ConfigError, Sockets};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn fixture(name: &str) -> PathBuf {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/fixtures/config")
|
||||
.join(name)
|
||||
}
|
||||
|
||||
/// What `home` must default to in this process. The test does not set the variable: changing the
|
||||
/// environment of a running test binary would race with the other tests.
|
||||
fn default_home() -> PathBuf {
|
||||
std::env::var_os("BOXMAKER_HOME")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| PathBuf::from("/var/lib/boxmaker"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_file_gets_every_default() {
|
||||
let c = Config::load(&fixture("empty.toml")).unwrap();
|
||||
assert_eq!(c.paths.home, default_home());
|
||||
assert_eq!(c.paths.grants, PathBuf::from("/etc/boxmaker/grants"));
|
||||
assert_eq!(c.sockets, Sockets::default());
|
||||
assert_eq!(c.approvals, Approvals { ttl_ms: 900_000 });
|
||||
assert_eq!(Approvals::default(), Approvals { ttl_ms: 900_000 });
|
||||
assert_eq!(c, Config::parse("").unwrap());
|
||||
assert_eq!(c, Config::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sockets_and_directories_default_to_places_under_home() {
|
||||
let c = Config::load(&fixture("home_only.toml")).unwrap();
|
||||
assert_eq!(c.paths.home, PathBuf::from("/srv/boxmaker"));
|
||||
assert_eq!(
|
||||
c.broker_socket(),
|
||||
PathBuf::from("/srv/boxmaker/run/loop-broker/broker.sock")
|
||||
);
|
||||
assert_eq!(
|
||||
c.admin_socket(),
|
||||
PathBuf::from("/srv/boxmaker/run/owner-broker/admin.sock")
|
||||
);
|
||||
assert_eq!(c.audit_dir(), PathBuf::from("/srv/boxmaker/audit"));
|
||||
assert_eq!(
|
||||
c.state_dir(),
|
||||
PathBuf::from("/srv/boxmaker/broker/sessions")
|
||||
);
|
||||
// The grants are not under home: the owner writes them, brokerd only reads them.
|
||||
assert_eq!(c.paths.grants, PathBuf::from("/etc/boxmaker/grants"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_key_can_be_set() {
|
||||
let c = Config::load(&fixture("full.toml")).unwrap();
|
||||
assert_eq!(c.paths.home, PathBuf::from("/srv/boxmaker"));
|
||||
assert_eq!(c.paths.grants, PathBuf::from("/srv/boxmaker-grants"));
|
||||
assert_eq!(c.broker_socket(), PathBuf::from("/run/bx/broker.sock"));
|
||||
assert_eq!(c.admin_socket(), PathBuf::from("/run/bx/admin.sock"));
|
||||
assert_eq!(c.approvals.ttl_ms, 60_000);
|
||||
// The two directories always follow home.
|
||||
assert_eq!(c.audit_dir(), PathBuf::from("/srv/boxmaker/audit"));
|
||||
assert_eq!(
|
||||
c.state_dir(),
|
||||
PathBuf::from("/srv/boxmaker/broker/sessions")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_socket_set_leaves_the_other_at_its_default() {
|
||||
let c =
|
||||
Config::parse("[paths]\nhome = \"/h\"\n[sockets]\nadmin = \"/x/admin.sock\"\n").unwrap();
|
||||
assert_eq!(c.admin_socket(), PathBuf::from("/x/admin.sock"));
|
||||
assert_eq!(
|
||||
c.broker_socket(),
|
||||
PathBuf::from("/h/run/loop-broker/broker.sock")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_keys_and_tables_are_errors() {
|
||||
for name in ["unknown_key.toml", "unknown_table.toml", "wrong_type.toml"] {
|
||||
match Config::load(&fixture(name)) {
|
||||
Err(ConfigError::Parse(path, _)) => assert_eq!(path, fixture(name)),
|
||||
other => panic!("{name}: expected a parse error, got {other:?}"),
|
||||
}
|
||||
}
|
||||
// In every table, not only the one the fixture shows.
|
||||
for text in [
|
||||
"[paths]\nhome = \"/h\"\nhouse = \"/h\"\n",
|
||||
"[sockets]\nbroker = \"/b.sock\"\nloop = \"/l.sock\"\n",
|
||||
"[approvals]\nttl_ms = 1\nttl_s = 1\n",
|
||||
"top = 1\n",
|
||||
"[approvals]\nttl_ms = -5\n",
|
||||
] {
|
||||
assert!(Config::parse(text).is_err(), "accepted: {text}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_file_is_a_read_error_that_names_the_file() {
|
||||
let path = fixture("does-not-exist.toml");
|
||||
match Config::load(&path) {
|
||||
Err(ConfigError::Read(p, _)) => assert_eq!(p, path),
|
||||
other => panic!("expected a read error, got {other:?}"),
|
||||
}
|
||||
let text = Config::load(&path).unwrap_err().to_string();
|
||||
assert!(text.contains("does-not-exist.toml"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_parse_error_names_the_file_and_the_key() {
|
||||
let text = Config::load(&fixture("unknown_key.toml"))
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert!(text.contains("unknown_key.toml"), "{text}");
|
||||
assert!(text.contains("ttl"), "{text}");
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
# Nothing set: every value is a default.
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
# Every key set.
|
||||
[paths]
|
||||
home = "/srv/boxmaker"
|
||||
grants = "/srv/boxmaker-grants"
|
||||
|
||||
[sockets]
|
||||
broker = "/run/bx/broker.sock"
|
||||
admin = "/run/bx/admin.sock"
|
||||
|
||||
[approvals]
|
||||
ttl_ms = 60000
|
||||
@@ -0,0 +1,2 @@
|
||||
[paths]
|
||||
home = "/srv/boxmaker"
|
||||
@@ -0,0 +1,3 @@
|
||||
[approvals]
|
||||
ttl_ms = 60000
|
||||
ttl = 5
|
||||
@@ -0,0 +1,2 @@
|
||||
[secrets]
|
||||
store = "/etc/boxmaker/secrets"
|
||||
@@ -0,0 +1,2 @@
|
||||
[approvals]
|
||||
ttl_ms = "15 min"
|
||||
Reference in New Issue
Block a user