@@ -41,21 +41,47 @@ impl CniTransactionServer { |
41 | 41 | /// |
42 | 42 | /// Returns an error when the paths are unsafe, durable state is invalid, or |
43 | 43 | /// the socket cannot be bound with owner-only permissions. |
| 44 | + #[cfg(test)] |
44 | 45 | pub fn bind( |
45 | 46 | socket_path: PathBuf, |
46 | 47 | state_path: &Path, |
47 | 48 | provider: NodeBlockProvider, |
48 | 49 | readiness_lease_path: PathBuf, |
49 | 50 | readiness_heartbeat: Duration, |
| 51 | + ) -> Result<Self> { |
| 52 | + Self::bind_with_initializer( |
| 53 | + socket_path, |
| 54 | + state_path, |
| 55 | + provider, |
| 56 | + readiness_lease_path, |
| 57 | + readiness_heartbeat, |
| 58 | + |_| Ok(()), |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + /// Initialize retirement while exclusively owning startup and before any |
| 63 | + /// socket/readiness publication. Existing live sockets reject first, before |
| 64 | + /// schema migration. An unhooked schema-5 journal must never report Ready. |
| 65 | + pub(super) fn bind_with_initializer( |
| 66 | + socket_path: PathBuf, |
| 67 | + state_path: &Path, |
| 68 | + provider: NodeBlockProvider, |
| 69 | + readiness_lease_path: PathBuf, |
| 70 | + readiness_heartbeat: Duration, |
| 71 | + initialize: impl FnOnce(&mut AttachmentJournal) -> Result<()>, |
50 | 72 | ) -> Result<Self> { |
51 | 73 | validate_socket_path(&socket_path)?; |
52 | 74 | validate_readiness_lease_path(&readiness_lease_path)?; |
53 | 75 | if readiness_heartbeat.is_zero() { |
54 | 76 | bail!("CNI readiness heartbeat must be greater than zero"); |
55 | 77 | } |
56 | | - let journal = AttachmentJournal::open(state_path, provider) |
57 | | - .with_context(|| format!("open CNI attachment journal {}", state_path.display()))?; |
58 | 78 | prepare_socket_path(&socket_path)?; |
| 79 | + let mut journal = AttachmentJournal::open(state_path, provider) |
| 80 | + .with_context(|| format!("open CNI attachment journal {}", state_path.display()))?; |
| 81 | + initialize(&mut journal)?; |
| 82 | + if journal.retirement_required() && journal.cut().is_none() { |
| 83 | + bail!("CNI retirement-required journal has no live startup gate"); |
| 84 | + } |
59 | 85 | let listener = UnixListener::bind(&socket_path) |
60 | 86 | .with_context(|| format!("bind CNI transaction socket {}", socket_path.display()))?; |
61 | 87 | fs::set_permissions(&socket_path, fs::Permissions::from_mode(0o600)) |
@@ -371,6 +397,99 @@ mod tests { |
371 | 397 | ) |
372 | 398 | } |
373 | 399 |
|
| 400 | + struct NoopRetirement; |
| 401 | + impl unf_cni_state::AttachmentRetirement for NoopRetirement { |
| 402 | + fn retire(&self, _: &[u8; 32]) -> io::Result<()> { |
| 403 | + Ok(()) |
| 404 | + } |
| 405 | + } |
| 406 | + |
| 407 | + #[tokio::test] |
| 408 | + async fn required_journal_without_initializer_never_binds_or_reports_ready() { |
| 409 | + let directory = tempfile::tempdir().unwrap(); |
| 410 | + let journal_path = directory.path().join("attachments.json"); |
| 411 | + let socket = directory.path().join("cni.sock"); |
| 412 | + let lease = directory.path().join("readiness"); |
| 413 | + let mut journal = AttachmentJournal::open(&journal_path, provider()).unwrap(); |
| 414 | + journal |
| 415 | + .install_required_retirement(Box::new(NoopRetirement)) |
| 416 | + .unwrap(); |
| 417 | + drop(journal); |
| 418 | + let before = fs::read(&journal_path).unwrap(); |
| 419 | + assert!( |
| 420 | + CniTransactionServer::bind( |
| 421 | + socket.clone(), |
| 422 | + &journal_path, |
| 423 | + provider(), |
| 424 | + lease.clone(), |
| 425 | + Duration::from_secs(1) |
| 426 | + ) |
| 427 | + .is_err() |
| 428 | + ); |
| 429 | + assert!(!socket.exists() && !lease.exists()); |
| 430 | + assert_eq!(fs::read(&journal_path).unwrap(), before); |
| 431 | + } |
| 432 | + |
| 433 | + #[tokio::test] |
| 434 | + async fn initializer_precedes_socket_and_readiness_but_not_live_owner_check() { |
| 435 | + let directory = tempfile::tempdir().unwrap(); |
| 436 | + let journal_path = directory.path().join("attachments.json"); |
| 437 | + let socket = directory.path().join("cni.sock"); |
| 438 | + let lease = directory.path().join("readiness"); |
| 439 | + let server = CniTransactionServer::bind_with_initializer( |
| 440 | + socket.clone(), |
| 441 | + &journal_path, |
| 442 | + provider(), |
| 443 | + lease.clone(), |
| 444 | + Duration::from_secs(1), |
| 445 | + |journal| { |
| 446 | + assert!(!socket.exists() && !lease.exists()); |
| 447 | + journal.install_required_retirement(Box::new(NoopRetirement))?; |
| 448 | + Ok(()) |
| 449 | + }, |
| 450 | + ) |
| 451 | + .unwrap(); |
| 452 | + assert!(server.journal.lock().await.cut().is_some()); |
| 453 | + let before = fs::read(&journal_path).unwrap(); |
| 454 | + let mut called = false; |
| 455 | + assert!( |
| 456 | + CniTransactionServer::bind_with_initializer( |
| 457 | + socket.clone(), |
| 458 | + &journal_path, |
| 459 | + provider(), |
| 460 | + lease.clone(), |
| 461 | + Duration::from_secs(1), |
| 462 | + |_| { |
| 463 | + called = true; |
| 464 | + Ok(()) |
| 465 | + } |
| 466 | + ) |
| 467 | + .is_err() |
| 468 | + ); |
| 469 | + assert!(!called && !lease.exists()); |
| 470 | + assert_eq!(fs::read(&journal_path).unwrap(), before); |
| 471 | + drop(server); |
| 472 | + } |
| 473 | + |
| 474 | + #[tokio::test] |
| 475 | + async fn failed_initializer_stops_before_socket_and_readiness() { |
| 476 | + let directory = tempfile::tempdir().unwrap(); |
| 477 | + let socket = directory.path().join("cni.sock"); |
| 478 | + let lease = directory.path().join("readiness"); |
| 479 | + assert!( |
| 480 | + CniTransactionServer::bind_with_initializer( |
| 481 | + socket.clone(), |
| 482 | + &directory.path().join("attachments.json"), |
| 483 | + provider(), |
| 484 | + lease.clone(), |
| 485 | + Duration::from_secs(1), |
| 486 | + |_| bail!("injected gate failure") |
| 487 | + ) |
| 488 | + .is_err() |
| 489 | + ); |
| 490 | + assert!(!socket.exists() && !lease.exists()); |
| 491 | + } |
| 492 | + |
374 | 493 | fn request(operation: TransactionOperation) -> Vec<u8> { |
375 | 494 | serde_json::to_vec(&TransactionRequest::new( |
376 | 495 | CNI_TRANSACTION_SCHEMA_VERSION, |
|