Skip to content

Implement Debug w/ detailed manifest for Reader #473

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

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
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
38 changes: 24 additions & 14 deletions sdk/src/manifest_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub struct ManifestStore {
#[serde(skip_serializing_if = "Option::is_none")]
/// ValidationStatus generated when loading the ManifestStore from an asset
validation_status: Option<Vec<ValidationStatus>>,
#[serde(skip)]
/// The internal store representing the manifest store
store: Store,
}

impl ManifestStore {
Expand All @@ -54,6 +57,7 @@ impl ManifestStore {
active_manifest: None,
manifests: HashMap::<String, Manifest>::new(),
validation_status: None,
store: Store::new(),
}
}

Expand Down Expand Up @@ -112,7 +116,7 @@ impl ManifestStore {
}

/// creates a ManifestStore from a Store with validation
pub(crate) fn from_store(store: &Store, validation_log: &impl StatusTracker) -> ManifestStore {
pub(crate) fn from_store(store: Store, validation_log: &impl StatusTracker) -> ManifestStore {
Self::from_store_impl(
store,
validation_log,
Expand All @@ -124,7 +128,7 @@ impl ManifestStore {
/// creates a ManifestStore from a Store writing resources to resource_path
#[cfg(feature = "file_io")]
pub(crate) fn from_store_with_resources(
store: &Store,
store: Store,
validation_log: &impl StatusTracker,
resource_path: &Path,
) -> ManifestStore {
Expand All @@ -133,15 +137,17 @@ impl ManifestStore {

// internal implementation of from_store
fn from_store_impl(
store: &Store,
store: Store,
validation_log: &impl StatusTracker,
#[cfg(feature = "file_io")] resource_path: Option<&Path>,
) -> ManifestStore {
let mut statuses = status_for_store(store, validation_log);
let mut statuses = status_for_store(&store, validation_log);

let mut manifest_store = ManifestStore::new();
manifest_store.active_manifest = store.provenance_label();
manifest_store.store = store;

let store = &manifest_store.store;
for claim in store.claims() {
let manifest_label = claim.label();
#[cfg(feature = "file_io")]
Expand All @@ -167,13 +173,17 @@ impl ManifestStore {
manifest_store
}

pub(crate) fn store(&self) -> &Store {
&self.store
}

/// Creates a new Manifest Store from a Manifest
#[allow(dead_code)]
pub fn from_manifest(manifest: &Manifest) -> Result<Self> {
use crate::status_tracker::OneShotStatusTracker;
let store = manifest.to_store()?;
Ok(Self::from_store_impl(
&store,
store,
&OneShotStatusTracker::new(),
#[cfg(feature = "file_io")]
manifest.resources().base_path(),
Expand All @@ -186,7 +196,7 @@ impl ManifestStore {
let mut validation_log = DetailedStatusTracker::new();

Store::load_from_memory(format, image_bytes, verify, &mut validation_log)
.map(|store| Self::from_store(&store, &validation_log))
.map(|store| Self::from_store(store, &validation_log))
}

/// Generate a Store from a format string and stream.
Expand Down Expand Up @@ -221,7 +231,7 @@ impl ManifestStore {
.await?;
}
}
Ok(Self::from_store(&store, &validation_log))
Ok(Self::from_store(store, &validation_log))
}

#[cfg(feature = "file_io")]
Expand All @@ -242,7 +252,7 @@ impl ManifestStore {
let mut validation_log = DetailedStatusTracker::new();

let store = Store::load_from_asset(path.as_ref(), true, &mut validation_log)?;
Ok(Self::from_store(&store, &validation_log))
Ok(Self::from_store(store, &validation_log))
}

#[cfg(feature = "file_io")]
Expand Down Expand Up @@ -270,7 +280,7 @@ impl ManifestStore {

let store = Store::load_from_asset(path.as_ref(), true, &mut validation_log)?;
Ok(Self::from_store_with_resources(
&store,
store,
&validation_log,
resource_path.as_ref(),
))
Expand All @@ -287,7 +297,7 @@ impl ManifestStore {

Store::load_from_memory_async(format, image_bytes, verify, &mut validation_log)
.await
.map(|store| Self::from_store(&store, &validation_log))
.map(|store| Self::from_store(store, &validation_log))
}

/// Loads a ManifestStore from an init segment and fragment. This
Expand All @@ -309,7 +319,7 @@ impl ManifestStore {
&mut validation_log,
)
.await
.map(|store| Self::from_store(&store, &validation_log))
.map(|store| Self::from_store(store, &validation_log))
}

/// Asynchronously loads a manifest from a buffer holding a binary manifest (.c2pa) and validates against an asset buffer
Expand Down Expand Up @@ -348,7 +358,7 @@ impl ManifestStore {
)
.await?;

Ok(Self::from_store(&store, &validation_log))
Ok(Self::from_store(store, &validation_log))
}

/// Synchronously loads a manifest from a buffer holding a binary manifest (.c2pa) and validates against an asset buffer
Expand Down Expand Up @@ -384,7 +394,7 @@ impl ManifestStore {
&mut validation_log,
)?;

Ok(Self::from_store(&store, &validation_log))
Ok(Self::from_store(store, &validation_log))
}
}

Expand Down Expand Up @@ -463,7 +473,7 @@ mod tests {
fn manifest_report() {
let store = create_test_store().expect("creating test store");

let manifest_store = ManifestStore::from_store(&store, &OneShotStatusTracker::new());
let manifest_store = ManifestStore::from_store(store, &OneShotStatusTracker::new());
assert!(manifest_store.active_manifest.is_some());
assert!(!manifest_store.manifests.is_empty());
let manifest = manifest_store.get_active().unwrap();
Expand Down
13 changes: 10 additions & 3 deletions sdk/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ use crate::error::Error;
use crate::{
claim::ClaimAssetData, error::Result, manifest_store::ManifestStore,
settings::get_settings_value, status_tracker::DetailedStatusTracker, store::Store,
validation_status::ValidationStatus, Manifest,
validation_status::ValidationStatus, Manifest, ManifestStoreReport,
};

/// A reader for the manifest store.
#[derive(Debug)]
pub struct Reader {
pub(crate) manifest_store: ManifestStore,
}
Expand Down Expand Up @@ -150,7 +149,7 @@ impl Reader {
}

Ok(Reader {
manifest_store: ManifestStore::from_store(&store, &validation_log),
manifest_store: ManifestStore::from_store(store, &validation_log),
})
}

Expand Down Expand Up @@ -235,3 +234,11 @@ impl std::fmt::Display for Reader {
f.write_str(self.json().as_str())
}
}

impl std::fmt::Debug for Reader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let report = ManifestStoreReport::from_store(self.manifest_store.store())
.map_err(|_| std::fmt::Error)?;
f.write_str(&report.to_string())
}
}
7 changes: 6 additions & 1 deletion sdk/src/trust_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use std::{
collections::HashSet,
io::{read_to_string, Cursor, Read},
panic::{RefUnwindSafe, UnwindSafe},
str::FromStr,
};

Expand All @@ -27,7 +28,10 @@ pub(crate) static OCSP_SIGNING_OID: Oid<'static> = oid!(1.3.6 .1 .5 .5 .7 .3 .9)
pub(crate) static DOCUMENT_SIGNING_OID: Oid<'static> = oid!(1.3.6 .1 .5 .5 .7 .3 .36);

// Trait for supply configuration and handling of trust lists and EKU configuration store
pub(crate) trait TrustHandlerConfig: Sync + Send {
//
// `RefUnwindSafe` + `UnwindSafe` were added to ensure `Store` is unwind safe and to preserve
// backwards compatbility.
pub(crate) trait TrustHandlerConfig: RefUnwindSafe + UnwindSafe + Sync + Send {
fn new() -> Self
where
Self: Sized;
Expand Down Expand Up @@ -122,6 +126,7 @@ pub(crate) fn load_trust_from_data(trust_data: &[u8]) -> Result<Vec<Vec<u8>>> {

// Pass through trust for the case of claim signer usage since it has known trust with context
// configured to all email protection, timestamping, ocsp signing and document signing
#[derive(Debug)]
pub(crate) struct TrustPassThrough {
allowed_cert_set: HashSet<String>,
config_store: Vec<u8>,
Expand Down
Loading