[edge] Tests first: secret never leaks, forward-auth denial never reaches upstream (red)
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
//! Runs the real `ianitor-edge` binary on 127.0.0.1 with a compiled config, a test CA for
|
||||
//! `tls = "domi"` hosts and a credentials directory. stderr goes to a file the tests inspect.
|
||||
|
||||
#![allow(dead_code, clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
use std::net::SocketAddr;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use ianitor_core::compile::{self, CompileOptions};
|
||||
use ianitor_core::{load, SourceFile, TlsMode};
|
||||
use ianitor_testkit::proc::{free_port, local, wait_for_port, ChildGuard, TempDir};
|
||||
use ianitor_testkit::tls::TestCa;
|
||||
use rustls::RootCertStore;
|
||||
|
||||
pub struct Edge {
|
||||
pub child: ChildGuard,
|
||||
pub http: SocketAddr,
|
||||
pub https: SocketAddr,
|
||||
pub dir: TempDir,
|
||||
pub ca: TestCa,
|
||||
pub config_text: String,
|
||||
}
|
||||
|
||||
impl Edge {
|
||||
pub fn roots(&self) -> Arc<RootCertStore> {
|
||||
self.ca.roots()
|
||||
}
|
||||
|
||||
pub fn stderr(&self) -> String {
|
||||
std::fs::read_to_string(self.dir.join("edge.stderr")).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn access_log(&self) -> String {
|
||||
std::fs::read_to_string(self.dir.join("logs/access.log")).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Everything edge wrote or was given, except the credentials themselves.
|
||||
pub fn all_output(&self) -> String {
|
||||
format!("{}\n{}\n{}", self.stderr(), self.access_log(), self.config_text)
|
||||
}
|
||||
|
||||
pub fn exited(&mut self) -> Option<std::process::ExitStatus> {
|
||||
self.child.0.try_wait().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EdgeSpec<'a> {
|
||||
pub site: &'a str,
|
||||
pub hosts: Vec<(String, String)>,
|
||||
pub secrets: Vec<(String, Vec<u8>)>,
|
||||
pub log_level: &'a str,
|
||||
pub extra_args: Vec<String>,
|
||||
}
|
||||
|
||||
impl Default for EdgeSpec<'_> {
|
||||
fn default() -> Self {
|
||||
EdgeSpec { site: "", hosts: Vec::new(), secrets: Vec::new(), log_level: "info", extra_args: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Compile, write certificates and credentials, start the binary. Does not wait for readiness.
|
||||
pub fn spawn(spec: EdgeSpec<'_>) -> Edge {
|
||||
let dir = TempDir::new("edge");
|
||||
for d in ["certs", "state", "creds", "logs"] {
|
||||
std::fs::create_dir_all(dir.join(d)).unwrap();
|
||||
}
|
||||
let sources: Vec<SourceFile> = spec.hosts.iter().map(|(n, c)| SourceFile { name: n, contents: c }).collect();
|
||||
let model = load(&SourceFile { name: "ianitor.toml", contents: spec.site }, &sources).expect("valid routes");
|
||||
|
||||
let ca = TestCa::new("Ianitor Test CA");
|
||||
for h in model.hosts() {
|
||||
if h.tls == TlsMode::Domi {
|
||||
let leaf = ca.issue(&[h.hostname.as_str()]);
|
||||
std::fs::write(dir.join(&format!("certs/{}.crt", h.hostname)), leaf.cert_pem).unwrap();
|
||||
std::fs::write(dir.join(&format!("certs/{}.key", h.hostname)), leaf.key_pem).unwrap();
|
||||
}
|
||||
}
|
||||
for (name, value) in &spec.secrets {
|
||||
std::fs::write(dir.join(&format!("creds/{name}")), value).unwrap();
|
||||
}
|
||||
|
||||
let http = local(free_port());
|
||||
let https = local(free_port());
|
||||
let opts = CompileOptions {
|
||||
http_listen: http,
|
||||
https_listen: https,
|
||||
cert_dir: dir.str("certs"),
|
||||
state_dir: dir.str("state"),
|
||||
access_log: dir.str("logs/access.log"),
|
||||
caddy_credentials_dir: "/run/credentials/caddy-fallback.service".into(),
|
||||
caddy_admin: None,
|
||||
};
|
||||
let config_text = compile::edge(&model, &opts).unwrap();
|
||||
std::fs::write(dir.join("edge.json"), &config_text).unwrap();
|
||||
|
||||
let stderr = std::fs::File::create(dir.join("edge.stderr")).unwrap();
|
||||
let mut cmd = Command::new(env!("CARGO_BIN_EXE_ianitor-edge"));
|
||||
cmd.arg("--config").arg(dir.join("edge.json"));
|
||||
cmd.args(&spec.extra_args);
|
||||
cmd.env("CREDENTIALS_DIRECTORY", dir.join("creds"));
|
||||
cmd.env("IANITOR_LOG", spec.log_level);
|
||||
cmd.stdout(Stdio::from(stderr.try_clone().unwrap())).stderr(Stdio::from(stderr));
|
||||
let child = ChildGuard(cmd.spawn().expect("spawn ianitor-edge"));
|
||||
Edge { child, http, https, dir, ca, config_text }
|
||||
}
|
||||
|
||||
/// Spawn and wait until both listeners accept connections.
|
||||
pub fn start(spec: EdgeSpec<'_>) -> Edge {
|
||||
let mut e = spawn(spec);
|
||||
let ok = wait_for_port(e.https, Duration::from_secs(20)) && wait_for_port(e.http, Duration::from_secs(5));
|
||||
if !ok || e.exited().is_some() {
|
||||
panic!("ianitor-edge did not start:\n{}", e.stderr());
|
||||
}
|
||||
e
|
||||
}
|
||||
|
||||
pub fn host_file(hostname: &str, body: &str) -> (String, String) {
|
||||
(format!("hosts/{hostname}.toml"), format!("hostname = \"{hostname}\"\ntls = \"domi\"\n{body}"))
|
||||
}
|
||||
Reference in New Issue
Block a user