saddle-core 0.2.0-rc.18

Shared contracts for Saddle components
Documentation
use serde::Deserialize;
use std::sync::atomic::{AtomicBool, Ordering};

const APPROVED: &[u8] = include_bytes!("approved-inputs/golden-c8-rc8-applicability.json");
static CONSUMED: AtomicBool = AtomicBool::new(false);

/// The sole source-controlled Golden/C8 rc.8 applicability input. It carries
/// no production, signing, listener-open, release or publish authority.
#[doc(hidden)]
pub struct ApprovedGoldenC8ApplicabilityInput {
    bytes: &'static [u8],
}

/// Opaque, non-Clone and single-run permission to pass Golden/C8 COMPOSE.
/// It cannot be converted into production applicability.
#[doc(hidden)]
pub struct VerifiedGoldenC8ApplicabilityOwner {
    _input: ApprovedGoldenC8ApplicabilityInput,
}

#[doc(hidden)]
pub struct GoldenC8ApplicabilityRejection {
    input: ApprovedGoldenC8ApplicabilityInput,
}

impl GoldenC8ApplicabilityRejection {
    #[doc(hidden)]
    pub fn into_input(self) -> ApprovedGoldenC8ApplicabilityInput {
        self.input
    }
}

#[doc(hidden)]
pub fn approved_golden_c8_applicability_input() -> ApprovedGoldenC8ApplicabilityInput {
    ApprovedGoldenC8ApplicabilityInput { bytes: APPROVED }
}

#[doc(hidden)]
pub fn verify_golden_c8_applicability(
    input: ApprovedGoldenC8ApplicabilityInput,
) -> Result<VerifiedGoldenC8ApplicabilityOwner, GoldenC8ApplicabilityRejection> {
    if !matches_approved(&input)
        || CONSUMED
            .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
            .is_err()
    {
        return Err(GoldenC8ApplicabilityRejection { input });
    }
    Ok(VerifiedGoldenC8ApplicabilityOwner { _input: input })
}

fn matches_approved(input: &ApprovedGoldenC8ApplicabilityInput) -> bool {
    if input.bytes.as_ptr() != APPROVED.as_ptr() || input.bytes.len() != APPROVED.len() {
        return false;
    }
    let Ok(value) = serde_json::from_slice::<Document>(input.bytes) else {
        return false;
    };
    value.schema == "saddle-golden-c8-nonproduction-applicability/1"
        && value.authority_scope == "golden-c8-listener-preclosure-only"
        && value.artifact_pair_identity
            == "849db705286b52a2fed28701d7b2dc41e2db2495eb097ddb108a41da21d2e9f2"
        && value.rust_manifest_sha256
            == "f51c0034f6767586a523d4fcb0b642601be6ba2be6b1b5cabca9537e182dee85"
        && value.skill_tgz_sha256
            == "6f18e0d4d258d559d148a374f7dfa08a56c9b31586b6a7266cea5cae82507877"
        && value.compatibility_manifest_sha256
            == "52c60c74dc9ba03171a88c724c0d0e1231564cd7773b552d82647d718af42a71"
        && value.application_a_identity
            == "7162651c48b9f15c2c66279a4e4b7b0190767d4d2d0244ac7f7d79de78a52896"
        && value.application_b_identity
            == "f9030ef0607640f84f93d8e99bfd847b54ea14ebe1a2d0faf8db0be3a24568c1"
        && value.environment_identity
            == "c15e85b00535e8c26a0bd056256251ea4d75332f33253d75c0ba03b41b7cca70"
        && value.run_identity == "0c7642a6dc78178fd114432ccf30a23e68d6bfbb554b49114a59d5f30cf374c3"
        && value.permit_sha256 == "e1f078839aa5b233427fb634337b538f6269d186ad3d72741e01eb30e60c206d"
        && value.single_use
        && value.listener_preclosure_authority
        && !value.listener_open_authority
        && !value.production_authority
        && !value.release_authority
        && !value.publish_authority
        && !value.signing_authority
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Document {
    schema: String,
    authority_scope: String,
    artifact_pair_identity: String,
    rust_manifest_sha256: String,
    skill_tgz_sha256: String,
    compatibility_manifest_sha256: String,
    application_a_identity: String,
    application_b_identity: String,
    environment_identity: String,
    run_identity: String,
    permit_sha256: String,
    single_use: bool,
    listener_preclosure_authority: bool,
    listener_open_authority: bool,
    production_authority: bool,
    release_authority: bool,
    publish_authority: bool,
    signing_authority: bool,
}

#[cfg(test)]
mod tests {
    use super::*;

    fn reject(value: serde_json::Value) {
        let bytes = Box::leak(serde_json::to_vec(&value).unwrap().into_boxed_slice());
        let input = ApprovedGoldenC8ApplicabilityInput { bytes };
        let restored = verify_golden_c8_applicability(input)
            .err()
            .expect("drift must fail")
            .into_input();
        assert_eq!(restored.bytes, bytes);
    }

    #[test]
    fn exact_input_is_single_run_and_drift_is_recoverable() {
        let base: serde_json::Value = serde_json::from_slice(APPROVED).unwrap();
        for field in [
            "artifact_pair_identity",
            "application_a_identity",
            "application_b_identity",
            "environment_identity",
            "run_identity",
        ] {
            let mut changed = base.clone();
            changed[field] = serde_json::Value::String("0".repeat(64));
            reject(changed);
        }
        let _owner = verify_golden_c8_applicability(approved_golden_c8_applicability_input())
            .ok()
            .expect("approved input must issue once");
        assert!(verify_golden_c8_applicability(approved_golden_c8_applicability_input()).is_err());
    }
}