CI / UI Checks (pull_request) Successful in 15s
CI / Rust Checks (pull_request) Failing after 4m58s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Failing after 1s
CI / Deploy (pull_request) Skipped
mcp-server запускает сохранённые фоновые задачи: захват по PostgreSQL lease с fence-токеном, зашифрованный input и повторная проверка доступа до сетевого вызова, checkpoint с AttemptID до dispatch и восстановление только по PG. Устаревший worker не может зафиксировать результат. Добавлены миграции v26–v28 (V25 не меняется), guard их отсутствия в старой схеме, метрики/трассы попыток (attempt_trace) и остановка до отправки запроса: стоп в окне после checkpoint и до permit завершает задачу как failed(shutdown_before_dispatch) без сетевых вызовов, соседние queued-задачи остаются доступными. Retry-попытки получают отдельные дочерние spans с durable current_attempt_id. Shared admission/fairness из Story 4.1 сохранены. Проверки: durable_tasks 13/13, stages 11/11 (retry 5), runtime lib 47, bin shutdown 1, registry tasks 16, admission 11, migrations 58 (независимая QA и исходный runtime-прогон), точный exact-head CI остаётся обязательным.
980 lines
33 KiB
Rust
980 lines
33 KiB
Rust
use super::*;
|
|
|
|
pub(super) const MASTER_KEY_PAGE_LIMIT_MAX: i64 = 10_000;
|
|
|
|
impl PostgresRegistry {
|
|
pub async fn verify_or_register_master_key_identity(
|
|
&self,
|
|
candidate: MasterKeyIdentityCandidate<'_>,
|
|
) -> Result<MasterKeyIdentityRecord, RegistryError> {
|
|
validate_master_key_candidate(&candidate)?;
|
|
let mut tx = self.pool.begin().await?;
|
|
lock_master_key_authority(&mut tx).await?;
|
|
let row = sqlx::query(
|
|
"select
|
|
epoch,
|
|
fingerprint,
|
|
cipher_contract,
|
|
status,
|
|
backup_ref,
|
|
created_at,
|
|
activated_at,
|
|
retired_at
|
|
from master_key_identities
|
|
where status = 'active'
|
|
order by epoch desc
|
|
limit 1
|
|
for update",
|
|
)
|
|
.fetch_optional(&mut *tx)
|
|
.await?;
|
|
|
|
let record = if let Some(row) = row {
|
|
let record = map_master_key_identity(row);
|
|
if record.epoch != candidate.epoch
|
|
|| record.fingerprint != candidate.fingerprint
|
|
|| record.cipher_contract != candidate.cipher_contract
|
|
{
|
|
return Err(RegistryError::MasterKeyIdentityMismatch {
|
|
epoch: record.epoch,
|
|
});
|
|
}
|
|
record
|
|
} else {
|
|
sqlx::query(
|
|
"insert into master_key_identities (
|
|
epoch,
|
|
fingerprint,
|
|
cipher_contract,
|
|
status,
|
|
backup_ref,
|
|
created_at,
|
|
activated_at,
|
|
retired_at
|
|
) values (
|
|
$1, $2, $3, 'active', null, $4::timestamptz, $4::timestamptz, null
|
|
)",
|
|
)
|
|
.bind(candidate.epoch)
|
|
.bind(candidate.fingerprint)
|
|
.bind(candidate.cipher_contract)
|
|
.bind(candidate.observed_at)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
MasterKeyIdentityRecord {
|
|
epoch: candidate.epoch,
|
|
fingerprint: candidate.fingerprint.to_owned(),
|
|
cipher_contract: candidate.cipher_contract.to_owned(),
|
|
status: "active".to_owned(),
|
|
backup_ref: None,
|
|
created_at: *candidate.observed_at,
|
|
activated_at: Some(*candidate.observed_at),
|
|
retired_at: None,
|
|
}
|
|
};
|
|
|
|
tx.commit().await?;
|
|
Ok(record)
|
|
}
|
|
|
|
pub async fn master_key_rotation_status(
|
|
&self,
|
|
) -> Result<MasterKeyRotationStatus, RegistryError> {
|
|
let active_identity = self.active_master_key_identity().await?;
|
|
let rows = sqlx::query(
|
|
"select
|
|
id,
|
|
source_epoch,
|
|
target_epoch,
|
|
target_fingerprint,
|
|
state,
|
|
backup_ref,
|
|
checkpoint_secret_id,
|
|
total_secret_versions,
|
|
processed_secret_versions,
|
|
verified_secret_versions,
|
|
failure_code,
|
|
created_at,
|
|
updated_at
|
|
from master_key_rotations
|
|
order by created_at desc, id asc",
|
|
)
|
|
.fetch_all(&self.pool)
|
|
.await?;
|
|
let rotations = rows.into_iter().map(map_master_key_rotation).collect();
|
|
Ok(MasterKeyRotationStatus {
|
|
active_identity,
|
|
rotations,
|
|
})
|
|
}
|
|
|
|
pub async fn active_master_key_identity(
|
|
&self,
|
|
) -> Result<Option<MasterKeyIdentityRecord>, RegistryError> {
|
|
let row = sqlx::query(
|
|
"select
|
|
epoch,
|
|
fingerprint,
|
|
cipher_contract,
|
|
status,
|
|
backup_ref,
|
|
created_at,
|
|
activated_at,
|
|
retired_at
|
|
from master_key_identities
|
|
where status = 'active'
|
|
order by epoch desc
|
|
limit 1",
|
|
)
|
|
.fetch_optional(&self.pool)
|
|
.await?;
|
|
Ok(row.map(map_master_key_identity))
|
|
}
|
|
|
|
pub async fn master_key_fingerprint_exists(
|
|
&self,
|
|
fingerprint: &str,
|
|
) -> Result<bool, RegistryError> {
|
|
let valid_fingerprint = fingerprint.len() == 64
|
|
&& fingerprint
|
|
.bytes()
|
|
.all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase());
|
|
if !valid_fingerprint {
|
|
return Err(RegistryError::InvalidMasterKeyIdentity);
|
|
}
|
|
let count = sqlx::query_scalar::<_, i64>(
|
|
"select count(*) from master_key_identities where fingerprint = $1",
|
|
)
|
|
.bind(fingerprint)
|
|
.fetch_one(&self.pool)
|
|
.await?;
|
|
Ok(count > 0)
|
|
}
|
|
|
|
pub async fn list_secret_versions_for_master_key_epoch(
|
|
&self,
|
|
epoch: i64,
|
|
) -> Result<Vec<SecretVersionRecord>, RegistryError> {
|
|
if epoch < 1 {
|
|
return Err(RegistryError::InvalidMasterKeyIdentity);
|
|
}
|
|
let rows = sqlx::query(
|
|
"select
|
|
secret_id,
|
|
version,
|
|
ciphertext,
|
|
key_version,
|
|
master_key_epoch,
|
|
target_ciphertext,
|
|
target_key_version,
|
|
target_master_key_epoch,
|
|
created_at,
|
|
created_by
|
|
from secret_versions
|
|
where master_key_epoch = $1
|
|
order by secret_id asc, version asc",
|
|
)
|
|
.bind(epoch)
|
|
.fetch_all(&self.pool)
|
|
.await?;
|
|
rows.into_iter().map(map_secret_version_record).collect()
|
|
}
|
|
|
|
pub async fn list_secret_versions_for_master_key_epoch_page(
|
|
&self,
|
|
epoch: i64,
|
|
after_secret_id: Option<&str>,
|
|
after_version: Option<u32>,
|
|
limit: i64,
|
|
) -> Result<Vec<SecretVersionRecord>, RegistryError> {
|
|
if epoch < 1 || !(1..=MASTER_KEY_PAGE_LIMIT_MAX).contains(&limit) {
|
|
return Err(RegistryError::InvalidMasterKeyIdentity);
|
|
}
|
|
let after_db_version = after_version.map(to_db_version);
|
|
let rows = sqlx::query(
|
|
"select
|
|
secret_id,
|
|
version,
|
|
ciphertext,
|
|
key_version,
|
|
master_key_epoch,
|
|
target_ciphertext,
|
|
target_key_version,
|
|
target_master_key_epoch,
|
|
created_at,
|
|
created_by
|
|
from secret_versions
|
|
where master_key_epoch = $1
|
|
and (
|
|
$2::text is null
|
|
or (secret_id, version) > ($2::text, $3::integer)
|
|
)
|
|
order by secret_id asc, version asc
|
|
limit $4",
|
|
)
|
|
.bind(epoch)
|
|
.bind(after_secret_id)
|
|
.bind(after_db_version)
|
|
.bind(limit)
|
|
.fetch_all(&self.pool)
|
|
.await?;
|
|
rows.into_iter().map(map_secret_version_record).collect()
|
|
}
|
|
|
|
pub async fn list_target_secret_versions_for_master_key_rotation(
|
|
&self,
|
|
target_epoch: i64,
|
|
) -> Result<Vec<SecretVersionRecord>, RegistryError> {
|
|
if target_epoch < 1 {
|
|
return Err(RegistryError::InvalidMasterKeyIdentity);
|
|
}
|
|
let rows = sqlx::query(
|
|
"select
|
|
secret_id,
|
|
version,
|
|
ciphertext,
|
|
key_version,
|
|
master_key_epoch,
|
|
target_ciphertext,
|
|
target_key_version,
|
|
target_master_key_epoch,
|
|
created_at,
|
|
created_by
|
|
from secret_versions
|
|
where target_master_key_epoch = $1
|
|
order by secret_id asc, version asc",
|
|
)
|
|
.bind(target_epoch)
|
|
.fetch_all(&self.pool)
|
|
.await?;
|
|
rows.into_iter().map(map_secret_version_record).collect()
|
|
}
|
|
|
|
pub async fn list_target_secret_versions_for_master_key_rotation_page(
|
|
&self,
|
|
target_epoch: i64,
|
|
after_secret_id: Option<&str>,
|
|
after_version: Option<u32>,
|
|
limit: i64,
|
|
) -> Result<Vec<SecretVersionRecord>, RegistryError> {
|
|
if target_epoch < 1 || !(1..=MASTER_KEY_PAGE_LIMIT_MAX).contains(&limit) {
|
|
return Err(RegistryError::InvalidMasterKeyIdentity);
|
|
}
|
|
let after_db_version = after_version.map(to_db_version);
|
|
let rows = sqlx::query(
|
|
"select
|
|
secret_id,
|
|
version,
|
|
ciphertext,
|
|
key_version,
|
|
master_key_epoch,
|
|
target_ciphertext,
|
|
target_key_version,
|
|
target_master_key_epoch,
|
|
created_at,
|
|
created_by
|
|
from secret_versions
|
|
where target_master_key_epoch = $1
|
|
and (
|
|
$2::text is null
|
|
or (secret_id, version) > ($2::text, $3::integer)
|
|
)
|
|
order by secret_id asc, version asc
|
|
limit $4",
|
|
)
|
|
.bind(target_epoch)
|
|
.bind(after_secret_id)
|
|
.bind(after_db_version)
|
|
.bind(limit)
|
|
.fetch_all(&self.pool)
|
|
.await?;
|
|
rows.into_iter().map(map_secret_version_record).collect()
|
|
}
|
|
|
|
pub async fn begin_master_key_rotation(
|
|
&self,
|
|
source_epoch: i64,
|
|
target_epoch: i64,
|
|
target_fingerprint: &str,
|
|
backup_ref: Option<&str>,
|
|
now: &OffsetDateTime,
|
|
) -> Result<MasterKeyRotationRecord, RegistryError> {
|
|
validate_rotation_request(source_epoch, target_epoch, target_fingerprint, backup_ref)?;
|
|
let mut tx = self.pool.begin().await?;
|
|
lock_master_key_authority(&mut tx).await?;
|
|
let active = select_active_master_key_identity_for_update(&mut tx).await?;
|
|
let Some(active) = active else {
|
|
return Err(RegistryError::MasterKeyRotationConflict);
|
|
};
|
|
if active.epoch != source_epoch || active.fingerprint == target_fingerprint {
|
|
return Err(RegistryError::MasterKeyRotationConflict);
|
|
}
|
|
|
|
let existing_active = sqlx::query(
|
|
"select
|
|
id,
|
|
source_epoch,
|
|
target_epoch,
|
|
target_fingerprint,
|
|
state,
|
|
backup_ref,
|
|
checkpoint_secret_id,
|
|
total_secret_versions,
|
|
processed_secret_versions,
|
|
verified_secret_versions,
|
|
failure_code,
|
|
created_at,
|
|
updated_at
|
|
from master_key_rotations
|
|
where state in ('running', 'verifying', 'verified')
|
|
order by created_at asc
|
|
limit 1
|
|
for update",
|
|
)
|
|
.fetch_optional(&mut *tx)
|
|
.await?;
|
|
if let Some(row) = existing_active {
|
|
let record = map_master_key_rotation(row);
|
|
if record.source_epoch == source_epoch
|
|
&& record.target_epoch == target_epoch
|
|
&& record.target_fingerprint == target_fingerprint
|
|
{
|
|
tx.commit().await?;
|
|
return Ok(record);
|
|
}
|
|
return Err(RegistryError::MasterKeyRotationInProgress);
|
|
}
|
|
|
|
if sqlx::query_scalar::<_, i64>(
|
|
"select count(*) from master_key_identities where fingerprint = $1",
|
|
)
|
|
.bind(target_fingerprint)
|
|
.fetch_one(&mut *tx)
|
|
.await?
|
|
> 0
|
|
{
|
|
return Err(RegistryError::MasterKeyRotationConflict);
|
|
}
|
|
|
|
let secret_total = sqlx::query_scalar::<_, i64>(
|
|
"select count(*) from secret_versions where master_key_epoch = $1",
|
|
)
|
|
.bind(source_epoch)
|
|
.fetch_one(&mut *tx)
|
|
.await?;
|
|
let task_total = sqlx::query_scalar::<_, i64>(
|
|
"select count(*) from tasks where executable_input_master_key_epoch = $1",
|
|
)
|
|
.bind(source_epoch)
|
|
.fetch_one(&mut *tx)
|
|
.await?;
|
|
let total = secret_total + task_total;
|
|
let rotation_id = format!("master-key-e{source_epoch}-to-e{target_epoch}");
|
|
let aborted = sqlx::query(
|
|
"select
|
|
id,
|
|
source_epoch,
|
|
target_epoch,
|
|
target_fingerprint,
|
|
state,
|
|
backup_ref,
|
|
checkpoint_secret_id,
|
|
total_secret_versions,
|
|
processed_secret_versions,
|
|
verified_secret_versions,
|
|
failure_code,
|
|
created_at,
|
|
updated_at
|
|
from master_key_rotations
|
|
where id = $1 and state in ('aborted', 'failed')
|
|
for update",
|
|
)
|
|
.bind(&rotation_id)
|
|
.fetch_optional(&mut *tx)
|
|
.await?;
|
|
if let Some(row) = aborted {
|
|
let record = map_master_key_rotation(row);
|
|
if record.source_epoch != source_epoch
|
|
|| record.target_epoch != target_epoch
|
|
|| record.target_fingerprint != target_fingerprint
|
|
{
|
|
return Err(RegistryError::MasterKeyRotationConflict);
|
|
}
|
|
sqlx::query(
|
|
"update secret_versions
|
|
set target_ciphertext = null,
|
|
target_key_version = null,
|
|
target_master_key_epoch = null
|
|
where target_master_key_epoch = $1",
|
|
)
|
|
.bind(target_epoch)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
sqlx::query(
|
|
"update tasks
|
|
set target_executable_input_ciphertext = null,
|
|
target_executable_input_key_version = null,
|
|
target_executable_input_master_key_epoch = null
|
|
where target_executable_input_master_key_epoch = $1",
|
|
)
|
|
.bind(target_epoch)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
sqlx::query(
|
|
"update master_key_rotations
|
|
set state = 'running',
|
|
backup_ref = $2,
|
|
checkpoint_secret_id = null,
|
|
total_secret_versions = $3,
|
|
processed_secret_versions = 0,
|
|
verified_secret_versions = 0,
|
|
failure_code = null,
|
|
updated_at = $4::timestamptz
|
|
where id = $1",
|
|
)
|
|
.bind(&rotation_id)
|
|
.bind(backup_ref)
|
|
.bind(total)
|
|
.bind(now)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
let record = select_master_key_rotation_for_update(&mut tx, &rotation_id).await?;
|
|
tx.commit().await?;
|
|
return Ok(record);
|
|
}
|
|
sqlx::query(
|
|
"insert into master_key_rotations (
|
|
id,
|
|
source_epoch,
|
|
target_epoch,
|
|
target_fingerprint,
|
|
state,
|
|
backup_ref,
|
|
checkpoint_secret_id,
|
|
total_secret_versions,
|
|
processed_secret_versions,
|
|
verified_secret_versions,
|
|
failure_code,
|
|
created_at,
|
|
updated_at
|
|
) values (
|
|
$1, $2, $3, $4, 'running', $5, null, $6, 0, 0, null, $7::timestamptz, $7::timestamptz
|
|
)",
|
|
)
|
|
.bind(&rotation_id)
|
|
.bind(source_epoch)
|
|
.bind(target_epoch)
|
|
.bind(target_fingerprint)
|
|
.bind(backup_ref)
|
|
.bind(total)
|
|
.bind(now)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
let record = select_master_key_rotation_for_update(&mut tx, &rotation_id).await?;
|
|
tx.commit().await?;
|
|
Ok(record)
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub async fn stage_master_key_rotation_ciphertext(
|
|
&self,
|
|
rotation_id: &str,
|
|
secret_id: &SecretId,
|
|
version: u32,
|
|
source_epoch: i64,
|
|
target_ciphertext: &str,
|
|
target_key_version: &str,
|
|
target_epoch: i64,
|
|
now: &OffsetDateTime,
|
|
) -> Result<MasterKeyRotationRecord, RegistryError> {
|
|
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 secret_versions
|
|
set target_ciphertext = $4,
|
|
target_key_version = $5,
|
|
target_master_key_epoch = $6
|
|
where secret_id = $1
|
|
and version = $2
|
|
and master_key_epoch = $3
|
|
and (
|
|
target_master_key_epoch is null
|
|
or target_master_key_epoch = $6
|
|
)
|
|
and (
|
|
target_ciphertext is null
|
|
or target_ciphertext = $4
|
|
)",
|
|
)
|
|
.bind(secret_id.as_str())
|
|
.bind(to_db_version(version))
|
|
.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 = $2,
|
|
processed_secret_versions = $3,
|
|
updated_at = $4::timestamptz
|
|
where id = $1",
|
|
)
|
|
.bind(rotation_id)
|
|
.bind(secret_id.as_str())
|
|
.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 async fn finish_master_key_rotation_batches(
|
|
&self,
|
|
rotation_id: &str,
|
|
now: &OffsetDateTime,
|
|
) -> Result<MasterKeyRotationRecord, RegistryError> {
|
|
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.processed_secret_versions != rotation.total_secret_versions
|
|
{
|
|
return Err(RegistryError::MasterKeyRotationConflict);
|
|
}
|
|
sqlx::query(
|
|
"update master_key_rotations
|
|
set state = 'verifying',
|
|
updated_at = $2::timestamptz
|
|
where id = $1",
|
|
)
|
|
.bind(rotation_id)
|
|
.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 async fn verify_master_key_rotation(
|
|
&self,
|
|
rotation_id: &str,
|
|
verified_secret_versions: i64,
|
|
now: &OffsetDateTime,
|
|
) -> Result<MasterKeyRotationRecord, RegistryError> {
|
|
let mut tx = self.pool.begin().await?;
|
|
let rotation = select_master_key_rotation_for_update(&mut tx, rotation_id).await?;
|
|
if rotation.state != "verifying"
|
|
|| verified_secret_versions != rotation.total_secret_versions
|
|
|| rotation.processed_secret_versions != rotation.total_secret_versions
|
|
{
|
|
return Err(RegistryError::MasterKeyRotationVerificationFailed);
|
|
}
|
|
sqlx::query(
|
|
"update master_key_rotations
|
|
set state = 'verified',
|
|
verified_secret_versions = $2,
|
|
updated_at = $3::timestamptz
|
|
where id = $1",
|
|
)
|
|
.bind(rotation_id)
|
|
.bind(verified_secret_versions)
|
|
.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 async fn promote_master_key_rotation(
|
|
&self,
|
|
rotation_id: &str,
|
|
target_identity: MasterKeyIdentityCandidate<'_>,
|
|
now: &OffsetDateTime,
|
|
) -> Result<MasterKeyRotationRecord, RegistryError> {
|
|
validate_master_key_candidate(&target_identity)?;
|
|
let mut tx = self.pool.begin().await?;
|
|
let rotation = select_master_key_rotation_for_update(&mut tx, rotation_id).await?;
|
|
if rotation.state != "verified"
|
|
|| rotation.target_epoch != target_identity.epoch
|
|
|| rotation.target_fingerprint != target_identity.fingerprint
|
|
|| rotation.verified_secret_versions != rotation.total_secret_versions
|
|
{
|
|
return Err(RegistryError::MasterKeyRotationConflict);
|
|
}
|
|
let processed =
|
|
count_staged_rotation_targets(&mut tx, rotation.source_epoch, rotation.target_epoch)
|
|
.await?;
|
|
if processed != rotation.total_secret_versions {
|
|
return Err(RegistryError::MasterKeyRotationVerificationFailed);
|
|
}
|
|
sqlx::query(
|
|
"update master_key_identities
|
|
set status = 'retired',
|
|
retired_at = $2::timestamptz
|
|
where status = 'active' and epoch = $1",
|
|
)
|
|
.bind(rotation.source_epoch)
|
|
.bind(now)
|
|
.execute(&mut *tx)
|
|
.await
|
|
.and_then(|result| {
|
|
if result.rows_affected() == 1 {
|
|
Ok(result)
|
|
} else {
|
|
Err(sqlx::Error::RowNotFound)
|
|
}
|
|
})
|
|
.map_err(|error| match error {
|
|
sqlx::Error::RowNotFound => RegistryError::MasterKeyRotationConflict,
|
|
other => RegistryError::Storage(other),
|
|
})?;
|
|
sqlx::query(
|
|
"insert into master_key_identities (
|
|
epoch,
|
|
fingerprint,
|
|
cipher_contract,
|
|
status,
|
|
backup_ref,
|
|
created_at,
|
|
activated_at,
|
|
retired_at
|
|
) values (
|
|
$1, $2, $3, 'active', $4, $5::timestamptz, $5::timestamptz, null
|
|
)",
|
|
)
|
|
.bind(target_identity.epoch)
|
|
.bind(target_identity.fingerprint)
|
|
.bind(target_identity.cipher_contract)
|
|
.bind(rotation.backup_ref.as_deref())
|
|
.bind(now)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
sqlx::query(
|
|
"update secret_versions
|
|
set ciphertext = target_ciphertext,
|
|
key_version = target_key_version,
|
|
master_key_epoch = target_master_key_epoch,
|
|
target_ciphertext = null,
|
|
target_key_version = null,
|
|
target_master_key_epoch = null
|
|
where master_key_epoch = $1
|
|
and target_master_key_epoch = $2",
|
|
)
|
|
.bind(rotation.source_epoch)
|
|
.bind(rotation.target_epoch)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
sqlx::query(
|
|
"update tasks
|
|
set executable_input_ciphertext = target_executable_input_ciphertext,
|
|
executable_input_key_version = target_executable_input_key_version,
|
|
executable_input_master_key_epoch = target_executable_input_master_key_epoch,
|
|
target_executable_input_ciphertext = null,
|
|
target_executable_input_key_version = null,
|
|
target_executable_input_master_key_epoch = null
|
|
where executable_input_master_key_epoch = $1
|
|
and target_executable_input_master_key_epoch = $2",
|
|
)
|
|
.bind(rotation.source_epoch)
|
|
.bind(rotation.target_epoch)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
sqlx::query(
|
|
"update master_key_rotations
|
|
set state = 'promoted',
|
|
updated_at = $2::timestamptz
|
|
where id = $1",
|
|
)
|
|
.bind(rotation_id)
|
|
.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 async fn abort_master_key_rotation(
|
|
&self,
|
|
rotation_id: &str,
|
|
now: &OffsetDateTime,
|
|
) -> Result<MasterKeyRotationRecord, RegistryError> {
|
|
let mut tx = self.pool.begin().await?;
|
|
let rotation = select_master_key_rotation_for_update(&mut tx, rotation_id).await?;
|
|
if rotation.state == "promoted" {
|
|
return Err(RegistryError::MasterKeyRotationConflict);
|
|
}
|
|
sqlx::query(
|
|
"update secret_versions
|
|
set target_ciphertext = null,
|
|
target_key_version = null,
|
|
target_master_key_epoch = null
|
|
where target_master_key_epoch = $1",
|
|
)
|
|
.bind(rotation.target_epoch)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
sqlx::query(
|
|
"update tasks
|
|
set target_executable_input_ciphertext = null,
|
|
target_executable_input_key_version = null,
|
|
target_executable_input_master_key_epoch = null
|
|
where target_executable_input_master_key_epoch = $1",
|
|
)
|
|
.bind(rotation.target_epoch)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
sqlx::query(
|
|
"update master_key_rotations
|
|
set state = 'aborted',
|
|
updated_at = $2::timestamptz
|
|
where id = $1",
|
|
)
|
|
.bind(rotation_id)
|
|
.bind(now)
|
|
.execute(&mut *tx)
|
|
.await?;
|
|
let updated = select_master_key_rotation_for_update(&mut tx, rotation_id).await?;
|
|
tx.commit().await?;
|
|
Ok(updated)
|
|
}
|
|
}
|
|
|
|
fn validate_master_key_candidate(
|
|
candidate: &MasterKeyIdentityCandidate<'_>,
|
|
) -> Result<(), RegistryError> {
|
|
let valid_fingerprint = candidate.fingerprint.len() == 64
|
|
&& candidate
|
|
.fingerprint
|
|
.bytes()
|
|
.all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase());
|
|
if candidate.epoch < 1
|
|
|| !valid_fingerprint
|
|
|| candidate.cipher_contract != crate::model::MASTER_KEY_CIPHER_CONTRACT
|
|
{
|
|
return Err(RegistryError::InvalidMasterKeyIdentity);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn map_master_key_identity(row: sqlx::postgres::PgRow) -> MasterKeyIdentityRecord {
|
|
MasterKeyIdentityRecord {
|
|
epoch: row.get::<i64, _>("epoch"),
|
|
fingerprint: row.get::<String, _>("fingerprint"),
|
|
cipher_contract: row.get::<String, _>("cipher_contract"),
|
|
status: row.get::<String, _>("status"),
|
|
backup_ref: row.get::<Option<String>, _>("backup_ref"),
|
|
created_at: row.get("created_at"),
|
|
activated_at: row.get("activated_at"),
|
|
retired_at: row.get("retired_at"),
|
|
}
|
|
}
|
|
|
|
fn map_master_key_rotation(row: sqlx::postgres::PgRow) -> MasterKeyRotationRecord {
|
|
MasterKeyRotationRecord {
|
|
id: row.get::<String, _>("id"),
|
|
source_epoch: row.get::<i64, _>("source_epoch"),
|
|
target_epoch: row.get::<i64, _>("target_epoch"),
|
|
target_fingerprint: row.get::<String, _>("target_fingerprint"),
|
|
state: row.get::<String, _>("state"),
|
|
backup_ref: row.get::<Option<String>, _>("backup_ref"),
|
|
checkpoint_secret_id: row.get::<Option<String>, _>("checkpoint_secret_id"),
|
|
total_secret_versions: row.get::<i64, _>("total_secret_versions"),
|
|
processed_secret_versions: row.get::<i64, _>("processed_secret_versions"),
|
|
verified_secret_versions: row.get::<i64, _>("verified_secret_versions"),
|
|
failure_code: row.get::<Option<String>, _>("failure_code"),
|
|
created_at: row.get("created_at"),
|
|
updated_at: row.get("updated_at"),
|
|
}
|
|
}
|
|
|
|
fn map_secret_version_record(
|
|
row: sqlx::postgres::PgRow,
|
|
) -> Result<SecretVersionRecord, RegistryError> {
|
|
Ok(SecretVersionRecord {
|
|
secret_version: SecretVersion {
|
|
secret_id: SecretId::new(row.get::<String, _>("secret_id")),
|
|
version: from_db_version(row.get::<i32, _>("version"), "version")?,
|
|
ciphertext: row.get::<String, _>("ciphertext"),
|
|
key_version: row.get::<String, _>("key_version"),
|
|
created_at: row.get("created_at"),
|
|
created_by: row.get::<Option<String>, _>("created_by").map(UserId::new),
|
|
},
|
|
master_key_epoch: row.get::<i64, _>("master_key_epoch"),
|
|
target_ciphertext: row.get::<Option<String>, _>("target_ciphertext"),
|
|
target_key_version: row.get::<Option<String>, _>("target_key_version"),
|
|
target_master_key_epoch: row.get::<Option<i64>, _>("target_master_key_epoch"),
|
|
})
|
|
}
|
|
|
|
fn validate_rotation_request(
|
|
source_epoch: i64,
|
|
target_epoch: i64,
|
|
target_fingerprint: &str,
|
|
backup_ref: Option<&str>,
|
|
) -> Result<(), RegistryError> {
|
|
let candidate_observed_at = OffsetDateTime::UNIX_EPOCH;
|
|
validate_master_key_candidate(&MasterKeyIdentityCandidate {
|
|
epoch: target_epoch,
|
|
fingerprint: target_fingerprint,
|
|
cipher_contract: crate::model::MASTER_KEY_CIPHER_CONTRACT,
|
|
observed_at: &candidate_observed_at,
|
|
})?;
|
|
if source_epoch < 1 || target_epoch <= source_epoch {
|
|
return Err(RegistryError::InvalidMasterKeyIdentity);
|
|
}
|
|
if let Some(value) = backup_ref {
|
|
let invalid = value.is_empty()
|
|
|| value.len() > 256
|
|
|| value.bytes().any(|byte| byte.is_ascii_control())
|
|
|| value.contains("://");
|
|
if invalid {
|
|
return Err(RegistryError::InvalidMasterKeyIdentity);
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub(super) async fn ensure_active_master_key_epoch(
|
|
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
|
master_key_epoch: i64,
|
|
) -> Result<(), RegistryError> {
|
|
if master_key_epoch < 1 {
|
|
return Err(RegistryError::InvalidMasterKeyIdentity);
|
|
}
|
|
let active = select_active_master_key_identity_for_update(transaction).await?;
|
|
match active {
|
|
Some(identity) if identity.epoch == master_key_epoch => Ok(()),
|
|
Some(identity) => Err(RegistryError::MasterKeyIdentityMismatch {
|
|
epoch: identity.epoch,
|
|
}),
|
|
None => Err(RegistryError::InvalidMasterKeyIdentity),
|
|
}
|
|
}
|
|
|
|
pub(super) async fn ensure_no_active_master_key_rotation(
|
|
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
|
) -> Result<(), RegistryError> {
|
|
lock_master_key_authority(transaction).await?;
|
|
let row = sqlx::query(
|
|
"select id
|
|
from master_key_rotations
|
|
where state in ('running', 'verifying', 'verified')
|
|
order by created_at asc
|
|
limit 1
|
|
for update",
|
|
)
|
|
.fetch_optional(&mut **transaction)
|
|
.await?;
|
|
if row.is_some() {
|
|
return Err(RegistryError::MasterKeyRotationInProgress);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
async fn lock_master_key_authority(
|
|
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
|
) -> Result<(), RegistryError> {
|
|
sqlx::query(
|
|
"lock table
|
|
master_key_identities,
|
|
master_key_rotations
|
|
in share row exclusive mode",
|
|
)
|
|
.execute(&mut **transaction)
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
|
|
async fn select_active_master_key_identity_for_update(
|
|
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
|
) -> Result<Option<MasterKeyIdentityRecord>, RegistryError> {
|
|
let row = sqlx::query(
|
|
"select
|
|
epoch,
|
|
fingerprint,
|
|
cipher_contract,
|
|
status,
|
|
backup_ref,
|
|
created_at,
|
|
activated_at,
|
|
retired_at
|
|
from master_key_identities
|
|
where status = 'active'
|
|
order by epoch desc
|
|
limit 1
|
|
for update",
|
|
)
|
|
.fetch_optional(&mut **transaction)
|
|
.await?;
|
|
Ok(row.map(map_master_key_identity))
|
|
}
|
|
|
|
pub(super) async fn select_master_key_rotation_for_update(
|
|
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
|
rotation_id: &str,
|
|
) -> Result<MasterKeyRotationRecord, RegistryError> {
|
|
let row = sqlx::query(
|
|
"select
|
|
id,
|
|
source_epoch,
|
|
target_epoch,
|
|
target_fingerprint,
|
|
state,
|
|
backup_ref,
|
|
checkpoint_secret_id,
|
|
total_secret_versions,
|
|
processed_secret_versions,
|
|
verified_secret_versions,
|
|
failure_code,
|
|
created_at,
|
|
updated_at
|
|
from master_key_rotations
|
|
where id = $1
|
|
for update",
|
|
)
|
|
.bind(rotation_id)
|
|
.fetch_optional(&mut **transaction)
|
|
.await?;
|
|
row.map(map_master_key_rotation)
|
|
.ok_or_else(|| RegistryError::MasterKeyRotationNotFound {
|
|
rotation_id: rotation_id.to_owned(),
|
|
})
|
|
}
|
|
|
|
pub(super) async fn count_staged_rotation_targets(
|
|
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
|
source_epoch: i64,
|
|
target_epoch: i64,
|
|
) -> Result<i64, RegistryError> {
|
|
let count = sqlx::query_scalar::<_, i64>(
|
|
"select
|
|
(select count(*) from secret_versions
|
|
where master_key_epoch = $1
|
|
and target_master_key_epoch = $2) +
|
|
(select count(*) from tasks
|
|
where executable_input_master_key_epoch = $1
|
|
and target_executable_input_master_key_epoch = $2)",
|
|
)
|
|
.bind(source_epoch)
|
|
.bind(target_epoch)
|
|
.fetch_one(&mut **transaction)
|
|
.await?;
|
|
Ok(count)
|
|
}
|