From 7fc844ff0167fb0943e3fa85af671d8f3bbf005d Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 17 Nov 2026 16:18:58 +0000 Subject: continuum-party: options from configuration files, as the node's `continuum-party` takes its options (`node`, `token`, `key`, `record`: `config::PARTY_OPTIONS`) on the command line and in configuration files of the node's format: `--config`, repeatable, arguments winning, `--unset`, includes and imports. The configuration's loader takes the options it reads as a table (`config::with_options`, `config::merged`, `file::load_with`), the node's or the client's, and a key naming none of them is an error in either; `node` and `token` are secrets, never logged. --- src/federation/party.rs | 114 +++++++++++++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 44 deletions(-) (limited to 'src/federation') diff --git a/src/federation/party.rs b/src/federation/party.rs index aaa983f..f0ae288 100644 --- a/src/federation/party.rs +++ b/src/federation/party.rs @@ -2,7 +2,8 @@ //! party's events with one of its keys (an OpenSSH private key file, `--key`), puts them //! through a node's API (`--node`, its user and password in the URL, or `--token`), and //! keeps its party's record (`--record`, `client::Record`), by which it votes, written -//! before a vote is sent. +//! before a vote is sent. Its options may be set in configuration files as the node's are +//! (`--config`, `config::PARTY_OPTIONS`): the same format, imports and all. //! //! * `init PARTY`: begins a record of `PARTY`, on the version current now; with `--root //! FILE`, from the file the store's swarm starts from, for a party that never voted in @@ -34,6 +35,7 @@ use std::path::{Path, PathBuf}; use anyhow::{Context, Result, anyhow, bail}; use clap::{Arg, ArgAction, ArgMatches, Command}; use ed25519_dalek::SigningKey; +use serde::Deserialize; use serde_json::Value as JsonValue; use super::client::{self, Record}; @@ -44,38 +46,29 @@ use super::store::Id; use super::store::memory::Store; use super::version::Version; use crate::access::sshsig::PublicKey; +use crate::config; + +/// The client's options (`config::PARTY_OPTIONS`), as the command line and configuration +/// files set them. +#[derive(Deserialize)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +struct Options { + #[serde(default = "default_node")] + node: String, + token: Option, + key: Option, + record: Option, +} + +fn default_node() -> String { + "http://localhost:3000".to_owned() +} fn command() -> Command { let id = |name: &'static str| Arg::new(name).required(true); - Command::new("continuum-party") - .about("A party's client of a federation: signs its events, and votes by its record") - .arg( - Arg::new("node") - .long("node") - .value_name("URL") - .default_value("http://localhost:3000") - .help( - "The node whose API it puts events through; its user and password in the URL", - ), - ) - .arg( - Arg::new("token") - .long("token") - .value_name("TOKEN") - .help("A token the node issued, instead of a password"), - ) - .arg( - Arg::new("key") - .long("key") - .value_name("FILE") - .help("The party's key it signs with: an OpenSSH private key file, unencrypted"), - ) - .arg( - Arg::new("record") - .long("record") - .value_name("FILE") - .help("The party's record of its votes, written before each vote is sent"), - ) + let command = Command::new("continuum-party") + .about("A party's client of a federation: signs its events, and votes by its record"); + config::with_options(command, config::PARTY_OPTIONS) .subcommand_required(true) .subcommand( Command::new("init") @@ -158,6 +151,8 @@ fn command() -> Command { pub(crate) async fn main() -> Result<()> { let matches = command().get_matches(); let (name, sub) = matches.subcommand().expect("a subcommand is required"); + let (merged, _) = config::merged(&matches, config::PARTY_OPTIONS)?; + let options: Options = config::read(&merged)?; if name == "twice" { let records = sub .get_many::("records") @@ -184,7 +179,7 @@ pub(crate) async fn main() -> Result<()> { } return out(&super::fork::root_file(&root, &[])); } - let node = Node::new(&matches)?; + let node = Node::new(&options)?; if name == "proposals" { return proposals(&node).await; } @@ -232,8 +227,9 @@ pub(crate) async fn main() -> Result<()> { return Ok(()); } let path = PathBuf::from( - matches - .get_one::("record") + options + .record + .as_ref() .context("--record names the party's record")?, ); if name == "init" { @@ -260,7 +256,7 @@ pub(crate) async fn main() -> Result<()> { return Ok(()); } let mut record = Record::load(&path).map_err(|why| anyhow!(why))?; - let key = signing_key(&matches)?; + let key = signing_key(&options)?; let signer = Signer { key, party: record.party.clone(), @@ -345,9 +341,10 @@ fn strings(matches: &ArgMatches, name: &str) -> Vec { .collect() } -fn signing_key(matches: &ArgMatches) -> Result { - let file = matches - .get_one::("key") +fn signing_key(options: &Options) -> Result { + let file = options + .key + .as_ref() .context("--key names the party's key")?; let text = std::fs::read_to_string(file).with_context(|| format!("Reading {file}"))?; client::signing_key(&text).map_err(|why| anyhow!("{file}: {why}")) @@ -544,13 +541,9 @@ struct Node { } impl Node { - fn new(matches: &ArgMatches) -> Result { + fn new(options: &Options) -> Result { let _ = rustls::crypto::ring::default_provider().install_default(); - let mut url: reqwest::Url = matches - .get_one::("node") - .expect("defaulted") - .parse() - .context("--node isn't a URL")?; + let mut url: reqwest::Url = options.node.parse().context("--node isn't a URL")?; let password = (!url.username().is_empty()) .then(|| (url.username().to_owned(), url.password().map(str::to_owned))); let _ = url.set_username(""); @@ -559,7 +552,7 @@ impl Node { url, http: reqwest::Client::new(), password, - token: matches.get_one::("token").cloned(), + token: options.token.clone(), }) } @@ -677,4 +670,37 @@ mod tests { fn its_arguments_are_well_formed() { super::command().debug_assert(); } + + /// Its options come from configuration files as the node's do, arguments winning. + #[test] + fn its_options_come_from_configuration_files_and_arguments() { + use crate::config; + let dir = crate::files::scratch_dir("party-config"); + let file = dir.join("party.toml"); + std::fs::write( + &file, + "node = \"http://tv:secret@localhost:3001\"\nkey = \"tv.key\"\nrecord = \"tv.record\"\n", + ) + .unwrap(); + let path = file.to_str().unwrap(); + let matches = super::command() + .try_get_matches_from([ + "continuum-party", + "--config", + path, + "--key", + "laptop.key", + "proposals", + ]) + .unwrap(); + let (merged, _) = config::merged(&matches, config::PARTY_OPTIONS).unwrap(); + let options: super::Options = config::read(&merged).unwrap(); + assert_eq!(options.node, "http://tv:secret@localhost:3001"); + assert_eq!(options.key.as_deref(), Some("laptop.key")); + assert_eq!(options.record.as_deref(), Some("tv.record")); + assert_eq!(options.token, None); + // A key naming none of the client's options is an error, as in the node's files. + std::fs::write(&file, "bind-addr = \"127.0.0.1:3000\"\n").unwrap(); + assert!(config::merged(&matches, config::PARTY_OPTIONS).is_err()); + } } -- cgit v1.3.1