[edge] Proxy: Pingora data plane with SNI certs, forward-auth, header rules, health checks, ACME
This commit is contained in:
+125
-1
@@ -2,4 +2,128 @@
|
||||
// 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/.
|
||||
|
||||
//! Ianitor edge: the Pingora data plane.
|
||||
//! 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.
|
||||
|
||||
pub mod acme;
|
||||
pub mod error;
|
||||
pub mod fetch;
|
||||
pub mod forward_auth;
|
||||
pub mod health;
|
||||
pub mod http_app;
|
||||
pub mod logging;
|
||||
pub mod proxy;
|
||||
pub mod runtime;
|
||||
pub mod secrets;
|
||||
pub mod tls;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use pingora::connectors::http::Connector;
|
||||
use pingora::listeners::tls::TlsSettings;
|
||||
use pingora::proxy::http_proxy_service;
|
||||
use pingora::server::Server;
|
||||
use pingora::server::configuration::ServerConf;
|
||||
use pingora::services::background::background_service;
|
||||
use pingora::services::listening::Service;
|
||||
|
||||
pub use error::EdgeError;
|
||||
pub use runtime::Runtime;
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct Options {
|
||||
/// The compiled edge config (`ianitor-core` edge compiler output).
|
||||
pub config: PathBuf,
|
||||
/// Extra trust root for the ACME directory's TLS (Pebble in tests).
|
||||
pub acme_root: Option<PathBuf>,
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
|
||||
/// Assemble the Pingora server with all services. Listeners bind when it runs.
|
||||
pub fn server(rt: Arc<Runtime>, opts: &Options) -> Result<Server, EdgeError> {
|
||||
pingora::tls::install_default_crypto_provider();
|
||||
let conf = ServerConf {
|
||||
threads: 2,
|
||||
grace_period_seconds: Some(5),
|
||||
graceful_shutdown_timeout_seconds: Some(10),
|
||||
..ServerConf::default()
|
||||
};
|
||||
let mut server = Server::new_with_opt_and_conf(None, conf);
|
||||
server.bootstrap();
|
||||
|
||||
let challenges = Arc::new(acme::Challenges::default());
|
||||
|
||||
let mut https = http_proxy_service(
|
||||
&server.configuration,
|
||||
proxy::EdgeProxy {
|
||||
rt: rt.clone(),
|
||||
connector: Connector::new(None),
|
||||
},
|
||||
);
|
||||
let mut tls = TlsSettings::intermediate("", "").map_err(|e| EdgeError::Listen {
|
||||
address: rt.cfg.https_listen.clone(),
|
||||
reason: e.to_string(),
|
||||
})?;
|
||||
tls.set_cert_resolver(rt.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(),
|
||||
challenges: challenges.clone(),
|
||||
},
|
||||
);
|
||||
http.add_tcp(&rt.cfg.http_listen);
|
||||
|
||||
server.add_service(https);
|
||||
server.add_service(http);
|
||||
server.add_service(background_service(
|
||||
"ianitor health checks",
|
||||
health::HealthChecks { rt: rt.clone() },
|
||||
));
|
||||
server.add_service(background_service(
|
||||
"ianitor acme",
|
||||
acme::AcmeService {
|
||||
rt: rt.clone(),
|
||||
certs: rt.certs.clone(),
|
||||
challenges,
|
||||
ca_root: opts.acme_root.clone(),
|
||||
},
|
||||
));
|
||||
Ok(server)
|
||||
}
|
||||
|
||||
/// Run until `stop` resolves (or is dropped). For running edge inside a test process.
|
||||
#[cfg(unix)]
|
||||
pub fn run_until(server: Server, stop: tokio::sync::oneshot::Receiver<()>) {
|
||||
use pingora::server::{RunArgs, ShutdownSignal, ShutdownSignalWatch};
|
||||
|
||||
struct Stop(tokio::sync::Mutex<Option<tokio::sync::oneshot::Receiver<()>>>);
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl ShutdownSignalWatch for Stop {
|
||||
async fn recv(&self) -> ShutdownSignal {
|
||||
if let Some(rx) = self.0.lock().await.take() {
|
||||
let _ = rx.await;
|
||||
}
|
||||
ShutdownSignal::FastShutdown
|
||||
}
|
||||
}
|
||||
|
||||
server.run(RunArgs {
|
||||
shutdown_signal: Box::new(Stop(tokio::sync::Mutex::new(Some(stop)))),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user