Files
ianitor/crates/core/src/compile/edge.rs
T

97 lines
3.9 KiB
Rust

// 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/.
//! Model → [`EdgeConfig`] for the Pingora data plane.
use super::{CompileOptions, DEVICE_ALLOW_FILE, DEVICE_CA_FILE, join, strip_headers};
use crate::edge_config::{
EDGE_CONFIG_VERSION, EdgeAcme, EdgeConfig, EdgeDeviceCerts, EdgeForwardAuth, EdgeHeaderOp,
EdgeHealthCheck, EdgeHost, EdgeTls, EdgeUpstream, EdgeValue,
};
use crate::{AccessLog, AuthMode, HeaderRule, HeaderValue, Model, Scheme, TlsMode, Upstream};
pub(super) fn compile(model: &Model, o: &CompileOptions) -> EdgeConfig {
let site = model.site();
let any_acme = model.hosts().iter().any(|h| h.tls == TlsMode::LetsEncrypt);
EdgeConfig {
version: EDGE_CONFIG_VERSION,
http_listen: o.http_listen.to_string(),
https_listen: o.https_listen.to_string(),
https_port: o.https_listen.port(),
cert_dir: o.cert_dir.clone(),
state_dir: o.state_dir.clone(),
access_log: o.access_log.clone(),
acme: any_acme.then(|| EdgeAcme {
directory: site.acme.directory.clone(),
email: site.acme.email.clone(),
}),
device_certs: model
.hosts()
.iter()
.any(|h| h.device_certs)
.then(|| EdgeDeviceCerts {
ca: join(&o.cert_dir, DEVICE_CA_FILE),
allow: join(&o.cert_dir, DEVICE_ALLOW_FILE),
}),
trusted_proxies: o.trusted_proxies.iter().map(ToString::to_string).collect(),
forward_auth: site.forward_auth.as_ref().map(|fa| EdgeForwardAuth {
upstream: upstream(&fa.upstream),
path_and_query: fa.path_and_query.clone(),
copy_headers: fa.copy_headers.clone(),
strip_headers: strip_headers(fa),
}),
hosts: model
.hosts()
.iter()
.map(|h| EdgeHost {
hostname: h.hostname.to_string(),
tls: match h.tls {
TlsMode::LetsEncrypt => EdgeTls::Acme,
TlsMode::Domi => EdgeTls::Files {
cert: join(&o.cert_dir, &format!("{}.crt", h.hostname)),
key: join(&o.cert_dir, &format!("{}.key", h.hostname)),
},
},
upstreams: h.upstreams.iter().map(upstream).collect(),
forward_auth: h.auth == AuthMode::ForwardAuth,
websockets: h.websockets,
request_headers: h.request_headers.iter().map(op).collect(),
response_headers: h.response_headers.iter().map(op).collect(),
health_check: h.health_check.as_ref().map(|hc| EdgeHealthCheck {
path: hc.path.clone(),
interval_secs: hc.interval_secs,
}),
access_log: h.access_log == AccessLog::Truncated,
share_links: h.share_links,
device_certs: h.device_certs,
})
.collect(),
}
}
fn upstream(u: &Upstream) -> EdgeUpstream {
EdgeUpstream {
address: u.authority(),
tls: u.scheme == Scheme::Https,
// Like Go's TLS client in Caddy: the dial host is the server name; for an IP literal
// rustls sends no SNI and checks the IP SAN.
sni: u.host_str(),
}
}
fn op(r: &HeaderRule) -> EdgeHeaderOp {
match r {
HeaderRule::Set { name, value } => EdgeHeaderOp::Set {
name: name.clone(),
value: match value {
HeaderValue::Literal(v) => EdgeValue::Literal { value: v.clone() },
HeaderValue::Secret(s) => EdgeValue::Secret {
name: s.to_string(),
},
},
},
HeaderRule::Remove { name } => EdgeHeaderOp::Remove { name: name.clone() },
}
}