lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

commit 5e3e7bf3b0cacd5b7302fe642c3fcb49b163ffb2
parent ba83a1c74dddb0ad4b04b5fdbf5def3e9e9f55eb
Author: triesap <tyson@radroots.org>
Date:   Fri, 16 Oct 2026 06:01:07 +0000

xtask: align service source and artifact commands on Rust 1.99

- Wire the bounded V4 codec into source-lock and artifact commands.
- Bind canonical committed trees instead of transfer bundle history.
- Validate current compiler inputs and preserve historical contracts.
- Pass 619 tests, strict checks and actual contract validation.

Diffstat:
MAGENTS.md | 59+++++++++++++++++++++++++++++------------------------------
Acontracts/architecture/decisions/services_hardening_release_artifacts.v5.json | 143+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtools/xtask/src/main.rs | 2++
Mtools/xtask/src/service_release_artifacts.rs | 141++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Mtools/xtask/src/service_source_lock_command.rs | 453++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mtools/xtask/src/service_source_lock_v3.rs | 22++++++++++++++++++++++
Atools/xtask/src/service_source_lock_v4.rs | 701+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 1275 insertions(+), 246 deletions(-)

diff --git a/AGENTS.md b/AGENTS.md @@ -45,43 +45,42 @@ This file exists for compatibility with tools that look for AGENTS.md. accepting a service consumer marker must not expose an artifact route. Tera owns its application packages and native/WASM artifact routes. Lib must not regain an application package or a dependency on the Tera application. -- The canonical service source lock is the bounded, canonical - `radroots.service.source-lock.v2.toml` model. It binds the exact active public - Lib repository and full revision, Lib source-archive and workspace-catalog - digests, the service `Cargo.lock` digest, Rust `1.97.1`, the `service-host` - feature profile, positive config, state, admin, status, and provider contract - versions, and an exact closed Nix-material state. `absent` requires both Nix - files to be absent. `deferred` independently binds an exact mutually - consistent `flake.nix` and `flake.lock` revision and digest without claiming - Nix qualification or active-revision alignment. Keep the model and - diagnostics private to repo tooling, reject noncanonical or extra fields, - and never put credentials, local paths, floating refs, or private repository - identity in it. -- Generate or verify that lock with `cargo xtask service-source-lock --mode - write|check --service-root <absolute-directory> --source-archive - <absolute-bundle>`. The service root supplies the exact - `workspace.metadata.radroots.service_source_lock` Cargo metadata. Every Lib - dependency, the Cargo lock, source archive, and canonical public remote must - agree on the active revision. Deferred Nix inputs must agree with each other - and remain remotely reachable, but need not equal the active revision before - terminal Nix alignment. The command rejects every source-tree change except - the exact generated lock path. +- The current service source lock is the bounded, canonical + `radroots.service.source_lock.v4.toml`, defined by + `contracts/architecture/decisions/services_hardening_source_lock.v4.json`. + It binds exact public Lib source, catalog, Cargo/Nix lock and service artifact + inputs to Rust `1.99.0`, the `service-host` profile and positive contract + versions. Bound Nix inputs must match the active Lib revision and do not + establish executed qualification. Preserve V1-V3 contracts and bootstrap + evidence as history. Reject noncanonical/extra fields and coexisting + predecessor locks; keep the codec and diagnostics private to repo tooling. +- Generate or verify the current lock with `cargo xtask service-source-lock + --mode write|check --service-root <absolute-directory> --source-archive + <absolute-bundle>`. The verified bundle transports the exact public revision; + the lock binds its canonical exact-tree archive, never its Git history. + Every Lib dependency, Cargo/Nix input and public remote must agree. The + service-owned V4 artifact contract must be committed in that service revision. + Reject every source-tree change except the exact generated lock path. +- Current unsigned artifact preparation is governed by + `contracts/architecture/decisions/services_hardening_release_artifacts.v5.json`. + Bound source and generated outputs require separate executed platform and + release evidence; this command never activates a candidate. - Generate or verify one immutable service release artifact set with `cargo xtask service-release-artifacts --mode write|check --service-root - <absolute-directory> --input-root <absolute-directory> --output-root - <absolute-directory> --target <rust-target> --source-date-epoch <seconds>`. + <absolute-directory> --lib-root <absolute-directory> --input-root + <absolute-directory> --output-root <absolute-directory> --target <rust-target> + --source-date-epoch <seconds> --candidate-digest <sha256>`. The command consumes the exact fixed input inventory, validates clean and stable service and Lib source bundles, and emits the canonical binary archive, OCI/source metadata, CycloneDX SBOM, notices, manifest, unsigned provenance signing input, and checksums. Signing credentials and signatures remain external; generated artifacts must contain no protected material. -- The native shared-build qualification contract is - `contracts/architecture/decisions/services_hardening_build_qualification.v2.json`. - It freezes the supported Rust targets, standalone Cargo and xtask commands, - native release evidence, and the fixture agreement among Cargo metadata, - the source lock, and release metadata. Nix package/app/check, development - shell, NixOS-module, and Nix-produced OCI outputs are explicitly deferred - and are not qualified by that contract. +- Historical shared-build qualification contracts V2 and V3 remain under + `contracts/architecture/decisions/services_hardening_build_qualification.*.json`. + Their fixed targets, fixtures and evidence do not qualify current compiler + changes. Current native checks use the approved shared SDK compiler target; + successor qualification and actual platform execution remain required. + Preserve the historical Nix output deferrals without claiming execution. - Current source and tests are implementation evidence. They do not silently override machine contracts. Release V2 preserves Release V1 as historical authority; the approved shared SDK decision governs the successor target. diff --git a/contracts/architecture/decisions/services_hardening_release_artifacts.v5.json b/contracts/architecture/decisions/services_hardening_release_artifacts.v5.json @@ -0,0 +1,143 @@ +{ + "schema": "radroots.service.release_artifacts.v5", + "contract_version": 5, + "decision_state": "active_unsigned_preparation", + "predecessor": { + "schema": "radroots.services-hardening.release-artifacts-decisions.v4", + "filename": "services_hardening_release_artifacts.v4.json", + "transition": "forward_only_replace" + }, + "command": "cargo xtask service-release-artifacts", + "modes": [ + "check", + "write" + ], + "required_arguments": [ + "mode", + "service_root", + "lib_root", + "input_root", + "output_root", + "target", + "source_date_epoch", + "candidate_digest" + ], + "service_metadata_path": "Cargo.toml.workspace.metadata.radroots.service_release", + "service_metadata_fields": [ + "service", + "service_package", + "binary_name", + "version" + ], + "service_license_path": "Cargo.toml.workspace.package.license_or_package.license", + "source_lock_schema": "radroots.service.source_lock.v4", + "source_lock_definition": "contracts/architecture/decisions/services_hardening_source_lock.v4.json", + "artifact_contract_binding": "source_lock_exact_regular_file_bytes_in_same_service_revision", + "artifact_admission_contract": "contracts/architecture/decisions/services_hardening_artifact_admission.v1.json", + "supported_targets": [ + "aarch64-apple-darwin", + "x86_64-unknown-linux-gnu" + ], + "candidate_binding": "explicit_sha256_candidate_identity_digest", + "lib_root_binding": "canonical_public_lib_git_root_containing_the_locked_revision", + "binary_admission": "exact_format_architecture_linkage_structural_and_native_bounded_help_smoke", + "oci_admission": "safe_materialization_exact_manifest_config_AGPL_labels_layers_and_entrypoint", + "input_inventory": [ + "config.example.toml", + "config.schema.json", + "nixos-module.nix", + "oci-image.tar.gz", + "service-binary", + "systemd.service" + ], + "excluded_parent_owned_inputs": [ + "backup_restore_runbook", + "operator_runbook" + ], + "service_root_inventory": [ + "LICENSE-APACHE", + "LICENSE-MIT", + "radroots.service.source_lock.v4.toml" + ], + "output_inventory": [ + "LICENSE-APACHE", + "LICENSE-MIT", + "SHA256SUMS", + "THIRD-PARTY-LICENSES.txt", + "THIRD-PARTY-NOTICES.txt", + "artifact-manifest.v2.json", + "artifact-scan.v1.json", + "binary.tar.gz", + "config.example.toml", + "config.schema.json", + "lib-source.tar", + "nixos-module.nix", + "oci-image.tar.gz", + "oci-image.v1.json", + "provenance.intoto.jsonl", + "radroots.service.source_lock.v4.toml", + "sbom.cdx.json", + "service-source.tar", + "source-archives.v3.json", + "systemd.service" + ], + "canonical_json": "compact_utf8_json_with_one_final_lf", + "checksum_format": "sha256_lower_hex_two_spaces_path_lf_sorted_by_path", + "source_archive_format": "canonical_uncompressed_ustar_exact_git_revision_tree_without_history", + "sbom_format": "cyclonedx_json_1_6_complete_cargo_nix_and_artifact_closure", + "license_evidence": "exact_dependency_attribution_with_bounded_license_texts", + "provenance_posture": "candidate_derived_unsigned_intoto_statement_slsa_v1_exact_manifest_subjects", + "protected_material_scan_scope": "all_artifact_bytes_and_bounded_nested_binary_oci_layer_and_source_archive_payloads", + "confidentiality_state": "derived_only_from_the_exact_artifact_scan_record", + "source_cleanliness": "no_tracked_staged_or_untracked_changes", + "revision_stability": "same_service_head_before_and_after_generation", + "no_protected_material": true, + "maximums": { + "text_input_bytes": 1048576, + "generated_document_bytes": 16777216, + "service_cargo_lock_bytes": 16777216, + "service_flake_lock_bytes": 4194304, + "binary_bytes": 536870912, + "source_archive_bytes": 1073741824, + "source_archive_member_bytes": 67108864, + "source_archive_members": 65536, + "oci_bytes": 2147483648, + "artifact_scan_expanded_bytes": 17179869184, + "cargo_metadata_bytes": 33554432, + "packages": 8192, + "workspace_packages": 64, + "source_lock_bytes": 16384 + }, + "required_negative_vectors": [ + "cyclonedx_schema_drift", + "missing_dependency_component", + "unreconciled_artifact_subject", + "missing_or_mismatched_license_text", + "invented_candidate_digest", + "git_history_bundle", + "secret_in_binary", + "secret_in_oci_layer", + "secret_in_source_archive", + "sensitive_archive_path", + "scan_confidentiality_mismatch" + ], + "negative_error_codes": [ + "invalid_contract", + "invalid_service_root", + "dirty_service_source", + "invalid_service_metadata", + "invalid_input_root", + "invalid_input_artifact", + "invalid_source_lock", + "invalid_source_bundle", + "invalid_package_inventory", + "protected_material_detected", + "invalid_output_root", + "stale_output", + "generation_failure" + ], + "implementation_scope": "current_compiler_preparation", + "rust_version": "1.99.0", + "edition": "2024", + "qualification_posture": "source_bound_preparation_requires_separate_executed_platform_and_release_evidence" +} diff --git a/tools/xtask/src/main.rs b/tools/xtask/src/main.rs @@ -57,7 +57,9 @@ mod service_repro_install; mod service_source_lock; mod service_source_lock_command; mod service_source_lock_parts; +#[cfg(test)] mod service_source_lock_v3; +mod service_source_lock_v4; #[cfg_attr(coverage_nightly, coverage(off))] mod supply_chain_qualification; #[cfg_attr(coverage_nightly, coverage(off))] diff --git a/tools/xtask/src/service_release_artifacts.rs b/tools/xtask/src/service_release_artifacts.rs @@ -15,13 +15,14 @@ use tempfile::TempDir; use crate::safe_artifact_io::{TarGzipLimits, TraversalLimits}; use crate::service_source_lock::{LIB_REPOSITORY, validate_deferred_nix_material}; -use crate::service_source_lock_v3::{ - LOCK_FILENAME, PREDECESSOR_LOCK_FILENAME, ServiceSourceLockV3, +use crate::service_source_lock_v4::{ + LOCK_FILENAME, MAX_LOCK_BYTES, PREDECESSOR_LOCK_FILENAMES, RUST_VERSION, ServiceSourceLockV4, + validate_lib_compiler_inputs, validate_toolchain_bytes, }; use crate::{artifact_admission, exact_tree_archive, safe_artifact_io}; const CONTRACT_RELATIVE: &str = - "contracts/architecture/decisions/services_hardening_release_artifacts.v4.json"; + "contracts/architecture/decisions/services_hardening_release_artifacts.v5.json"; const INPUT_NAMES: [&str; 6] = [ "config.example.toml", "config.schema.json", @@ -46,7 +47,7 @@ const OUTPUT_NAMES: [&str; 20] = [ "oci-image.tar.gz", "oci-image.v1.json", "provenance.intoto.jsonl", - "radroots.service.source-lock.v3.toml", + "radroots.service.source_lock.v4.toml", "sbom.cdx.json", "service-source.tar", "source-archives.v3.json", @@ -66,7 +67,8 @@ const MAX_CONTRACT_BYTES: usize = 65_536; const MAX_MANIFEST_BYTES: usize = 1_048_576; const MAX_TEXT_INPUT_BYTES: u64 = 1_048_576; const MAX_GENERATED_DOCUMENT_BYTES: u64 = 16_777_216; -const MAX_SOURCE_LOCK_BYTES: u64 = 4_096; +const MAX_SOURCE_LOCK_BYTES: u64 = MAX_LOCK_BYTES as u64; +const MAX_TOOLCHAIN_BYTES: usize = 65_536; const MAX_SERVICE_CARGO_LOCK_BYTES: u64 = 16_777_216; const MAX_SERVICE_FLAKE_LOCK_BYTES: u64 = 4_194_304; const MAX_BINARY_BYTES: u64 = 536_870_912; @@ -440,7 +442,10 @@ struct ReleaseDecision { schema: String, contract_version: u32, decision_state: String, - owner_step: u32, + implementation_scope: String, + rust_version: String, + edition: String, + qualification_posture: String, predecessor: ReleasePredecessor, command: String, modes: Vec<String>, @@ -488,6 +493,7 @@ struct ReleasePredecessor { #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] struct ReleaseMaximums { + source_lock_bytes: u64, text_input_bytes: u64, generated_document_bytes: u64, service_cargo_lock_bytes: u64, @@ -555,7 +561,7 @@ fn run_inner(arguments: Arguments<'_>) -> Result<(), ReleaseArtifactError> { MAX_SOURCE_LOCK_BYTES, ReleaseArtifactError::InvalidSourceLock, )?; - let source_lock = ServiceSourceLockV3::from_canonical_bytes(&source_lock_bytes) + let source_lock = ServiceSourceLockV4::from_canonical_bytes(&source_lock_bytes) .map_err(|_| ReleaseArtifactError::InvalidSourceLock)?; if source_lock.service() != metadata.service { return Err(ReleaseArtifactError::InvalidSourceLock); @@ -666,6 +672,22 @@ fn run_inner(arguments: Arguments<'_>) -> Result<(), ReleaseArtifactError> { MAX_TEXT_INPUT_BYTES as usize, ) .map_err(|_| ReleaseArtifactError::InvalidSourceBundle)?; + let lib_manifest = exact_tree_archive::read_blob( + &lib_root, + source_lock.revision(), + "Cargo.toml", + MAX_TEXT_INPUT_BYTES as usize, + ) + .map_err(|_| ReleaseArtifactError::InvalidSourceBundle)?; + let lib_toolchain = exact_tree_archive::read_blob( + &lib_root, + source_lock.revision(), + "rust-toolchain.toml", + MAX_TOOLCHAIN_BYTES, + ) + .map_err(|_| ReleaseArtifactError::InvalidSourceBundle)?; + validate_lib_compiler_inputs(&lib_manifest, &lib_toolchain, &catalog) + .map_err(|_| ReleaseArtifactError::InvalidSourceBundle)?; if sha256_bytes(&catalog) != source_lock.workspace_catalog_sha256() { return Err(ReleaseArtifactError::InvalidSourceBundle); } @@ -747,7 +769,7 @@ fn run_inner(arguments: Arguments<'_>) -> Result<(), ReleaseArtifactError> { source_date_epoch, service_revision: initial_head.clone(), lib_revision: source_lock.revision().to_owned(), - rust_version: "1.97.1", + rust_version: RUST_VERSION, host_feature_profile: "service-host", contract_versions: ContractVersionsDocument { config: versions.config(), @@ -872,17 +894,20 @@ fn read_release_metadata(root: &Path) -> Result<ReleaseMetadata, ReleaseArtifact fn validate_source_lock_files( root: &Path, - source_lock: &ServiceSourceLockV3, + source_lock: &ServiceSourceLockV4, ) -> Result<(), ReleaseArtifactError> { - for name in [ - PREDECESSOR_LOCK_FILENAME, - "radroots.service.source-lock.v1.toml", - ] { + for name in PREDECESSOR_LOCK_FILENAMES { match fs::symlink_metadata(root.join(name)) { Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} _ => return Err(ReleaseArtifactError::InvalidSourceLock), } } + let toolchain = read_bounded_regular( + &root.join("rust-toolchain.toml"), + MAX_TOOLCHAIN_BYTES as u64, + ReleaseArtifactError::InvalidSourceLock, + )?; + validate_toolchain_bytes(&toolchain).map_err(|_| ReleaseArtifactError::InvalidSourceLock)?; let cargo_lock = hash_regular(&root.join("Cargo.lock"), MAX_SERVICE_CARGO_LOCK_BYTES) .map_err(|_| ReleaseArtifactError::InvalidSourceLock)?; if cargo_lock.sha256 != source_lock.cargo_lock_sha256() { @@ -915,6 +940,14 @@ fn validate_source_lock_files( } } +#[cfg(test)] +pub(crate) fn validate_writer_fixture( + root: &Path, + source_lock: &ServiceSourceLockV4, +) -> Result<(), String> { + validate_source_lock_files(root, source_lock).map_err(|error| error.to_string()) +} + fn cargo_metadata(root: &Path) -> Result<CargoMetadata, ReleaseArtifactError> { let mut command = Command::new("cargo"); command @@ -2297,7 +2330,7 @@ fn output_maximum(name: &str) -> Result<u64, ReleaseArtifactError> { | "config.example.toml" | "config.schema.json" | "nixos-module.nix" - | "radroots.service.source-lock.v3.toml" + | "radroots.service.source_lock.v4.toml" | "systemd.service" => Ok(MAX_TEXT_INPUT_BYTES), "SHA256SUMS" | "THIRD-PARTY-LICENSES.txt" @@ -2669,13 +2702,17 @@ fn validate_decision(decision: &ReleaseDecision) -> Result<(), ReleaseArtifactEr ReleaseArtifactError::StaleOutput, ReleaseArtifactError::GenerationFailure, ]; - if decision.schema != "radroots.services-hardening.release-artifacts-decisions.v4" - || decision.contract_version != 4 - || decision.decision_state != "active" - || decision.owner_step != 305 + if decision.schema != "radroots.service.release_artifacts.v5" + || decision.contract_version != 5 + || decision.decision_state != "active_unsigned_preparation" + || decision.implementation_scope != "current_compiler_preparation" + || decision.rust_version != RUST_VERSION + || decision.edition != "2024" + || decision.qualification_posture + != "source_bound_preparation_requires_separate_executed_platform_and_release_evidence" || decision.predecessor.schema - != "radroots.services-hardening.release-artifacts-decisions.v3" - || decision.predecessor.filename != "services_hardening_release_artifacts.v3.json" + != "radroots.services-hardening.release-artifacts-decisions.v4" + || decision.predecessor.filename != "services_hardening_release_artifacts.v4.json" || decision.predecessor.transition != "forward_only_replace" || decision.command != "cargo xtask service-release-artifacts" || decision.modes != ["check", "write"] @@ -2696,9 +2733,9 @@ fn validate_decision(decision: &ReleaseDecision) -> Result<(), ReleaseArtifactEr != ["service", "service_package", "binary_name", "version"] || decision.service_license_path != "Cargo.toml.workspace.package.license_or_package.license" - || decision.source_lock_schema != "radroots.service.source-lock.v3" + || decision.source_lock_schema != "radroots.service.source_lock.v4" || decision.source_lock_definition - != "contracts/architecture/decisions/services_hardening_source_lock.v3.json" + != "contracts/architecture/decisions/services_hardening_source_lock.v4.json" || decision.artifact_contract_binding != "source_lock_exact_regular_file_bytes_in_same_service_revision" || decision.artifact_admission_contract @@ -2729,6 +2766,7 @@ fn validate_decision(decision: &ReleaseDecision) -> Result<(), ReleaseArtifactEr || decision.source_cleanliness != "no_tracked_staged_or_untracked_changes" || decision.revision_stability != "same_service_head_before_and_after_generation" || !decision.no_protected_material + || decision.maximums.source_lock_bytes != MAX_SOURCE_LOCK_BYTES || decision.maximums.text_input_bytes != MAX_TEXT_INPUT_BYTES || decision.maximums.generated_document_bytes != MAX_GENERATED_DOCUMENT_BYTES || decision.maximums.service_cargo_lock_bytes != MAX_SERVICE_CARGO_LOCK_BYTES @@ -2769,7 +2807,7 @@ mod tests { use std::process::Command; use crate::service_source_lock::ContractVersions; - use crate::service_source_lock_v3::FixtureParts; + use crate::service_source_lock_v4::ServiceSourceLockV4Parts; use super::*; @@ -2795,9 +2833,15 @@ mod tests { write_file( &lib.join("contracts/crates/catalog.v2.toml"), - b"schema_version = 2\n", + b"schema_version = 2\nschema = \"radroots.workspace.catalog.v2\"\narchitecture = \"radroots.crates.release.v2\"\nversion = \"0.1.0-alpha\"\nrust_version = \"1.99.0\"\nedition = \"2024\"\nresolver = \"3\"\n", ); write_file(&lib.join("README.md"), b"fixture Lib source\n"); + write_file(&lib.join("Cargo.toml"), + b"[workspace]\nresolver = \"3\"\n[workspace.package]\nversion = \"0.1.0-alpha\"\nrust-version = \"1.99.0\"\nedition = \"2024\"\n"); + write_file( + &lib.join("rust-toolchain.toml"), + b"[toolchain]\nchannel = \"1.99.0\"\n", + ); initialize_git(&lib, "https://github.com/radrootslabs/lib"); let lib_revision = git_output(&lib, &["rev-parse", "HEAD"]); let lib_archive_path = canonical_root.join("lib-source.tar"); @@ -2818,6 +2862,7 @@ mod tests { name = "fixture-service" version = "0.1.0-alpha" edition = "2024" +rust-version = "1.99.0" license = "AGPL-3.0-or-later" [[bin]] @@ -2836,6 +2881,10 @@ version = "0.1.0-alpha" ); write_file(&service.join("src/main.rs"), b"fn main() {}\n"); write_file( + &service.join("rust-toolchain.toml"), + b"[toolchain]\nchannel = \"1.99.0\"\n", + ); + write_file( &service.join("Cargo.lock"), br#"# This file is automatically @generated by Cargo. # It is not intended for manual editing. @@ -2866,7 +2915,7 @@ version = "0.1.0-alpha" ); write_file(&service.join("LICENSE-MIT"), b"MIT fixture license\n"); let artifact_contract_path = - service.join("contracts/release/myc-artifact-contract.v3.json"); + service.join("contracts/release/myc-artifact-contract.v4.json"); fs::create_dir_all( artifact_contract_path .parent() @@ -2877,7 +2926,7 @@ version = "0.1.0-alpha" let cargo_lock = fs::read(service.join("Cargo.lock")).expect("Cargo lock"); let flake_lock = fs::read(service.join("flake.lock")).expect("flake lock"); let artifact_contract = fs::read(&artifact_contract_path).expect("artifact contract"); - let source_lock = ServiceSourceLockV3::fixture(FixtureParts { + let source_lock = ServiceSourceLockV4::new(ServiceSourceLockV4Parts { service: "myc", revision: &lib_revision, workspace_catalog_sha256: &sha256_bytes(&catalog), @@ -3442,7 +3491,11 @@ version = "0.1.0-alpha" ("/schema", serde_json::json!("other")), ("/contract_version", serde_json::json!(1)), ("/decision_state", serde_json::json!("draft")), - ("/owner_step", serde_json::json!(1)), + ("/implementation_scope", serde_json::json!("other")), + ("/rust_version", serde_json::json!("1.97.1")), + ("/edition", serde_json::json!("2021")), + ("/qualification_posture", serde_json::json!("qualified")), + ("/maximums/source_lock_bytes", serde_json::json!(1)), ("/predecessor/schema", serde_json::json!("other")), ("/predecessor/filename", serde_json::json!("other")), ("/predecessor/transition", serde_json::json!("other")), @@ -4084,7 +4137,7 @@ version = "0.1.0-alpha" Err(ReleaseArtifactError::StaleOutput) ); - let source_lock = ServiceSourceLockV3::from_canonical_bytes( + let source_lock = ServiceSourceLockV4::from_canonical_bytes( &fs::read(fixture.service.join(LOCK_FILENAME)).expect("source lock"), ) .expect("source lock"); @@ -4419,6 +4472,20 @@ version = "0.1.0-alpha" ); } + let manifest: serde_json::Value = serde_json::from_slice( + &fs::read(fixture.output_a.join("artifact-manifest.v2.json")).expect("manifest"), + ) + .expect("manifest JSON"); + assert_eq!(manifest["rust_version"], "1.99.0"); + let produced_lock = ServiceSourceLockV4::from_canonical_bytes( + &fs::read(fixture.output_a.join(LOCK_FILENAME)).expect("output source lock"), + ) + .expect("current canonical output"); + assert!( + std::str::from_utf8(produced_lock.canonical_bytes()) + .expect("UTF-8") + .contains("material = \"bound\"") + ); let sums = fs::read_to_string(fixture.output_a.join("SHA256SUMS")).expect("checksum inventory"); let mut prior = ""; @@ -4483,11 +4550,11 @@ version = "0.1.0-alpha" } #[test] - fn qualified_nix_material_is_required_and_preserved() { + fn bound_nix_inputs_are_required_and_preserved_without_qualification_claim() { let fixture = ReleaseFixture::new(); fixture .write(&fixture.output_a) - .expect("qualified-Nix release"); + .expect("bound-input preparation"); let document: serde_json::Value = serde_json::from_slice( &fs::read(fixture.output_a.join("source-archives.v3.json")) @@ -4503,23 +4570,23 @@ version = "0.1.0-alpha" 64 )); - let lock = ServiceSourceLockV3::from_canonical_bytes( + let lock = ServiceSourceLockV4::from_canonical_bytes( &fs::read(fixture.service.join(LOCK_FILENAME)).expect("source lock"), ) .expect("valid source lock"); for name in ["flake.nix", "flake.lock"] { let path = fixture.service.join(name); - let original = fs::read(&path).expect("qualified Nix file"); - fs::remove_file(&path).expect("remove qualified Nix file"); + let original = fs::read(&path).expect("bound Nix file"); + fs::remove_file(&path).expect("remove bound Nix file"); assert_eq!( validate_source_lock_files(&fixture.service, &lock), Err(ReleaseArtifactError::InvalidSourceLock), - "accepted missing qualified-Nix file {name}" + "accepted missing bound-Nix file {name}" ); write_file(&path, &original); } write_file( - &fixture.service.join(PREDECESSOR_LOCK_FILENAME), + &fixture.service.join(PREDECESSOR_LOCK_FILENAMES[2]), b"unexpected", ); assert_eq!( @@ -4568,7 +4635,7 @@ version = "0.1.0-alpha" ); let fixture = ReleaseFixture::new(); - let current = ServiceSourceLockV3::from_canonical_bytes( + let current = ServiceSourceLockV4::from_canonical_bytes( &fs::read(fixture.service.join(LOCK_FILENAME)).expect("source lock"), ) .expect("source lock"); @@ -4832,7 +4899,7 @@ version = "0.1.0-alpha" source_date_epoch: 1_700_000_000, service_revision: "1".repeat(40), lib_revision: "2".repeat(40), - rust_version: "1.97.1", + rust_version: RUST_VERSION, host_feature_profile: "service-host", contract_versions: ContractVersionsDocument { config: 1, diff --git a/tools/xtask/src/service_source_lock_command.rs b/tools/xtask/src/service_source_lock_command.rs @@ -13,10 +13,13 @@ use tempfile::{NamedTempFile, TempDir}; use walkdir::WalkDir; use crate::build_output::is_build_output_directory; +use crate::exact_tree_archive; use crate::service_source_lock::{ - ContractVersions, LIB_REPOSITORY, LOCK_FILENAME, NixMaterialParts, NixMaterialState, - PREDECESSOR_LOCK_FILENAME, ServiceSourceLockParts, ServiceSourceLockV2, - validate_deferred_nix_material, + ContractVersions, LIB_REPOSITORY, validate_deferred_nix_material, +}; +use crate::service_source_lock_v4::{ + LOCK_FILENAME, MAX_LOCK_BYTES, PREDECESSOR_LOCK_FILENAMES, ServiceSourceLockV4, + ServiceSourceLockV4Parts, validate_lib_compiler_inputs, validate_toolchain_bytes, }; const CATALOG_RELATIVE: &str = "contracts/crates/catalog.v2.toml"; @@ -28,7 +31,6 @@ const RUST_TOOLCHAIN: &str = "rust-toolchain.toml"; const LIB_VERSION_REQUIREMENT: &str = "=0.1.0-alpha"; const HOST_PACKAGE: &str = "radroots_service_host"; const HOST_FEATURE_PROFILE: &str = "service-host"; -const RUST_VERSION: &str = "1.97.1"; const MAX_MANIFEST_BYTES: usize = 1_048_576; const MAX_CARGO_LOCK_BYTES: usize = 16_777_216; const MAX_FLAKE_LOCK_BYTES: usize = 4_194_304; @@ -53,6 +55,7 @@ enum CommandError { InvalidServiceMetadata, InvalidCargoManifest, InvalidCargoLock, + InvalidArtifactContract, InvalidNixMaterial, InvalidFlakeLock, InvalidToolchain, @@ -71,7 +74,8 @@ impl fmt::Display for CommandError { Self::InvalidServiceMetadata => "service source-lock metadata is invalid", Self::InvalidCargoManifest => "service Cargo manifest dependency is invalid", Self::InvalidCargoLock => "service Cargo lock is invalid", - Self::InvalidNixMaterial => "service deferred Nix material is invalid", + Self::InvalidArtifactContract => "service artifact contract is invalid", + Self::InvalidNixMaterial => "service bound Nix material is invalid", Self::InvalidFlakeLock => "service flake lock is invalid", Self::InvalidToolchain => "service Rust toolchain is invalid", Self::InvalidSourceArchive => "Lib source archive is invalid", @@ -88,39 +92,13 @@ impl std::error::Error for CommandError {} #[derive(Clone, Debug, Eq, PartialEq)] struct ServiceMetadata { service: String, - nix_material: NixMaterialState, contract_versions: ContractVersions, } #[derive(Clone, Debug, Eq, PartialEq)] -enum NixMaterialEvidence { - Absent, - Deferred { - lib_revision: String, - flake_lock_sha256: String, - }, -} - -impl NixMaterialEvidence { - fn as_parts(&self) -> NixMaterialParts<'_> { - match self { - Self::Absent => NixMaterialParts::Absent, - Self::Deferred { - lib_revision, - flake_lock_sha256, - } => NixMaterialParts::Deferred { - lib_revision, - flake_lock_sha256, - }, - } - } - - fn revision(&self) -> Option<&str> { - match self { - Self::Absent => None, - Self::Deferred { lib_revision, .. } => Some(lib_revision), - } - } +struct NixMaterialEvidence { + lib_revision: String, + flake_lock_sha256: String, } #[derive(Debug, Deserialize)] @@ -187,9 +165,11 @@ fn run_with( reachability: &dyn RevisionReachability, ) -> Result<(), CommandError> { let service_root = validate_service_root(service_root)?; - match fs::symlink_metadata(service_root.join(PREDECESSOR_LOCK_FILENAME)) { - Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} - _ => return Err(CommandError::InvalidSourceLock), + for predecessor in PREDECESSOR_LOCK_FILENAMES { + match fs::symlink_metadata(service_root.join(predecessor)) { + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + _ => return Err(CommandError::InvalidSourceLock), + } } let initial_head = service_head(&service_root)?; validate_service_cleanliness(&service_root)?; @@ -203,7 +183,10 @@ fn run_with( let metadata = parse_service_metadata(&root_manifest)?; let revision = validate_cargo_manifests(&service_root, None)?; - let nix = read_nix_material(&service_root, metadata.nix_material)?; + let nix = read_nix_material(&service_root)?; + if nix.lib_revision != revision { + return Err(CommandError::InvalidNixMaterial); + } validate_toolchain(&service_root)?; let archive = validate_archive(source_archive, &revision)?; @@ -219,23 +202,39 @@ fn run_with( validate_cargo_lock(&cargo_lock, &revision, &archive.package_names)?; let cargo_lock_sha256 = sha256(&cargo_lock); reachability.verify(&archive.revision)?; - if let Some(nix_revision) = nix.revision() - && nix_revision != archive.revision - { - reachability.verify(nix_revision)?; + let artifact_path = format!( + "contracts/release/{}-artifact-contract.v4.json", + metadata.service + ); + let artifact_contract = exact_tree_archive::read_blob( + &service_root, + &initial_head, + &artifact_path, + MAX_MANIFEST_BYTES, + ) + .map_err(|_| CommandError::InvalidArtifactContract)?; + let live_contract = read_bounded_regular( + &service_root.join(&artifact_path), + MAX_MANIFEST_BYTES, + CommandError::InvalidArtifactContract, + )?; + if artifact_contract != live_contract { + return Err(CommandError::InvalidArtifactContract); } + let artifact_contract_sha256 = sha256(&artifact_contract); validate_service_cleanliness(&service_root)?; if service_head(&service_root)? != initial_head { return Err(CommandError::DirtyServiceSource); } - let desired = ServiceSourceLockV2::new(ServiceSourceLockParts { + let desired = ServiceSourceLockV4::new(ServiceSourceLockV4Parts { service: &metadata.service, revision: &archive.revision, workspace_catalog_sha256: &archive.catalog_sha256, source_archive_sha256: &archive.archive_sha256, cargo_lock_sha256: &cargo_lock_sha256, - nix: nix.as_parts(), + flake_lock_sha256: &nix.flake_lock_sha256, + artifact_contract_sha256: &artifact_contract_sha256, contract_versions: metadata.contract_versions, }) .map_err(|_| CommandError::InvalidServiceMetadata)?; @@ -243,8 +242,9 @@ fn run_with( let lock_path = service_root.join(LOCK_FILENAME); let result = match mode { CommandMode::Check => { - let current = read_bounded_regular(&lock_path, 4096, CommandError::InvalidSourceLock)?; - let current = ServiceSourceLockV2::from_canonical_bytes(&current) + let current = + read_bounded_regular(&lock_path, MAX_LOCK_BYTES, CommandError::InvalidSourceLock)?; + let current = ServiceSourceLockV4::from_canonical_bytes(&current) .map_err(|_| CommandError::InvalidSourceLock)?; if current == desired { Ok(()) @@ -254,8 +254,9 @@ fn run_with( } CommandMode::Write => { atomic_write_lock(&service_root, &lock_path, desired.canonical_bytes())?; - let current = read_bounded_regular(&lock_path, 4096, CommandError::WriteFailure)?; - let current = ServiceSourceLockV2::from_canonical_bytes(&current) + let current = + read_bounded_regular(&lock_path, MAX_LOCK_BYTES, CommandError::WriteFailure)?; + let current = ServiceSourceLockV4::from_canonical_bytes(&current) .map_err(|_| CommandError::WriteFailure)?; if current == desired { Ok(()) @@ -318,7 +319,7 @@ fn validate_service_cleanliness(root: &Path) -> Result<(), CommandError> { "--quiet", "--", ".", - ":(exclude)radroots.service.source-lock.v2.toml", + ":(exclude)radroots.service.source_lock.v4.toml", ], ) .map_err(|_| CommandError::DirtyServiceSource)?; @@ -330,7 +331,7 @@ fn validate_service_cleanliness(root: &Path) -> Result<(), CommandError> { "--quiet", "--", ".", - ":(exclude)radroots.service.source-lock.v2.toml", + ":(exclude)radroots.service.source_lock.v4.toml", ], ) .map_err(|_| CommandError::DirtyServiceSource)?; @@ -388,14 +389,11 @@ fn parse_service_metadata(root: &toml::Value) -> Result<ServiceMetadata, Command if text("host_feature_profile")? != HOST_FEATURE_PROFILE { return Err(CommandError::InvalidServiceMetadata); } - let nix_material = match text("nix_material")? { - "absent" => NixMaterialState::Absent, - "deferred" => NixMaterialState::Deferred, - _ => return Err(CommandError::InvalidServiceMetadata), - }; + if text("nix_material")? != "bound" || !matches!(text("service")?, "myc" | "rhi") { + return Err(CommandError::InvalidServiceMetadata); + } Ok(ServiceMetadata { service: text("service")?.to_owned(), - nix_material, contract_versions: ContractVersions::new( version("config_contract_version")?, version("state_contract_version")?, @@ -576,41 +574,25 @@ fn validate_cargo_lock( } } -fn read_nix_material( - root: &Path, - state: NixMaterialState, -) -> Result<NixMaterialEvidence, CommandError> { - match state { - NixMaterialState::Absent => { - for name in [FLAKE_NIX, FLAKE_LOCK] { - match fs::symlink_metadata(root.join(name)) { - Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} - _ => return Err(CommandError::InvalidNixMaterial), - } - } - Ok(NixMaterialEvidence::Absent) - } - NixMaterialState::Deferred => { - let expression = read_bounded_regular( - &root.join(FLAKE_NIX), - MAX_FLAKE_NIX_BYTES, - CommandError::InvalidNixMaterial, - )?; - let lock = read_bounded_regular( - &root.join(FLAKE_LOCK), - MAX_FLAKE_LOCK_BYTES, - CommandError::InvalidFlakeLock, - )?; - crate::service_source_lock::validate_deferred_nix_lock(&lock) - .map_err(|_| CommandError::InvalidFlakeLock)?; - let evidence = validate_deferred_nix_material(&expression, &lock) - .map_err(|_| CommandError::InvalidNixMaterial)?; - Ok(NixMaterialEvidence::Deferred { - lib_revision: evidence.lib_revision().to_owned(), - flake_lock_sha256: evidence.flake_lock_sha256().to_owned(), - }) - } - } +fn read_nix_material(root: &Path) -> Result<NixMaterialEvidence, CommandError> { + let expression = read_bounded_regular( + &root.join(FLAKE_NIX), + MAX_FLAKE_NIX_BYTES, + CommandError::InvalidNixMaterial, + )?; + let lock = read_bounded_regular( + &root.join(FLAKE_LOCK), + MAX_FLAKE_LOCK_BYTES, + CommandError::InvalidFlakeLock, + )?; + crate::service_source_lock::validate_deferred_nix_lock(&lock) + .map_err(|_| CommandError::InvalidFlakeLock)?; + let evidence = validate_deferred_nix_material(&expression, &lock) + .map_err(|_| CommandError::InvalidNixMaterial)?; + Ok(NixMaterialEvidence { + lib_revision: evidence.lib_revision().to_owned(), + flake_lock_sha256: evidence.flake_lock_sha256().to_owned(), + }) } #[cfg(test)] @@ -625,17 +607,7 @@ fn validate_toolchain(root: &Path) -> Result<(), CommandError> { MAX_TOOLCHAIN_BYTES, CommandError::InvalidToolchain, )?; - let value = parse_toml(&bytes, CommandError::InvalidToolchain)?; - if value - .get("toolchain") - .and_then(|value| value.get("channel")) - .and_then(toml::Value::as_str) - == Some(RUST_VERSION) - { - Ok(()) - } else { - Err(CommandError::InvalidToolchain) - } + validate_toolchain_bytes(&bytes).map_err(|_| CommandError::InvalidToolchain) } fn validate_archive(path: &Path, revision: &str) -> Result<ArchiveEvidence, CommandError> { @@ -655,7 +627,6 @@ fn validate_archive(path: &Path, revision: &str) -> Result<ArchiveEvidence, Comm .metadata() .map_err(|_| CommandError::InvalidSourceArchive)?; let mut stable = NamedTempFile::new().map_err(|_| CommandError::InvalidSourceArchive)?; - let mut hasher = Sha256::new(); let mut total = 0_u64; let mut buffer = [0_u8; 65_536]; loop { @@ -669,7 +640,6 @@ fn validate_archive(path: &Path, revision: &str) -> Result<ArchiveEvidence, Comm .checked_add(u64::try_from(read).map_err(|_| CommandError::InvalidSourceArchive)?) .filter(|total| *total <= MAX_ARCHIVE_BYTES) .ok_or(CommandError::InvalidSourceArchive)?; - hasher.update(&buffer[..read]); stable .write_all(&buffer[..read]) .map_err(|_| CommandError::InvalidSourceArchive)?; @@ -739,9 +709,37 @@ fn validate_archive(path: &Path, revision: &str) -> Result<ArchiveEvidence, Comm return Err(CommandError::InvalidSourceArchive); } let package_names = catalog_package_names(&catalog)?; + let manifest = exact_tree_archive::read_blob( + &fs::canonicalize(verification.path()).map_err(|_| CommandError::InvalidSourceArchive)?, + revision, + CARGO_MANIFEST, + MAX_MANIFEST_BYTES, + ) + .map_err(|_| CommandError::InvalidSourceArchive)?; + let verification_root = + fs::canonicalize(verification.path()).map_err(|_| CommandError::InvalidSourceArchive)?; + let toolchain = exact_tree_archive::read_blob( + &verification_root, + revision, + RUST_TOOLCHAIN, + MAX_TOOLCHAIN_BYTES, + ) + .map_err(|_| CommandError::InvalidSourceArchive)?; + validate_lib_compiler_inputs(&manifest, &toolchain, &catalog) + .map_err(|_| CommandError::InvalidSourceArchive)?; + // The bundle is transfer evidence. The lock always binds the canonical tree. + let mtime = exact_tree_archive::commit_timestamp(&verification_root, revision) + .map_err(|_| CommandError::InvalidSourceArchive)?; + let tree = exact_tree_archive::create( + &verification_root, + revision, + &verification_root.join("source-tree.tar"), + mtime, + ) + .map_err(|_| CommandError::InvalidSourceArchive)?; Ok(ArchiveEvidence { revision: revision.to_owned(), - archive_sha256: hex::encode(hasher.finalize()), + archive_sha256: tree.sha256, catalog_sha256: sha256(&catalog), package_names, }) @@ -963,6 +961,9 @@ mod tests { schema = "radroots.workspace.catalog.v2" architecture = "radroots.crates.release.v2" version = "0.1.0-alpha" +rust_version = "1.99.0" +edition = "2024" +resolver = "3" package_count = 2 [[package]] @@ -973,6 +974,14 @@ name = "radroots_service_host" "#, ) .expect("catalog"); + fs::write(lib.join(CARGO_MANIFEST), + "[workspace]\nresolver = \"3\"\n[workspace.package]\nversion = \"0.1.0-alpha\"\nrust-version = \"1.99.0\"\nedition = \"2024\"\n" + ).expect("Lib manifest"); + fs::write( + lib.join(RUST_TOOLCHAIN), + "[toolchain]\nchannel = \"1.99.0\"\n", + ) + .expect("Lib toolchain"); git(&lib, &["add", "."]); git(&lib, &["commit", "--quiet", "-m", "fixture"]); git(&lib, &["branch", "-M", "archive"]); @@ -1000,9 +1009,9 @@ name = "radroots_service_host" resolver = "3" [workspace.metadata.radroots.service_source_lock] -service = "fixture_service" +service = "myc" host_feature_profile = "service-host" -nix_material = "deferred" +nix_material = "bound" config_contract_version = 1 state_contract_version = 2 admin_contract_version = 3 @@ -1039,9 +1048,15 @@ radroots_service_host = {{ git = "{LIB_REPOSITORY}", rev = "{revision}", version .expect("flake.lock"); fs::write( service.join(RUST_TOOLCHAIN), - "[toolchain]\nchannel = \"1.97.1\"\ncomponents = [\"clippy\", \"rustfmt\"]\n", + "[toolchain]\nchannel = \"1.99.0\"\ncomponents = [\"clippy\", \"rustfmt\"]\n", ) .expect("toolchain"); + fs::create_dir_all(service.join("contracts/release")).expect("artifact parent"); + fs::write( + service.join("contracts/release/myc-artifact-contract.v4.json"), + b"{}\n", + ) + .expect("artifact contract"); git(&service, &["init", "--quiet"]); git( &service, @@ -1095,12 +1110,30 @@ radroots_service_host = {{ git = "{LIB_REPOSITORY}", rev = "{revision}", version let bytes = fs::read(fixture.service.join(LOCK_FILENAME)).expect("lock"); let text = std::str::from_utf8(&bytes).expect("UTF-8"); - assert!(text.contains("service = \"fixture_service\"")); + assert!(text.contains("service = \"myc\"")); assert!(text.contains(&format!("revision = \"{}\"", fixture.revision))); - assert!(text.contains("material = \"deferred\"")); + assert!(text.contains("material = \"bound\"")); assert!(text.contains(&format!("lib_revision = \"{}\"", fixture.revision))); assert!(text.contains("config = 1\nstate = 2\nadmin = 3\nstatus = 4\nprovider = 5")); assert!(fixture.root.path().exists()); + let lock = ServiceSourceLockV4::from_canonical_bytes(&bytes).expect("V4 lock"); + let lib = fs::canonicalize(fixture.root.path().join("lib")).expect("Lib root"); + let tree = exact_tree_archive::create( + &lib, + &fixture.revision, + &lib.parent() + .expect("fixture parent") + .join("expected-source.tar"), + exact_tree_archive::commit_timestamp(&lib, &fixture.revision).expect("commit time"), + ) + .expect("exact source tree"); + assert_eq!(lock.source_archive_sha256(), tree.sha256); + assert_ne!( + lock.source_archive_sha256(), + sha256(&fs::read(&fixture.archive).expect("bundle")) + ); + crate::service_release_artifacts::validate_writer_fixture(&fixture.service, &lock) + .expect("artifact consumer admits actual writer output"); } #[test] @@ -1217,7 +1250,7 @@ radroots_service_host = {{ git = "{LIB_REPOSITORY}", rev = "{revision}", version ), ( RUST_TOOLCHAIN, - "1.97.1", + "1.99.0", "1.97.0", CommandError::InvalidToolchain, ), @@ -1390,88 +1423,85 @@ radroots_service_host = {{ git = "{LIB_REPOSITORY}", rev = "{revision}", version } #[test] - fn absent_and_independently_revisioned_deferred_nix_material_are_exact() { - let fixture = Fixture::new(); - let manifest = fixture.service.join(CARGO_MANIFEST); - let source = fs::read_to_string(&manifest).expect("manifest"); - fs::write( - &manifest, - source.replacen( - "nix_material = \"deferred\"", - "nix_material = \"absent\"", - 1, - ), - ) - .expect("absent metadata"); - fs::remove_file(fixture.service.join(FLAKE_NIX)).expect("remove flake expression"); - fs::remove_file(fixture.service.join(FLAKE_LOCK)).expect("remove flake lock"); - git(&fixture.service, &["add", "-A"]); - git(&fixture.service, &["commit", "--quiet", "-m", "absent Nix"]); - run_with( - CommandMode::Write, - &fixture.service, - &fixture.archive, - &fixture.reachable(), - ) - .expect("write absent-Nix lock"); - let lock = fs::read(fixture.service.join(LOCK_FILENAME)).expect("source lock"); - let lock = ServiceSourceLockV2::from_canonical_bytes(&lock).expect("canonical lock"); - assert_eq!(lock.nix_material_state(), NixMaterialState::Absent); - assert_eq!(lock.nix_lib_revision(), None); - assert_eq!(lock.flake_lock_sha256(), None); - - let fixture = Fixture::new(); - let deferred_revision = "b".repeat(40); - for name in [FLAKE_NIX, FLAKE_LOCK] { - let path = fixture.service.join(name); - let current = fs::read_to_string(&path).expect("Nix source"); + fn current_lock_rejects_historical_bootstrap_and_independently_revisioned_nix() { + for state in ["absent", "deferred", "qualified"] { + let fixture = Fixture::new(); + let manifest = fixture.service.join(CARGO_MANIFEST); + let source = fs::read_to_string(&manifest).expect("manifest"); fs::write( - &path, - current.replace(&fixture.revision, &deferred_revision), + &manifest, + source.replace( + "nix_material = \"bound\"", + &format!("nix_material = \"{state}\""), + ), ) - .expect("deferred revision"); + .expect("historical metadata"); + if state == "absent" { + fs::remove_file(fixture.service.join(FLAKE_NIX)).expect("remove flake"); + fs::remove_file(fixture.service.join(FLAKE_LOCK)).expect("remove lock"); + } + git(&fixture.service, &["add", "-A"]); + git( + &fixture.service, + &["commit", "--quiet", "-m", "historical state"], + ); + assert_eq!( + run_with( + CommandMode::Write, + &fixture.service, + &fixture.archive, + &fixture.reachable() + ), + Err(CommandError::InvalidServiceMetadata) + ); + assert!(!fixture.service.join(LOCK_FILENAME).exists()); } - git(&fixture.service, &["add", "."]); - git( - &fixture.service, - &["commit", "--quiet", "-m", "defer Nix revision"], - ); - run_with( - CommandMode::Write, - &fixture.service, - &fixture.archive, - &fixture.reachable(), - ) - .expect("write independently revisioned lock"); - let lock = fs::read(fixture.service.join(LOCK_FILENAME)).expect("source lock"); - let lock = ServiceSourceLockV2::from_canonical_bytes(&lock).expect("canonical lock"); - assert_eq!(lock.revision(), fixture.revision); - assert_eq!(lock.nix_material_state(), NixMaterialState::Deferred); - assert_eq!(lock.nix_lib_revision(), Some(deferred_revision.as_str())); - } - - #[test] - fn predecessor_source_lock_is_rejected_instead_of_forming_dual_authority() { let fixture = Fixture::new(); - fs::write( - fixture.service.join(PREDECESSOR_LOCK_FILENAME), - b"historical lock", - ) - .expect("predecessor lock"); + for name in [FLAKE_NIX, FLAKE_LOCK] { + let path = fixture.service.join(name); + let source = fs::read_to_string(&path).expect("Nix input"); + fs::write(&path, source.replace(&fixture.revision, &"b".repeat(40))) + .expect("independent revision"); + } git(&fixture.service, &["add", "."]); git( &fixture.service, - &["commit", "--quiet", "-m", "retain predecessor lock"], + &["commit", "--quiet", "-m", "independent revision"], ); assert_eq!( run_with( CommandMode::Write, &fixture.service, &fixture.archive, - &fixture.reachable(), + &fixture.reachable() ), - Err(CommandError::InvalidSourceLock) + Err(CommandError::InvalidNixMaterial) ); + assert!(!fixture.service.join(LOCK_FILENAME).exists()); + } + + #[test] + fn predecessor_source_lock_is_rejected_instead_of_forming_dual_authority() { + for predecessor in PREDECESSOR_LOCK_FILENAMES { + let fixture = Fixture::new(); + fs::write(fixture.service.join(predecessor), b"historical lock") + .expect("predecessor lock"); + git(&fixture.service, &["add", "."]); + git( + &fixture.service, + &["commit", "--quiet", "-m", "predecessor lock"], + ); + assert_eq!( + run_with( + CommandMode::Write, + &fixture.service, + &fixture.archive, + &fixture.reachable() + ), + Err(CommandError::InvalidSourceLock) + ); + assert!(!fixture.service.join(LOCK_FILENAME).exists()); + } } #[test] @@ -1636,6 +1666,9 @@ radroots_service_host = {{ git = "{LIB_REPOSITORY}", rev = "{revision}", version let catalog = br#"schema = "radroots.workspace.catalog.v2" architecture = "radroots.crates.release.v2" version = "0.1.0-alpha" +rust_version = "1.99.0" +edition = "2024" +resolver = "3" package_count = 2 [[package]] @@ -1981,6 +2014,7 @@ package = [] CommandError::InvalidServiceMetadata, CommandError::InvalidCargoManifest, CommandError::InvalidCargoLock, + CommandError::InvalidArtifactContract, CommandError::InvalidNixMaterial, CommandError::InvalidFlakeLock, CommandError::InvalidToolchain, @@ -1993,9 +2027,70 @@ package = [] for error in errors { assert!(error.source().is_none()); let rendered = format!("{error:?} {error}"); - assert!(!rendered.contains("fixture_service")); + assert!(!rendered.contains("myc")); assert!(!rendered.contains("radrootslabs")); assert!(!rendered.contains("/tmp")); } } + + #[test] + fn archive_rejects_obsolete_committed_lib_compiler_inputs() { + for name in [CARGO_MANIFEST, RUST_TOOLCHAIN, CATALOG_RELATIVE] { + let fixture = Fixture::new(); + let lib = fixture.root.path().join("lib"); + let path = lib.join(name); + let original = fs::read_to_string(&path).expect("Lib input"); + fs::write(&path, original.replace("1.99.0", "1.97.1")).expect("obsolete Lib input"); + git(&lib, &["add", "."]); + git( + &lib, + &["commit", "--quiet", "-m", "obsolete compiler input"], + ); + let revision = + String::from_utf8(git_stdout(&lib, ["rev-parse", "HEAD"], 128).expect("revision")) + .expect("UTF-8") + .trim() + .to_owned(); + let bundle = fixture.root.path().join("obsolete.bundle"); + let status = Command::new("git") + .args(["bundle", "create"]) + .arg(&bundle) + .arg("refs/heads/archive") + .current_dir(&lib) + .status() + .expect("bundle"); + assert!(status.success()); + assert!( + matches!( + validate_archive(&bundle, &revision), + Err(CommandError::InvalidSourceArchive) + ), + "{name}" + ); + } + } + + #[test] + fn current_artifact_contract_must_exist_in_the_committed_service_revision() { + let fixture = Fixture::new(); + let artifact = fixture + .service + .join("contracts/release/myc-artifact-contract.v4.json"); + fs::remove_file(&artifact).expect("remove contract"); + git(&fixture.service, &["add", "-A"]); + git( + &fixture.service, + &["commit", "--quiet", "-m", "missing artifact contract"], + ); + assert_eq!( + run_with( + CommandMode::Write, + &fixture.service, + &fixture.archive, + &fixture.reachable() + ), + Err(CommandError::InvalidArtifactContract) + ); + assert!(!fixture.service.join(LOCK_FILENAME).exists()); + } } diff --git a/tools/xtask/src/service_source_lock_v3.rs b/tools/xtask/src/service_source_lock_v3.rs @@ -497,6 +497,28 @@ provider = 5 assert_eq!(lock.canonical_bytes(), MYC_LOCK); assert_eq!(lock.service(), "myc"); assert_eq!(lock.revision(), "1".repeat(40)); + assert_eq!(LOCK_FILENAME, "radroots.service.source-lock.v3.toml"); + assert_eq!( + PREDECESSOR_LOCK_FILENAME, + "radroots.service.source-lock.v2.toml" + ); + assert_eq!(lock.workspace_catalog_sha256(), "2".repeat(64)); + assert_eq!(lock.source_archive_sha256(), "3".repeat(64)); + assert_eq!(lock.cargo_lock_sha256(), "4".repeat(64)); + assert_eq!(lock.flake_lock_sha256(), "5".repeat(64)); + assert_eq!(lock.artifact_contract_sha256(), "6".repeat(64)); + let reconstructed = ServiceSourceLockV3::fixture(FixtureParts { + service: lock.service(), + revision: lock.revision(), + workspace_catalog_sha256: lock.workspace_catalog_sha256(), + source_archive_sha256: lock.source_archive_sha256(), + cargo_lock_sha256: lock.cargo_lock_sha256(), + flake_lock_sha256: lock.flake_lock_sha256(), + artifact_contract_sha256: lock.artifact_contract_sha256(), + contract_versions: lock.contract_versions(), + }) + .expect("historical constructor"); + assert_eq!(reconstructed.canonical_bytes(), MYC_LOCK); assert_eq!( lock.artifact_contract_path(), "contracts/release/myc-artifact-contract.v3.json" diff --git a/tools/xtask/src/service_source_lock_v4.rs b/tools/xtask/src/service_source_lock_v4.rs @@ -0,0 +1,701 @@ +use std::fmt; + +use serde::Deserialize; + +use crate::service_source_lock::{ContractVersions, LIB_REPOSITORY}; +use crate::service_source_lock_parts::{ + RawArtifactContract, RawSourceArchiveContract, RawSqliteContract, +}; + +pub(crate) const LOCK_FILENAME: &str = "radroots.service.source_lock.v4.toml"; +pub(crate) const PREDECESSOR_LOCK_FILENAME: &str = "radroots.service.source-lock.v3.toml"; +pub(crate) const PREDECESSOR_LOCK_FILENAMES: [&str; 3] = [ + "radroots.service.source-lock.v1.toml", + "radroots.service.source-lock.v2.toml", + PREDECESSOR_LOCK_FILENAME, +]; +pub(crate) const MAX_LOCK_BYTES: usize = 16 * 1024; +const ARCHITECTURE: &str = "radroots.crates.release.v2"; +const LIB_VERSION: &str = "0.1.0-alpha"; +pub(crate) const RUST_VERSION: &str = "1.99.0"; +const HOST_FEATURE_PROFILE: &str = "service-host"; + +#[derive(Clone, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +struct RawPublicInputLock { + path: String, + sha256: String, + binding: String, + mutable_reference: String, + lib_input: String, +} + +#[derive(Clone, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +struct RawParentResult { + embedded_in_public_input_lock: bool, + embedded_in_source_lock: bool, + storage: String, +} + +#[derive(Clone, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +struct RawBoundNix { + material: String, + lib_revision: String, + supported_systems: Vec<String>, + public_input_lock: RawPublicInputLock, + parent_result: RawParentResult, +} + +#[derive(Clone, Debug, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +struct RawServiceSourceLockV4 { + schema: String, + contract_version: u32, + service: String, + repository: String, + revision: String, + architecture: String, + workspace_catalog_sha256: String, + version: String, + source_archive_sha256: String, + source_archive_contract: RawSourceArchiveContract, + cargo_lock_sha256: String, + rust_version: String, + host_feature_profile: String, + nix: RawBoundNix, + artifact_contract: RawArtifactContract, + sqlite: RawSqliteContract, + contract_versions: ContractVersions, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(crate) enum ServiceSourceLockV4Error { + TooLarge, + Malformed, + Noncanonical, + Invalid, +} + +impl fmt::Display for ServiceSourceLockV4Error { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(match self { + Self::TooLarge => "service source lock v4 exceeds its byte limit", + Self::Malformed => "service source lock v4 is malformed", + Self::Noncanonical => "service source lock v4 is not canonical", + Self::Invalid => "service source lock v4 is invalid", + }) + } +} + +impl std::error::Error for ServiceSourceLockV4Error {} + +#[derive(Clone, Eq, PartialEq)] +pub(crate) struct ServiceSourceLockV4 { + raw: RawServiceSourceLockV4, + canonical: Box<[u8]>, +} + +pub(crate) struct ServiceSourceLockV4Parts<'a> { + pub(crate) service: &'a str, + pub(crate) revision: &'a str, + pub(crate) workspace_catalog_sha256: &'a str, + pub(crate) source_archive_sha256: &'a str, + pub(crate) cargo_lock_sha256: &'a str, + pub(crate) flake_lock_sha256: &'a str, + pub(crate) artifact_contract_sha256: &'a str, + pub(crate) contract_versions: ContractVersions, +} + +impl fmt::Debug for ServiceSourceLockV4 { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter + .debug_struct("ServiceSourceLockV4") + .finish_non_exhaustive() + } +} + +impl ServiceSourceLockV4 { + pub(crate) fn from_canonical_bytes(bytes: &[u8]) -> Result<Self, ServiceSourceLockV4Error> { + if bytes.len() > MAX_LOCK_BYTES { + return Err(ServiceSourceLockV4Error::TooLarge); + } + let text = std::str::from_utf8(bytes).map_err(|_| ServiceSourceLockV4Error::Malformed)?; + let raw = toml::from_str::<RawServiceSourceLockV4>(text) + .map_err(|_| ServiceSourceLockV4Error::Malformed)?; + validate(&raw)?; + let canonical = canonical_bytes(&raw); + if bytes != canonical.as_slice() { + return Err(ServiceSourceLockV4Error::Noncanonical); + } + Ok(Self { + raw, + canonical: canonical.into_boxed_slice(), + }) + } + + pub(crate) fn canonical_bytes(&self) -> &[u8] { + &self.canonical + } + + pub(crate) fn service(&self) -> &str { + &self.raw.service + } + + pub(crate) fn revision(&self) -> &str { + &self.raw.revision + } + + pub(crate) fn workspace_catalog_sha256(&self) -> &str { + &self.raw.workspace_catalog_sha256 + } + + pub(crate) fn source_archive_sha256(&self) -> &str { + &self.raw.source_archive_sha256 + } + + pub(crate) fn cargo_lock_sha256(&self) -> &str { + &self.raw.cargo_lock_sha256 + } + + pub(crate) fn flake_lock_sha256(&self) -> &str { + &self.raw.nix.public_input_lock.sha256 + } + + pub(crate) fn artifact_contract_path(&self) -> &str { + &self.raw.artifact_contract.path + } + + pub(crate) fn artifact_contract_sha256(&self) -> &str { + &self.raw.artifact_contract.sha256 + } + + pub(crate) const fn contract_versions(&self) -> ContractVersions { + self.raw.contract_versions + } + + pub(crate) fn new( + parts: ServiceSourceLockV4Parts<'_>, + ) -> Result<Self, ServiceSourceLockV4Error> { + let ServiceSourceLockV4Parts { + service, + revision, + workspace_catalog_sha256, + source_archive_sha256, + cargo_lock_sha256, + flake_lock_sha256, + artifact_contract_sha256, + contract_versions, + } = parts; + let artifact_path = format!("contracts/release/{service}-artifact-contract.v4.json"); + let raw = RawServiceSourceLockV4 { + schema: "radroots.service.source_lock.v4".to_owned(), + contract_version: 4, + service: service.to_owned(), + repository: LIB_REPOSITORY.to_owned(), + revision: revision.to_owned(), + architecture: ARCHITECTURE.to_owned(), + workspace_catalog_sha256: workspace_catalog_sha256.to_owned(), + version: LIB_VERSION.to_owned(), + source_archive_sha256: source_archive_sha256.to_owned(), + source_archive_contract: RawSourceArchiveContract { + binding: "sha256_of_canonical_exact_lib_revision_tree_archive".to_owned(), + format: "ustar".to_owned(), + compression: "none".to_owned(), + compression_timestamp: "not_applicable".to_owned(), + entry_order: "bytewise_git_path".to_owned(), + path_prefix: "none".to_owned(), + file_mode: "git_index_100644_or_100755".to_owned(), + uid: 0, + gid: 0, + uname: String::new(), + gname: String::new(), + mtime: "lib_revision_commit_timestamp".to_owned(), + pax_headers: "forbidden".to_owned(), + directory_entries: "omitted".to_owned(), + symlinks: "forbidden".to_owned(), + hardlinks: "forbidden".to_owned(), + submodules: "forbidden".to_owned(), + trailer: "two_zero_blocks".to_owned(), + }, + cargo_lock_sha256: cargo_lock_sha256.to_owned(), + rust_version: RUST_VERSION.to_owned(), + host_feature_profile: HOST_FEATURE_PROFILE.to_owned(), + nix: RawBoundNix { + material: "bound".to_owned(), + lib_revision: revision.to_owned(), + supported_systems: vec!["aarch64-darwin".to_owned(), "x86_64-linux".to_owned()], + public_input_lock: RawPublicInputLock { + path: "flake.lock".to_owned(), + sha256: flake_lock_sha256.to_owned(), + binding: "exact_regular_file_bytes".to_owned(), + mutable_reference: "forbidden".to_owned(), + lib_input: "lib".to_owned(), + }, + parent_result: RawParentResult { + embedded_in_public_input_lock: false, + embedded_in_source_lock: false, + storage: "separate_source_scoped_execution_evidence".to_owned(), + }, + }, + artifact_contract: RawArtifactContract { + path: artifact_path, + sha256: artifact_contract_sha256.to_owned(), + binding: "exact_regular_file_bytes_in_same_source_revision".to_owned(), + }, + sqlite: RawSqliteContract { + high_level_authority: "sqlx_only".to_owned(), + second_pool_connection_query_transaction_migration_authority: "forbidden" + .to_owned(), + incremental_backup_adapter: "sealed_native_sqlx_owned_locked_handle_only" + .to_owned(), + native_linkage_count: 1, + }, + contract_versions, + }; + validate(&raw)?; + let canonical = canonical_bytes(&raw).into_boxed_slice(); + Ok(Self { raw, canonical }) + } +} + +fn validate(raw: &RawServiceSourceLockV4) -> Result<(), ServiceSourceLockV4Error> { + let expected_artifact = match raw.service.as_str() { + "myc" => "contracts/release/myc-artifact-contract.v4.json", + "rhi" => "contracts/release/rhi-artifact-contract.v4.json", + _ => return Err(ServiceSourceLockV4Error::Invalid), + }; + let archive = &raw.source_archive_contract; + if raw.schema != "radroots.service.source_lock.v4" + || raw.contract_version != 4 + || raw.repository != LIB_REPOSITORY + || !valid_lower_hex(&raw.revision, 40) + || raw.architecture != ARCHITECTURE + || !valid_lower_hex(&raw.workspace_catalog_sha256, 64) + || raw.version != LIB_VERSION + || !valid_lower_hex(&raw.source_archive_sha256, 64) + || !valid_lower_hex(&raw.cargo_lock_sha256, 64) + || raw.rust_version != RUST_VERSION + || raw.host_feature_profile != HOST_FEATURE_PROFILE + || archive.binding != "sha256_of_canonical_exact_lib_revision_tree_archive" + || archive.format != "ustar" + || archive.compression != "none" + || archive.compression_timestamp != "not_applicable" + || archive.entry_order != "bytewise_git_path" + || archive.path_prefix != "none" + || archive.file_mode != "git_index_100644_or_100755" + || archive.uid != 0 + || archive.gid != 0 + || !archive.uname.is_empty() + || !archive.gname.is_empty() + || archive.mtime != "lib_revision_commit_timestamp" + || archive.pax_headers != "forbidden" + || archive.directory_entries != "omitted" + || archive.symlinks != "forbidden" + || archive.hardlinks != "forbidden" + || archive.submodules != "forbidden" + || archive.trailer != "two_zero_blocks" + || raw.nix.material != "bound" + || raw.nix.lib_revision != raw.revision + || raw.nix.supported_systems != ["aarch64-darwin", "x86_64-linux"] + || raw.nix.public_input_lock.path != "flake.lock" + || !valid_lower_hex(&raw.nix.public_input_lock.sha256, 64) + || raw.nix.public_input_lock.binding != "exact_regular_file_bytes" + || raw.nix.public_input_lock.mutable_reference != "forbidden" + || raw.nix.public_input_lock.lib_input != "lib" + || raw.nix.parent_result.embedded_in_public_input_lock + || raw.nix.parent_result.embedded_in_source_lock + || raw.nix.parent_result.storage != "separate_source_scoped_execution_evidence" + || raw.artifact_contract.path != expected_artifact + || !valid_lower_hex(&raw.artifact_contract.sha256, 64) + || raw.artifact_contract.binding != "exact_regular_file_bytes_in_same_source_revision" + || raw.sqlite.high_level_authority != "sqlx_only" + || raw + .sqlite + .second_pool_connection_query_transaction_migration_authority + != "forbidden" + || raw.sqlite.incremental_backup_adapter != "sealed_native_sqlx_owned_locked_handle_only" + || raw.sqlite.native_linkage_count != 1 + || !contract_versions_valid(raw.contract_versions) + { + Err(ServiceSourceLockV4Error::Invalid) + } else { + Ok(()) + } +} + +fn contract_versions_valid(versions: ContractVersions) -> bool { + versions.config() != 0 + && versions.state() != 0 + && versions.admin() != 0 + && versions.status() != 0 + && versions.provider() != 0 +} + +fn canonical_bytes(raw: &RawServiceSourceLockV4) -> Vec<u8> { + format!( + concat!( + "schema = \"radroots.service.source_lock.v4\"\n", + "contract_version = 4\n", + "service = \"{}\"\n", + "repository = \"https://github.com/radrootslabs/lib\"\n", + "revision = \"{}\"\n", + "architecture = \"radroots.crates.release.v2\"\n", + "workspace_catalog_sha256 = \"{}\"\n", + "version = \"0.1.0-alpha\"\n", + "source_archive_sha256 = \"{}\"\n", + "cargo_lock_sha256 = \"{}\"\n", + "rust_version = \"1.99.0\"\n", + "host_feature_profile = \"service-host\"\n\n", + "[source_archive_contract]\n", + "binding = \"sha256_of_canonical_exact_lib_revision_tree_archive\"\n", + "format = \"ustar\"\n", + "compression = \"none\"\n", + "compression_timestamp = \"not_applicable\"\n", + "entry_order = \"bytewise_git_path\"\n", + "path_prefix = \"none\"\n", + "file_mode = \"git_index_100644_or_100755\"\n", + "uid = 0\n", + "gid = 0\n", + "uname = \"\"\n", + "gname = \"\"\n", + "mtime = \"lib_revision_commit_timestamp\"\n", + "pax_headers = \"forbidden\"\n", + "directory_entries = \"omitted\"\n", + "symlinks = \"forbidden\"\n", + "hardlinks = \"forbidden\"\n", + "submodules = \"forbidden\"\n", + "trailer = \"two_zero_blocks\"\n\n", + "[nix]\n", + "material = \"bound\"\n", + "lib_revision = \"{}\"\n", + "supported_systems = [\"aarch64-darwin\", \"x86_64-linux\"]\n\n", + "[nix.public_input_lock]\n", + "path = \"flake.lock\"\n", + "sha256 = \"{}\"\n", + "binding = \"exact_regular_file_bytes\"\n", + "mutable_reference = \"forbidden\"\n", + "lib_input = \"lib\"\n\n", + "[nix.parent_result]\n", + "embedded_in_public_input_lock = false\n", + "embedded_in_source_lock = false\n", + "storage = \"separate_source_scoped_execution_evidence\"\n\n", + "[artifact_contract]\n", + "path = \"{}\"\n", + "sha256 = \"{}\"\n", + "binding = \"exact_regular_file_bytes_in_same_source_revision\"\n\n", + "[sqlite]\n", + "high_level_authority = \"sqlx_only\"\n", + "second_pool_connection_query_transaction_migration_authority = \"forbidden\"\n", + "incremental_backup_adapter = \"sealed_native_sqlx_owned_locked_handle_only\"\n", + "native_linkage_count = 1\n\n", + "[contract_versions]\n", + "config = {}\n", + "state = {}\n", + "admin = {}\n", + "status = {}\n", + "provider = {}\n" + ), + raw.service, + raw.revision, + raw.workspace_catalog_sha256, + raw.source_archive_sha256, + raw.cargo_lock_sha256, + raw.nix.lib_revision, + raw.nix.public_input_lock.sha256, + raw.artifact_contract.path, + raw.artifact_contract.sha256, + raw.contract_versions.config(), + raw.contract_versions.state(), + raw.contract_versions.admin(), + raw.contract_versions.status(), + raw.contract_versions.provider(), + ) + .into_bytes() +} + +pub(crate) fn validate_toolchain_bytes(bytes: &[u8]) -> Result<(), ServiceSourceLockV4Error> { + let text = std::str::from_utf8(bytes).map_err(|_| ServiceSourceLockV4Error::Malformed)?; + let value: toml::Value = + toml::from_str(text).map_err(|_| ServiceSourceLockV4Error::Malformed)?; + if value + .get("toolchain") + .and_then(|v| v.get("channel")) + .and_then(toml::Value::as_str) + == Some(RUST_VERSION) + { + Ok(()) + } else { + Err(ServiceSourceLockV4Error::Invalid) + } +} + +pub(crate) fn validate_lib_compiler_inputs( + manifest: &[u8], + toolchain: &[u8], + catalog: &[u8], +) -> Result<(), ServiceSourceLockV4Error> { + validate_toolchain_bytes(toolchain)?; + let parse = |bytes: &[u8]| -> Result<toml::Value, ServiceSourceLockV4Error> { + let text = std::str::from_utf8(bytes).map_err(|_| ServiceSourceLockV4Error::Malformed)?; + toml::from_str(text).map_err(|_| ServiceSourceLockV4Error::Malformed) + }; + let manifest = parse(manifest)?; + let catalog = parse(catalog)?; + let package = manifest + .get("workspace") + .and_then(|v| v.get("package")) + .ok_or(ServiceSourceLockV4Error::Invalid)?; + if package.get("rust-version").and_then(toml::Value::as_str) != Some(RUST_VERSION) + || package.get("edition").and_then(toml::Value::as_str) != Some("2024") + || package.get("version").and_then(toml::Value::as_str) != Some(LIB_VERSION) + || catalog.get("schema").and_then(toml::Value::as_str) + != Some("radroots.workspace.catalog.v2") + || catalog.get("architecture").and_then(toml::Value::as_str) != Some(ARCHITECTURE) + || catalog.get("version").and_then(toml::Value::as_str) != Some(LIB_VERSION) + || catalog.get("rust_version").and_then(toml::Value::as_str) != Some(RUST_VERSION) + || catalog.get("edition").and_then(toml::Value::as_str) != Some("2024") + || catalog.get("resolver").and_then(toml::Value::as_str) != Some("3") + { + Err(ServiceSourceLockV4Error::Invalid) + } else { + Ok(()) + } +} + +fn valid_lower_hex(value: &str, length: usize) -> bool { + value.len() == length + && value + .bytes() + .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)) +} + +#[cfg(test)] +mod tests { + use super::*; + + const MYC_LOCK: &[u8] = br#"schema = "radroots.service.source_lock.v4" +contract_version = 4 +service = "myc" +repository = "https://github.com/radrootslabs/lib" +revision = "1111111111111111111111111111111111111111" +architecture = "radroots.crates.release.v2" +workspace_catalog_sha256 = "2222222222222222222222222222222222222222222222222222222222222222" +version = "0.1.0-alpha" +source_archive_sha256 = "3333333333333333333333333333333333333333333333333333333333333333" +cargo_lock_sha256 = "4444444444444444444444444444444444444444444444444444444444444444" +rust_version = "1.99.0" +host_feature_profile = "service-host" + +[source_archive_contract] +binding = "sha256_of_canonical_exact_lib_revision_tree_archive" +format = "ustar" +compression = "none" +compression_timestamp = "not_applicable" +entry_order = "bytewise_git_path" +path_prefix = "none" +file_mode = "git_index_100644_or_100755" +uid = 0 +gid = 0 +uname = "" +gname = "" +mtime = "lib_revision_commit_timestamp" +pax_headers = "forbidden" +directory_entries = "omitted" +symlinks = "forbidden" +hardlinks = "forbidden" +submodules = "forbidden" +trailer = "two_zero_blocks" + +[nix] +material = "bound" +lib_revision = "1111111111111111111111111111111111111111" +supported_systems = ["aarch64-darwin", "x86_64-linux"] + +[nix.public_input_lock] +path = "flake.lock" +sha256 = "5555555555555555555555555555555555555555555555555555555555555555" +binding = "exact_regular_file_bytes" +mutable_reference = "forbidden" +lib_input = "lib" + +[nix.parent_result] +embedded_in_public_input_lock = false +embedded_in_source_lock = false +storage = "separate_source_scoped_execution_evidence" + +[artifact_contract] +path = "contracts/release/myc-artifact-contract.v4.json" +sha256 = "6666666666666666666666666666666666666666666666666666666666666666" +binding = "exact_regular_file_bytes_in_same_source_revision" + +[sqlite] +high_level_authority = "sqlx_only" +second_pool_connection_query_transaction_migration_authority = "forbidden" +incremental_backup_adapter = "sealed_native_sqlx_owned_locked_handle_only" +native_linkage_count = 1 + +[contract_versions] +config = 1 +state = 2 +admin = 3 +status = 4 +provider = 5 +"#; + + #[test] + fn canonical_v4_lock_round_trips_and_exposes_exact_bindings() { + let lock = ServiceSourceLockV4::from_canonical_bytes(MYC_LOCK).expect("v4 lock"); + assert_eq!(lock.canonical_bytes(), MYC_LOCK); + assert_eq!(lock.service(), "myc"); + assert_eq!(lock.revision(), "1".repeat(40)); + assert_eq!( + lock.artifact_contract_path(), + "contracts/release/myc-artifact-contract.v4.json" + ); + assert_eq!( + lock.contract_versions(), + ContractVersions::new(1, 2, 3, 4, 5) + ); + } + + #[test] + fn v4_lock_rejects_noncanonical_and_independent_semantic_drift() { + let noncanonical = String::from_utf8(MYC_LOCK.to_vec()) + .expect("UTF-8") + .replace("schema =", "schema ="); + assert!(ServiceSourceLockV4::from_canonical_bytes(noncanonical.as_bytes()).is_err()); + for (from, to) in [ + ("service = \"myc\"", "service = \"other\""), + ("material = \"bound\"", "material = \"deferred\""), + ("native_linkage_count = 1", "native_linkage_count = 2"), + ("trailer = \"two_zero_blocks\"", "trailer = \"other\""), + ] { + let drifted = String::from_utf8(MYC_LOCK.to_vec()) + .expect("UTF-8") + .replace(from, to); + assert!( + ServiceSourceLockV4::from_canonical_bytes(drifted.as_bytes()).is_err(), + "{from}" + ); + } + } + + #[test] + fn current_constructor_and_json_schema_agree_for_both_services() { + let original = ServiceSourceLockV4::from_canonical_bytes(MYC_LOCK).expect("input"); + let schema: serde_json::Value = serde_json::from_str(include_str!( + "../../../contracts/architecture/decisions/services_hardening_source_lock.v4.json" + )) + .expect("schema"); + let validator = jsonschema::validator_for(&schema).expect("schema validator"); + for service in ["myc", "rhi"] { + let lock = ServiceSourceLockV4::new(ServiceSourceLockV4Parts { + service, + revision: original.revision(), + workspace_catalog_sha256: original.workspace_catalog_sha256(), + source_archive_sha256: original.source_archive_sha256(), + cargo_lock_sha256: original.cargo_lock_sha256(), + flake_lock_sha256: original.flake_lock_sha256(), + artifact_contract_sha256: original.artifact_contract_sha256(), + contract_versions: original.contract_versions(), + }) + .expect("current constructor"); + assert_eq!( + ServiceSourceLockV4::from_canonical_bytes(lock.canonical_bytes()) + .expect("canonical round trip"), + lock + ); + let document: toml::Value = + toml::from_str(std::str::from_utf8(lock.canonical_bytes()).expect("UTF-8")) + .expect("TOML"); + assert!(validator.is_valid(&serde_json::to_value(document).expect("JSON model"))); + } + } + + #[test] + fn current_parser_rejects_oversize_malformed_unknown_and_predecessor_bytes() { + assert_eq!( + ServiceSourceLockV4::from_canonical_bytes(&vec![b' '; MAX_LOCK_BYTES + 1]), + Err(ServiceSourceLockV4Error::TooLarge) + ); + assert_eq!( + ServiceSourceLockV4::from_canonical_bytes(b"\xff"), + Err(ServiceSourceLockV4Error::Malformed) + ); + let valid = std::str::from_utf8(MYC_LOCK).expect("UTF-8"); + for invalid in [ + valid.replace("schema =", "extra = true\nschema ="), + valid.replace( + "contract_version = 4", + "contract_version = 4\ncontract_version = 4", + ), + valid.replace("rust_version = \"1.99.0\"", "rust_version = \"1.97.1\""), + valid.replace("material = \"bound\"", "material = \"qualified\""), + valid.replace( + "radroots.service.source_lock.v4", + "radroots.service.source-lock.v3", + ), + valid.replace( + "lib_revision = \"1111111111111111111111111111111111111111\"", + "lib_revision = \"9999999999999999999999999999999999999999\"", + ), + valid.replace("myc-artifact-contract.v4", "rhi-artifact-contract.v4"), + ] { + assert!(ServiceSourceLockV4::from_canonical_bytes(invalid.as_bytes()).is_err()); + } + let mut padded = MYC_LOCK.to_vec(); + padded.resize(MAX_LOCK_BYTES, b' '); + assert_eq!( + ServiceSourceLockV4::from_canonical_bytes(&padded), + Err(ServiceSourceLockV4Error::Noncanonical) + ); + } + + #[test] + fn compiler_input_policy_rejects_independent_manifest_selector_and_catalog_drift() { + let manifest = "[workspace.package]\nversion = \"0.1.0-alpha\"\nrust-version = \"1.99.0\"\nedition = \"2024\"\n"; + let toolchain = "[toolchain]\nchannel = \"1.99.0\"\n"; + let catalog = "schema = \"radroots.workspace.catalog.v2\"\narchitecture = \"radroots.crates.release.v2\"\nversion = \"0.1.0-alpha\"\nrust_version = \"1.99.0\"\nedition = \"2024\"\nresolver = \"3\"\n"; + validate_lib_compiler_inputs( + manifest.as_bytes(), + toolchain.as_bytes(), + catalog.as_bytes(), + ) + .expect("current compiler inputs"); + for (input, original) in [(0, manifest), (1, toolchain), (2, catalog)] { + let old = original.replace("1.99.0", "1.97.1"); + let mut inputs = [ + manifest.as_bytes(), + toolchain.as_bytes(), + catalog.as_bytes(), + ]; + inputs[input] = old.as_bytes(); + assert!(validate_lib_compiler_inputs(inputs[0], inputs[1], inputs[2]).is_err()); + } + for compiler in ["stable", "nightly", "1.99.1", "1.100.0"] { + assert!( + validate_toolchain_bytes(toolchain.replace("1.99.0", compiler).as_bytes()).is_err() + ); + } + assert!( + validate_lib_compiler_inputs( + manifest.replace("2024", "2021").as_bytes(), + toolchain.as_bytes(), + catalog.as_bytes() + ) + .is_err() + ); + assert!( + validate_lib_compiler_inputs( + manifest.as_bytes(), + toolchain.as_bytes(), + catalog + .replace("resolver = \"3\"", "resolver = \"2\"") + .as_bytes() + ) + .is_err() + ); + } +}