[control] Fix round 1: changes apply while Caddy serves (admin socket, watch through Caddy), revocations that fail are errors and catch up, proof only on the forward-auth UI host, root keys found by content, intermediates checked before install, stale SNI cleared before each handshake
This commit is contained in:
@@ -335,7 +335,10 @@ impl BackgroundService for RevocationPoller {
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = shutdown.changed() => return,
|
||||
_ = tokio::time::sleep(Duration::from_secs(5)) => self.shared.poll_revocations(),
|
||||
_ = tokio::time::sleep(Duration::from_secs(5)) => {
|
||||
self.shared.poll_revocations();
|
||||
self.shared.poll_device_trust();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,6 +130,9 @@ pub fn server(shared: Arc<Shared>, opts: &Options) -> Result<Server, EdgeError>
|
||||
tls.set_client_cert_verifier(shared.devices.clone());
|
||||
tls.enable_h2();
|
||||
https.add_tls_with_settings(&rt.cfg.https_listen, None, tls);
|
||||
https
|
||||
.endpoints()
|
||||
.set_pre_tls_callback(Arc::new(tls::HandshakeStart));
|
||||
|
||||
let mut http = Service::new(
|
||||
"ianitor http".to_owned(),
|
||||
|
||||
@@ -175,6 +175,16 @@ impl ProxyHttp for EdgeProxy {
|
||||
}
|
||||
}
|
||||
|
||||
if host.sends_proof {
|
||||
// Control trusts Remote-* only next to its proof; a client never supplies either.
|
||||
let req = session.req_header_mut();
|
||||
for name in ianitor_core::AUTHELIA_COPY_HEADERS
|
||||
.iter()
|
||||
.chain(&["X-Ianitor-Proof"])
|
||||
{
|
||||
req.remove_header(*name);
|
||||
}
|
||||
}
|
||||
if host.cfg.forward_auth {
|
||||
let fa = rt.forward_auth.as_ref().ok_or_else(|| {
|
||||
Error::explain(ErrorType::HTTPStatus(500), "forward-auth is not configured")
|
||||
|
||||
@@ -97,6 +97,9 @@ pub struct HostRt {
|
||||
pub response_ops: Vec<Op>,
|
||||
/// Values of the secrets this host injects; a response header containing one is dropped.
|
||||
pub secret_values: Vec<Vec<u8>>,
|
||||
/// The host sends control's UI proof upstream: whatever the client says about who it is
|
||||
/// (Remote-*) or about the proof is dropped first, with or without forward-auth.
|
||||
pub sends_proof: bool,
|
||||
}
|
||||
|
||||
pub struct ForwardAuthRt {
|
||||
@@ -378,6 +381,9 @@ pub fn build(cfg: EdgeConfig, credentials_dir: Option<&Path>) -> Result<Runtime,
|
||||
let mut secret_values = Vec::new();
|
||||
let request_ops = ops(&h.request_headers, &secrets, &mut secret_values)?;
|
||||
let response_ops = ops(&h.response_headers, &secrets, &mut secret_values)?;
|
||||
let sends_proof = h.request_headers.iter().any(|op| {
|
||||
matches!(op, EdgeHeaderOp::Set { value: EdgeValue::Secret { name }, .. } if name == ianitor_core::UI_PROOF_SECRET)
|
||||
});
|
||||
let upstreams = h
|
||||
.upstreams
|
||||
.iter()
|
||||
@@ -398,6 +404,7 @@ pub fn build(cfg: EdgeConfig, credentials_dir: Option<&Path>) -> Result<Runtime,
|
||||
request_ops,
|
||||
response_ops,
|
||||
secret_values,
|
||||
sends_proof,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -96,6 +96,10 @@ pub struct Shared {
|
||||
pub certs: Arc<CertStore>,
|
||||
/// The listener's device-certificate check; its trust follows the runtime.
|
||||
pub devices: Arc<crate::tls::DeviceVerifier>,
|
||||
/// Serialises building the device trust with installing a runtime.
|
||||
trust_lock: Mutex<()>,
|
||||
/// Size and mtime of the device files when they were last read.
|
||||
device_stamp: Mutex<Option<DeviceStamp>>,
|
||||
generation: AtomicU64,
|
||||
changed: tokio::sync::watch::Sender<u64>,
|
||||
trial: Mutex<Option<Trial>>,
|
||||
@@ -142,6 +146,8 @@ impl Shared {
|
||||
current: ArcSwap::from_pointee(rt),
|
||||
certs,
|
||||
devices,
|
||||
trust_lock: Mutex::new(()),
|
||||
device_stamp: Mutex::new(None),
|
||||
generation: AtomicU64::new(1),
|
||||
changed,
|
||||
trial: Mutex::new(None),
|
||||
@@ -196,8 +202,12 @@ impl Shared {
|
||||
rt.device_trust.clone()
|
||||
}
|
||||
};
|
||||
// Trust and runtime change together (reload_device_trust checks the runtime under the
|
||||
// same lock).
|
||||
let guard = self.trust_lock.lock();
|
||||
self.devices.replace(trust);
|
||||
self.current.store(rt);
|
||||
drop(guard);
|
||||
self.generation.fetch_add(1, Ordering::SeqCst);
|
||||
self.notify();
|
||||
self.acme_wake.notify_one();
|
||||
@@ -530,11 +540,53 @@ impl Shared {
|
||||
/// Re-read the device CA bundle and allow list now (control calls this after an issue
|
||||
/// or a revocation; allowed during a trial). Returns how many devices are allowed.
|
||||
pub fn reload_device_trust(&self) -> Result<usize, String> {
|
||||
// Built from the runtime that serves when it is installed: if a reload swapped the
|
||||
// runtime meanwhile, build again from the new one (its host set may differ).
|
||||
for _ in 0..5 {
|
||||
let rt = self.current();
|
||||
let t = fresh_trust(&rt)?;
|
||||
let n = t.as_ref().map_or(0, |t| t.allowed());
|
||||
let _g = self
|
||||
.trust_lock
|
||||
.lock()
|
||||
.map_err(|_| "device lock".to_owned())?;
|
||||
if Arc::ptr_eq(&rt, &self.current()) {
|
||||
self.devices.replace(t);
|
||||
log::info!(target: "ianitor::tls", "device certificates re-read: {n} allowed");
|
||||
return Ok(n);
|
||||
}
|
||||
}
|
||||
Err("the config changed while the device certificates were read; try again".into())
|
||||
}
|
||||
|
||||
/// The backstop for revocations (every 5 s, like the share deny list): re-read the device
|
||||
/// files when they changed on disk, so a revocation counts even if control's nudge is lost.
|
||||
pub fn poll_device_trust(&self) {
|
||||
let rt = self.current();
|
||||
let t = fresh_trust(&rt)?;
|
||||
let n = t.as_ref().map_or(0, |t| t.allowed());
|
||||
self.devices.replace(t);
|
||||
log::info!(target: "ianitor::tls", "device certificates re-read: {n} allowed");
|
||||
Ok(n)
|
||||
let Some(dc) = &rt.cfg.device_certs else {
|
||||
return;
|
||||
};
|
||||
let stamp = |p: &str| {
|
||||
std::fs::metadata(p)
|
||||
.ok()
|
||||
.map(|m| (m.len(), m.modified().ok()))
|
||||
};
|
||||
let now = (stamp(&dc.ca), stamp(&dc.allow));
|
||||
let changed = match self.device_stamp.lock() {
|
||||
Ok(mut s) => {
|
||||
let c = s.as_ref() != Some(&now);
|
||||
*s = Some(now);
|
||||
c
|
||||
}
|
||||
Err(_) => true,
|
||||
};
|
||||
if changed && let Err(e) = self.reload_device_trust() {
|
||||
// Fail closed: an unreadable list lets no device in until it is fixed.
|
||||
log::error!(target: "ianitor::tls", "{e}; no device certificate is accepted until it is fixed");
|
||||
self.devices.replace(None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type FileStamp = Option<(u64, Option<std::time::SystemTime>)>;
|
||||
type DeviceStamp = (FileStamp, FileStamp);
|
||||
|
||||
@@ -48,6 +48,8 @@ impl CertStore {
|
||||
impl ResolvesServerCert for CertStore {
|
||||
fn resolve(&self, hello: ClientHello<'_>) -> Option<Arc<CertifiedKey>> {
|
||||
// No SNI or an unknown name: no certificate, the handshake fails (as with Caddy).
|
||||
// Whatever an earlier handshake on this thread left is not this one's.
|
||||
let _ = take_hello_sni();
|
||||
let name = hello.server_name()?.to_ascii_lowercase();
|
||||
let found = match self.files.load().get(&name) {
|
||||
Some(k) => Some(k.clone()),
|
||||
@@ -102,6 +104,30 @@ fn take_hello_sni() -> Option<String> {
|
||||
HELLO_SNI.with(|s| s.borrow_mut().take())
|
||||
}
|
||||
|
||||
/// Call before each TLS handshake, on the thread that creates its `ServerConnection` (edge
|
||||
/// does it in a pre-TLS hook): a handshake that ended after the certificate resolver but
|
||||
/// before the certificate request (failed, or resumed) leaves its name behind, and rustls asks
|
||||
/// the verifier once more when the next connection is created, before that connection's
|
||||
/// hello is read. A stale name there would turn off the TLS 1.2 transcript a client
|
||||
/// certificate needs.
|
||||
pub fn begin_handshake() {
|
||||
let _ = take_hello_sni();
|
||||
}
|
||||
|
||||
/// The pre-TLS hook that calls [`begin_handshake`].
|
||||
pub struct HandshakeStart;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl pingora::listeners::PreTlsProcess for HandshakeStart {
|
||||
async fn process(
|
||||
&self,
|
||||
_stream: &mut pingora::protocols::l4::stream::Stream,
|
||||
) -> pingora::Result<()> {
|
||||
begin_handshake();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// What one config says about device certificates.
|
||||
pub struct DeviceTrust {
|
||||
inner: Arc<dyn ClientCertVerifier>,
|
||||
|
||||
Reference in New Issue
Block a user