[protocol] Protocol v1: enrollment codes and book, messages, job guard, agent registry, release check, archive paths (32 tests)
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
// 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/.
|
||||
|
||||
//! The agent's job guard: expired, future, too long-lived and replayed jobs are refused.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::msg::JobHeader;
|
||||
|
||||
/// A job lives at most an hour.
|
||||
pub const MAX_JOB_SECONDS: i64 = 3600;
|
||||
/// Clock skew allowed between agent and server.
|
||||
pub const SKEW_SECONDS: i64 = 300;
|
||||
/// Seen ids kept at most (a flood of jobs cannot exhaust memory).
|
||||
const MAX_SEEN: usize = 100_000;
|
||||
|
||||
/// Why a job is refused before anything else is looked at.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum GuardError {
|
||||
/// The id is not 32 lowercase hex characters.
|
||||
#[error("the job id is malformed")]
|
||||
BadId,
|
||||
/// Past its expiry.
|
||||
#[error("the job has expired")]
|
||||
Expired,
|
||||
/// Issued in the future (beyond the allowed skew).
|
||||
#[error("the job was issued in the future")]
|
||||
Future,
|
||||
/// Lives longer than an hour.
|
||||
#[error("the job would live longer than an hour")]
|
||||
TooLong,
|
||||
/// Seen before.
|
||||
#[error("the job was already received once")]
|
||||
Replayed,
|
||||
/// Too many live jobs at once.
|
||||
#[error("too many jobs at once")]
|
||||
Full,
|
||||
}
|
||||
|
||||
impl GuardError {
|
||||
/// The stable code used in reports.
|
||||
pub fn code(&self) -> &'static str {
|
||||
match self {
|
||||
GuardError::BadId => "bad_request",
|
||||
GuardError::Expired => "expired",
|
||||
GuardError::Future => "future",
|
||||
GuardError::TooLong => "expired",
|
||||
GuardError::Replayed => "replayed",
|
||||
GuardError::Full => "busy",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The ids of jobs seen and not yet expired. Saved to the agent's state so a restart does not
|
||||
/// open a replay window.
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||
pub struct JobGuard {
|
||||
seen: BTreeMap<String, i64>,
|
||||
}
|
||||
|
||||
pub(crate) fn valid_job_id(id: &str) -> bool {
|
||||
id.len() == 32 && id.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
|
||||
}
|
||||
|
||||
impl JobGuard {
|
||||
/// An empty guard.
|
||||
pub fn new() -> JobGuard {
|
||||
JobGuard::default()
|
||||
}
|
||||
|
||||
/// Accept the job at `now` (and remember its id), or say why not.
|
||||
pub fn check(&mut self, h: &JobHeader, now: i64) -> Result<(), GuardError> {
|
||||
self.prune(now);
|
||||
if !valid_job_id(&h.id) {
|
||||
return Err(GuardError::BadId);
|
||||
}
|
||||
if h.expires_at <= now {
|
||||
return Err(GuardError::Expired);
|
||||
}
|
||||
if h.expires_at.saturating_sub(h.issued_at) > MAX_JOB_SECONDS {
|
||||
return Err(GuardError::TooLong);
|
||||
}
|
||||
if h.issued_at > now.saturating_add(SKEW_SECONDS) {
|
||||
return Err(GuardError::Future);
|
||||
}
|
||||
if self.seen.contains_key(&h.id) {
|
||||
return Err(GuardError::Replayed);
|
||||
}
|
||||
if self.seen.len() >= MAX_SEEN {
|
||||
return Err(GuardError::Full);
|
||||
}
|
||||
self.seen.insert(h.id.clone(), h.expires_at);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Forget ids whose jobs have expired by `now` (they would be refused as expired anyway).
|
||||
pub fn prune(&mut self, now: i64) {
|
||||
self.seen.retain(|_, exp| *exp > now);
|
||||
}
|
||||
|
||||
/// How many ids are remembered.
|
||||
pub fn len(&self) -> usize {
|
||||
self.seen.len()
|
||||
}
|
||||
|
||||
/// No ids remembered.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.seen.is_empty()
|
||||
}
|
||||
|
||||
/// `<id> <expires_at>` per line.
|
||||
pub fn save(&self) -> String {
|
||||
self.seen
|
||||
.iter()
|
||||
.map(|(id, exp)| format!("{id} {exp}\n"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Read what [`save`](Self::save) wrote; anything else is an error.
|
||||
pub fn load(text: &str) -> Result<JobGuard, GuardError> {
|
||||
let mut seen = BTreeMap::new();
|
||||
for line in text.lines().filter(|l| !l.trim().is_empty()) {
|
||||
let (id, exp) = line.trim().split_once(' ').ok_or(GuardError::BadId)?;
|
||||
let exp: i64 = exp.parse().map_err(|_| GuardError::BadId)?;
|
||||
if !valid_job_id(id) || seen.len() >= MAX_SEEN {
|
||||
return Err(GuardError::BadId);
|
||||
}
|
||||
seen.insert(id.to_owned(), exp);
|
||||
}
|
||||
Ok(JobGuard { seen })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user