//! Task ciphertext paging/staging of the master-key rotation (Story 4.3). //! //! Extracted from `master_key.rs` without logic changes so that module stays within the //! code-health line limit; the operations and their SQL are unchanged. use super::master_key::{ MASTER_KEY_PAGE_LIMIT_MAX, count_staged_rotation_targets, select_master_key_rotation_for_update, }; use super::*; fn map_master_key_task_version( row: sqlx::postgres::PgRow, ) -> Result { Ok(MasterKeyTaskVersion { task_id: row.get::("task_id"), ciphertext: row .get::, _>("executable_input_ciphertext") .unwrap_or_default(), key_version: row .get::, _>("executable_input_key_version") .unwrap_or_default(), epoch: row .get::, _>("executable_input_master_key_epoch") .unwrap_or_default(), target_ciphertext: row.get::, _>("target_executable_input_ciphertext"), target_key_version: row.get::, _>("target_executable_input_key_version"), target_epoch: row.get::, _>("target_executable_input_master_key_epoch"), }) } impl PostgresRegistry { pub async fn list_tasks_for_master_key_epoch_page( &self, epoch: i64, after_task_id: Option<&str>, limit: i64, ) -> Result, RegistryError> { if epoch < 1 || !(1..=MASTER_KEY_PAGE_LIMIT_MAX).contains(&limit) { return Err(RegistryError::InvalidMasterKeyIdentity); } let rows = sqlx::query( "select task_id::text as task_id, executable_input_ciphertext, executable_input_key_version, executable_input_master_key_epoch, target_executable_input_ciphertext, target_executable_input_key_version, target_executable_input_master_key_epoch from tasks where executable_input_master_key_epoch = $1 and ($2::text is null or task_id::text > $2::text) order by task_id asc limit $3", ) .bind(epoch) .bind(after_task_id) .bind(limit) .fetch_all(&self.pool) .await?; rows.into_iter().map(map_master_key_task_version).collect() } pub async fn list_target_tasks_for_master_key_rotation_page( &self, target_epoch: i64, after_task_id: Option<&str>, limit: i64, ) -> Result, RegistryError> { if target_epoch < 1 || !(1..=MASTER_KEY_PAGE_LIMIT_MAX).contains(&limit) { return Err(RegistryError::InvalidMasterKeyIdentity); } let rows = sqlx::query( "select task_id::text as task_id, executable_input_ciphertext, executable_input_key_version, executable_input_master_key_epoch, target_executable_input_ciphertext, target_executable_input_key_version, target_executable_input_master_key_epoch from tasks where target_executable_input_master_key_epoch = $1 and ($2::text is null or task_id::text > $2::text) order by task_id asc limit $3", ) .bind(target_epoch) .bind(after_task_id) .bind(limit) .fetch_all(&self.pool) .await?; rows.into_iter().map(map_master_key_task_version).collect() } #[allow(clippy::too_many_arguments)] pub async fn stage_master_key_rotation_task_ciphertext( &self, rotation_id: &str, task_id: &str, source_epoch: i64, target_ciphertext: &str, target_key_version: &str, target_epoch: i64, now: &OffsetDateTime, ) -> Result { let mut tx = self.pool.begin().await?; let rotation = select_master_key_rotation_for_update(&mut tx, rotation_id).await?; if rotation.state != "running" || rotation.source_epoch != source_epoch || rotation.target_epoch != target_epoch { return Err(RegistryError::MasterKeyRotationConflict); } let result = sqlx::query( "update tasks set target_executable_input_ciphertext = $3, target_executable_input_key_version = $4, target_executable_input_master_key_epoch = $5 where task_id::text = $1 and executable_input_master_key_epoch = $2 and ( target_executable_input_master_key_epoch is null or target_executable_input_master_key_epoch = $5 ) and ( target_executable_input_ciphertext is null or target_executable_input_ciphertext = $3 )", ) .bind(task_id) .bind(source_epoch) .bind(target_ciphertext) .bind(target_key_version) .bind(target_epoch) .execute(&mut *tx) .await?; if result.rows_affected() != 1 { return Err(RegistryError::MasterKeyRotationConflict); } let processed = count_staged_rotation_targets(&mut tx, source_epoch, target_epoch).await?; sqlx::query( "update master_key_rotations set checkpoint_secret_id = checkpoint_secret_id, processed_secret_versions = $2, updated_at = $3::timestamptz where id = $1", ) .bind(rotation_id) .bind(processed) .bind(now) .execute(&mut *tx) .await?; let updated = select_master_key_rotation_for_update(&mut tx, rotation_id).await?; tx.commit().await?; Ok(updated) } } pub struct MasterKeyTaskVersion { pub task_id: String, pub ciphertext: String, pub key_version: String, pub epoch: i64, pub target_ciphertext: Option, pub target_key_version: Option, pub target_epoch: Option, }