Keyed parent on ureq 3 (rustls + ring, bundled roots), no async runtime: config from the environment (secret never printed, JWT expiry logged), JSON line logs with a field allowlist (child stderr escaped and truncated), strict key validation before any URL, Backend trait over PostgREST RPCs and Storage objects with closed error kinds, one job per claim with the STORAGE.md §3.4 mapping (content codes, processing_error on signals / timeout / overflow, Broken stops the loop with exit 20, a refused commit deletes nothing), sweeper (expire pending, purge queue, quarantine leftovers), per-stage watchdog (exit 21), WORKER_ONCE for tests, run --check extended. 11 loop tests on a fake backend and decoder; sandbox tests and seccomp derivation unchanged (the child still makes the same 7 syscalls); image 11.6 MB.
108 lines
3.7 KiB
Rust
108 lines
3.7 KiB
Rust
//! What the parent needs from Supabase (STORAGE.md §2.2 RPCs and §3.3 Storage calls),
|
|
//! behind one trait so the job and sweep logic is tested on an in-memory fake and the HTTP
|
|
//! implementation stays thin.
|
|
|
|
use std::fmt;
|
|
|
|
use serde::Deserialize;
|
|
|
|
/// One claimed row of `assets` (`claim_avatar_jobs`).
|
|
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
|
pub struct Job {
|
|
pub asset_id: String,
|
|
pub owner_id: String,
|
|
pub bucket_id: String,
|
|
pub object_key: String,
|
|
pub quarantine_key: Option<String>,
|
|
pub lock_token: String,
|
|
pub attempts: i64,
|
|
}
|
|
|
|
/// One claimed purge (`claim_purges`).
|
|
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
|
pub struct Purge {
|
|
pub purge_id: String,
|
|
pub bucket_id: String,
|
|
pub object_name: String,
|
|
}
|
|
|
|
/// One quarantine object without a live row (`claim_quarantine_leftovers`).
|
|
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
|
pub struct Leftover {
|
|
pub bucket_id: String,
|
|
pub object_name: String,
|
|
pub asset_id: Option<String>,
|
|
}
|
|
|
|
/// Closed failure kinds of a backend call. `Refused` carries the closed code a `P0001`
|
|
/// refusal names (`invalid_state`, `invalid_reason`…); every other kind is transport-side.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum BackendError {
|
|
Network(String),
|
|
Status(u16),
|
|
Refused(String),
|
|
Decode(String),
|
|
/// The downloaded body exceeded the input cap.
|
|
Overflow,
|
|
Internal(String),
|
|
}
|
|
|
|
impl fmt::Display for BackendError {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Self::Network(detail) => write!(f, "network: {detail}"),
|
|
Self::Status(status) => write!(f, "http status {status}"),
|
|
Self::Refused(code) => write!(f, "refused: {code}"),
|
|
Self::Decode(detail) => write!(f, "decode: {detail}"),
|
|
Self::Overflow => f.write_str("body over the cap"),
|
|
Self::Internal(detail) => write!(f, "internal: {detail}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for BackendError {}
|
|
|
|
impl BackendError {
|
|
/// Stable name for the `code` log field.
|
|
pub fn kind(&self) -> &'static str {
|
|
match self {
|
|
Self::Network(_) => "network",
|
|
Self::Status(_) => "status",
|
|
Self::Refused(_) => "refused",
|
|
Self::Decode(_) => "decode",
|
|
Self::Overflow => "overflow",
|
|
Self::Internal(_) => "internal",
|
|
}
|
|
}
|
|
}
|
|
|
|
pub type BackendResult<T> = Result<T, BackendError>;
|
|
|
|
/// The eleven operations of STORAGE.md §2.2 and §3.3.
|
|
pub trait Backend {
|
|
fn claim_jobs(&self, max_jobs: u32) -> BackendResult<Vec<Job>>;
|
|
/// `Status(404)` when the object never landed; `Overflow` past the input cap.
|
|
fn download_quarantine(&self, bucket: &str, key: &str) -> BackendResult<Vec<u8>>;
|
|
/// Upsert of the clean bytes at the final key (idempotent on retry).
|
|
fn put_clean(&self, bucket: &str, key: &str, bytes: &[u8]) -> BackendResult<()>;
|
|
fn complete(
|
|
&self,
|
|
asset_id: &str,
|
|
lock_token: &str,
|
|
byte_size: usize,
|
|
sha256: &str,
|
|
) -> BackendResult<()>;
|
|
fn reject(&self, asset_id: &str, lock_token: &str, reason: &str) -> BackendResult<()>;
|
|
/// A missing object counts as deleted.
|
|
fn delete_object(&self, bucket: &str, key: &str) -> BackendResult<()>;
|
|
fn mark_quarantine_purged(&self, asset_id: &str) -> BackendResult<()>;
|
|
fn claim_purges(&self, max_items: u32) -> BackendResult<Vec<Purge>>;
|
|
fn ack_purge(&self, purge_id: &str) -> BackendResult<()>;
|
|
fn claim_quarantine_leftovers(
|
|
&self,
|
|
older_than_seconds: u64,
|
|
max_items: u32,
|
|
) -> BackendResult<Vec<Leftover>>;
|
|
fn expire_pending_uploads(&self, max_age_seconds: u64) -> BackendResult<i64>;
|
|
}
|