use std::path::{Path, PathBuf};
pub const ANCHORS: &str = "ca.pem";
pub const CHAIN: &str = "client.pem";
pub const KEY: &str = "client.key";
pub const ADDRESS: &str = "address";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Whose {
Own,
Elsewhere,
}
impl Whose {
pub fn remedy(self) -> String {
let four = format!("`{ANCHORS}`, `{CHAIN}`, `{KEY}` and `{ADDRESS}`");
match self {
Self::Own => format!(
"if a server runs on this box its material already exists — its own boot \
minted it — so the act is a COPY: take {four} out of that server's wire \
directory ($XDG_DATA_HOME/yog/wire) and put them here, under those names. \
`{ADDRESS}` must name a stated host:port; a server that bound `:0` asked the \
kernel for a port and told only its own window, so re-issue with one stated \
(`WIRE_HOST=<host> WIRE_PORT=<port> yog wire-certs`) before copying. If the \
server is on ANOTHER box, register this box with it instead: a seat already \
connected there mints an offer, and `lernie register <url>` claims it with a \
key made here"
),
Self::Elsewhere => format!(
"`lernie register <url>` files a whole entry from a one-time offer, with a \
key made on this box, in a directory that holds nothing yet. By hand instead, \
the pair is minted on the host that issued it — `WIRE_LEAF=<name> yog \
wire-certs` there, the assignment BEFORE the verb, because `WIRE_LEAF` is an \
environment variable and not an argument it parses — and carried here as \
{four}, under those names"
),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Material {
pub anchors: PathBuf,
pub chain: PathBuf,
pub key: PathBuf,
pub addresses: Vec<String>,
}
impl Material {
pub fn address(&self) -> String {
self.addresses.join(", ")
}
}
pub fn read_dir(dir: &Path, whose: Whose) -> Result<Option<Material>, String> {
let wanted = [ANCHORS, CHAIN, KEY, ADDRESS];
let missing: Vec<&str> = wanted
.iter()
.copied()
.filter(|f| !dir.join(f).is_file())
.collect();
if missing.len() == wanted.len() {
return Ok(None);
}
if !missing.is_empty() {
return Err(format!(
"{} is half-provisioned: missing {} — {}",
dir.display(),
missing.join(", "),
whose.remedy()
));
}
let addresses = list(&std::fs::read_to_string(dir.join(ADDRESS)).unwrap_or_default());
if addresses.is_empty() {
return Err(format!(
"{} names no address; it must hold one host:port per line — {}",
dir.join(ADDRESS).display(),
whose.remedy()
));
}
Ok(Some(Material {
anchors: dir.join(ANCHORS),
chain: dir.join(CHAIN),
key: dir.join(KEY),
addresses,
}))
}
pub fn list(text: &str) -> Vec<String> {
text.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.map(str::to_owned)
.collect()
}
#[cfg(test)]
mod tests;