[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
+89 -31
View File
@@ -21,13 +21,14 @@ use pingora::server::ShutdownWatch;
use pingora::services::background::BackgroundService;
use crate::logging::now_secs;
use crate::runtime::Runtime;
use crate::tls::{CertStore, certified_key, not_after};
use crate::shared::{CertState, Shared};
use crate::tls::{certified_key, not_after};
const RENEW_BEFORE_SECS: f64 = 30.0 * 86_400.0;
const CHECK_EVERY: Duration = Duration::from_secs(12 * 3600);
const FIRST_RETRY: Duration = Duration::from_secs(30);
const MAX_RETRY: Duration = Duration::from_secs(3600);
const MIN_GAP: Duration = Duration::from_secs(5);
/// HTTP-01 tokens → key authorizations, shared with the HTTP listener.
#[derive(Default)]
@@ -52,8 +53,7 @@ impl Challenges {
}
pub struct AcmeService {
pub rt: Arc<Runtime>,
pub certs: Arc<CertStore>,
pub shared: Arc<Shared>,
pub challenges: Arc<Challenges>,
/// Extra trust root for the ACME directory (tests against Pebble only).
pub ca_root: Option<PathBuf>,
@@ -84,7 +84,7 @@ fn fnv(data: &[u8]) -> String {
impl AcmeService {
fn dir(&self) -> PathBuf {
self.rt.acme_dir()
self.shared.current().acme_dir()
}
fn paths(&self, host: &str) -> (PathBuf, PathBuf) {
@@ -102,7 +102,8 @@ impl AcmeService {
let expiry = not_after(&cert_pem)?;
match certified_key(&cert_pem, &key_pem) {
Ok(ck) => {
self.certs.insert(host, ck);
self.shared.certs.insert(host, ck);
self.ok(host, expiry);
Some(expiry)
}
Err(e) => {
@@ -207,18 +208,30 @@ impl AcmeService {
let (crt, key) = self.paths(host);
write_private(&key, key_pem.as_bytes())?;
write_private(&crt, chain_pem.as_bytes())?;
self.certs.insert(host, ck);
self.shared.certs.insert(host, ck);
self.ok(host, expiry);
log::info!(target: "ianitor::acme", "{host}: certificate issued, valid until {expiry}");
Ok(expiry)
}
/// One pass over every ACME host. Returns false if any host failed.
fn ok(&self, host: &str, expiry: i64) {
self.shared.set_cert_state(
host,
CertState {
state: "ok",
not_after: Some(expiry),
error: None,
},
);
}
async fn round(&self, expiries: &mut HashMap<String, i64>) -> bool {
let Some(acme) = &self.rt.cfg.acme else {
let rt = self.shared.current();
let Some(acme) = &rt.cfg.acme else {
return true;
};
let due: Vec<String> = self
.rt
let due: Vec<String> = rt
.acme_hosts()
.into_iter()
.filter(|h| {
@@ -230,10 +243,25 @@ impl AcmeService {
if due.is_empty() {
return true;
}
for h in &due {
if !expiries.contains_key(h) {
self.shared.set_cert_state(
h,
CertState {
state: "pending",
not_after: None,
error: None,
},
);
}
}
let account = match self.account(&acme.directory, acme.email.as_deref()).await {
Ok(a) => a,
Err(e) => {
log::error!(target: "ianitor::acme", "{e}");
for h in &due {
self.failed(h, expiries.get(h).copied(), &e);
}
return false;
}
};
@@ -245,43 +273,73 @@ impl AcmeService {
}
Err(e) => {
log::error!(target: "ianitor::acme", "{host}: could not obtain a certificate: {e}");
self.failed(&host, expiries.get(&host).copied(), &e);
ok = false;
}
}
}
ok
}
/// A host that still has a valid certificate stays `ok` while renewal retries.
fn failed(&self, host: &str, expiry: Option<i64>, error: &str) {
let still_valid = expiry.is_some_and(|e| (e as f64) > now_secs());
self.shared.set_cert_state(
host,
CertState {
state: if still_valid { "ok" } else { "failed" },
not_after: expiry,
error: Some(error.chars().take(300).collect()),
},
);
}
}
#[async_trait]
impl BackgroundService for AcmeService {
async fn start(&self, mut shutdown: ShutdownWatch) {
if self.rt.acme_hosts().is_empty() {
return;
}
let dir = self.dir();
if let Err(e) = std::fs::create_dir_all(&dir) {
log::error!(target: "ianitor::acme", "cannot create {}: {e}", dir.display());
return;
}
let mut expiries = HashMap::new();
for h in self.rt.acme_hosts() {
if let Some(e) = self.load_stored(&h) {
expiries.insert(h, e);
}
}
let mut retry = FIRST_RETRY;
let mut last: Option<std::time::Instant> = None;
loop {
let wait = if self.round(&mut expiries).await {
retry = FIRST_RETRY;
CHECK_EVERY
} else {
let w = retry;
retry = (retry * 2).min(MAX_RETRY);
w
};
// Reloads can come many times a second; ACME rounds at most every 5 s.
if let Some(l) = last
&& l.elapsed() < MIN_GAP
{
tokio::select! {
_ = tokio::time::sleep(MIN_GAP - l.elapsed()) => {}
_ = shutdown.changed() => return,
}
}
last = Some(std::time::Instant::now());
let hosts = self.shared.current().acme_hosts();
let mut wait = CHECK_EVERY;
if !hosts.is_empty() {
let dir = self.dir();
if let Err(e) = std::fs::create_dir_all(&dir) {
log::error!(target: "ianitor::acme", "cannot create {}: {e}", dir.display());
} else {
for h in hosts {
if !expiries.contains_key(&h)
&& let Some(e) = self.load_stored(&h)
{
expiries.insert(h, e);
}
}
wait = if self.round(&mut expiries).await {
retry = FIRST_RETRY;
CHECK_EVERY
} else {
let w = retry;
retry = (retry * 2).min(MAX_RETRY);
w
};
}
}
// Woken early by a reload (new hosts) or a "try again" from control.
tokio::select! {
_ = tokio::time::sleep(wait) => {}
_ = self.shared.acme_wake.notified() => {}
_ = shutdown.changed() => return,
}
}