Skip to content

Commit 5463470

Browse files
authored
Merge pull request #229 from cgwalters/no-sigverify-prep
Add a helper for generating sigpolicy
2 parents 2ee270f + 9138153 commit 5463470

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

lib/src/cli.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use fn_error_context::context;
99
use ostree::gio;
1010
use ostree_container::store::PrepareResult;
1111
use ostree_ext::container as ostree_container;
12-
use ostree_ext::container::SignatureSource;
1312
use ostree_ext::keyfileext::KeyFileExt;
1413
use ostree_ext::ostree;
1514
use std::ffi::OsString;
@@ -20,6 +19,7 @@ use std::process::Command;
2019
use crate::deploy::RequiredHostSpec;
2120
use crate::spec::Host;
2221
use crate::spec::ImageReference;
22+
use crate::utils::sigpolicy_from_opts;
2323

2424
/// Perform an upgrade operation
2525
#[derive(Debug, Parser)]
@@ -363,13 +363,10 @@ async fn switch(opts: SwitchOpts) -> Result<()> {
363363
transport,
364364
name: opts.target.to_string(),
365365
};
366-
let sigverify = if opts.no_signature_verification {
367-
SignatureSource::ContainerPolicyAllowInsecure
368-
} else if let Some(remote) = opts.ostree_remote.as_ref() {
369-
SignatureSource::OstreeRemote(remote.to_string())
370-
} else {
371-
SignatureSource::ContainerPolicy
372-
};
366+
let sigverify = sigpolicy_from_opts(
367+
opts.no_signature_verification,
368+
opts.ostree_remote.as_deref(),
369+
);
373370
let target = ostree_container::OstreeImageReference { sigverify, imgref };
374371
let target = ImageReference::from(target);
375372

lib/src/install.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ use rustix::fs::MetadataExt;
3232
use fn_error_context::context;
3333
use ostree::gio;
3434
use ostree_ext::container as ostree_container;
35-
use ostree_ext::container::SignatureSource;
3635
use ostree_ext::ostree;
3736
use ostree_ext::prelude::Cast;
3837
use serde::{Deserialize, Serialize};
3938

4039
use self::baseline::InstallBlockDeviceOpts;
4140
use crate::containerenv::ContainerExecutionInfo;
4241
use crate::task::Task;
42+
use crate::utils::sigpolicy_from_opts;
4343

4444
/// The default "stateroot" or "osname"; see https://github.com/ostreedev/ostree/issues/2794
4545
const STATEROOT_DEFAULT: &str = "default";
@@ -917,13 +917,10 @@ async fn prepare_install(
917917

918918
// Parse the target CLI image reference options and create the *target* image
919919
// reference, which defaults to pulling from a registry.
920-
let target_sigverify = if target_opts.target_no_signature_verification {
921-
SignatureSource::ContainerPolicyAllowInsecure
922-
} else if let Some(remote) = target_opts.target_ostree_remote.as_deref() {
923-
SignatureSource::OstreeRemote(remote.to_string())
924-
} else {
925-
SignatureSource::ContainerPolicy
926-
};
920+
let target_sigverify = sigpolicy_from_opts(
921+
target_opts.target_no_signature_verification,
922+
target_opts.target_ostree_remote.as_deref(),
923+
);
927924
let target_imgname = target_opts
928925
.target_imgref
929926
.as_deref()

lib/src/utils.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::process::Command;
33

44
use anyhow::{Context, Result};
55
use ostree::glib;
6+
use ostree_ext::container::SignatureSource;
67
use ostree_ext::ostree;
78

89
/// Try to look for keys injected by e.g. rpm-ostree requesting machine-local
@@ -52,6 +53,20 @@ pub(crate) fn spawn_editor(tmpf: &tempfile::NamedTempFile) -> Result<()> {
5253
Ok(())
5354
}
5455

56+
/// Convert a combination of values (likely from CLI parsing) into a signature source
57+
pub(crate) fn sigpolicy_from_opts(
58+
disable_verification: bool,
59+
ostree_remote: Option<&str>,
60+
) -> SignatureSource {
61+
if disable_verification {
62+
SignatureSource::ContainerPolicyAllowInsecure
63+
} else if let Some(remote) = ostree_remote {
64+
SignatureSource::OstreeRemote(remote.to_owned())
65+
} else {
66+
SignatureSource::ContainerPolicy
67+
}
68+
}
69+
5570
/// Output a warning message
5671
pub(crate) fn warning(s: &str) {
5772
anstream::eprintln!(
@@ -94,3 +109,23 @@ fn test_find_mount_option() {
94109
assert_eq!(find_mount_option(V1, "rw"), None);
95110
assert_eq!(find_mount_option(V1, "somethingelse"), None);
96111
}
112+
113+
#[test]
114+
fn test_sigpolicy_from_opts() {
115+
assert_eq!(
116+
sigpolicy_from_opts(false, None),
117+
SignatureSource::ContainerPolicy
118+
);
119+
assert_eq!(
120+
sigpolicy_from_opts(true, None),
121+
SignatureSource::ContainerPolicyAllowInsecure
122+
);
123+
assert_eq!(
124+
sigpolicy_from_opts(false, Some("foo")),
125+
SignatureSource::OstreeRemote("foo".to_owned())
126+
);
127+
assert_eq!(
128+
sigpolicy_from_opts(true, Some("foo")),
129+
SignatureSource::ContainerPolicyAllowInsecure
130+
);
131+
}

0 commit comments

Comments
 (0)