Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepareroot: Also accept composefs enabled=signed #635

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions lib/src/ostree_prepareroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,47 @@ impl Tristate {
}
}

#[derive(Debug, PartialEq, Eq)]
enum ComposefsState {
Signed,
Tristate(Tristate),
}

impl Default for ComposefsState {
fn default() -> Self {
Self::Tristate(Tristate::default())
}
}

impl FromStr for ComposefsState {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self> {
let r = match s {
"signed" => Self::Signed,
o => Self::Tristate(Tristate::from_str(o)?),
};
Ok(r)
}
}

impl ComposefsState {
pub(crate) fn maybe_enabled(&self) -> bool {
match self {
ComposefsState::Signed => true,
ComposefsState::Tristate(t) => t.maybe_enabled(),
}
}
}

/// Query whether the config uses an overlayfs model (composefs or plain overlayfs).
pub fn overlayfs_enabled_in_config(config: &glib::KeyFile) -> Result<bool> {
let root_transient = config
.optional_bool("root", "transient")?
.unwrap_or_default();
let composefs = config
.optional_string("composefs", "enabled")?
.map(|s| Tristate::from_str(s.as_str()))
.map(|s| ComposefsState::from_str(s.as_str()))
.transpose()
.log_err_default()
.unwrap_or_default();
Expand All @@ -112,6 +145,27 @@ fn test_tristate() {
}
}

#[test]
fn test_composefs_state() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

assert_eq!(
ComposefsState::from_str("signed").unwrap(),
ComposefsState::Signed
);
for v in ["yes", "true", "1"] {
assert_eq!(
ComposefsState::from_str(v).unwrap(),
ComposefsState::Tristate(Tristate::Enabled)
);
}
assert_eq!(Tristate::from_str("maybe").unwrap(), Tristate::Maybe);
for v in ["no", "false", "0"] {
assert_eq!(
ComposefsState::from_str(v).unwrap(),
ComposefsState::Tristate(Tristate::Disabled)
);
}
}

#[test]
fn test_overlayfs_enabled() {
let d0 = indoc::indoc! { r#"
Expand All @@ -136,7 +190,8 @@ enabled = false
let e0 = format!("{d0}\n[root]\ntransient = true");
let e1 = format!("{d1}\n[composefs]\nenabled = true\n[other]\nsomekey = someval");
let e2 = format!("{d1}\n[composefs]\nenabled = yes");
for v in [e0, e1, e2] {
let e3 = format!("{d1}\n[composefs]\nenabled = signed");
for v in [e0, e1, e2, e3] {
let kf = glib::KeyFile::new();
kf.load_from_data(&v, glib::KeyFileFlags::empty()).unwrap();
assert_eq!(overlayfs_enabled_in_config(&kf).unwrap(), true);
Expand Down
Loading