recovery.rs (15801B)
1 //! Original obtained-evidence recovery on the same namespace owner. 2 use super::{ 3 AttachmentInput, AttachmentProof, WorkspaceClock, WorkspaceCrypto, 4 admission::Journal, 5 fs::{Kind, Namespace}, 6 record::GrantRecord, 7 stock, worker, 8 }; 9 use radroots_storage::{ 10 command::{ArtifactEvidence, EvidenceCaptureOutcome, OperationError}, 11 inventory::{ 12 StockDeliveryObservation, StockDeliveryObservationOutcome, StockEnvelopeOutcome, 13 StockEvidenceOutcome, StockEvidenceRequest, StockRewrapRequest, 14 }, 15 workspace::seller_stock::SellerAuthoritySelection, 16 }; 17 use sqlx::{Connection, SqliteConnection, sqlite::SqliteConnectOptions}; 18 19 enum GrantAuthorization { 20 Outbox(GrantRecord), 21 Stock(stock::CaptureAuthorization), 22 } 23 impl GrantAuthorization { 24 fn authenticate( 25 &self, 26 input: &AttachmentInput, 27 proof: &AttachmentProof, 28 ) -> Result<(), OperationError> { 29 match self { 30 Self::Outbox(record) => record 31 .authenticate_retained_capture(input, proof) 32 .map_err(Into::into), 33 Self::Stock(record) => record.authenticate(input, proof).map_err(Into::into), 34 } 35 } 36 } 37 pub(super) struct RecoveryAuthorization { 38 grant: GrantAuthorization, 39 close_confirmed: bool, 40 } 41 enum Material { 42 Outbox(Box<(AttachmentInput, ArtifactEvidence)>), 43 Stock(Box<StockEvidenceRequest>), 44 Unissued(Box<stock::issuance::unissued::Request>), 45 Envelope( 46 Box<( 47 stock::envelope::CaptureRequest, 48 radroots_storage::command::PrivateEnvelopeCandidate, 49 )>, 50 ), 51 RewrapAdmission(Box<StockRewrapRequest>), 52 Observation(Box<(super::WorkspaceBinding, StockDeliveryObservation)>), 53 } 54 impl Material { 55 fn input(&self) -> &AttachmentInput { 56 match self { 57 Self::Outbox(value) => &value.0, 58 Self::Stock(value) => value.binding().input(), 59 Self::Unissued(value) => value.binding.input(), 60 Self::Envelope(value) => value.0.source().binding().input(), 61 Self::RewrapAdmission(value) => value.source().binding().input(), 62 Self::Observation(value) => value.0.input(), 63 } 64 } 65 } 66 enum Captured { 67 Outbox(EvidenceCaptureOutcome), 68 Stock(StockEvidenceOutcome), 69 Unissued(stock::StockUnissuedOutcome), 70 Envelope(StockEnvelopeOutcome), 71 Observation(StockDeliveryObservationOutcome), 72 RewrapAdmission, 73 } 74 impl Captured { 75 fn committed(&self) -> bool { 76 matches!( 77 self, 78 Self::RewrapAdmission 79 | Self::Outbox(EvidenceCaptureOutcome::Committed(_)) 80 | Self::Stock(StockEvidenceOutcome::Committed(_)) 81 | Self::Unissued(stock::StockUnissuedOutcome::Committed(_)) 82 | Self::Envelope(StockEnvelopeOutcome::Committed(_)) 83 | Self::Observation(StockDeliveryObservationOutcome::Committed(_)) 84 ) 85 } 86 } 87 pub(super) struct RecoveryTurn<'a> { 88 pub connection: &'a mut Option<SqliteConnection>, 89 pub cached_grant: &'a mut Option<RecoveryAuthorization>, 90 pub namespace: &'a Namespace, 91 pub selected: &'a AttachmentInput, 92 pub crypto: &'a WorkspaceCrypto, 93 pub clock: &'a dyn WorkspaceClock, 94 pub uncertain: &'a mut bool, 95 pub options: &'a SqliteConnectOptions, 96 } 97 impl RecoveryTurn<'_> { 98 pub(super) async fn capture( 99 &mut self, 100 input: AttachmentInput, 101 proof: AttachmentProof, 102 evidence: ArtifactEvidence, 103 ) -> Result<EvidenceCaptureOutcome, OperationError> { 104 if self.namespace.kind() != Kind::Outbox { 105 return Err(OperationError::UnsupportedOperation); 106 } 107 if input != *self.selected { 108 return Err(OperationError::Unauthorized); 109 } 110 match self 111 .recover(Material::Outbox(Box::new((input, evidence))), proof) 112 .await? 113 { 114 Captured::Outbox(value) => Ok(value), 115 _ => Err(OperationError::RecoveryRequired), 116 } 117 } 118 pub(super) async fn capture_stock( 119 &mut self, 120 request: StockEvidenceRequest, 121 proof: AttachmentProof, 122 ) -> Result<StockEvidenceOutcome, OperationError> { 123 if self.namespace.kind() != Kind::SellerStock { 124 return Err(OperationError::UnsupportedOperation); 125 } 126 if !SellerAuthoritySelection::from_attachment(self.selected) 127 .matches_attachment(request.binding().input()) 128 { 129 return Err(OperationError::Unauthorized); 130 } 131 match self 132 .recover(Material::Stock(Box::new(request)), proof) 133 .await? 134 { 135 Captured::Stock(value) => Ok(value), 136 _ => Err(OperationError::RecoveryRequired), 137 } 138 } 139 pub(super) async fn capture_envelope( 140 &mut self, 141 request: stock::envelope::CaptureRequest, 142 proof: AttachmentProof, 143 candidate: radroots_storage::command::PrivateEnvelopeCandidate, 144 ) -> Result<StockEnvelopeOutcome, OperationError> { 145 if self.namespace.kind() != Kind::SellerStock { 146 return Err(OperationError::UnsupportedOperation); 147 } 148 if !SellerAuthoritySelection::from_attachment(self.selected) 149 .matches_attachment(request.source().binding().input()) 150 { 151 return Err(OperationError::Unauthorized); 152 } 153 match self 154 .recover(Material::Envelope(Box::new((request, candidate))), proof) 155 .await? 156 { 157 Captured::Envelope(value) => Ok(value), 158 _ => Err(OperationError::RecoveryRequired), 159 } 160 } 161 pub(super) async fn reconcile_rewrap_admission( 162 &mut self, 163 request: StockRewrapRequest, 164 proof: AttachmentProof, 165 ) -> Result<(), OperationError> { 166 if self.namespace.kind() != Kind::SellerStock { 167 return Err(OperationError::UnsupportedOperation); 168 } 169 if !SellerAuthoritySelection::from_attachment(self.selected) 170 .matches_attachment(request.source().binding().input()) 171 { 172 return Err(OperationError::Unauthorized); 173 } 174 match self 175 .recover(Material::RewrapAdmission(Box::new(request)), proof) 176 .await? 177 { 178 Captured::RewrapAdmission => Ok(()), 179 _ => Err(OperationError::RecoveryRequired), 180 } 181 } 182 pub(super) async fn capture_observation( 183 &mut self, 184 binding: super::WorkspaceBinding, 185 proof: AttachmentProof, 186 observation: StockDeliveryObservation, 187 ) -> Result<StockDeliveryObservationOutcome, OperationError> { 188 if self.namespace.kind() != Kind::SellerStock { 189 return Err(OperationError::UnsupportedOperation); 190 } 191 if !SellerAuthoritySelection::from_attachment(self.selected) 192 .matches_attachment(binding.input()) 193 { 194 return Err(OperationError::Unauthorized); 195 } 196 match self 197 .recover( 198 Material::Observation(Box::new((binding, observation))), 199 proof, 200 ) 201 .await? 202 { 203 Captured::Observation(value) => Ok(value), 204 _ => Err(OperationError::RecoveryRequired), 205 } 206 } 207 pub(super) async fn capture_unissued( 208 &mut self, 209 request: stock::issuance::unissued::Request, 210 proof: AttachmentProof, 211 ) -> Result<stock::StockUnissuedOutcome, OperationError> { 212 if self.namespace.kind() != Kind::SellerStock { 213 return Err(OperationError::UnsupportedOperation); 214 } 215 if !SellerAuthoritySelection::from_attachment(self.selected) 216 .matches_attachment(request.binding.input()) 217 { 218 return Err(OperationError::Unauthorized); 219 } 220 match self 221 .recover(Material::Unissued(Box::new(request)), proof) 222 .await? 223 { 224 Captured::Unissued(value) => Ok(value), 225 _ => Err(OperationError::RecoveryRequired), 226 } 227 } 228 async fn recover( 229 &mut self, 230 material: Material, 231 proof: AttachmentProof, 232 ) -> Result<Captured, OperationError> { 233 let replacing = *self.uncertain; 234 if replacing { 235 self.prepare(material.input(), &proof).await?; 236 } 237 let connection = self 238 .connection 239 .as_mut() 240 .ok_or(OperationError::RecoveryRequired)?; 241 let mut capture_uncertain = false; 242 let uncertainty = if replacing { 243 &mut capture_uncertain 244 } else { 245 &mut *self.uncertain 246 }; 247 let result = match material { 248 Material::Envelope(value) => { 249 let (request, candidate) = *value; 250 Box::pin(stock::envelope::capture_obtained( 251 &mut stock::inventory::Turn { 252 connection, 253 namespace: self.namespace, 254 selected: self.selected, 255 crypto: self.crypto, 256 clock: self.clock, 257 uncertain: uncertainty, 258 }, 259 request, 260 proof, 261 candidate, 262 )) 263 .await 264 .map(Captured::Envelope) 265 } 266 Material::RewrapAdmission(request) => { 267 Box::pin(stock::envelope::reconcile_rewrap_admission( 268 &mut stock::inventory::Turn { 269 connection, 270 namespace: self.namespace, 271 selected: self.selected, 272 crypto: self.crypto, 273 clock: self.clock, 274 uncertain: uncertainty, 275 }, 276 *request, 277 proof, 278 )) 279 .await 280 .map(|()| Captured::RewrapAdmission) 281 } 282 Material::Observation(value) => { 283 let (binding, observation) = *value; 284 Box::pin(stock::delivery::capture_observation( 285 &mut stock::inventory::Turn { 286 connection, 287 namespace: self.namespace, 288 selected: self.selected, 289 crypto: self.crypto, 290 clock: self.clock, 291 uncertain: uncertainty, 292 }, 293 binding, 294 proof, 295 observation, 296 )) 297 .await 298 .map(Captured::Observation) 299 } 300 Material::Outbox(value) => { 301 let (input, evidence) = *value; 302 Journal { 303 connection, 304 namespace: self.namespace, 305 selected: self.selected, 306 crypto: self.crypto, 307 clock: self.clock, 308 uncertain: uncertainty, 309 } 310 .capture_issuance(input, proof, evidence) 311 .await 312 .map(Captured::Outbox) 313 } 314 Material::Unissued(request) => Box::pin(stock::issuance::unissued::capture( 315 &mut stock::inventory::Turn { 316 connection, 317 namespace: self.namespace, 318 selected: self.selected, 319 crypto: self.crypto, 320 clock: self.clock, 321 uncertain: uncertainty, 322 }, 323 *request, 324 proof, 325 )) 326 .await 327 .map(Captured::Unissued), 328 Material::Stock(request) => Box::pin(stock::issuance::evidence::capture( 329 &mut stock::inventory::Turn { 330 connection, 331 namespace: self.namespace, 332 selected: self.selected, 333 crypto: self.crypto, 334 clock: self.clock, 335 uncertain: uncertainty, 336 }, 337 *request, 338 proof, 339 )) 340 .await 341 .map(Captured::Stock), 342 }; 343 if replacing && result.as_ref().is_ok_and(Captured::committed) { 344 *self.uncertain = false; 345 self.cached_grant.take(); 346 } 347 result 348 } 349 async fn prepare( 350 &mut self, 351 input: &AttachmentInput, 352 proof: &AttachmentProof, 353 ) -> Result<(), OperationError> { 354 self.namespace.validate()?; 355 // Authenticate original capture ownership before disposing the existing 356 // connection. No current lease, clock or fresh command grant is acquired. 357 if let Some(connection) = self.connection.as_mut() { 358 let grant = match self.namespace.kind() { 359 Kind::Outbox => { 360 let protected = worker::read(connection, self.selected).await?; 361 let plaintext = self 362 .crypto 363 .open( 364 self.selected.workspace_id(), 365 self.selected.grant_id(), 366 &protected, 367 ) 368 .await?; 369 GrantAuthorization::Outbox(plaintext.expose_secret(GrantRecord::decode_outbox)?) 370 } 371 Kind::SellerStock => GrantAuthorization::Stock( 372 stock::capture_authorization(connection, input, self.crypto, proof).await?, 373 ), 374 }; 375 grant.authenticate(input, proof)?; 376 *self.cached_grant = Some(RecoveryAuthorization { 377 grant, 378 close_confirmed: false, 379 }); 380 } else { 381 let cached = self 382 .cached_grant 383 .as_ref() 384 .ok_or(OperationError::RecoveryRequired)?; 385 cached.grant.authenticate(input, proof)?; 386 if !cached.close_confirmed { 387 return Err(OperationError::RecoveryRequired); 388 } 389 } 390 // The namespace writer lease stays owned through confirmed close, 391 // read-only preflight and replacement. No initialization or migration. 392 if let Some(connection) = self.connection.take() { 393 connection 394 .close() 395 .await 396 .map_err(|_| OperationError::RecoveryRequired)?; 397 self.cached_grant 398 .as_mut() 399 .ok_or(OperationError::RecoveryRequired)? 400 .close_confirmed = true; 401 } 402 worker::preflight_existing(self.namespace, self.selected, self.crypto).await?; 403 let connection = SqliteConnection::connect_with(self.options) 404 .await 405 .map_err(|_| OperationError::RecoveryRequired)?; 406 *self.connection = Some(connection); 407 self.cached_grant 408 .as_mut() 409 .ok_or(OperationError::RecoveryRequired)? 410 .close_confirmed = false; 411 let connection = self 412 .connection 413 .as_mut() 414 .ok_or(OperationError::RecoveryRequired)?; 415 // A failed validation retains the replacement; its close must be 416 // confirmed by this owner before a subsequent replacement. 417 match self.namespace.kind() { 418 Kind::Outbox => { 419 worker::initialize(connection, self.namespace, self.selected, None, self.crypto) 420 .await? 421 } 422 Kind::SellerStock => { 423 stock::initialize(connection, self.namespace, self.selected, None, self.crypto) 424 .await? 425 } 426 } 427 Ok(()) 428 } 429 }