143 lines
6 KiB
Rust
143 lines
6 KiB
Rust
//! `hivectl peer-config` — print the block a peer operator pastes to
|
|
//! federate with this hive.
|
|
|
|
use std::path::Path;
|
|
|
|
use anyhow::{Context as _, Result};
|
|
|
|
use crate::util::query_hive_urls;
|
|
|
|
/// Host path of this hive's TLS trust bundle (matches the
|
|
/// `services.hyperhive.deploy.hive-controller.tls.stateDir` default in
|
|
/// hive-tls.nix). Its existence means the gateway is in self-signed mode,
|
|
/// serving a leaf the secret store issued, so a federating peer needs an
|
|
/// anchor for it. Absent = ACME / operator cert, trusted by the default CA
|
|
/// bundle with nothing to distribute.
|
|
const HIVE_TLS_TRUST_BUNDLE_PATH: &str = "/var/lib/hive-tls/trust-bundle.pem";
|
|
|
|
/// Host path of the secret store's `pki` root (hive-tls.nix's
|
|
/// `$svcroot`, folded into the trust bundle). The anchor a whole swarm
|
|
/// shares: every hive's gateway leaf and the swarm's service names are
|
|
/// issued under it, so a peer host installs it once and every present
|
|
/// *and future* hive validates.
|
|
const STORE_PKI_ROOT_PATH: &str = "/var/lib/hive-tls/swarm-services-root.pem";
|
|
|
|
/// The option whose value keys a hive's entry in `services.hyperhive.swarm.hives`.
|
|
const HIVE_NAME_OPTION: &str = "services.hyperhive.deploy.hive-controller.hiveName";
|
|
|
|
/// Attrset key for a hive in `services.hyperhive.swarm.hives`, derived
|
|
/// from its domain's first DNS label.
|
|
///
|
|
/// A hive occupies `<hiveName>.<swarm.domain>`, so the first label *is*
|
|
/// the hive name in any deployment that hasn't overridden `domain` by
|
|
/// hand. Where it has, a wrong key fails loudly rather than quietly: on
|
|
/// that hive's own host the `hives.<hiveName>` assertion fires, because
|
|
/// the directory is supposed to be the same attrset everywhere. The
|
|
/// snippet below says so rather than presenting the guess as fact.
|
|
fn hive_key(domain: &str) -> &str {
|
|
domain.split('.').next().unwrap_or(domain)
|
|
}
|
|
|
|
/// Require this hive's domain from the daemon for snippet generation.
|
|
/// Errors with a clear hint when it can't be resolved, so `peer-config`
|
|
/// never silently emits a wrong key. An unreachable daemon propagates its
|
|
/// own (classified) connect error; only a *reachable* daemon with no domain
|
|
/// gets the config hint.
|
|
pub(crate) async fn require_hive_domain(socket: &Path) -> Result<String> {
|
|
query_hive_urls(socket)
|
|
.await
|
|
.context("could not determine this hive's domain from the daemon")?
|
|
.and_then(|u| u.domain)
|
|
.context("the daemon reported no domain — set `services.hyperhive.domain`")
|
|
}
|
|
|
|
/// The first closing comment line of the `peer-config` snippet.
|
|
fn peer_config_key_note() -> String {
|
|
format!("# the key must be this hive's {HIVE_NAME_OPTION}, and the")
|
|
}
|
|
|
|
/// The `peer-config` step carrying the secret store's `pki` root: the
|
|
/// certificate itself, or where it will appear when this hive has not
|
|
/// fetched it yet.
|
|
fn store_root_step(pem: Option<&str>) -> String {
|
|
const HEAD: &str = "# 1. trust the SECRET STORE's pki root on the peer host (once, not per hive);\n\
|
|
# every hive's gateway leaf and the swarm's service names are issued under it:\n";
|
|
match pem.map(str::trim_end).filter(|p| !p.is_empty()) {
|
|
Some(p) => format!(
|
|
"{HEAD}{p}\n\
|
|
# (a self-signed hive in this swarm fetches it itself, into {STORE_PKI_ROOT_PATH})\n"
|
|
),
|
|
None => format!(
|
|
"{HEAD}# not on this host yet: swarm-services-cert.service writes it to\n\
|
|
# {STORE_PKI_ROOT_PATH}; rerun once that unit has succeeded\n"
|
|
),
|
|
}
|
|
}
|
|
|
|
/// `peer-config` — print the `swarm.hives."<name>"` block a peer
|
|
/// operator pastes to federate with THIS hive, plus the store-root step
|
|
/// when this hive serves a self-signed chain. Reads local state only (the
|
|
/// TLS trust bundle and the store root); prints, never mutates.
|
|
pub(crate) fn peer_config(domain: &str) {
|
|
let self_signed = Path::new(HIVE_TLS_TRUST_BUNDLE_PATH).exists();
|
|
let key = hive_key(domain);
|
|
|
|
if self_signed {
|
|
let store_root = std::fs::read_to_string(STORE_PKI_ROOT_PATH).ok();
|
|
print!("{}", store_root_step(store_root.as_deref()));
|
|
println!();
|
|
println!("# 2. paste into the peer hive's NixOS config:");
|
|
} else {
|
|
println!("# paste into the peer hive's NixOS config:");
|
|
}
|
|
println!("services.hyperhive.swarm.hives.\"{key}\" = {{");
|
|
println!(" domain = \"{domain}\";");
|
|
println!("}};");
|
|
println!("{}", peer_config_key_note());
|
|
println!("# same entry belongs in every hive's config — `hives` is the swarm.");
|
|
if !self_signed {
|
|
println!(
|
|
"# (this hive's cert chains to a public CA — nothing to install; \
|
|
it's trusted by the default bundle.)"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::{STORE_PKI_ROOT_PATH, peer_config_key_note, store_root_step};
|
|
|
|
const PEM: &str = "-----BEGIN CERTIFICATE-----\nMIIB\n-----END CERTIFICATE-----";
|
|
|
|
/// The snippet tells the operator to set the deploy option, never the
|
|
/// removed `services.hyperhive` spelling.
|
|
#[test]
|
|
fn key_note_names_the_deploy_option() {
|
|
let note = peer_config_key_note();
|
|
assert!(
|
|
note.contains("services.hyperhive.deploy.hive-controller.hiveName"),
|
|
"{note}"
|
|
);
|
|
assert!(!note.contains("services.hyperhive.hiveName"), "{note}");
|
|
}
|
|
|
|
/// The certificate is printed whole, on lines of its own, so a peer
|
|
/// operator can copy it out of the output as a PEM file.
|
|
#[test]
|
|
fn store_root_is_printed_verbatim() {
|
|
let s = store_root_step(Some(&format!("{PEM}\n")));
|
|
assert!(s.contains(&format!("\n{PEM}\n")), "{s}");
|
|
}
|
|
|
|
/// A hive that has not fetched the root yet says where it will be,
|
|
/// rather than printing an empty step.
|
|
#[test]
|
|
fn missing_store_root_names_the_path_and_the_unit() {
|
|
for pem in [None, Some(""), Some("\n")] {
|
|
let s = store_root_step(pem);
|
|
assert!(s.contains(STORE_PKI_ROOT_PATH), "{s}");
|
|
assert!(s.contains("swarm-services-cert.service"), "{s}");
|
|
assert!(!s.contains("BEGIN CERTIFICATE"), "{s}");
|
|
}
|
|
}
|
|
}
|