workspace_attachment.rs (17055B)
1 #![cfg(unix)] 2 3 use radroots_domain::{ 4 ActorRole, AuthorityRef, CheckpointDigest, CheckpointRef, ExecutorId, Generation, GrantId, 5 PublicKey, RouteDigest, UnixSeconds, WorkspaceId, 6 }; 7 use radroots_secrets::{ 8 SecretId, SecretRef, 9 envelope::Nonce, 10 file::{FileOpenMode, FileProvider}, 11 id::{BackendKind, KeyVersion}, 12 provider::AccessPolicy, 13 wrapping::SecretMaterial, 14 }; 15 use radroots_storage::workspace::{ 16 AttachmentInput, AttachmentProof, WorkspaceClock, WorkspaceError, 17 }; 18 use radroots_storage_sqlite::workspace::{LocalOutboxConfig, NativeWorkspace, WorkspaceCrypto}; 19 use std::{ 20 path::PathBuf, 21 sync::{ 22 Arc, 23 atomic::{AtomicU64, Ordering}, 24 }, 25 }; 26 27 struct Clock(AtomicU64); 28 impl WorkspaceClock for Clock { 29 fn now(&self) -> Result<UnixSeconds, WorkspaceError> { 30 UnixSeconds::new(self.0.load(Ordering::SeqCst)).map_err(|_| WorkspaceError::Unavailable) 31 } 32 } 33 struct Fixture { 34 _temp: tempfile::TempDir, 35 path: PathBuf, 36 provider: Arc<FileProvider>, 37 reference: SecretRef, 38 clock: Arc<Clock>, 39 } 40 impl Fixture { 41 fn new() -> Self { 42 let temp = tempfile::tempdir().unwrap(); 43 let base = temp.path().canonicalize().unwrap(); 44 let provider = Arc::new( 45 FileProvider::open( 46 base.join("vault"), 47 FileOpenMode::CreateNew, 48 SecretMaterial::from_slice(&[0xA5; 32]).unwrap(), 49 ) 50 .unwrap(), 51 ); 52 let reference = SecretRef::new( 53 SecretId::parse("workspace-key").unwrap(), 54 BackendKind::File, 55 KeyVersion::new(1).unwrap(), 56 ); 57 provider 58 .provision( 59 &reference, 60 &SecretMaterial::from_slice(&[0x41; 32]).unwrap(), 61 Nonce::new([0x11; 24]), 62 ) 63 .unwrap(); 64 Self { 65 _temp: temp, 66 path: base.join("outbox"), 67 provider, 68 reference, 69 clock: Arc::new(Clock(AtomicU64::new(100))), 70 } 71 } 72 fn crypto(&self) -> WorkspaceCrypto { 73 self.crypto_with([0x41; 32]) 74 } 75 fn crypto_with(&self, key: [u8; 32]) -> WorkspaceCrypto { 76 let reference = SecretRef::new( 77 self.reference.id().clone(), 78 self.reference.backend(), 79 self.reference.key_version(), 80 ); 81 WorkspaceCrypto::new( 82 self.provider.clone(), 83 reference, 84 SecretMaterial::from_slice(&key).unwrap(), 85 AccessPolicy::standard(), 86 ) 87 .unwrap() 88 } 89 fn config(&self) -> LocalOutboxConfig { 90 LocalOutboxConfig::new( 91 key(), 92 key(), 93 "synthetic-device", 94 UnixSeconds::new(200).unwrap(), 95 ) 96 .unwrap() 97 } 98 async fn create(&self) -> (NativeWorkspace, AttachmentInput, AttachmentProof) { 99 NativeWorkspace::initialize_local_outbox( 100 &self.path, 101 self.config(), 102 self.crypto(), 103 self.clock.clone(), 104 ) 105 .await 106 .unwrap() 107 } 108 } 109 fn key() -> PublicKey { 110 PublicKey::from_hex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798").unwrap() 111 } 112 113 #[tokio::test] 114 async fn same_size_schema_substitution_is_refused_without_repair() { 115 use sqlx::Connection; 116 117 for statement in [ 118 "DROP INDEX operation_transaction_identity; CREATE UNIQUE INDEX operation_transaction_identity ON operations(workspace,operation_id)", 119 "DROP INDEX operation_transaction_identity; CREATE INDEX foreign_index ON operations(workspace,transaction_id)", 120 "CREATE TRIGGER foreign_trigger BEFORE INSERT ON operations BEGIN SELECT RAISE(ABORT,'synthetic catalog fault'); END", 121 ] { 122 let fixture = Fixture::new(); 123 let (store, input, _) = fixture.create().await; 124 store.close().await.unwrap(); 125 let database = fixture.path.join("execution_v1.sqlite"); 126 let options = sqlx::sqlite::SqliteConnectOptions::new().filename(&database); 127 let mut connection = sqlx::SqliteConnection::connect_with(&options) 128 .await 129 .unwrap(); 130 sqlx::raw_sql(statement) 131 .execute(&mut connection) 132 .await 133 .unwrap(); 134 connection.close().await.unwrap(); 135 let retained = std::fs::read(&database).unwrap(); 136 assert!(matches!( 137 NativeWorkspace::open_existing( 138 &fixture.path, 139 input, 140 fixture.crypto(), 141 fixture.clock.clone(), 142 ) 143 .await, 144 Err(WorkspaceError::RecoveryRequired) 145 )); 146 assert_eq!(std::fs::read(&database).unwrap(), retained); 147 } 148 } 149 fn second_key() -> PublicKey { 150 PublicKey::from_hex("c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5").unwrap() 151 } 152 fn altered(input: &AttachmentInput, field: u8) -> AttachmentInput { 153 AttachmentInput::new( 154 if field == 0 { 155 WorkspaceId::from_bytes([9; 32]) 156 } else { 157 input.workspace_id() 158 }, 159 if field == 1 { 160 AuthorityRef::new( 161 ExecutorId::from_bytes([9; 32]), 162 input.authority().generation(), 163 ) 164 } else if field == 2 { 165 AuthorityRef::new(input.authority().executor_id(), Generation::new(2).unwrap()) 166 } else { 167 input.authority() 168 }, 169 if field == 3 { 170 second_key() 171 } else { 172 input.participant() 173 }, 174 if field == 4 { 175 "local://other" 176 } else { 177 input.endpoint() 178 }, 179 if field == 5 { 180 second_key() 181 } else { 182 input.endpoint_identity() 183 }, 184 if field == 6 { 185 GrantId::from_bytes([9; 32]) 186 } else { 187 input.grant_id() 188 }, 189 if field == 7 { 190 CheckpointRef::new( 191 CheckpointDigest::from_bytes([9; 32]), 192 input.checkpoint().sequence(), 193 input.checkpoint().coverage(), 194 vec![], 195 ) 196 .unwrap() 197 } else { 198 input.checkpoint().clone() 199 }, 200 if field == 8 { 201 RouteDigest::from_bytes([9; 32]) 202 } else { 203 input.route_digest() 204 }, 205 ) 206 .unwrap() 207 } 208 209 #[tokio::test] 210 async fn actual_protected_outbox_authenticates_exact_selection_and_reopens() { 211 let fixture = Fixture::new(); 212 let (store, input, proof) = fixture.create().await; 213 let binding = store.authenticate(input.clone(), &proof).await.unwrap(); 214 assert_eq!(binding.actor().role(), ActorRole::Buyer); 215 assert_eq!(binding.actor().grant_id(), Some(input.grant_id())); 216 assert_eq!(binding.principal_id(), "synthetic-device"); 217 assert_eq!(binding.observed_at().get(), 100); 218 assert_eq!(binding.grant_revision().get(), 1); 219 assert_eq!(input.checkpoint().sequence().get(), 0); 220 assert_eq!( 221 input.checkpoint().coverage(), 222 radroots_domain::Coverage::Partial 223 ); 224 for field in 0..9 { 225 assert!( 226 store 227 .authenticate(altered(&input, field), &proof) 228 .await 229 .is_err(), 230 "field {field}" 231 ); 232 } 233 let wrong = AttachmentProof::from_protected_input(&[0; 32]).unwrap(); 234 assert_eq!( 235 store.authenticate(input.clone(), &wrong).await.unwrap_err(), 236 WorkspaceError::Unauthorized 237 ); 238 assert!(matches!( 239 NativeWorkspace::open_existing( 240 &fixture.path, 241 input.clone(), 242 fixture.crypto(), 243 fixture.clock.clone() 244 ) 245 .await, 246 Err(WorkspaceError::WriterActive) 247 )); 248 let mut files = std::fs::read_dir(&fixture.path) 249 .unwrap() 250 .map(|entry| entry.unwrap().path()) 251 .collect::<Vec<_>>(); 252 files.push(fixture.path.join("execution_v1.sqlite")); 253 for path in files { 254 if !path.is_file() { 255 continue; 256 } 257 let bytes = std::fs::read(path).unwrap(); 258 assert!( 259 !bytes 260 .windows(b"synthetic-device".len()) 261 .any(|window| window == b"synthetic-device") 262 ); 263 proof.with_secret(|secret| assert!(!bytes.windows(32).any(|window| window == secret))); 264 } 265 assert_eq!(format!("{proof:?}"), "AttachmentProof(<redacted>)"); 266 assert_eq!(format!("{store:?}"), "NativeWorkspace(<redacted>)"); 267 store.close().await.unwrap(); 268 store.close().await.unwrap(); 269 assert_eq!( 270 store.authenticate(input.clone(), &proof).await.unwrap_err(), 271 WorkspaceError::Closed 272 ); 273 let reopened = NativeWorkspace::open_existing( 274 &fixture.path, 275 input.clone(), 276 fixture.crypto(), 277 fixture.clock.clone(), 278 ) 279 .await 280 .unwrap(); 281 reopened.authenticate(input.clone(), &proof).await.unwrap(); 282 reopened.close().await.unwrap(); 283 assert!(matches!( 284 NativeWorkspace::open_existing( 285 &fixture.path, 286 input, 287 fixture.crypto_with([0x42; 32]), 288 fixture.clock.clone() 289 ) 290 .await, 291 Err(WorkspaceError::Unavailable) 292 )); 293 } 294 295 #[tokio::test] 296 async fn actual_revocation_and_expiry_survive_reopen_without_reenrollment() { 297 let fixture = Fixture::new(); 298 let (store, input, proof) = fixture.create().await; 299 fixture.clock.0.store(200, Ordering::SeqCst); 300 assert_eq!( 301 store.authenticate(input.clone(), &proof).await.unwrap_err(), 302 WorkspaceError::GrantExpired 303 ); 304 fixture.clock.0.store(0, Ordering::SeqCst); 305 assert_eq!( 306 store.authenticate(input.clone(), &proof).await.unwrap_err(), 307 WorkspaceError::Unavailable 308 ); 309 fixture.clock.0.store(101, Ordering::SeqCst); 310 store.revoke(input.clone(), &proof).await.unwrap(); 311 assert_eq!( 312 store.authenticate(input.clone(), &proof).await.unwrap_err(), 313 WorkspaceError::GrantRevoked 314 ); 315 store.close().await.unwrap(); 316 let reopened = NativeWorkspace::open_existing( 317 &fixture.path, 318 input.clone(), 319 fixture.crypto(), 320 fixture.clock.clone(), 321 ) 322 .await 323 .unwrap(); 324 assert_eq!( 325 reopened.authenticate(input, &proof).await.unwrap_err(), 326 WorkspaceError::GrantRevoked 327 ); 328 reopened.close().await.unwrap(); 329 assert!( 330 NativeWorkspace::initialize_local_outbox( 331 &fixture.path, 332 fixture.config(), 333 fixture.crypto(), 334 fixture.clock.clone() 335 ) 336 .await 337 .is_err() 338 ); 339 } 340 341 #[tokio::test] 342 async fn existing_open_never_creates_or_accepts_wrong_namespace_or_ciphertext() { 343 let fixture = Fixture::new(); 344 let (store, input, proof) = fixture.create().await; 345 store.close().await.unwrap(); 346 assert!(matches!( 347 NativeWorkspace::open_existing( 348 &fixture.path, 349 altered(&input, 0), 350 fixture.crypto(), 351 fixture.clock.clone() 352 ) 353 .await, 354 Err(WorkspaceError::WrongNamespace) 355 )); 356 let missing = fixture.path.with_file_name("missing"); 357 assert!( 358 NativeWorkspace::open_existing( 359 &missing, 360 input.clone(), 361 fixture.crypto(), 362 fixture.clock.clone() 363 ) 364 .await 365 .is_err() 366 ); 367 assert!(!missing.exists()); 368 use sqlx::Connection; 369 let options = sqlx::sqlite::SqliteConnectOptions::new() 370 .filename(fixture.path.join("execution_v1.sqlite")); 371 let mut connection = sqlx::SqliteConnection::connect_with(&options) 372 .await 373 .unwrap(); 374 sqlx::query("UPDATE workspace_grant SET protected=zeroblob(1024)") 375 .execute(&mut connection) 376 .await 377 .unwrap(); 378 connection.close().await.unwrap(); 379 assert!(matches!( 380 NativeWorkspace::open_existing( 381 &fixture.path, 382 input, 383 fixture.crypto(), 384 fixture.clock.clone() 385 ) 386 .await, 387 Err(WorkspaceError::RecoveryRequired) 388 )); 389 assert_eq!(format!("{proof:?}"), "AttachmentProof(<redacted>)"); 390 } 391 392 struct PausedClock { 393 state: std::sync::Mutex<(bool, bool, bool)>, 394 changed: std::sync::Condvar, 395 } 396 impl PausedClock { 397 fn arm(&self) { 398 *self.state.lock().unwrap() = (true, false, false); 399 } 400 fn entered(&self) { 401 let state = self.state.lock().unwrap(); 402 let (state, timeout) = self 403 .changed 404 .wait_timeout_while(state, std::time::Duration::from_secs(5), |state| !state.1) 405 .unwrap(); 406 assert!( 407 state.1 && !timeout.timed_out(), 408 "worker did not reach injected clock" 409 ); 410 } 411 fn release(&self) { 412 self.state.lock().unwrap().2 = true; 413 self.changed.notify_all(); 414 } 415 } 416 impl WorkspaceClock for PausedClock { 417 fn now(&self) -> Result<UnixSeconds, WorkspaceError> { 418 let mut state = self.state.lock().unwrap(); 419 if state.0 { 420 state.1 = true; 421 self.changed.notify_all(); 422 while !state.2 { 423 state = self.changed.wait(state).unwrap(); 424 } 425 state.0 = false; 426 } 427 Ok(UnixSeconds::new(100).unwrap()) 428 } 429 } 430 #[tokio::test] 431 async fn cancelled_authentication_and_close_keep_writer_until_owned_work_drains() { 432 let fixture = Fixture::new(); 433 let clock = Arc::new(PausedClock { 434 state: std::sync::Mutex::new((false, false, false)), 435 changed: std::sync::Condvar::new(), 436 }); 437 let (store, input, proof) = NativeWorkspace::initialize_local_outbox( 438 &fixture.path, 439 fixture.config(), 440 fixture.crypto(), 441 clock.clone(), 442 ) 443 .await 444 .unwrap(); 445 clock.arm(); 446 let mut auth = Box::pin(store.authenticate(input.clone(), &proof)); 447 assert!(futures::poll!(auth.as_mut()).is_pending()); 448 clock.entered(); 449 let mut closing = Box::pin(store.close()); 450 assert!(futures::poll!(closing.as_mut()).is_pending()); 451 drop(auth); 452 drop(closing); 453 assert!(matches!( 454 NativeWorkspace::open_existing( 455 &fixture.path, 456 input.clone(), 457 fixture.crypto(), 458 fixture.clock.clone() 459 ) 460 .await, 461 Err(WorkspaceError::WriterActive) 462 )); 463 clock.release(); 464 store.close().await.unwrap(); 465 let reopened = NativeWorkspace::open_existing( 466 &fixture.path, 467 input.clone(), 468 fixture.crypto(), 469 fixture.clock.clone(), 470 ) 471 .await 472 .unwrap(); 473 reopened.authenticate(input, &proof).await.unwrap(); 474 reopened.close().await.unwrap(); 475 } 476 477 #[tokio::test] 478 async fn unsafe_links_permissions_and_schema_are_refused_without_repair() { 479 use std::os::unix::fs::{PermissionsExt, symlink}; 480 let fixture = Fixture::new(); 481 let (store, input, _proof) = fixture.create().await; 482 store.close().await.unwrap(); 483 let database = fixture.path.join("execution_v1.sqlite"); 484 let retained = fixture.path.join("retained.sqlite"); 485 std::fs::rename(&database, &retained).unwrap(); 486 symlink(&retained, &database).unwrap(); 487 assert!( 488 NativeWorkspace::open_existing( 489 &fixture.path, 490 input.clone(), 491 fixture.crypto(), 492 fixture.clock.clone() 493 ) 494 .await 495 .is_err() 496 ); 497 assert!( 498 std::fs::symlink_metadata(&database) 499 .unwrap() 500 .file_type() 501 .is_symlink() 502 ); 503 std::fs::remove_file(&database).unwrap(); 504 std::fs::rename(&retained, &database).unwrap(); 505 std::fs::set_permissions(&database, std::fs::Permissions::from_mode(0o644)).unwrap(); 506 assert!( 507 NativeWorkspace::open_existing( 508 &fixture.path, 509 input.clone(), 510 fixture.crypto(), 511 fixture.clock.clone() 512 ) 513 .await 514 .is_err() 515 ); 516 assert_eq!( 517 std::fs::metadata(&database).unwrap().permissions().mode() & 0o777, 518 0o644 519 ); 520 std::fs::set_permissions(&database, std::fs::Permissions::from_mode(0o600)).unwrap(); 521 use sqlx::Connection; 522 let options = sqlx::sqlite::SqliteConnectOptions::new().filename(&database); 523 let mut connection = sqlx::SqliteConnection::connect_with(&options) 524 .await 525 .unwrap(); 526 sqlx::query("CREATE TABLE unrelated(value TEXT)") 527 .execute(&mut connection) 528 .await 529 .unwrap(); 530 connection.close().await.unwrap(); 531 assert!(matches!( 532 NativeWorkspace::open_existing( 533 &fixture.path, 534 input, 535 fixture.crypto(), 536 fixture.clock.clone() 537 ) 538 .await, 539 Err(WorkspaceError::RecoveryRequired) 540 )); 541 } 542 543 #[path = "support/stock_abort_port.rs"] 544 mod stock_abort_port; 545 #[path = "support/stock_authority.rs"] 546 mod stock_authority; 547 #[path = "support/stock_envelope_port.rs"] 548 mod stock_envelope_port; 549 550 #[path = "support/pool_creation.rs"] 551 mod pool_creation;