From 479246f98be360b5943458bd58d07e6d46f81fd0 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 28 Nov 2026 22:11:12 +0000 Subject: config: what a configuration imports by, in a crate of its own MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Importers and placeholders (env:, file:, cmd:, ${…}, +json, $$, the tables) move from config::import to the crate config, crates/config, as its module import, as they were, public: the crate is for what a program's configuration is made of, from its files and its arguments, and other programs make theirs with it. The repository is a workspace of the node and that crate, both default members, so that cargo quick, cargo sim and clippy take in both. --- Cargo.lock | 9 + Cargo.toml | 9 + crates/config/Cargo.toml | 14 ++ crates/config/src/import.rs | 478 +++++++++++++++++++++++++++++++++++++++++++ crates/config/src/lib.rs | 6 + docs/architecture.md | 2 +- src/config/import.rs | 480 -------------------------------------------- src/config/mod.rs | 3 +- 8 files changed, 518 insertions(+), 483 deletions(-) create mode 100644 crates/config/Cargo.toml create mode 100644 crates/config/src/import.rs create mode 100644 crates/config/src/lib.rs delete mode 100644 src/config/import.rs diff --git a/Cargo.lock b/Cargo.lock index 94691ea..b567587 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -931,6 +931,14 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "config" +version = "0.1.0" +dependencies = [ + "anyhow", + "serde_json", +] + [[package]] name = "console-api" version = "0.9.0" @@ -1019,6 +1027,7 @@ dependencies = [ "blake3", "bytes", "clap", + "config", "console-subscriber", "dashmap", "ed25519-dalek", diff --git a/Cargo.toml b/Cargo.toml index e06ea1a..4ec2cbc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,9 @@ +# The node, and the libraries it is made with that other programs use too. Tests, +# checks and builds take them all in unless told otherwise. +[workspace] +members = ["crates/config"] +default-members = [".", "crates/config"] + [package] name = "continuum" version = "0.0.1" @@ -41,6 +47,9 @@ tor = [ most = ["dashboard", "federation", "libp2p", "mycelium", "tor"] [dependencies] +# What a configuration is made of, from files and arguments: a crate of its own, for +# other programs to make theirs alike. +config = { path = "crates/config" } anyhow = "1.0" # The tor feature: arti's crates are released together, at the same version. arti-client = { version = "0.48", optional = true, features = ["onion-service-client", "onion-service-service", "static-sqlite"] } diff --git a/crates/config/Cargo.toml b/crates/config/Cargo.toml new file mode 100644 index 0000000..090ee26 --- /dev/null +++ b/crates/config/Cargo.toml @@ -0,0 +1,14 @@ +# What a configuration is made of, from files and arguments (`src/lib.rs`): what +# continuum's is made with, as a library for other programs to make theirs alike. +[package] +name = "config" +version = "0.1.0" +edition = "2024" + +# Its doc comments are prose, not examples to test. +[lib] +doctest = false + +[dependencies] +anyhow = "1.0" +serde_json = "1.0" diff --git a/crates/config/src/import.rs b/crates/config/src/import.rs new file mode 100644 index 0000000..886caf2 --- /dev/null +++ b/crates/config/src/import.rs @@ -0,0 +1,478 @@ +//! Importers: values taken from elsewhere instead of written out, so a configuration +//! can say where a value comes from without holding it (secrets, host facts): as +//! continuum's does (its `docs/configuration.md`, Importing values). +//! +//! * `env:NAME`: an environment variable; +//! * `file:PATH`: a file's contents; +//! * `cmd:COMMAND`: the standard output of a shell command (`sh -c`). +//! +//! The content is text (trailing line breaks removed), converted like a value written +//! on the command line; with `+json` (`env+json:NAME`, …) it is parsed as JSON instead. +//! +//! On the command line, a value with one of these prefixes is imported; `literal:` +//! passes the rest as is. In configuration files, an importer is a table: +//! `{ env = "NAME" }`, `{ file = "PATH" }`, `{ command = "COMMAND" }` (or an argument +//! list, run without a shell), each with an optional `format = "json"`. +//! +//! Any other text value may hold placeholders, `${env:NAME}`, `${file:PATH}` or +//! `${cmd:COMMAND}`, each replaced by its content as text (`$$` is a `$`; a placeholder +//! ends at the first `}`), e.g. `node-${env:HOSTNAME}`. Relative paths are relative to +//! the file, and commands run in its directory, in placeholders too. + +use std::path::{Path, PathBuf}; +use std::process::Command; + +use anyhow::{Context, Result, anyhow, bail}; +use serde_json::{Map as JsonMap, Value as JsonValue}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Format { + Text, + Json, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Source { + Env(String), + File(PathBuf), + Shell { + command: String, + dir: Option, + }, + Argv { + argv: Vec, + dir: Option, + }, + /// Text with placeholders (`interpolated`). + Template { + template: String, + dir: Option, + }, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Importer { + pub source: Source, + pub format: Format, +} + +/// What a command-line value is: imported, or literal. +#[derive(Debug, PartialEq, Eq)] +pub enum Raw<'a> { + Import(Importer), + Literal(&'a str), +} + +/// Reads a command-line value. +pub fn parse_argument(raw: &str) -> Raw<'_> { + if let Some(rest) = raw.strip_prefix("literal:") { + return Raw::Literal(rest); + } + let Some((prefix, rest)) = raw.split_once(':') else { + return literal_or_interpolated(raw); + }; + let (kind, format) = match prefix.split_once('+') { + None => (prefix, Format::Text), + Some((kind, "json")) => (kind, Format::Json), + Some(_) => return literal_or_interpolated(raw), + }; + let source = match kind { + "env" => Source::Env(rest.to_owned()), + "file" => Source::File(PathBuf::from(rest)), + "cmd" => Source::Shell { + command: rest.to_owned(), + dir: None, + }, + _ => return literal_or_interpolated(raw), + }; + Raw::Import(Importer { source, format }) +} + +fn literal_or_interpolated(raw: &str) -> Raw<'_> { + match interpolated(raw, None) { + Some(importer) => Raw::Import(importer), + None => Raw::Literal(raw), + } +} + +/// `text` as an importer if it holds a `$` (a placeholder, `$$`, or a mistake that +/// expanding it reports), read in `dir`; None if it is plain text. +pub fn interpolated(text: &str, dir: Option<&Path>) -> Option { + text.contains('$').then(|| Importer { + source: Source::Template { + template: text.to_owned(), + dir: dir.map(Path::to_owned), + }, + format: Format::Text, + }) +} + +/// Reads an importer table from a file in `dir`; None if `object` isn't one. +pub fn parse_table(object: &JsonMap, dir: &Path) -> Result> { + let sources = ["env", "file", "command"] + .into_iter() + .filter(|key| object.contains_key(*key)) + .count(); + if sources == 0 { + return Ok(None); + } + if sources > 1 { + bail!("an importer takes exactly one of env, file and command"); + } + if let Some(unknown) = object + .keys() + .find(|key| !["env", "file", "command", "format"].contains(&key.as_str())) + { + bail!("unknown importer key {unknown:?}"); + } + let format = match object.get("format") { + None => Format::Text, + Some(JsonValue::String(format)) if format == "text" => Format::Text, + Some(JsonValue::String(format)) if format == "json" => Format::Json, + Some(other) => bail!("importer format must be \"text\" or \"json\", not {other}"), + }; + let text = |key: &str| -> Result { + match object.get(key) { + Some(JsonValue::String(value)) => Ok(value.clone()), + _ => Err(anyhow!("importer {key} must be a string")), + } + }; + let source = if object.contains_key("env") { + Source::Env(text("env")?) + } else if object.contains_key("file") { + Source::File(dir.join(text("file")?)) + } else { + match &object["command"] { + JsonValue::String(command) => Source::Shell { + command: command.clone(), + dir: Some(dir.to_owned()), + }, + JsonValue::Array(items) => { + let argv = items + .iter() + .map(|item| item.as_str().map(str::to_owned)) + .collect::>>() + .filter(|argv| !argv.is_empty()) + .ok_or_else(|| { + anyhow!("importer command must be a string or a non-empty list of strings") + })?; + Source::Argv { + argv, + dir: Some(dir.to_owned()), + } + } + _ => bail!("importer command must be a string or a non-empty list of strings"), + } + }; + Ok(Some(Importer { source, format })) +} + +impl Importer { + /// Shown instead of the value (which may be secret), e.g. `env:HOSTNAME`. + pub fn describe(&self) -> String { + let format = match self.format { + Format::Text => "", + Format::Json => "+json", + }; + match &self.source { + Source::Env(name) => format!("env{format}:{name}"), + Source::File(path) => format!("file{format}:{}", path.display()), + Source::Shell { command, .. } => format!("cmd{format}:{command}"), + Source::Argv { argv, .. } => format!("cmd{format}:{}", argv.join(" ")), + Source::Template { template, .. } => template.clone(), + } + } + + /// Whether this is text with placeholders (`interpolated`) rather than an importer + /// of the whole value: its result is shown, the importers' are not. + pub fn is_interpolation(&self) -> bool { + matches!(self.source, Source::Template { .. }) + } + + /// The imported content: text, or JSON. + pub fn fetch(&self) -> Result { + let text = match &self.source { + Source::Env(name) => std::env::var(name).with_context(|| { + format!("environment variable {name} is not set (or not UTF-8)") + })?, + Source::File(path) => std::fs::read_to_string(path) + .with_context(|| format!("reading {}", path.display()))?, + Source::Shell { command, dir } => { + let mut process = Command::new("sh"); + process.arg("-c").arg(command); + run(process, dir.as_deref(), command)? + } + Source::Argv { argv, dir } => { + let mut process = Command::new(&argv[0]); + process.args(&argv[1..]); + run(process, dir.as_deref(), &argv.join(" "))? + } + Source::Template { template, dir } => expand(template, dir.as_deref())?, + }; + let text = text.trim_end_matches(['\n', '\r']); + Ok(match self.format { + Format::Text => Imported::Text(text.to_owned()), + Format::Json => Imported::Json( + serde_json::from_str(text) + .with_context(|| format!("{} is not valid JSON", self.describe()))?, + ), + }) + } +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Imported { + Text(String), + Json(JsonValue), +} + +/// Where the placeholder that `text` starts (after its `${`) ends: at the `}` that +/// matches its `{`. Braces inside balance, so a command may hold `{...}` (a jq object, a +/// shell group); in a command (`cmd:`), braces in shell quotes (`'...'`, `"..."`) or +/// escaped with a backslash don't count either. +fn placeholder_end(text: &str) -> Option { + let shell = text.starts_with("cmd:") || text.starts_with("cmd+"); + let mut depth = 0usize; + let mut quote: Option = None; + let mut escaped = false; + for (at, char) in text.char_indices() { + if shell { + if escaped { + escaped = false; + continue; + } + match (quote, char) { + // Nothing escapes in single quotes. + (Some('\''), '\'') => quote = None, + (Some('\''), _) => {} + (_, '\\') => escaped = true, + (Some('"'), '"') => quote = None, + (Some('"'), _) => {} + (None, '\'' | '"') => quote = Some(char), + _ => {} + } + if quote.is_some() || char == '\'' || char == '"' || char == '\\' { + continue; + } + } + match char { + '{' => depth += 1, + '}' if depth == 0 => return Some(at), + '}' => depth -= 1, + _ => {} + } + } + None +} + +/// Text with its placeholders replaced by their content. +fn expand(template: &str, dir: Option<&Path>) -> Result { + let mut out = String::new(); + let mut rest = template; + while let Some(at) = rest.find('$') { + out.push_str(&rest[..at]); + let after = &rest[at + 1..]; + if let Some(after) = after.strip_prefix('$') { + out.push('$'); + rest = after; + } else if let Some(after) = after.strip_prefix('{') { + let end = placeholder_end(after) + .ok_or_else(|| anyhow!("unterminated placeholder in template {template:?}"))?; + let placeholder = &after[..end]; + let Raw::Import(mut importer) = parse_argument(placeholder) else { + bail!( + "placeholder {placeholder:?} in template {template:?} isn't env:, file: or cmd:" + ); + }; + match &mut importer.source { + Source::File(path) => { + if let Some(dir) = dir { + *path = dir.join(&*path); + } + } + Source::Shell { dir: run_in, .. } => *run_in = dir.map(Path::to_owned), + Source::Template { .. } => bail!("placeholders don't nest: {placeholder:?}"), + Source::Env(_) | Source::Argv { .. } => {} + } + if importer.format != Format::Text { + bail!("placeholder {placeholder:?} must be text (+json isn't for placeholders)"); + } + match importer.fetch()? { + Imported::Text(text) => out.push_str(&text), + Imported::Json(_) => unreachable!("text importer"), + } + rest = &after[end + 1..]; + } else { + // Most likely a shell-style `$NAME` that would silently stay as it is. + bail!( + "a `$` in {template:?} that starts no placeholder: write `$$` for a `$`, or \ + e.g. `${{env:NAME}}` for a variable" + ); + } + } + out.push_str(rest); + Ok(out) +} + +fn run(mut process: Command, dir: Option<&Path>, shown: &str) -> Result { + if let Some(dir) = dir { + process.current_dir(dir); + } + let output = process + .output() + .with_context(|| format!("running {shown}"))?; + if !output.status.success() { + bail!( + "{shown} failed ({}): {}", + output.status, + String::from_utf8_lossy(&output.stderr).trim() + ); + } + String::from_utf8(output.stdout).with_context(|| format!("output of {shown} is not UTF-8")) +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn command_line_values() { + assert_eq!(parse_argument("plain"), Raw::Literal("plain")); + assert_eq!( + parse_argument("tls://host:9651"), + Raw::Literal("tls://host:9651") + ); + assert_eq!(parse_argument("literal:env:X"), Raw::Literal("env:X")); + assert_eq!( + parse_argument("env+json:SEEDS"), + Raw::Import(Importer { + source: Source::Env("SEEDS".into()), + format: Format::Json + }) + ); + assert_eq!( + parse_argument("cmd:hostname -f"), + Raw::Import(Importer { + source: Source::Shell { + command: "hostname -f".into(), + dir: None + }, + format: Format::Text + }) + ); + assert_eq!(parse_argument("env+yaml:X"), Raw::Literal("env+yaml:X")); + // A `$` makes it interpolated; expanding it then says what's wrong. + assert!(matches!(parse_argument("a $5 price"), Raw::Import(_))); + assert_eq!(parse_argument("literal:a $5"), Raw::Literal("a $5")); + assert_eq!( + parse_argument("node-${env:HOST}"), + Raw::Import(Importer { + source: Source::Template { + template: "node-${env:HOST}".into(), + dir: None + }, + format: Format::Text + }) + ); + // A placeholder in a value that looks like a prefix is interpolated too. + assert!(matches!( + parse_argument("tls://${env:HOST}:9651"), + Raw::Import(_) + )); + assert_eq!( + parse_argument("literal:${env:HOST}"), + Raw::Literal("${env:HOST}") + ); + } + + #[test] + fn templates() { + let expanded = |template: &str| expand(template, None); + assert_eq!(expanded("a-${cmd:echo b}-c $$5").unwrap(), "a-b-c $5"); + assert!(expanded("$HOME/x").is_err()); + assert!(expanded("a $5 price").is_err()); + assert_eq!( + expanded("${cmd:printf one}${cmd:printf two}").unwrap(), + "onetwo" + ); + assert!(expanded("${cmd:echo b").is_err()); + assert!(expanded("${plain}").is_err()); + assert!(expanded("${env+json:X}").is_err()); + } + + #[test] + fn placeholders_end_at_their_matching_brace() { + fn end(text: &str) -> Option<&str> { + placeholder_end(text).map(|at| &text[..at]) + } + assert_eq!(end("env:HOST}-x"), Some("env:HOST")); + // Balanced braces, as a jq object has them. + assert_eq!(end("cmd:jq -r '{a: .b}'}/tcp"), Some("cmd:jq -r '{a: .b}'")); + assert_eq!(end("cmd:sh -c { true; }}"), Some("cmd:sh -c { true; }")); + // In a command, braces in quotes or escaped don't count. + assert_eq!(end("cmd:printf '}'}x"), Some("cmd:printf '}'")); + assert_eq!(end(r#"cmd:printf "}\""}x"#), Some(r#"cmd:printf "}\"""#)); + assert_eq!(end(r"cmd:printf \}}x"), Some(r"cmd:printf \}")); + assert_eq!(end("cmd:printf '}"), None); + assert_eq!(end("env:X"), None); + // The kind of command this is for: quotes of both kinds, and `$` inside. + let command = r#"cmd:ip -j addr show wireguard | jq -r '.[].addr_info[]|select(.family=="inet6" and .scope=="global").local'"#; + assert_eq!(end(&format!("{command}}}/tcp/9661")), Some(command)); + } + + #[test] + fn commands_with_braces_and_quotes() { + let expanded = |template: &str| expand(template, None); + assert_eq!( + expanded(r#"/ip6/${cmd:printf '%s' "$(echo '::1')" | tr -d '{}'}/tcp/9661"#).unwrap(), + "/ip6/::1/tcp/9661" + ); + assert_eq!(expanded("x${cmd:printf '{%s}' a}y").unwrap(), "x{a}y"); + } + + #[test] + fn tables() { + let dir = Path::new("/etc/continuum"); + let table = |value: JsonValue| parse_table(value.as_object().unwrap(), dir); + assert_eq!(table(json!({ "x": 1 })).unwrap(), None); + assert_eq!( + table(json!({ "file": "key", "format": "json" })).unwrap(), + Some(Importer { + source: Source::File("/etc/continuum/key".into()), + format: Format::Json + }) + ); + assert!(table(json!({ "env": "A", "file": "b" })).is_err()); + assert!(table(json!({ "env": "A", "extra": 1 })).is_err()); + assert!(table(json!({ "command": [] })).is_err()); + } + + #[test] + fn fetching() { + let echo = Importer { + source: Source::Argv { + argv: vec!["printf".into(), "[1, 2]\\n\\n".into()], + dir: None, + }, + format: Format::Json, + }; + assert_eq!(echo.fetch().unwrap(), Imported::Json(json!([1, 2]))); + let shell = Importer { + source: Source::Shell { + command: "echo hello".into(), + dir: None, + }, + format: Format::Text, + }; + assert_eq!(shell.fetch().unwrap(), Imported::Text("hello".into())); + let failing = Importer { + source: Source::Shell { + command: "exit 3".into(), + dir: None, + }, + format: Format::Text, + }; + assert!(failing.fetch().is_err()); + } +} diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs new file mode 100644 index 0000000..654c475 --- /dev/null +++ b/crates/config/src/lib.rs @@ -0,0 +1,6 @@ +//! What a program's configuration is made of, from its files and its arguments: a +//! library, which continuum's configuration is made with, and other programs' may be. +//! +//! * [`import`]: values taken from elsewhere instead of written out. + +pub mod import; diff --git a/docs/architecture.md b/docs/architecture.md index f462fe7..69b0cc3 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -305,7 +305,7 @@ Roughly by size and by how many rules interact there: | [`swarm/peer/`](../src/swarm/peer/) + [`sync.rs`](../src/swarm/sync.rs) + [`source.rs`](../src/swarm/source.rs) | 2,300 | The protocol state machine: handshake, liveness, sync sessions with deadlines, who syncs with whom while catching up. Timing-dependent; tested on the simulated network ([`simtest/`](../src/simtest/)). | | [`state/`](../src/state/) | 1,600 | `AppState` and its write helpers ([`mod.rs`](../src/state/mod.rs)), `PeerState` ([`peer.rs`](../src/state/peer.rs)), `Timing`, reads ([`read.rs`](../src/state/read.rs)) and the `Host` impl ([`host.rs`](../src/state/host.rs)). A hub, not a layer. | | [`transport/`](../src/transport/) | 11,500 | libp2p ([`libp2p/`](../src/transport/libp2p/), 4,400: addresses, relays, hole punching, which paths to keep), the paths and identity layers (2,300: measuring, failing over, proving node IDs), Mycelium and its daemon ([`mycelium/`](../src/transport/mycelium/), 1,200), Tor (700), the wire format ([`wire.rs`](../src/transport/wire.rs), derived), the simulator. The transports are self-contained behind the `Transport` trait; libp2p's choice of addresses is the subtle part. | -| [`config/`](../src/config/) | 2,800 | Layered sources, importers and placeholders, conflicts, the report. Self-contained. | +| [`config/`](../src/config/), [`crates/config/`](../crates/config/) | 3,300, 500 | Layered sources, conflicts, the report; importers and placeholders, in a crate of their own (`config`), which other programs use too. Self-contained. | | [`document/store.rs`](../src/document/store.rs) | 1,200 | Conflict-aware JSON over Automerge. Subtle (merged maps, canonical writes), well covered by the rest relying on it. | | [`swarm/topology/`](../src/swarm/topology/), [`blob.rs`](../src/swarm/blob.rs) | 1,500 | Pure algorithms with tests; the topology hashes each member once, then scores the N² pairs by cheap mixing. | | [`api/`](../src/api/) | 3,600 | Handlers; the Prometheus rendering is the longest file. The dashboard's page ([`dashboard.html`](../src/api/dashboard/dashboard.html), 2,000 more) is one file of HTML, CSS and script. | diff --git a/src/config/import.rs b/src/config/import.rs deleted file mode 100644 index b50521a..0000000 --- a/src/config/import.rs +++ /dev/null @@ -1,480 +0,0 @@ -//! Importers: values taken from elsewhere instead of written out, so a configuration -//! can say where a value comes from without holding it (secrets, host facts). -//! -//! * `env:NAME`: an environment variable; -//! * `file:PATH`: a file's contents; -//! * `cmd:COMMAND`: the standard output of a shell command (`sh -c`). -//! -//! The content is text (trailing line breaks removed), converted like a value written -//! on the command line; with `+json` (`env+json:NAME`, …) it is parsed as JSON instead. -//! -//! On the command line, a value with one of these prefixes is imported; `literal:` -//! passes the rest as is. In configuration files, an importer is a table: -//! `{ env = "NAME" }`, `{ file = "PATH" }`, `{ command = "COMMAND" }` (or an argument -//! list, run without a shell), each with an optional `format = "json"`. -//! -//! Any other text value may hold placeholders, `${env:NAME}`, `${file:PATH}` or -//! `${cmd:COMMAND}`, each replaced by its content as text (`$$` is a `$`; a placeholder -//! ends at the first `}`), e.g. `node-${env:HOSTNAME}`. Relative paths are relative to -//! the file, and commands run in its directory, in placeholders too. - -use std::path::{Path, PathBuf}; -use std::process::Command; - -use anyhow::{Context, Result, anyhow, bail}; -use serde_json::{Map as JsonMap, Value as JsonValue}; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum Format { - Text, - Json, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) enum Source { - Env(String), - File(PathBuf), - Shell { - command: String, - dir: Option, - }, - Argv { - argv: Vec, - dir: Option, - }, - /// Text with placeholders (`interpolated`). - Template { - template: String, - dir: Option, - }, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) struct Importer { - pub(crate) source: Source, - pub(crate) format: Format, -} - -/// What a command-line value is: imported, or literal. -#[derive(Debug, PartialEq, Eq)] -pub(crate) enum Raw<'a> { - Import(Importer), - Literal(&'a str), -} - -/// Reads a command-line value. -pub(crate) fn parse_argument(raw: &str) -> Raw<'_> { - if let Some(rest) = raw.strip_prefix("literal:") { - return Raw::Literal(rest); - } - let Some((prefix, rest)) = raw.split_once(':') else { - return literal_or_interpolated(raw); - }; - let (kind, format) = match prefix.split_once('+') { - None => (prefix, Format::Text), - Some((kind, "json")) => (kind, Format::Json), - Some(_) => return literal_or_interpolated(raw), - }; - let source = match kind { - "env" => Source::Env(rest.to_owned()), - "file" => Source::File(PathBuf::from(rest)), - "cmd" => Source::Shell { - command: rest.to_owned(), - dir: None, - }, - _ => return literal_or_interpolated(raw), - }; - Raw::Import(Importer { source, format }) -} - -fn literal_or_interpolated(raw: &str) -> Raw<'_> { - match interpolated(raw, None) { - Some(importer) => Raw::Import(importer), - None => Raw::Literal(raw), - } -} - -/// `text` as an importer if it holds a `$` (a placeholder, `$$`, or a mistake that -/// expanding it reports), read in `dir`; None if it is plain text. -pub(crate) fn interpolated(text: &str, dir: Option<&Path>) -> Option { - text.contains('$').then(|| Importer { - source: Source::Template { - template: text.to_owned(), - dir: dir.map(Path::to_owned), - }, - format: Format::Text, - }) -} - -/// Reads an importer table from a file in `dir`; None if `object` isn't one. -pub(crate) fn parse_table( - object: &JsonMap, - dir: &Path, -) -> Result> { - let sources = ["env", "file", "command"] - .into_iter() - .filter(|key| object.contains_key(*key)) - .count(); - if sources == 0 { - return Ok(None); - } - if sources > 1 { - bail!("an importer takes exactly one of env, file and command"); - } - if let Some(unknown) = object - .keys() - .find(|key| !["env", "file", "command", "format"].contains(&key.as_str())) - { - bail!("unknown importer key {unknown:?}"); - } - let format = match object.get("format") { - None => Format::Text, - Some(JsonValue::String(format)) if format == "text" => Format::Text, - Some(JsonValue::String(format)) if format == "json" => Format::Json, - Some(other) => bail!("importer format must be \"text\" or \"json\", not {other}"), - }; - let text = |key: &str| -> Result { - match object.get(key) { - Some(JsonValue::String(value)) => Ok(value.clone()), - _ => Err(anyhow!("importer {key} must be a string")), - } - }; - let source = if object.contains_key("env") { - Source::Env(text("env")?) - } else if object.contains_key("file") { - Source::File(dir.join(text("file")?)) - } else { - match &object["command"] { - JsonValue::String(command) => Source::Shell { - command: command.clone(), - dir: Some(dir.to_owned()), - }, - JsonValue::Array(items) => { - let argv = items - .iter() - .map(|item| item.as_str().map(str::to_owned)) - .collect::>>() - .filter(|argv| !argv.is_empty()) - .ok_or_else(|| { - anyhow!("importer command must be a string or a non-empty list of strings") - })?; - Source::Argv { - argv, - dir: Some(dir.to_owned()), - } - } - _ => bail!("importer command must be a string or a non-empty list of strings"), - } - }; - Ok(Some(Importer { source, format })) -} - -impl Importer { - /// Shown instead of the value (which may be secret), e.g. `env:HOSTNAME`. - pub(crate) fn describe(&self) -> String { - let format = match self.format { - Format::Text => "", - Format::Json => "+json", - }; - match &self.source { - Source::Env(name) => format!("env{format}:{name}"), - Source::File(path) => format!("file{format}:{}", path.display()), - Source::Shell { command, .. } => format!("cmd{format}:{command}"), - Source::Argv { argv, .. } => format!("cmd{format}:{}", argv.join(" ")), - Source::Template { template, .. } => template.clone(), - } - } - - /// Whether this is text with placeholders (`interpolated`) rather than an importer - /// of the whole value: its result is shown, the importers' are not. - pub(crate) fn is_interpolation(&self) -> bool { - matches!(self.source, Source::Template { .. }) - } - - /// The imported content: text, or JSON. - pub(crate) fn fetch(&self) -> Result { - let text = match &self.source { - Source::Env(name) => std::env::var(name).with_context(|| { - format!("environment variable {name} is not set (or not UTF-8)") - })?, - Source::File(path) => std::fs::read_to_string(path) - .with_context(|| format!("reading {}", path.display()))?, - Source::Shell { command, dir } => { - let mut process = Command::new("sh"); - process.arg("-c").arg(command); - run(process, dir.as_deref(), command)? - } - Source::Argv { argv, dir } => { - let mut process = Command::new(&argv[0]); - process.args(&argv[1..]); - run(process, dir.as_deref(), &argv.join(" "))? - } - Source::Template { template, dir } => expand(template, dir.as_deref())?, - }; - let text = text.trim_end_matches(['\n', '\r']); - Ok(match self.format { - Format::Text => Imported::Text(text.to_owned()), - Format::Json => Imported::Json( - serde_json::from_str(text) - .with_context(|| format!("{} is not valid JSON", self.describe()))?, - ), - }) - } -} - -#[derive(Debug, Clone, PartialEq)] -pub(crate) enum Imported { - Text(String), - Json(JsonValue), -} - -/// Where the placeholder that `text` starts (after its `${`) ends: at the `}` that -/// matches its `{`. Braces inside balance, so a command may hold `{...}` (a jq object, a -/// shell group); in a command (`cmd:`), braces in shell quotes (`'...'`, `"..."`) or -/// escaped with a backslash don't count either. -fn placeholder_end(text: &str) -> Option { - let shell = text.starts_with("cmd:") || text.starts_with("cmd+"); - let mut depth = 0usize; - let mut quote: Option = None; - let mut escaped = false; - for (at, char) in text.char_indices() { - if shell { - if escaped { - escaped = false; - continue; - } - match (quote, char) { - // Nothing escapes in single quotes. - (Some('\''), '\'') => quote = None, - (Some('\''), _) => {} - (_, '\\') => escaped = true, - (Some('"'), '"') => quote = None, - (Some('"'), _) => {} - (None, '\'' | '"') => quote = Some(char), - _ => {} - } - if quote.is_some() || char == '\'' || char == '"' || char == '\\' { - continue; - } - } - match char { - '{' => depth += 1, - '}' if depth == 0 => return Some(at), - '}' => depth -= 1, - _ => {} - } - } - None -} - -/// Text with its placeholders replaced by their content. -fn expand(template: &str, dir: Option<&Path>) -> Result { - let mut out = String::new(); - let mut rest = template; - while let Some(at) = rest.find('$') { - out.push_str(&rest[..at]); - let after = &rest[at + 1..]; - if let Some(after) = after.strip_prefix('$') { - out.push('$'); - rest = after; - } else if let Some(after) = after.strip_prefix('{') { - let end = placeholder_end(after) - .ok_or_else(|| anyhow!("unterminated placeholder in template {template:?}"))?; - let placeholder = &after[..end]; - let Raw::Import(mut importer) = parse_argument(placeholder) else { - bail!( - "placeholder {placeholder:?} in template {template:?} isn't env:, file: or cmd:" - ); - }; - match &mut importer.source { - Source::File(path) => { - if let Some(dir) = dir { - *path = dir.join(&*path); - } - } - Source::Shell { dir: run_in, .. } => *run_in = dir.map(Path::to_owned), - Source::Template { .. } => bail!("placeholders don't nest: {placeholder:?}"), - Source::Env(_) | Source::Argv { .. } => {} - } - if importer.format != Format::Text { - bail!("placeholder {placeholder:?} must be text (+json isn't for placeholders)"); - } - match importer.fetch()? { - Imported::Text(text) => out.push_str(&text), - Imported::Json(_) => unreachable!("text importer"), - } - rest = &after[end + 1..]; - } else { - // Most likely a shell-style `$NAME` that would silently stay as it is. - bail!( - "a `$` in {template:?} that starts no placeholder: write `$$` for a `$`, or \ - e.g. `${{env:NAME}}` for a variable" - ); - } - } - out.push_str(rest); - Ok(out) -} - -fn run(mut process: Command, dir: Option<&Path>, shown: &str) -> Result { - if let Some(dir) = dir { - process.current_dir(dir); - } - let output = process - .output() - .with_context(|| format!("running {shown}"))?; - if !output.status.success() { - bail!( - "{shown} failed ({}): {}", - output.status, - String::from_utf8_lossy(&output.stderr).trim() - ); - } - String::from_utf8(output.stdout).with_context(|| format!("output of {shown} is not UTF-8")) -} - -#[cfg(test)] -mod tests { - use super::*; - use serde_json::json; - - #[test] - fn command_line_values() { - assert_eq!(parse_argument("plain"), Raw::Literal("plain")); - assert_eq!( - parse_argument("tls://host:9651"), - Raw::Literal("tls://host:9651") - ); - assert_eq!(parse_argument("literal:env:X"), Raw::Literal("env:X")); - assert_eq!( - parse_argument("env+json:SEEDS"), - Raw::Import(Importer { - source: Source::Env("SEEDS".into()), - format: Format::Json - }) - ); - assert_eq!( - parse_argument("cmd:hostname -f"), - Raw::Import(Importer { - source: Source::Shell { - command: "hostname -f".into(), - dir: None - }, - format: Format::Text - }) - ); - assert_eq!(parse_argument("env+yaml:X"), Raw::Literal("env+yaml:X")); - // A `$` makes it interpolated; expanding it then says what's wrong. - assert!(matches!(parse_argument("a $5 price"), Raw::Import(_))); - assert_eq!(parse_argument("literal:a $5"), Raw::Literal("a $5")); - assert_eq!( - parse_argument("node-${env:HOST}"), - Raw::Import(Importer { - source: Source::Template { - template: "node-${env:HOST}".into(), - dir: None - }, - format: Format::Text - }) - ); - // A placeholder in a value that looks like a prefix is interpolated too. - assert!(matches!( - parse_argument("tls://${env:HOST}:9651"), - Raw::Import(_) - )); - assert_eq!( - parse_argument("literal:${env:HOST}"), - Raw::Literal("${env:HOST}") - ); - } - - #[test] - fn templates() { - let expanded = |template: &str| expand(template, None); - assert_eq!(expanded("a-${cmd:echo b}-c $$5").unwrap(), "a-b-c $5"); - assert!(expanded("$HOME/x").is_err()); - assert!(expanded("a $5 price").is_err()); - assert_eq!( - expanded("${cmd:printf one}${cmd:printf two}").unwrap(), - "onetwo" - ); - assert!(expanded("${cmd:echo b").is_err()); - assert!(expanded("${plain}").is_err()); - assert!(expanded("${env+json:X}").is_err()); - } - - #[test] - fn placeholders_end_at_their_matching_brace() { - fn end(text: &str) -> Option<&str> { - placeholder_end(text).map(|at| &text[..at]) - } - assert_eq!(end("env:HOST}-x"), Some("env:HOST")); - // Balanced braces, as a jq object has them. - assert_eq!(end("cmd:jq -r '{a: .b}'}/tcp"), Some("cmd:jq -r '{a: .b}'")); - assert_eq!(end("cmd:sh -c { true; }}"), Some("cmd:sh -c { true; }")); - // In a command, braces in quotes or escaped don't count. - assert_eq!(end("cmd:printf '}'}x"), Some("cmd:printf '}'")); - assert_eq!(end(r#"cmd:printf "}\""}x"#), Some(r#"cmd:printf "}\"""#)); - assert_eq!(end(r"cmd:printf \}}x"), Some(r"cmd:printf \}")); - assert_eq!(end("cmd:printf '}"), None); - assert_eq!(end("env:X"), None); - // The kind of command this is for: quotes of both kinds, and `$` inside. - let command = r#"cmd:ip -j addr show wireguard | jq -r '.[].addr_info[]|select(.family=="inet6" and .scope=="global").local'"#; - assert_eq!(end(&format!("{command}}}/tcp/9661")), Some(command)); - } - - #[test] - fn commands_with_braces_and_quotes() { - let expanded = |template: &str| expand(template, None); - assert_eq!( - expanded(r#"/ip6/${cmd:printf '%s' "$(echo '::1')" | tr -d '{}'}/tcp/9661"#).unwrap(), - "/ip6/::1/tcp/9661" - ); - assert_eq!(expanded("x${cmd:printf '{%s}' a}y").unwrap(), "x{a}y"); - } - - #[test] - fn tables() { - let dir = Path::new("/etc/continuum"); - let table = |value: JsonValue| parse_table(value.as_object().unwrap(), dir); - assert_eq!(table(json!({ "x": 1 })).unwrap(), None); - assert_eq!( - table(json!({ "file": "key", "format": "json" })).unwrap(), - Some(Importer { - source: Source::File("/etc/continuum/key".into()), - format: Format::Json - }) - ); - assert!(table(json!({ "env": "A", "file": "b" })).is_err()); - assert!(table(json!({ "env": "A", "extra": 1 })).is_err()); - assert!(table(json!({ "command": [] })).is_err()); - } - - #[test] - fn fetching() { - let echo = Importer { - source: Source::Argv { - argv: vec!["printf".into(), "[1, 2]\\n\\n".into()], - dir: None, - }, - format: Format::Json, - }; - assert_eq!(echo.fetch().unwrap(), Imported::Json(json!([1, 2]))); - let shell = Importer { - source: Source::Shell { - command: "echo hello".into(), - dir: None, - }, - format: Format::Text, - }; - assert_eq!(shell.fetch().unwrap(), Imported::Text("hello".into())); - let failing = Importer { - source: Source::Shell { - command: "exit 3".into(), - dir: None, - }, - format: Format::Text, - }; - assert!(failing.fetch().is_err()); - } -} diff --git a/src/config/mod.rs b/src/config/mod.rs index cfc2c76..fd1f8e1 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -11,7 +11,6 @@ //! into [`Settings`] and checked. mod file; -mod import; use std::collections::BTreeMap; use std::net::SocketAddr; @@ -25,7 +24,7 @@ use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; use tracing::{debug, info, warn}; -use import::{Imported, Raw}; +use ::config::import::{self, Imported, Raw}; /// How an option's value is written as text (command line, text imports). #[derive(Debug, Clone, Copy, PartialEq, Eq)] -- cgit v1.3.1