service_source_lock_v3.rs (20939B)
1 use std::fmt; 2 3 use serde::Deserialize; 4 5 use crate::service_source_lock::{ContractVersions, LIB_REPOSITORY}; 6 use crate::service_source_lock_parts::{ 7 RawArtifactContract, RawSourceArchiveContract, RawSqliteContract, 8 }; 9 10 pub(crate) const LOCK_FILENAME: &str = "radroots.service.source-lock.v3.toml"; 11 pub(crate) const PREDECESSOR_LOCK_FILENAME: &str = "radroots.service.source-lock.v2.toml"; 12 const MAX_LOCK_BYTES: usize = 16 * 1024; 13 const ARCHITECTURE: &str = "radroots.crates.release.v2"; 14 const LIB_VERSION: &str = "0.1.0-alpha"; 15 const RUST_VERSION: &str = "1.97.1"; 16 const HOST_FEATURE_PROFILE: &str = "service-host"; 17 18 #[derive(Clone, Debug, Deserialize, Eq, PartialEq)] 19 #[serde(deny_unknown_fields)] 20 struct RawPublicInputLock { 21 path: String, 22 sha256: String, 23 binding: String, 24 mutable_reference: String, 25 lib_input: String, 26 } 27 28 #[derive(Clone, Debug, Deserialize, Eq, PartialEq)] 29 #[serde(deny_unknown_fields)] 30 struct RawParentResult { 31 embedded_in_public_input_lock: bool, 32 embedded_in_source_lock: bool, 33 storage: String, 34 } 35 36 #[derive(Clone, Debug, Deserialize, Eq, PartialEq)] 37 #[serde(deny_unknown_fields)] 38 struct RawQualifiedNix { 39 material: String, 40 lib_revision: String, 41 supported_systems: Vec<String>, 42 public_input_lock: RawPublicInputLock, 43 parent_result: RawParentResult, 44 } 45 46 #[derive(Clone, Debug, Deserialize, Eq, PartialEq)] 47 #[serde(deny_unknown_fields)] 48 struct RawServiceSourceLockV3 { 49 schema: String, 50 contract_version: u32, 51 service: String, 52 repository: String, 53 revision: String, 54 architecture: String, 55 workspace_catalog_sha256: String, 56 version: String, 57 source_archive_sha256: String, 58 source_archive_contract: RawSourceArchiveContract, 59 cargo_lock_sha256: String, 60 rust_version: String, 61 host_feature_profile: String, 62 nix: RawQualifiedNix, 63 artifact_contract: RawArtifactContract, 64 sqlite: RawSqliteContract, 65 contract_versions: ContractVersions, 66 } 67 68 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 69 pub(crate) enum ServiceSourceLockV3Error { 70 TooLarge, 71 Malformed, 72 Noncanonical, 73 Invalid, 74 } 75 76 impl fmt::Display for ServiceSourceLockV3Error { 77 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { 78 formatter.write_str(match self { 79 Self::TooLarge => "service source lock v3 exceeds its byte limit", 80 Self::Malformed => "service source lock v3 is malformed", 81 Self::Noncanonical => "service source lock v3 is not canonical", 82 Self::Invalid => "service source lock v3 is invalid", 83 }) 84 } 85 } 86 87 impl std::error::Error for ServiceSourceLockV3Error {} 88 89 #[derive(Clone, Eq, PartialEq)] 90 pub(crate) struct ServiceSourceLockV3 { 91 raw: RawServiceSourceLockV3, 92 canonical: Box<[u8]>, 93 } 94 95 #[cfg(test)] 96 pub(crate) struct FixtureParts<'a> { 97 pub(crate) service: &'a str, 98 pub(crate) revision: &'a str, 99 pub(crate) workspace_catalog_sha256: &'a str, 100 pub(crate) source_archive_sha256: &'a str, 101 pub(crate) cargo_lock_sha256: &'a str, 102 pub(crate) flake_lock_sha256: &'a str, 103 pub(crate) artifact_contract_sha256: &'a str, 104 pub(crate) contract_versions: ContractVersions, 105 } 106 107 impl fmt::Debug for ServiceSourceLockV3 { 108 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { 109 formatter 110 .debug_struct("ServiceSourceLockV3") 111 .finish_non_exhaustive() 112 } 113 } 114 115 impl ServiceSourceLockV3 { 116 pub(crate) fn from_canonical_bytes(bytes: &[u8]) -> Result<Self, ServiceSourceLockV3Error> { 117 if bytes.len() > MAX_LOCK_BYTES { 118 return Err(ServiceSourceLockV3Error::TooLarge); 119 } 120 let text = std::str::from_utf8(bytes).map_err(|_| ServiceSourceLockV3Error::Malformed)?; 121 let raw = toml::from_str::<RawServiceSourceLockV3>(text) 122 .map_err(|_| ServiceSourceLockV3Error::Malformed)?; 123 validate(&raw)?; 124 let canonical = canonical_bytes(&raw); 125 if bytes != canonical.as_slice() { 126 return Err(ServiceSourceLockV3Error::Noncanonical); 127 } 128 Ok(Self { 129 raw, 130 canonical: canonical.into_boxed_slice(), 131 }) 132 } 133 134 #[cfg(test)] 135 pub(crate) fn canonical_bytes(&self) -> &[u8] { 136 &self.canonical 137 } 138 139 pub(crate) fn service(&self) -> &str { 140 &self.raw.service 141 } 142 143 pub(crate) fn revision(&self) -> &str { 144 &self.raw.revision 145 } 146 147 pub(crate) fn workspace_catalog_sha256(&self) -> &str { 148 &self.raw.workspace_catalog_sha256 149 } 150 151 pub(crate) fn source_archive_sha256(&self) -> &str { 152 &self.raw.source_archive_sha256 153 } 154 155 pub(crate) fn cargo_lock_sha256(&self) -> &str { 156 &self.raw.cargo_lock_sha256 157 } 158 159 pub(crate) fn flake_lock_sha256(&self) -> &str { 160 &self.raw.nix.public_input_lock.sha256 161 } 162 163 pub(crate) fn artifact_contract_path(&self) -> &str { 164 &self.raw.artifact_contract.path 165 } 166 167 pub(crate) fn artifact_contract_sha256(&self) -> &str { 168 &self.raw.artifact_contract.sha256 169 } 170 171 pub(crate) const fn contract_versions(&self) -> ContractVersions { 172 self.raw.contract_versions 173 } 174 175 #[cfg(test)] 176 pub(crate) fn fixture(parts: FixtureParts<'_>) -> Result<Self, ServiceSourceLockV3Error> { 177 let FixtureParts { 178 service, 179 revision, 180 workspace_catalog_sha256, 181 source_archive_sha256, 182 cargo_lock_sha256, 183 flake_lock_sha256, 184 artifact_contract_sha256, 185 contract_versions, 186 } = parts; 187 let artifact_path = format!("contracts/release/{service}-artifact-contract.v3.json"); 188 let raw = RawServiceSourceLockV3 { 189 schema: "radroots.service.source-lock.v3".to_owned(), 190 contract_version: 3, 191 service: service.to_owned(), 192 repository: LIB_REPOSITORY.to_owned(), 193 revision: revision.to_owned(), 194 architecture: ARCHITECTURE.to_owned(), 195 workspace_catalog_sha256: workspace_catalog_sha256.to_owned(), 196 version: LIB_VERSION.to_owned(), 197 source_archive_sha256: source_archive_sha256.to_owned(), 198 source_archive_contract: RawSourceArchiveContract { 199 binding: "sha256_of_canonical_exact_lib_revision_tree_archive".to_owned(), 200 format: "ustar".to_owned(), 201 compression: "none".to_owned(), 202 compression_timestamp: "not_applicable".to_owned(), 203 entry_order: "bytewise_git_path".to_owned(), 204 path_prefix: "none".to_owned(), 205 file_mode: "git_index_100644_or_100755".to_owned(), 206 uid: 0, 207 gid: 0, 208 uname: String::new(), 209 gname: String::new(), 210 mtime: "lib_revision_commit_timestamp".to_owned(), 211 pax_headers: "forbidden".to_owned(), 212 directory_entries: "omitted".to_owned(), 213 symlinks: "forbidden".to_owned(), 214 hardlinks: "forbidden".to_owned(), 215 submodules: "forbidden".to_owned(), 216 trailer: "two_zero_blocks".to_owned(), 217 }, 218 cargo_lock_sha256: cargo_lock_sha256.to_owned(), 219 rust_version: RUST_VERSION.to_owned(), 220 host_feature_profile: HOST_FEATURE_PROFILE.to_owned(), 221 nix: RawQualifiedNix { 222 material: "qualified".to_owned(), 223 lib_revision: revision.to_owned(), 224 supported_systems: vec!["aarch64-darwin".to_owned(), "x86_64-linux".to_owned()], 225 public_input_lock: RawPublicInputLock { 226 path: "flake.lock".to_owned(), 227 sha256: flake_lock_sha256.to_owned(), 228 binding: "exact_regular_file_bytes".to_owned(), 229 mutable_reference: "forbidden".to_owned(), 230 lib_input: "lib".to_owned(), 231 }, 232 parent_result: RawParentResult { 233 embedded_in_public_input_lock: false, 234 embedded_in_source_lock: false, 235 storage: "separate_generation_scoped_evidence".to_owned(), 236 }, 237 }, 238 artifact_contract: RawArtifactContract { 239 path: artifact_path, 240 sha256: artifact_contract_sha256.to_owned(), 241 binding: "exact_regular_file_bytes_in_same_source_revision".to_owned(), 242 }, 243 sqlite: RawSqliteContract { 244 high_level_authority: "sqlx_only".to_owned(), 245 second_pool_connection_query_transaction_migration_authority: "forbidden" 246 .to_owned(), 247 incremental_backup_adapter: "sealed_native_sqlx_owned_locked_handle_only" 248 .to_owned(), 249 native_linkage_count: 1, 250 }, 251 contract_versions, 252 }; 253 validate(&raw)?; 254 let canonical = canonical_bytes(&raw).into_boxed_slice(); 255 Ok(Self { raw, canonical }) 256 } 257 } 258 259 fn validate(raw: &RawServiceSourceLockV3) -> Result<(), ServiceSourceLockV3Error> { 260 let expected_artifact = match raw.service.as_str() { 261 "myc" => "contracts/release/myc-artifact-contract.v3.json", 262 "rhi" => "contracts/release/rhi-artifact-contract.v3.json", 263 _ => return Err(ServiceSourceLockV3Error::Invalid), 264 }; 265 let archive = &raw.source_archive_contract; 266 if raw.schema != "radroots.service.source-lock.v3" 267 || raw.contract_version != 3 268 || raw.repository != LIB_REPOSITORY 269 || !valid_lower_hex(&raw.revision, 40) 270 || raw.architecture != ARCHITECTURE 271 || !valid_lower_hex(&raw.workspace_catalog_sha256, 64) 272 || raw.version != LIB_VERSION 273 || !valid_lower_hex(&raw.source_archive_sha256, 64) 274 || !valid_lower_hex(&raw.cargo_lock_sha256, 64) 275 || raw.rust_version != RUST_VERSION 276 || raw.host_feature_profile != HOST_FEATURE_PROFILE 277 || archive.binding != "sha256_of_canonical_exact_lib_revision_tree_archive" 278 || archive.format != "ustar" 279 || archive.compression != "none" 280 || archive.compression_timestamp != "not_applicable" 281 || archive.entry_order != "bytewise_git_path" 282 || archive.path_prefix != "none" 283 || archive.file_mode != "git_index_100644_or_100755" 284 || archive.uid != 0 285 || archive.gid != 0 286 || !archive.uname.is_empty() 287 || !archive.gname.is_empty() 288 || archive.mtime != "lib_revision_commit_timestamp" 289 || archive.pax_headers != "forbidden" 290 || archive.directory_entries != "omitted" 291 || archive.symlinks != "forbidden" 292 || archive.hardlinks != "forbidden" 293 || archive.submodules != "forbidden" 294 || archive.trailer != "two_zero_blocks" 295 || raw.nix.material != "qualified" 296 || raw.nix.lib_revision != raw.revision 297 || raw.nix.supported_systems != ["aarch64-darwin", "x86_64-linux"] 298 || raw.nix.public_input_lock.path != "flake.lock" 299 || !valid_lower_hex(&raw.nix.public_input_lock.sha256, 64) 300 || raw.nix.public_input_lock.binding != "exact_regular_file_bytes" 301 || raw.nix.public_input_lock.mutable_reference != "forbidden" 302 || raw.nix.public_input_lock.lib_input != "lib" 303 || raw.nix.parent_result.embedded_in_public_input_lock 304 || raw.nix.parent_result.embedded_in_source_lock 305 || raw.nix.parent_result.storage != "separate_generation_scoped_evidence" 306 || raw.artifact_contract.path != expected_artifact 307 || !valid_lower_hex(&raw.artifact_contract.sha256, 64) 308 || raw.artifact_contract.binding != "exact_regular_file_bytes_in_same_source_revision" 309 || raw.sqlite.high_level_authority != "sqlx_only" 310 || raw 311 .sqlite 312 .second_pool_connection_query_transaction_migration_authority 313 != "forbidden" 314 || raw.sqlite.incremental_backup_adapter != "sealed_native_sqlx_owned_locked_handle_only" 315 || raw.sqlite.native_linkage_count != 1 316 || !contract_versions_valid(raw.contract_versions) 317 { 318 Err(ServiceSourceLockV3Error::Invalid) 319 } else { 320 Ok(()) 321 } 322 } 323 324 fn contract_versions_valid(versions: ContractVersions) -> bool { 325 versions.config() != 0 326 && versions.state() != 0 327 && versions.admin() != 0 328 && versions.status() != 0 329 && versions.provider() != 0 330 } 331 332 fn canonical_bytes(raw: &RawServiceSourceLockV3) -> Vec<u8> { 333 format!( 334 concat!( 335 "schema = \"radroots.service.source-lock.v3\"\n", 336 "contract_version = 3\n", 337 "service = \"{}\"\n", 338 "repository = \"https://github.com/radrootslabs/lib\"\n", 339 "revision = \"{}\"\n", 340 "architecture = \"radroots.crates.release.v2\"\n", 341 "workspace_catalog_sha256 = \"{}\"\n", 342 "version = \"0.1.0-alpha\"\n", 343 "source_archive_sha256 = \"{}\"\n", 344 "cargo_lock_sha256 = \"{}\"\n", 345 "rust_version = \"1.97.1\"\n", 346 "host_feature_profile = \"service-host\"\n\n", 347 "[source_archive_contract]\n", 348 "binding = \"sha256_of_canonical_exact_lib_revision_tree_archive\"\n", 349 "format = \"ustar\"\n", 350 "compression = \"none\"\n", 351 "compression_timestamp = \"not_applicable\"\n", 352 "entry_order = \"bytewise_git_path\"\n", 353 "path_prefix = \"none\"\n", 354 "file_mode = \"git_index_100644_or_100755\"\n", 355 "uid = 0\n", 356 "gid = 0\n", 357 "uname = \"\"\n", 358 "gname = \"\"\n", 359 "mtime = \"lib_revision_commit_timestamp\"\n", 360 "pax_headers = \"forbidden\"\n", 361 "directory_entries = \"omitted\"\n", 362 "symlinks = \"forbidden\"\n", 363 "hardlinks = \"forbidden\"\n", 364 "submodules = \"forbidden\"\n", 365 "trailer = \"two_zero_blocks\"\n\n", 366 "[nix]\n", 367 "material = \"qualified\"\n", 368 "lib_revision = \"{}\"\n", 369 "supported_systems = [\"aarch64-darwin\", \"x86_64-linux\"]\n\n", 370 "[nix.public_input_lock]\n", 371 "path = \"flake.lock\"\n", 372 "sha256 = \"{}\"\n", 373 "binding = \"exact_regular_file_bytes\"\n", 374 "mutable_reference = \"forbidden\"\n", 375 "lib_input = \"lib\"\n\n", 376 "[nix.parent_result]\n", 377 "embedded_in_public_input_lock = false\n", 378 "embedded_in_source_lock = false\n", 379 "storage = \"separate_generation_scoped_evidence\"\n\n", 380 "[artifact_contract]\n", 381 "path = \"{}\"\n", 382 "sha256 = \"{}\"\n", 383 "binding = \"exact_regular_file_bytes_in_same_source_revision\"\n\n", 384 "[sqlite]\n", 385 "high_level_authority = \"sqlx_only\"\n", 386 "second_pool_connection_query_transaction_migration_authority = \"forbidden\"\n", 387 "incremental_backup_adapter = \"sealed_native_sqlx_owned_locked_handle_only\"\n", 388 "native_linkage_count = 1\n\n", 389 "[contract_versions]\n", 390 "config = {}\n", 391 "state = {}\n", 392 "admin = {}\n", 393 "status = {}\n", 394 "provider = {}\n" 395 ), 396 raw.service, 397 raw.revision, 398 raw.workspace_catalog_sha256, 399 raw.source_archive_sha256, 400 raw.cargo_lock_sha256, 401 raw.nix.lib_revision, 402 raw.nix.public_input_lock.sha256, 403 raw.artifact_contract.path, 404 raw.artifact_contract.sha256, 405 raw.contract_versions.config(), 406 raw.contract_versions.state(), 407 raw.contract_versions.admin(), 408 raw.contract_versions.status(), 409 raw.contract_versions.provider(), 410 ) 411 .into_bytes() 412 } 413 414 fn valid_lower_hex(value: &str, length: usize) -> bool { 415 value.len() == length 416 && value 417 .bytes() 418 .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)) 419 } 420 421 #[cfg(test)] 422 mod tests { 423 use super::*; 424 425 const MYC_LOCK: &[u8] = br#"schema = "radroots.service.source-lock.v3" 426 contract_version = 3 427 service = "myc" 428 repository = "https://github.com/radrootslabs/lib" 429 revision = "1111111111111111111111111111111111111111" 430 architecture = "radroots.crates.release.v2" 431 workspace_catalog_sha256 = "2222222222222222222222222222222222222222222222222222222222222222" 432 version = "0.1.0-alpha" 433 source_archive_sha256 = "3333333333333333333333333333333333333333333333333333333333333333" 434 cargo_lock_sha256 = "4444444444444444444444444444444444444444444444444444444444444444" 435 rust_version = "1.97.1" 436 host_feature_profile = "service-host" 437 438 [source_archive_contract] 439 binding = "sha256_of_canonical_exact_lib_revision_tree_archive" 440 format = "ustar" 441 compression = "none" 442 compression_timestamp = "not_applicable" 443 entry_order = "bytewise_git_path" 444 path_prefix = "none" 445 file_mode = "git_index_100644_or_100755" 446 uid = 0 447 gid = 0 448 uname = "" 449 gname = "" 450 mtime = "lib_revision_commit_timestamp" 451 pax_headers = "forbidden" 452 directory_entries = "omitted" 453 symlinks = "forbidden" 454 hardlinks = "forbidden" 455 submodules = "forbidden" 456 trailer = "two_zero_blocks" 457 458 [nix] 459 material = "qualified" 460 lib_revision = "1111111111111111111111111111111111111111" 461 supported_systems = ["aarch64-darwin", "x86_64-linux"] 462 463 [nix.public_input_lock] 464 path = "flake.lock" 465 sha256 = "5555555555555555555555555555555555555555555555555555555555555555" 466 binding = "exact_regular_file_bytes" 467 mutable_reference = "forbidden" 468 lib_input = "lib" 469 470 [nix.parent_result] 471 embedded_in_public_input_lock = false 472 embedded_in_source_lock = false 473 storage = "separate_generation_scoped_evidence" 474 475 [artifact_contract] 476 path = "contracts/release/myc-artifact-contract.v3.json" 477 sha256 = "6666666666666666666666666666666666666666666666666666666666666666" 478 binding = "exact_regular_file_bytes_in_same_source_revision" 479 480 [sqlite] 481 high_level_authority = "sqlx_only" 482 second_pool_connection_query_transaction_migration_authority = "forbidden" 483 incremental_backup_adapter = "sealed_native_sqlx_owned_locked_handle_only" 484 native_linkage_count = 1 485 486 [contract_versions] 487 config = 1 488 state = 2 489 admin = 3 490 status = 4 491 provider = 5 492 "#; 493 494 #[test] 495 fn canonical_v3_lock_round_trips_and_exposes_exact_bindings() { 496 let lock = ServiceSourceLockV3::from_canonical_bytes(MYC_LOCK).expect("v3 lock"); 497 assert_eq!(lock.canonical_bytes(), MYC_LOCK); 498 assert_eq!(lock.service(), "myc"); 499 assert_eq!(lock.revision(), "1".repeat(40)); 500 assert_eq!(LOCK_FILENAME, "radroots.service.source-lock.v3.toml"); 501 assert_eq!( 502 PREDECESSOR_LOCK_FILENAME, 503 "radroots.service.source-lock.v2.toml" 504 ); 505 assert_eq!(lock.workspace_catalog_sha256(), "2".repeat(64)); 506 assert_eq!(lock.source_archive_sha256(), "3".repeat(64)); 507 assert_eq!(lock.cargo_lock_sha256(), "4".repeat(64)); 508 assert_eq!(lock.flake_lock_sha256(), "5".repeat(64)); 509 assert_eq!(lock.artifact_contract_sha256(), "6".repeat(64)); 510 let reconstructed = ServiceSourceLockV3::fixture(FixtureParts { 511 service: lock.service(), 512 revision: lock.revision(), 513 workspace_catalog_sha256: lock.workspace_catalog_sha256(), 514 source_archive_sha256: lock.source_archive_sha256(), 515 cargo_lock_sha256: lock.cargo_lock_sha256(), 516 flake_lock_sha256: lock.flake_lock_sha256(), 517 artifact_contract_sha256: lock.artifact_contract_sha256(), 518 contract_versions: lock.contract_versions(), 519 }) 520 .expect("historical constructor"); 521 assert_eq!(reconstructed.canonical_bytes(), MYC_LOCK); 522 assert_eq!( 523 lock.artifact_contract_path(), 524 "contracts/release/myc-artifact-contract.v3.json" 525 ); 526 assert_eq!( 527 lock.contract_versions(), 528 ContractVersions::new(1, 2, 3, 4, 5) 529 ); 530 } 531 532 #[test] 533 fn v3_lock_rejects_noncanonical_and_independent_semantic_drift() { 534 let noncanonical = String::from_utf8(MYC_LOCK.to_vec()) 535 .expect("UTF-8") 536 .replace("schema =", "schema ="); 537 assert!(ServiceSourceLockV3::from_canonical_bytes(noncanonical.as_bytes()).is_err()); 538 for (from, to) in [ 539 ("service = \"myc\"", "service = \"other\""), 540 ("material = \"qualified\"", "material = \"deferred\""), 541 ("native_linkage_count = 1", "native_linkage_count = 2"), 542 ("trailer = \"two_zero_blocks\"", "trailer = \"other\""), 543 ] { 544 let drifted = String::from_utf8(MYC_LOCK.to_vec()) 545 .expect("UTF-8") 546 .replace(from, to); 547 assert!( 548 ServiceSourceLockV3::from_canonical_bytes(drifted.as_bytes()).is_err(), 549 "{from}" 550 ); 551 } 552 } 553 }