fix: harden the vault before release

Require a private passphrase and loopback-or-token admin auth. New vaults use Argon2id; existing PBKDF2 vaults still open, and new snapshot metadata is encrypted. Wormhole tokens survive restart, offset uploads seal into the vault, and client-certificate uploads are real. Reject the command provider and keep Postgres passwords off the process arguments. Keep the interruptible canvas motion.
This commit is contained in:
2026-11-10 09:53:06 +07:00
parent 60ba8fed88
commit 95778d54e4
46 changed files with 2311 additions and 415 deletions

View File

@@ -1,7 +1,11 @@
use anyhow::{Context, Result};
use rustls::pki_types::ServerName;
use rustls::{ClientConfig, ClientConnection, RootCertStore, StreamOwned};
use std::collections::HashMap;
use std::io::{Read, Write};
use std::fs::File;
use std::io::{BufReader, Read, Write};
use std::net::TcpStream;
use std::sync::Arc;
pub fn post_json(url: &str, body: &serde_json::Value, headers: &[(&str, &str)]) -> Result<String> {
let payload = serde_json::to_string(body)?;
@@ -15,8 +19,12 @@ fn request(
content_type: Option<&str>,
headers: &[(&str, &str)],
) -> Result<String> {
let (host, port, path) = parse_url(url)?;
let mut stream = TcpStream::connect((host.as_str(), port)).context("connect")?;
let (https, host, port, path) = parse_url(url)?;
let mut stream: Box<dyn ReadWrite> = if https {
Box::new(connect_mtls(&host, port)?)
} else {
Box::new(TcpStream::connect((host.as_str(), port)).context("connect")?)
};
let mut req = format!("{method} {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n");
for (k, v) in headers {
req.push_str(&format!("{k}: {v}\r\n"));
@@ -38,20 +46,65 @@ fn request(
Ok(buf[body_start..].trim().to_string())
}
fn parse_url(url: &str) -> Result<(String, u16, String)> {
let url = url.strip_prefix("http://").context("only http supported")?;
let (host_port, path) = url.split_once('/').unwrap_or((url, ""));
trait ReadWrite: Read + Write {}
impl<T: Read + Write> ReadWrite for T {}
fn parse_url(url: &str) -> Result<(bool, String, u16, String)> {
let (https, rest) = if let Some(rest) = url.strip_prefix("https://") {
(true, rest)
} else if let Some(rest) = url.strip_prefix("http://") {
(false, rest)
} else {
anyhow::bail!("hub url must start with http:// or https://");
};
let (host_port, path) = rest.split_once('/').unwrap_or((rest, ""));
let path = if path.is_empty() {
"/".to_string()
} else {
format!("/{path}")
};
let (host, port) = if let Some((h, p)) = host_port.split_once(':') {
(h.to_string(), p.parse()?)
let (host, port) = if let Some((host, port)) = host_port.split_once(':') {
(host.to_string(), port.parse()?)
} else {
(host_port.to_string(), 80)
(host_port.to_string(), if https { 443 } else { 80 })
};
Ok((host, port, path))
Ok((https, host, port, path))
}
fn connect_mtls(host: &str, port: u16) -> Result<StreamOwned<ClientConnection, TcpStream>> {
let _ = rustls::crypto::ring::default_provider().install_default();
let ca = std::env::var("TESSERA_WORMHOLE_CA")
.context("TESSERA_WORMHOLE_CA is required for an https hub")?;
let cert = std::env::var("TESSERA_WORMHOLE_CLIENT_CERT")
.context("TESSERA_WORMHOLE_CLIENT_CERT is required for an https hub")?;
let key = std::env::var("TESSERA_WORMHOLE_CLIENT_KEY")
.context("TESSERA_WORMHOLE_CLIENT_KEY is required for an https hub")?;
let mut roots = RootCertStore::empty();
for ca_cert in load_certs(&ca)? {
roots.add(ca_cert).map_err(|error| anyhow::anyhow!("hub CA: {error}"))?;
}
let client = ClientConfig::builder()
.with_root_certificates(roots)
.with_client_auth_cert(load_certs(&cert)?, load_key(&key)?)
.map_err(|error| anyhow::anyhow!("client certificate: {error}"))?;
let name = ServerName::try_from(host.to_string()).context("hub hostname")?;
let connection = ClientConnection::new(Arc::new(client), name)?;
let tcp = TcpStream::connect((host, port)).context("connect")?;
Ok(StreamOwned::new(connection, tcp))
}
fn load_certs(path: &str) -> Result<Vec<rustls::pki_types::CertificateDer<'static>>> {
let mut reader = BufReader::new(File::open(path).with_context(|| format!("open {path}"))?);
rustls_pemfile::certs(&mut reader)
.collect::<Result<Vec<_>, _>>()
.context("parse certificate")
}
fn load_key(path: &str) -> Result<rustls::pki_types::PrivateKeyDer<'static>> {
let mut reader = BufReader::new(File::open(path).with_context(|| format!("open {path}"))?);
rustls_pemfile::private_key(&mut reader)
.context("parse private key")?
.context("no private key in client key file")
}
#[allow(dead_code)]