use crate::resolver::Source;
use serde::{Deserialize, Serialize};
use std::time::{Duration, SystemTime};
pub const BACKOFF_MIN: Duration = Duration::from_secs(3600);
pub const BACKOFF_MAX: Duration = Duration::from_secs(86400);
pub const FORCE_MIN_AGE: Duration = Duration::from_secs(86400);
pub const ARI_DEFAULT_RECHECK: Duration = Duration::from_secs(6 * 3600);
pub fn backoff(failures: u32) -> Duration {
if failures == 0 {
return Duration::ZERO;
}
let f = 1u64 << (failures - 1).min(16);
(BACKOFF_MIN * f as u32).min(BACKOFF_MAX)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Window {
pub start: SystemTime,
pub end: SystemTime,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Persisted {
pub failures: u32,
pub last_failure: Option<SystemTime>,
pub last_error: Option<String>,
pub last_issued: Option<SystemTime>,
pub ari: Option<(String, Window)>,
pub ari_recheck: Option<SystemTime>,
}
pub fn pick(w: Window, seed: &str) -> SystemTime {
use sha2::Digest;
let span = w.end.duration_since(w.start).unwrap_or_default();
if span.is_zero() {
return w.start;
}
let h = sha2::Sha256::digest(seed.as_bytes());
let x = u64::from_be_bytes(h[..8].try_into().unwrap());
w.start + Duration::from_secs(x % span.as_secs().max(1))
}
pub fn fallback(not_before: SystemTime, not_after: SystemTime) -> SystemTime {
let life = not_after.duration_since(not_before).unwrap_or_default();
not_before + life * 2 / 3
}
pub fn next_attempt(due: SystemTime, st: &Persisted) -> SystemTime {
match st.last_failure {
Some(t) if st.failures > 0 => (t + backoff(st.failures)).max(due),
_ => due,
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Schedule {
pub cert: Option<crate::CertInfo>,
pub due: SystemTime,
pub next_attempt: SystemTime,
pub ari: bool,
pub failures: u32,
pub last_error: Option<String>,
pub last_issued: Option<SystemTime>,
}
impl Schedule {
pub fn is_due(&self, now: SystemTime) -> bool {
self.next_attempt <= now
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
Renewed(crate::CertInfo),
NotDue(SystemTime),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum State {
Missing,
Issuing,
Valid,
Failing,
Expired,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Status {
pub set: String,
pub names: Vec<String>,
pub state: State,
pub source: Option<Source>,
pub not_after: Option<SystemTime>,
pub next_attempt: Option<SystemTime>,
pub failures: u32,
pub last_error: Option<String>,
pub ari: bool,
}
impl Status {
pub fn missing(set: &str, names: &[String]) -> Self {
Self {
set: set.to_string(),
names: names.to_vec(),
state: State::Missing,
source: None,
not_after: None,
next_attempt: None,
failures: 0,
last_error: None,
ari: false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn backoff_doubles_to_a_day() {
let h = |n| backoff(n).as_secs() / 3600;
assert_eq!([h(0), h(1), h(2), h(3), h(5), h(6), h(40)], [0, 1, 2, 4, 16, 24, 24]);
}
#[test]
fn fallback_is_two_thirds() {
let nb = SystemTime::UNIX_EPOCH + Duration::from_secs(1000);
assert_eq!(fallback(nb, nb + Duration::from_secs(90 * 86400)), nb + Duration::from_secs(60 * 86400));
}
#[test]
fn picks_stay_in_the_window_and_are_stable() {
let s = SystemTime::now();
let w = Window { start: s, end: s + Duration::from_secs(86400) };
let a = pick(w, "serial-a");
assert!(a >= w.start && a < w.end);
assert_eq!(a, pick(w, "serial-a"));
assert_ne!(a, pick(w, "serial-b"));
assert_eq!(pick(Window { start: s, end: s }, "x"), s);
}
#[test]
fn failures_push_the_next_attempt() {
let now = SystemTime::now();
let st = Persisted { failures: 2, last_failure: Some(now), ..Default::default() };
assert_eq!(next_attempt(now - Duration::from_secs(10), &st), now + Duration::from_secs(7200));
assert_eq!(next_attempt(now, &Persisted::default()), now);
}
}