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:
145
crates/tessera-hub/src/mtls.rs
Normal file
145
crates/tessera-hub/src/mtls.rs
Normal file
@@ -0,0 +1,145 @@
|
||||
use anyhow::{Context, Result};
|
||||
use axum::body::Body;
|
||||
use axum::extract::Request;
|
||||
use axum::response::Response;
|
||||
use axum::Router;
|
||||
use hyper::body::Incoming;
|
||||
use hyper_util::rt::TokioIo;
|
||||
use hyper_util::service::TowerToHyperService;
|
||||
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
|
||||
use rustls::server::WebPkiClientVerifier;
|
||||
use rustls::RootCertStore;
|
||||
use rustls::ServerConfig;
|
||||
use std::convert::Infallible;
|
||||
use std::fs::File;
|
||||
use std::future::Future;
|
||||
use std::io::BufReader;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::Path;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::task::{Context as TaskContext, Poll};
|
||||
use tokio::net::TcpListener;
|
||||
use tokio_rustls::TlsAcceptor;
|
||||
use tower::{Service, ServiceExt};
|
||||
|
||||
pub struct WormholeMtls {
|
||||
pub listen: SocketAddr,
|
||||
cert: Vec<CertificateDer<'static>>,
|
||||
key: PrivateKeyDer<'static>,
|
||||
client_ca: RootCertStore,
|
||||
}
|
||||
|
||||
pub fn from_env() -> Result<Option<WormholeMtls>> {
|
||||
let raw = std::env::var("TESSERA_WORMHOLE_MTLS").unwrap_or_default();
|
||||
let enabled = raw == "1" || raw.eq_ignore_ascii_case("true");
|
||||
if !enabled {
|
||||
return Ok(None);
|
||||
}
|
||||
let cert = std::env::var("TESSERA_WORMHOLE_CERT").context(
|
||||
"TESSERA_WORMHOLE_CERT is required when TESSERA_WORMHOLE_MTLS is enabled",
|
||||
)?;
|
||||
let key = std::env::var("TESSERA_WORMHOLE_KEY").context(
|
||||
"TESSERA_WORMHOLE_KEY is required when TESSERA_WORMHOLE_MTLS is enabled",
|
||||
)?;
|
||||
let ca = std::env::var("TESSERA_WORMHOLE_CLIENT_CA").context(
|
||||
"TESSERA_WORMHOLE_CLIENT_CA is required when TESSERA_WORMHOLE_MTLS is enabled",
|
||||
)?;
|
||||
let listen = std::env::var("TESSERA_WORMHOLE_LISTEN")
|
||||
.unwrap_or_else(|_| "127.0.0.1:8788".into())
|
||||
.parse()
|
||||
.context("TESSERA_WORMHOLE_LISTEN")?;
|
||||
install_provider();
|
||||
Ok(Some(WormholeMtls {
|
||||
listen,
|
||||
cert: load_certs(Path::new(&cert))?,
|
||||
key: load_key(Path::new(&key))?,
|
||||
client_ca: load_ca(Path::new(&ca))?,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn install_provider() {
|
||||
let _ = rustls::crypto::ring::default_provider().install_default();
|
||||
}
|
||||
|
||||
pub async fn serve(mtls: WormholeMtls, app: Router) -> Result<()> {
|
||||
install_provider();
|
||||
let verifier = WebPkiClientVerifier::builder(Arc::new(mtls.client_ca))
|
||||
.build()
|
||||
.map_err(|error| anyhow::anyhow!("wormhole client CA: {error}"))?;
|
||||
let server = ServerConfig::builder()
|
||||
.with_client_cert_verifier(verifier)
|
||||
.with_single_cert(mtls.cert, mtls.key)
|
||||
.map_err(|error| anyhow::anyhow!("wormhole certificate: {error}"))?;
|
||||
let acceptor = TlsAcceptor::from(Arc::new(server));
|
||||
let listener = TcpListener::bind(mtls.listen)
|
||||
.await
|
||||
.with_context(|| format!("bind wormhole mTLS {}", mtls.listen))?;
|
||||
tracing::info!(
|
||||
"wormhole mTLS listening on {}; a client certificate is required",
|
||||
mtls.listen
|
||||
);
|
||||
loop {
|
||||
let (tcp, _) = listener.accept().await.context("accept wormhole mTLS")?;
|
||||
let acceptor = acceptor.clone();
|
||||
let app = app.clone();
|
||||
tokio::spawn(async move {
|
||||
let Ok(tls) = acceptor.accept(tcp).await else {
|
||||
return;
|
||||
};
|
||||
let io = TokioIo::new(tls);
|
||||
let service = TowerToHyperService::new(HyperApp(app));
|
||||
let _ = hyper::server::conn::http1::Builder::new()
|
||||
.serve_connection(io, service)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct HyperApp(Router);
|
||||
|
||||
impl Service<Request<Incoming>> for HyperApp {
|
||||
type Response = Response;
|
||||
type Error = Infallible;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<Response, Infallible>> + Send>>;
|
||||
|
||||
fn poll_ready(&mut self, _cx: &mut TaskContext<'_>) -> Poll<Result<(), Infallible>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, request: Request<Incoming>) -> Self::Future {
|
||||
let app = self.0.clone();
|
||||
Box::pin(async move {
|
||||
Ok(app
|
||||
.oneshot(request.map(Body::new))
|
||||
.await
|
||||
.unwrap_or_else(|error| match error {}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_certs(path: &Path) -> Result<Vec<CertificateDer<'static>>> {
|
||||
let mut reader = BufReader::new(File::open(path).with_context(|| format!("open {}", path.display()))?);
|
||||
rustls_pemfile::certs(&mut reader)
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.context("parse certificate")
|
||||
}
|
||||
|
||||
pub fn load_key(path: &Path) -> Result<PrivateKeyDer<'static>> {
|
||||
let mut reader = BufReader::new(File::open(path).with_context(|| format!("open {}", path.display()))?);
|
||||
rustls_pemfile::private_key(&mut reader)
|
||||
.context("parse private key")?
|
||||
.context("certificate file has no private key")
|
||||
}
|
||||
|
||||
pub fn load_ca(path: &Path) -> Result<RootCertStore> {
|
||||
let mut roots = RootCertStore::empty();
|
||||
for cert in load_certs(path)? {
|
||||
roots.add(cert).map_err(|error| anyhow::anyhow!("client CA: {error}"))?;
|
||||
}
|
||||
if roots.is_empty() {
|
||||
anyhow::bail!("client CA file contained no certificates");
|
||||
}
|
||||
Ok(roots)
|
||||
}
|
||||
Reference in New Issue
Block a user