Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
lib: Make image configuration always present
Browse files Browse the repository at this point in the history
We should really always have it nowadays.  In the degenerate
case where it somehow still isn't present, just return an empty
config.

This improves ergonomics because it avoids the need to deal
with the `Option` in many consumers.
  • Loading branch information
cgwalters committed Jan 3, 2024
1 parent 7c3f8b8 commit 0b4f084
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 51 deletions.
63 changes: 28 additions & 35 deletions lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,40 +778,36 @@ async fn container_history(repo: &ostree::Repo, imgref: &ImageReference) -> Resu
let width = terminal_size::terminal_size()
.map(|x| x.0)
.unwrap_or(terminal_size::Width(80));
if let Some(config) = img.configuration.as_ref() {
{
let mut remaining = width;
for (name, width) in columns.iter() {
print_column(name, *width, &mut remaining);
}
println!();
{
let mut remaining = width;
for (name, width) in columns.iter() {
print_column(name, *width, &mut remaining);
}
println!();
}

let mut history = config.history().iter();
let layers = img.manifest.layers().iter();
for layer in layers {
let histent = history.next();
let created_by = histent
.and_then(|s| s.created_by().as_deref())
.unwrap_or("");

let mut remaining = width;

let digest = layer.digest().as_str();
// Verify it's OK to slice, this should all be ASCII
assert!(digest.chars().all(|c| c.is_ascii()));
let digest_max = columns[0].1;
let digest = &digest[0..digest_max as usize];
print_column(digest, digest_max, &mut remaining);
let size = glib::format_size(layer.size() as u64);
print_column(size.as_str(), columns[1].1, &mut remaining);
print_column(created_by, columns[2].1, &mut remaining);
println!();
}
Ok(())
} else {
anyhow::bail!("v0 image does not have fetched configuration");
let mut history = img.configuration.history().iter();
let layers = img.manifest.layers().iter();
for layer in layers {
let histent = history.next();
let created_by = histent
.and_then(|s| s.created_by().as_deref())
.unwrap_or("");

let mut remaining = width;

let digest = layer.digest().as_str();
// Verify it's OK to slice, this should all be ASCII
assert!(digest.chars().all(|c| c.is_ascii()));
let digest_max = columns[0].1;
let digest = &digest[0..digest_max as usize];
print_column(digest, digest_max, &mut remaining);
let size = glib::format_size(layer.size() as u64);
print_column(size.as_str(), columns[1].1, &mut remaining);
print_column(created_by, columns[2].1, &mut remaining);
println!();
}
Ok(())
}

/// Add IMA signatures to an ostree commit, generating a new commit.
Expand Down Expand Up @@ -974,10 +970,7 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
let stdout = std::io::stdout().lock();
let mut stdout = std::io::BufWriter::new(stdout);
if config {
let config = image
.configuration
.ok_or_else(|| anyhow::anyhow!("Missing configuration"))?;
serde_json::to_writer(&mut stdout, &config)?;
serde_json::to_writer(&mut stdout, &image.configuration)?;
} else {
serde_json::to_writer(&mut stdout, &image.manifest)?;
}
Expand Down
30 changes: 14 additions & 16 deletions lib/src/container/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ pub struct LayeredImageState {
pub manifest_digest: String,
/// The image manfiest
pub manifest: ImageManifest,
/// The image configuration; for v0 images, may not be available.
pub configuration: Option<ImageConfiguration>,
/// The image configuration
pub configuration: ImageConfiguration,
/// Metadata for (cached, previously fetched) updates to the image, if any.
pub cached_update: Option<CachedImageUpdate>,
}
Expand All @@ -143,9 +143,7 @@ impl LayeredImageState {

/// Retrieve the container image version.
pub fn version(&self) -> Option<&str> {
self.configuration
.as_ref()
.and_then(super::version_for_config)
super::version_for_config(&self.configuration)
}
}

Expand Down Expand Up @@ -328,14 +326,19 @@ fn manifest_data_from_commitmeta(
Ok((r, digest))
}

fn image_config_from_commitmeta(
commit_meta: &glib::VariantDict,
) -> Result<Option<ImageConfiguration>> {
commit_meta
fn image_config_from_commitmeta(commit_meta: &glib::VariantDict) -> Result<ImageConfiguration> {
let config = if let Some(config) = commit_meta
.lookup::<String>(META_CONFIG)?
.filter(|v| v != "null") // Format v0 apparently old versions injected `null` here sadly...
.map(|v| serde_json::from_str(&v).map_err(anyhow::Error::msg))
.transpose()
.transpose()?
{
config
} else {
tracing::debug!("No image configuration found");
Default::default()
};
Ok(config)
}

/// Return the original digest of the manifest stored in the commit metadata.
Expand Down Expand Up @@ -1579,13 +1582,8 @@ pub(crate) fn verify_container_image(
.expect("downcast");
merge_commit_root.ensure_resolved()?;

// This shouldn't happen anymore
let config = state
.configuration
.as_ref()
.ok_or_else(|| anyhow!("Missing configuration for image"))?;
let (commit_layer, _component_layers, remaining_layers) =
parse_manifest_layout(&state.manifest, config)?;
parse_manifest_layout(&state.manifest, &state.configuration)?;

let mut comparison_state = CompareState::default();

Expand Down

0 comments on commit 0b4f084

Please sign in to comment.