Refuse orphan state without Domain Store authority
This commit is contained in:
parent
24d7d5b6e0
commit
e97ab6471c
2 changed files with 103 additions and 36 deletions
|
|
@ -6,7 +6,8 @@ use std::path::PathBuf;
|
|||
use lazaret_core::records::Domain;
|
||||
use lazaret_host_coordinator::domain_store::{HostCoordinatorDomainStore, StateResetScopeV1};
|
||||
use lazaret_host_coordinator::state_maintenance::{
|
||||
DomainStateReport, StateAnchorEntryKind, inspect_domain_state, reset_domain_state,
|
||||
DomainStateReport, StateAnchorEntryKind, inspect_absent_domain_state, inspect_domain_state,
|
||||
reset_domain_state,
|
||||
};
|
||||
use lazaret_linux::filesystem::try_exclusive_flock;
|
||||
use rustix::fs::{Mode, OFlags, ResolveFlags, openat2};
|
||||
|
|
@ -64,11 +65,9 @@ pub fn run_command(arguments: &[OsString]) -> Result<i32, Box<dyn std::error::Er
|
|||
) {
|
||||
Ok(database) => database,
|
||||
Err(rustix::io::Errno::NOENT) => {
|
||||
return Ok(write_absent(
|
||||
&configuration,
|
||||
command.operation,
|
||||
command.machine,
|
||||
));
|
||||
let report = inspect_absent_domain_state(&configuration)?;
|
||||
write_report(&report, command.operation, command.machine);
|
||||
return Ok(0);
|
||||
}
|
||||
Err(error) => return Err(io::Error::from(error).into()),
|
||||
};
|
||||
|
|
@ -212,35 +211,6 @@ fn confirm(
|
|||
}
|
||||
}
|
||||
|
||||
fn write_absent(
|
||||
configuration: &lazaret_core::configuration::ResolvedDomainConfiguration,
|
||||
operation: Operation,
|
||||
machine: bool,
|
||||
) -> i32 {
|
||||
let report = DomainStateReport {
|
||||
profile: configuration.selected_profile().to_owned(),
|
||||
workspace: configuration
|
||||
.workspace()
|
||||
.canonical_label()
|
||||
.as_bytes()
|
||||
.to_vec(),
|
||||
domain_id: *configuration.domain_id().as_bytes(),
|
||||
anchors: configuration
|
||||
.fingerprint_record()
|
||||
.persistent_dirs
|
||||
.iter()
|
||||
.map(
|
||||
|path| lazaret_host_coordinator::state_maintenance::StateAnchorReport {
|
||||
container_path: path.clone(),
|
||||
changes: Vec::new(),
|
||||
},
|
||||
)
|
||||
.collect(),
|
||||
};
|
||||
write_report(&report, operation, machine);
|
||||
0
|
||||
}
|
||||
|
||||
fn write_report(report: &DomainStateReport, operation: Operation, machine: bool) {
|
||||
if machine {
|
||||
let anchors = report
|
||||
|
|
|
|||
|
|
@ -42,6 +42,52 @@ fn reset_refuses_an_arbitrary_container_or_host_path_before_state_access() {
|
|||
);
|
||||
}
|
||||
|
||||
#[allow(clippy::disallowed_methods, clippy::disallowed_types)]
|
||||
#[test]
|
||||
fn missing_store_succeeds_only_when_the_complete_state_anchor_is_absent() {
|
||||
let fixture = Fixture::new();
|
||||
for operation in ["list", "inspect", "verify", "reset"] {
|
||||
let mut command = fixture.command(operation);
|
||||
if operation == "reset" {
|
||||
command.args(["--all", "--yes"]);
|
||||
}
|
||||
let absent = command.arg("--json").output().unwrap();
|
||||
assert!(
|
||||
absent.status.success(),
|
||||
"{operation}: {}",
|
||||
String::from_utf8_lossy(&absent.stderr)
|
||||
);
|
||||
}
|
||||
|
||||
let domain = fixture.domain_root();
|
||||
assert!(!domain.join("domain.redb").exists());
|
||||
fs::create_dir_all(domain.join("state-anchor/reset-quarantine")).unwrap();
|
||||
fs::create_dir_all(domain.join("state-anchor/upper/usr")).unwrap();
|
||||
fs::write(domain.join("state-anchor/upper/usr/value"), b"persisted").unwrap();
|
||||
|
||||
for operation in ["list", "inspect", "verify", "reset"] {
|
||||
let mut command = fixture.command(operation);
|
||||
if operation == "reset" {
|
||||
command.args(["--all", "--yes"]);
|
||||
}
|
||||
let output = command.output().unwrap();
|
||||
assert!(
|
||||
!output.status.success(),
|
||||
"{operation} accepted orphan state"
|
||||
);
|
||||
assert!(
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
.contains("State Anchor exists without Domain Store authority")
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read(domain.join("state-anchor/upper/usr/value")).unwrap(),
|
||||
b"persisted"
|
||||
);
|
||||
assert!(domain.join("state-anchor/reset-quarantine").is_dir());
|
||||
assert!(!domain.join("domain.redb").exists());
|
||||
}
|
||||
}
|
||||
|
||||
struct Fixture {
|
||||
root: tempfile::TempDir,
|
||||
}
|
||||
|
|
@ -77,4 +123,11 @@ impl Fixture {
|
|||
.env("XDG_RUNTIME_DIR", self.root.path().join("runtime"));
|
||||
command
|
||||
}
|
||||
|
||||
fn domain_root(&self) -> std::path::PathBuf {
|
||||
let mut domains = fs::read_dir(self.root.path().join("data/domains")).unwrap();
|
||||
let domain = domains.next().unwrap().unwrap().path();
|
||||
assert!(domains.next().is_none());
|
||||
domain
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue