Skip to main content

canic_backup/restore/apply/
mod.rs

1use super::{RestorePlan, RestorePlanMember};
2use crate::{
3    artifacts::{ArtifactChecksum, ArtifactChecksumError},
4    manifest::VerificationCheck,
5    persistence::resolve_backup_artifact_path,
6};
7use serde::{Deserialize, Serialize};
8use std::{
9    fs,
10    path::{Path, PathBuf},
11};
12use thiserror::Error as ThisError;
13
14mod journal;
15
16pub(in crate::restore) use journal::RestoreApplyCommandOutputPair;
17pub(in crate::restore) use journal::RestoreApplyJournalReport;
18pub use journal::{
19    RestoreApplyCommandConfig, RestoreApplyCommandOutput, RestoreApplyCommandPreview,
20    RestoreApplyJournal, RestoreApplyJournalError, RestoreApplyJournalOperation,
21    RestoreApplyOperationKind, RestoreApplyOperationKindCounts, RestoreApplyOperationReceipt,
22    RestoreApplyOperationReceiptOutcome, RestoreApplyOperationState, RestoreApplyPendingSummary,
23    RestoreApplyProgressSummary, RestoreApplyReportOperation, RestoreApplyReportOutcome,
24    RestoreApplyRunnerCommand,
25};
26
27///
28/// RestoreApplyDryRun
29///
30
31#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
32#[serde(deny_unknown_fields)]
33pub struct RestoreApplyDryRun {
34    pub dry_run_version: u16,
35    pub backup_id: String,
36    pub ready: bool,
37    pub readiness_reasons: Vec<String>,
38    pub member_count: usize,
39    #[serde(default)]
40    pub planned_canister_stops: usize,
41    #[serde(default)]
42    pub planned_canister_starts: usize,
43    pub planned_snapshot_uploads: usize,
44    pub planned_snapshot_loads: usize,
45    pub planned_verification_checks: usize,
46    pub planned_operations: usize,
47    pub rendered_operations: usize,
48    #[serde(default)]
49    pub operation_counts: RestoreApplyOperationKindCounts,
50    pub artifact_validation: Option<RestoreApplyArtifactValidation>,
51    pub operations: Vec<RestoreApplyDryRunOperation>,
52}
53
54impl RestoreApplyDryRun {
55    /// Build a no-mutation apply dry-run from a restore plan.
56    #[must_use]
57    pub fn from_plan(plan: &RestorePlan) -> Self {
58        Self::from_validated_plan(plan)
59    }
60
61    /// Build an apply dry-run and verify all referenced artifacts under a backup root.
62    pub fn try_from_plan_with_artifacts(
63        plan: &RestorePlan,
64        backup_root: &Path,
65    ) -> Result<Self, RestoreApplyDryRunError> {
66        let mut dry_run = Self::from_plan(plan);
67        dry_run.artifact_validation = Some(validate_restore_apply_artifacts(plan, backup_root)?);
68        Ok(dry_run)
69    }
70
71    // Build a no-mutation apply dry-run from a restore plan.
72    fn from_validated_plan(plan: &RestorePlan) -> Self {
73        let mut next_sequence = 0;
74        let ordered_members = plan.ordered_members();
75        let mut operations = Vec::new();
76        append_member_phase(
77            &mut operations,
78            &mut next_sequence,
79            RestoreApplyOperationKind::UploadSnapshot,
80            ordered_members.iter().copied(),
81        );
82        append_member_phase(
83            &mut operations,
84            &mut next_sequence,
85            RestoreApplyOperationKind::StopCanister,
86            ordered_members.iter().copied(),
87        );
88        append_member_phase(
89            &mut operations,
90            &mut next_sequence,
91            RestoreApplyOperationKind::LoadSnapshot,
92            ordered_members.iter().copied(),
93        );
94        append_member_phase(
95            &mut operations,
96            &mut next_sequence,
97            RestoreApplyOperationKind::StartCanister,
98            ordered_members.iter().rev().copied(),
99        );
100        append_member_verification_operations(
101            &mut operations,
102            &mut next_sequence,
103            &ordered_members,
104        );
105        append_deployment_verification_operations(plan, &mut operations, &mut next_sequence);
106        let rendered_operations = operations.len();
107        let operation_counts =
108            RestoreApplyOperationKindCounts::from_dry_run_operations(&operations);
109
110        Self {
111            dry_run_version: 1,
112            backup_id: plan.backup_id.clone(),
113            ready: plan.readiness_summary.ready,
114            readiness_reasons: plan.readiness_summary.reasons.clone(),
115            member_count: plan.member_count,
116            planned_canister_stops: plan.operation_summary.planned_canister_stops,
117            planned_canister_starts: plan.operation_summary.planned_canister_starts,
118            planned_snapshot_uploads: plan.operation_summary.planned_snapshot_uploads,
119            planned_snapshot_loads: plan.operation_summary.planned_snapshot_loads,
120            planned_verification_checks: plan.operation_summary.planned_verification_checks,
121            planned_operations: plan.operation_summary.planned_operations,
122            rendered_operations,
123            operation_counts,
124            artifact_validation: None,
125            operations,
126        }
127    }
128}
129
130// Verify every planned restore artifact against one backup directory root.
131fn validate_restore_apply_artifacts(
132    plan: &RestorePlan,
133    backup_root: &Path,
134) -> Result<RestoreApplyArtifactValidation, RestoreApplyDryRunError> {
135    let backup_root = canonical_restore_backup_root(backup_root)?;
136    let mut checks = Vec::new();
137
138    for member in plan.ordered_members() {
139        checks.push(validate_restore_apply_artifact(member, &backup_root)?);
140    }
141
142    let members_with_expected_checksums = checks
143        .iter()
144        .filter(|check| check.checksum_expected.is_some())
145        .count();
146    let artifacts_present = checks.iter().all(|check| check.exists);
147    let checksums_verified = members_with_expected_checksums == plan.member_count
148        && checks.iter().all(|check| check.checksum_verified);
149
150    Ok(RestoreApplyArtifactValidation {
151        backup_root: backup_root.to_string_lossy().to_string(),
152        checked_members: checks.len(),
153        artifacts_present,
154        checksums_verified,
155        members_with_expected_checksums,
156        checks,
157    })
158}
159
160// Verify one planned restore artifact path and checksum.
161fn validate_restore_apply_artifact(
162    member: &RestorePlanMember,
163    backup_root: &Path,
164) -> Result<RestoreApplyArtifactCheck, RestoreApplyDryRunError> {
165    let resolved_path = safe_restore_artifact_path(
166        backup_root,
167        &member.source_canister,
168        &member.source_snapshot.artifact_path,
169    )?;
170
171    validate_restore_artifact_type(
172        backup_root,
173        &member.source_canister,
174        &member.source_snapshot.artifact_path,
175        &resolved_path,
176    )?;
177
178    let (checksum_actual, checksum_verified) =
179        if let Some(expected) = &member.source_snapshot.checksum {
180            let checksum = ArtifactChecksum::from_relative_path_no_follow(
181                backup_root,
182                Path::new(&member.source_snapshot.artifact_path),
183            )
184            .map_err(|source| RestoreApplyDryRunError::ArtifactChecksum {
185                source_canister: member.source_canister.clone(),
186                artifact_path: member.source_snapshot.artifact_path.clone(),
187                source,
188            })?;
189            checksum.verify(expected).map_err(|source| {
190                RestoreApplyDryRunError::ArtifactChecksum {
191                    source_canister: member.source_canister.clone(),
192                    artifact_path: member.source_snapshot.artifact_path.clone(),
193                    source,
194                }
195            })?;
196            (Some(checksum.hash), true)
197        } else {
198            (None, false)
199        };
200
201    Ok(RestoreApplyArtifactCheck {
202        source_canister: member.source_canister.clone(),
203        target_canister: member.target_canister.clone(),
204        snapshot_id: member.source_snapshot.snapshot_id.clone(),
205        artifact_path: member.source_snapshot.artifact_path.clone(),
206        resolved_path: resolved_path.to_string_lossy().to_string(),
207        exists: true,
208        checksum_algorithm: member.source_snapshot.checksum_algorithm.clone(),
209        checksum_expected: member.source_snapshot.checksum.clone(),
210        checksum_actual,
211        checksum_verified,
212    })
213}
214
215fn canonical_restore_backup_root(backup_root: &Path) -> Result<PathBuf, RestoreApplyDryRunError> {
216    let metadata = fs::symlink_metadata(backup_root).map_err(|source| {
217        RestoreApplyDryRunError::ArtifactRootIo {
218            backup_root: backup_root.to_path_buf(),
219            source,
220        }
221    })?;
222    if metadata.file_type().is_symlink() || !metadata.is_dir() {
223        return Err(RestoreApplyDryRunError::ArtifactRootUnsafe {
224            backup_root: backup_root.to_path_buf(),
225        });
226    }
227    backup_root
228        .canonicalize()
229        .map_err(|source| RestoreApplyDryRunError::ArtifactRootIo {
230            backup_root: backup_root.to_path_buf(),
231            source,
232        })
233}
234
235fn validate_restore_artifact_type(
236    backup_root: &Path,
237    source_canister: &str,
238    artifact_path: &str,
239    resolved_path: &Path,
240) -> Result<(), RestoreApplyDryRunError> {
241    let mut current = backup_root.to_path_buf();
242    let mut artifact_metadata = None;
243    for component in Path::new(artifact_path).components() {
244        current.push(component);
245        let metadata = match fs::symlink_metadata(&current) {
246            Ok(metadata) => metadata,
247            Err(source) if source.kind() == std::io::ErrorKind::NotFound => {
248                return Err(RestoreApplyDryRunError::ArtifactMissing {
249                    source_canister: source_canister.to_string(),
250                    artifact_path: artifact_path.to_string(),
251                    resolved_path: resolved_path.to_string_lossy().to_string(),
252                });
253            }
254            Err(source) => {
255                return Err(RestoreApplyDryRunError::ArtifactChecksum {
256                    source_canister: source_canister.to_string(),
257                    artifact_path: artifact_path.to_string(),
258                    source: ArtifactChecksumError::Io(source),
259                });
260            }
261        };
262        if metadata.file_type().is_symlink() {
263            return Err(RestoreApplyDryRunError::ArtifactUnsafeType {
264                source_canister: source_canister.to_string(),
265                artifact_path: current.to_string_lossy().to_string(),
266                kind: "symlink".to_string(),
267            });
268        }
269        artifact_metadata = Some(metadata);
270    }
271    let metadata =
272        artifact_metadata.ok_or_else(|| RestoreApplyDryRunError::ArtifactPathEscapesBackup {
273            source_canister: source_canister.to_string(),
274            artifact_path: artifact_path.to_string(),
275        })?;
276    if metadata.is_file() || metadata.is_dir() {
277        Ok(())
278    } else {
279        Err(RestoreApplyDryRunError::ArtifactUnsafeType {
280            source_canister: source_canister.to_string(),
281            artifact_path: artifact_path.to_string(),
282            kind: "special".to_string(),
283        })
284    }
285}
286
287// Reject absolute paths and parent traversal before joining with the backup root.
288fn safe_restore_artifact_path(
289    backup_root: &Path,
290    source_canister: &str,
291    artifact_path: &str,
292) -> Result<PathBuf, RestoreApplyDryRunError> {
293    if let Some(path) = resolve_backup_artifact_path(backup_root, artifact_path) {
294        return Ok(path);
295    }
296
297    Err(RestoreApplyDryRunError::ArtifactPathEscapesBackup {
298        source_canister: source_canister.to_string(),
299        artifact_path: artifact_path.to_string(),
300    })
301}
302
303///
304/// RestoreApplyArtifactValidation
305///
306
307#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
308#[serde(deny_unknown_fields)]
309pub struct RestoreApplyArtifactValidation {
310    pub backup_root: String,
311    pub checked_members: usize,
312    pub artifacts_present: bool,
313    pub checksums_verified: bool,
314    pub members_with_expected_checksums: usize,
315    pub checks: Vec<RestoreApplyArtifactCheck>,
316}
317
318///
319/// RestoreApplyArtifactCheck
320///
321
322#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
323#[serde(deny_unknown_fields)]
324pub struct RestoreApplyArtifactCheck {
325    pub source_canister: String,
326    pub target_canister: String,
327    pub snapshot_id: String,
328    pub artifact_path: String,
329    pub resolved_path: String,
330    pub exists: bool,
331    pub checksum_algorithm: String,
332    pub checksum_expected: Option<String>,
333    pub checksum_actual: Option<String>,
334    pub checksum_verified: bool,
335}
336
337// Append a full member operation phase across the selected topology.
338fn append_member_phase<'a>(
339    operations: &mut Vec<RestoreApplyDryRunOperation>,
340    next_sequence: &mut usize,
341    operation: RestoreApplyOperationKind,
342    members: impl IntoIterator<Item = &'a RestorePlanMember>,
343) {
344    for member in members {
345        push_member_operation(operations, next_sequence, operation.clone(), member, None);
346    }
347}
348
349// Append member verification checks after restore mutations complete.
350fn append_member_verification_operations(
351    operations: &mut Vec<RestoreApplyDryRunOperation>,
352    next_sequence: &mut usize,
353    members: &[&RestorePlanMember],
354) {
355    for member in members {
356        for check in &member.verification_checks {
357            push_member_operation(
358                operations,
359                next_sequence,
360                RestoreApplyOperationKind::VerifyMember,
361                member,
362                Some(check),
363            );
364        }
365    }
366}
367
368// Append one member-level dry-run operation using the member's restore order.
369fn push_member_operation(
370    operations: &mut Vec<RestoreApplyDryRunOperation>,
371    next_sequence: &mut usize,
372    operation: RestoreApplyOperationKind,
373    member: &RestorePlanMember,
374    check: Option<&VerificationCheck>,
375) {
376    let sequence = *next_sequence;
377    *next_sequence += 1;
378
379    let snapshot_id = operation_snapshot_id(&operation, member);
380    let artifact_path = operation_artifact_path(&operation, member);
381    let artifact_checksum = operation_artifact_checksum(&operation, member);
382
383    operations.push(RestoreApplyDryRunOperation {
384        sequence,
385        operation,
386        member_order: member.member_order,
387        source_canister: member.source_canister.clone(),
388        target_canister: member.target_canister.clone(),
389        role: member.role.clone(),
390        snapshot_id,
391        artifact_path,
392        artifact_checksum,
393        verification_kind: check.map(|check| check.kind.clone()),
394    });
395}
396
397fn operation_snapshot_id(
398    operation: &RestoreApplyOperationKind,
399    member: &RestorePlanMember,
400) -> Option<String> {
401    matches!(
402        operation,
403        RestoreApplyOperationKind::UploadSnapshot | RestoreApplyOperationKind::LoadSnapshot
404    )
405    .then(|| member.source_snapshot.snapshot_id.clone())
406}
407
408fn operation_artifact_path(
409    operation: &RestoreApplyOperationKind,
410    member: &RestorePlanMember,
411) -> Option<String> {
412    matches!(
413        operation,
414        RestoreApplyOperationKind::UploadSnapshot | RestoreApplyOperationKind::LoadSnapshot
415    )
416    .then(|| member.source_snapshot.artifact_path.clone())
417}
418
419fn operation_artifact_checksum(
420    operation: &RestoreApplyOperationKind,
421    member: &RestorePlanMember,
422) -> Option<ArtifactChecksum> {
423    matches!(
424        operation,
425        RestoreApplyOperationKind::UploadSnapshot | RestoreApplyOperationKind::LoadSnapshot
426    )
427    .then(|| {
428        member
429            .source_snapshot
430            .checksum
431            .as_ref()
432            .map(|hash| ArtifactChecksum {
433                algorithm: member.source_snapshot.checksum_algorithm.clone(),
434                hash: hash.clone(),
435            })
436    })
437    .flatten()
438}
439
440// Append deployment-level verification checks after all member operations.
441fn append_deployment_verification_operations(
442    plan: &RestorePlan,
443    operations: &mut Vec<RestoreApplyDryRunOperation>,
444    next_sequence: &mut usize,
445) {
446    if plan.deployment_verification_checks.is_empty() {
447        return;
448    }
449
450    let root = plan
451        .members
452        .iter()
453        .find(|member| member.source_canister == plan.source_root_canister);
454    let source_canister = root.map_or_else(
455        || plan.source_root_canister.clone(),
456        |member| member.source_canister.clone(),
457    );
458    let target_canister = root.map_or_else(
459        || plan.source_root_canister.clone(),
460        |member| member.target_canister.clone(),
461    );
462    for check in &plan.deployment_verification_checks {
463        push_deployment_operation(
464            operations,
465            next_sequence,
466            &source_canister,
467            &target_canister,
468            check,
469        );
470    }
471}
472
473// Append one deployment-level dry-run verification operation.
474fn push_deployment_operation(
475    operations: &mut Vec<RestoreApplyDryRunOperation>,
476    next_sequence: &mut usize,
477    source_canister: &str,
478    target_canister: &str,
479    check: &VerificationCheck,
480) {
481    let sequence = *next_sequence;
482    *next_sequence += 1;
483    let member_order = operations.len();
484
485    operations.push(RestoreApplyDryRunOperation {
486        sequence,
487        operation: RestoreApplyOperationKind::VerifyDeployment,
488        member_order,
489        source_canister: source_canister.to_string(),
490        target_canister: target_canister.to_string(),
491        role: "deployment".to_string(),
492        snapshot_id: None,
493        artifact_path: None,
494        artifact_checksum: None,
495        verification_kind: Some(check.kind.clone()),
496    });
497}
498
499///
500/// RestoreApplyDryRunOperation
501///
502
503#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
504#[serde(deny_unknown_fields)]
505pub struct RestoreApplyDryRunOperation {
506    pub sequence: usize,
507    pub operation: RestoreApplyOperationKind,
508    pub member_order: usize,
509    pub source_canister: String,
510    pub target_canister: String,
511    pub role: String,
512    pub snapshot_id: Option<String>,
513    pub artifact_path: Option<String>,
514    pub artifact_checksum: Option<ArtifactChecksum>,
515    pub verification_kind: Option<String>,
516}
517
518///
519/// RestoreApplyDryRunError
520///
521
522#[derive(Debug, ThisError)]
523pub enum RestoreApplyDryRunError {
524    #[error("restore artifact path for {source_canister} escapes backup root: {artifact_path}")]
525    ArtifactPathEscapesBackup {
526        source_canister: String,
527        artifact_path: String,
528    },
529
530    #[error("restore backup artifact root IO failed at {backup_root}")]
531    ArtifactRootIo {
532        backup_root: PathBuf,
533        #[source]
534        source: std::io::Error,
535    },
536
537    #[error("restore backup artifact root is not a direct directory: {backup_root}")]
538    ArtifactRootUnsafe { backup_root: PathBuf },
539
540    #[error(
541        "restore artifact for {source_canister} has unsupported filesystem type {kind}: {artifact_path}"
542    )]
543    ArtifactUnsafeType {
544        source_canister: String,
545        artifact_path: String,
546        kind: String,
547    },
548
549    #[error(
550        "restore artifact for {source_canister} is missing: {artifact_path} at {resolved_path}"
551    )]
552    ArtifactMissing {
553        source_canister: String,
554        artifact_path: String,
555        resolved_path: String,
556    },
557
558    #[error("restore artifact checksum failed for {source_canister} at {artifact_path}: {source}")]
559    ArtifactChecksum {
560        source_canister: String,
561        artifact_path: String,
562        #[source]
563        source: ArtifactChecksumError,
564    },
565}