[edge] Reload and share links: in-process config swap with self-reverting trials, admin socket with health and certificate status, share-link cookies, visitor pages and throttle

This commit is contained in:
2026-11-07 17:49:53 +01:00
parent c0ed9c65c5
commit 6ce1fe9d2b
18 changed files with 2137 additions and 186 deletions
+80 -15
View File
@@ -5,23 +5,29 @@
//! Ianitor edge: the Pingora data plane. It reads the config compiled by `ianitor-core`
//! and serves it; it never decides policy.
//!
//! Services: HTTPS reverse proxy (SNI certificates, forward-auth, websockets, header rules,
//! round-robin over healthy upstreams), plain-HTTP listener (ACME HTTP-01 and redirects),
//! health checks, ACME issuance and renewal.
//! Services: HTTPS reverse proxy (SNI certificates, forward-auth, share links, websockets,
//! header rules, round-robin over healthy upstreams), plain-HTTP listener (ACME HTTP-01 and
//! redirects), health checks, ACME issuance and renewal, and the admin socket through which
//! control reloads the config in process (see `shared`).
pub mod acme;
#[cfg(unix)]
pub mod admin;
pub mod error;
pub mod fetch;
pub mod forward_auth;
pub mod health;
pub mod http_app;
pub mod logging;
pub mod pages;
pub mod proxy;
pub mod runtime;
pub mod secrets;
pub mod share;
pub mod shared;
pub mod tls;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use pingora::connectors::http::Connector;
@@ -34,24 +40,63 @@ use pingora::services::listening::Service;
pub use error::EdgeError;
pub use runtime::Runtime;
pub use shared::Shared;
#[derive(Debug, Clone, Default)]
pub struct Options {
/// The compiled edge config (`ianitor-core` edge compiler output).
pub config: PathBuf,
/// The trial config control writes before an apply; default `<config>.trial`.
pub trial_config: Option<PathBuf>,
/// Admin socket for control (Unix only); none when `None`.
pub admin_socket: Option<PathBuf>,
/// The share-link revocation file control writes; without it, share links are refused.
pub share_revocations: Option<PathBuf>,
/// Extra trust root for the ACME directory's TLS (Pebble in tests).
pub acme_root: Option<PathBuf>,
}
impl Options {
pub fn trial_path(&self) -> PathBuf {
self.trial_config
.clone()
.unwrap_or_else(|| default_trial_path(&self.config))
}
}
/// `<config>.trial`, next to the config.
pub fn default_trial_path(config: &Path) -> PathBuf {
let mut name = config.file_name().unwrap_or_default().to_os_string();
name.push(".trial");
config.with_file_name(name)
}
fn credentials_dir() -> Option<PathBuf> {
std::env::var_os("CREDENTIALS_DIRECTORY").map(PathBuf::from)
}
/// Read the config, load secrets from `$CREDENTIALS_DIRECTORY`, load certificates.
pub fn prepare(opts: &Options) -> Result<Arc<Runtime>, EdgeError> {
let cfg = runtime::read_config(&opts.config)?;
let creds = std::env::var_os("CREDENTIALS_DIRECTORY").map(PathBuf::from);
runtime::build(cfg, creds.as_deref()).map(Arc::new)
pub fn prepare(opts: &Options) -> Result<Arc<Shared>, EdgeError> {
let creds = credentials_dir();
let rt = runtime::build_file(&opts.config, creds.as_deref())?;
Ok(shared_for(rt, opts))
}
/// Wrap an already built runtime (tests run edge in process this way).
pub fn shared_for(rt: Runtime, opts: &Options) -> Arc<Shared> {
Shared::new(
rt,
shared::Paths {
config: opts.config.clone(),
trial: opts.trial_path(),
revocations: opts.share_revocations.clone(),
},
credentials_dir(),
)
}
/// Assemble the Pingora server with all services. Listeners bind when it runs.
pub fn server(rt: Arc<Runtime>, opts: &Options) -> Result<Server, EdgeError> {
pub fn server(shared: Arc<Shared>, opts: &Options) -> Result<Server, EdgeError> {
pingora::tls::install_default_crypto_provider();
let conf = ServerConf {
threads: 2,
@@ -63,11 +108,12 @@ pub fn server(rt: Arc<Runtime>, opts: &Options) -> Result<Server, EdgeError> {
server.bootstrap();
let challenges = Arc::new(acme::Challenges::default());
let rt = shared.current();
let mut https = http_proxy_service(
&server.configuration,
proxy::EdgeProxy {
rt: rt.clone(),
shared: shared.clone(),
connector: Connector::new(None),
},
);
@@ -75,14 +121,14 @@ pub fn server(rt: Arc<Runtime>, opts: &Options) -> Result<Server, EdgeError> {
address: rt.cfg.https_listen.clone(),
reason: e.to_string(),
})?;
tls.set_cert_resolver(rt.certs.clone());
tls.set_cert_resolver(shared.certs.clone());
tls.enable_h2();
https.add_tls_with_settings(&rt.cfg.https_listen, None, tls);
let mut http = Service::new(
"ianitor http".to_owned(),
http_app::HttpApp {
rt: rt.clone(),
shared: shared.clone(),
challenges: challenges.clone(),
},
);
@@ -92,17 +138,36 @@ pub fn server(rt: Arc<Runtime>, opts: &Options) -> Result<Server, EdgeError> {
server.add_service(http);
server.add_service(background_service(
"ianitor health checks",
health::HealthChecks { rt: rt.clone() },
health::HealthChecks {
shared: shared.clone(),
},
));
server.add_service(background_service(
"ianitor acme",
acme::AcmeService {
rt: rt.clone(),
certs: rt.certs.clone(),
shared: shared.clone(),
challenges,
ca_root: opts.acme_root.clone(),
},
));
#[cfg(unix)]
{
server.add_service(background_service(
"ianitor share revocations",
admin::RevocationPoller {
shared: shared.clone(),
},
));
if let Some(path) = &opts.admin_socket {
server.add_service(background_service(
"ianitor admin socket",
admin::AdminSocket {
shared: shared.clone(),
path: path.clone(),
},
));
}
}
Ok(server)
}