Files
tessera/crates/tessera-core/src/restore.rs
Adnan Zahir 648c8c6a2a
Some checks failed
ci / rust (push) Has been cancelled
ci / supply-chain (push) Has been cancelled
ci / web (push) Has been cancelled
feat: undo a restore from the safety copy it took
- POST /refresh-runs accepts undo_of: restores the safety copy of a completed restore back onto the same
  target through the normal run engine (checks, countdown, progress, and a new safety copy so the undo can be
  undone); refused when something was restored after it or the copy is gone
- a safety copy is accepted as a source only for the target it was taken from (hub and agent both check)
- run views carry undo and undo_available; audited as refresh.undo_started
- the modal offers Undo last restore and, after a restore, Undo this restore
- proven with a real agent and PostgreSQL: restore, undo back to the original contents, undo of the undo

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 16:47:02 +07:00

138 lines
4.0 KiB
Rust

use crate::job::CaptureFormat;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Engine {
Postgres,
Mysql,
}
impl CaptureFormat {
pub fn engine(self) -> Option<Engine> {
match self {
Self::PostgresCustom | Self::PostgresSql => Some(Engine::Postgres),
Self::MysqlSql => Some(Engine::Mysql),
Self::Filesystem => None,
}
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct DatabaseIdentity {
pub instance: String,
pub database: String,
}
impl DatabaseIdentity {
pub fn valid(&self, engine: Engine) -> bool {
let instance_valid = match engine {
Engine::Postgres => self
.instance
.parse::<u64>()
.is_ok_and(|id| id.to_string() == self.instance),
Engine::Mysql => {
Uuid::parse_str(&self.instance).is_ok_and(|id| id.to_string() == self.instance)
}
};
instance_valid
&& (1..=255).contains(&self.database.len())
&& !self.database.chars().any(char::is_control)
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RestoreTarget {
pub id: Uuid,
pub application_id: Uuid,
pub outpost_id: Uuid,
pub name: String,
pub engine: Engine,
pub identity: DatabaseIdentity,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RestoreRequest {
pub snapshot_id: Uuid,
pub target_id: Uuid,
pub confirmation: String,
}
impl RestoreRequest {
pub fn confirmation(snapshot: Uuid, target: Uuid) -> String {
format!("RESTORE {snapshot} TO {target}")
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RestoreJob {
pub id: Uuid,
pub snapshot_id: Uuid,
pub target: RestoreTarget,
pub actor: String,
pub status: String,
pub phase: String,
pub safety_snapshot_id: Option<Uuid>,
pub created_at: i64,
pub updated_at: i64,
/// Why the job failed, as the agent reported it (cleaned). Only administrators ever see it.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub failure_reason: Option<String>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RestoreArtifact {
pub format: CaptureFormat,
pub identity: DatabaseIdentity,
pub sha256: String,
/// The artifact is the safety copy a restore took of the very target it is now restored onto (an undo).
/// Its source identity is then the target's own, so the "target is the source" refusal does not apply.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub rollback: bool,
}
#[derive(Deserialize, Serialize)]
pub struct RestoreLease {
pub job: RestoreJob,
pub token: Uuid,
pub artifact: RestoreArtifact,
}
/// Header on a restore report that carries the reason for a failure.
pub const FAILURE_REASON_HEADER: &str = "x-tessera-failure-reason";
const FAILURE_REASON_MAX: usize = 300;
/// Make a failure reason safe to store, log and send in a header: printable ASCII on one line, at most 300
/// bytes. Anything else becomes a space (control characters) or `?` (non-ASCII). Empty input gives `None`.
pub fn clean_failure_reason(text: &str) -> Option<String> {
let mapped: String = text
.chars()
.map(|c| match c {
c if c.is_ascii_graphic() => c,
c if c.is_ascii() => ' ',
_ => '?',
})
.collect();
let line = mapped.split_whitespace().collect::<Vec<_>>().join(" ");
let line: String = line.chars().take(FAILURE_REASON_MAX).collect();
let line = line.trim_end().to_string();
(!line.is_empty()).then_some(line)
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RestoreProgress {
Heartbeat,
Checking,
Safety,
InvalidPayload,
WrongTarget,
SafetyFailed,
Applied,
ApplyFailed,
}