186 lines
7.1 KiB
Rust
186 lines
7.1 KiB
Rust
use super::*;
|
|
use crank_registry::{ApplyImportPlanRequest, ImportPlanApplyReport};
|
|
|
|
const MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES: usize = 128;
|
|
|
|
impl AdminService {
|
|
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), plan_id = %plan_id.as_str()))]
|
|
pub async fn apply_openapi_import_plan(
|
|
&self,
|
|
workspace_id: &WorkspaceId,
|
|
plan_id: &ImportPlanId,
|
|
payload: OpenApiImportPlanApplyPayload,
|
|
) -> Result<ImportPlanApplyReport, ApiError> {
|
|
self.ensure_workspace_exists(workspace_id).await?;
|
|
validate_apply_payload(&payload)?;
|
|
|
|
// The replay row is deliberately checked before the plan or job.
|
|
// Its report remains authoritative after normal TTL cleanup removes
|
|
// those source rows.
|
|
if let Some(application) = self
|
|
.registry
|
|
.get_import_plan_application(workspace_id, &payload.idempotency_key)
|
|
.await?
|
|
{
|
|
if application.plan_digest != payload.plan_digest {
|
|
return Err(key_digest_conflict());
|
|
}
|
|
return Ok(application.result.report);
|
|
}
|
|
|
|
let plan = self
|
|
.registry
|
|
.get_import_plan(workspace_id, plan_id)
|
|
.await?
|
|
.ok_or_else(|| {
|
|
ApiError::not_found_with_context(
|
|
"import plan was not found in this workspace",
|
|
json!({ "error_code": "import_plan_not_found" }),
|
|
)
|
|
})?;
|
|
|
|
if plan.digest != payload.plan_digest {
|
|
return Err(import_plan_repreview_error("import_plan_digest_mismatch"));
|
|
}
|
|
let now = OffsetDateTime::now_utc();
|
|
if plan.expires_at <= now {
|
|
return Err(import_plan_repreview_error("import_plan_expired"));
|
|
}
|
|
|
|
let job = self
|
|
.registry
|
|
.get_import_job(workspace_id, &plan.job_id)
|
|
.await?
|
|
.ok_or_else(|| import_plan_repreview_error("import_plan_job_unavailable"))?;
|
|
if job.kind != ImportJobKind::OpenApi {
|
|
return Err(import_plan_repreview_error("import_plan_job_kind_mismatch"));
|
|
}
|
|
if job.status != ImportJobStatus::Pending || job.expires_at <= now {
|
|
return Err(import_plan_repreview_error("import_plan_job_terminal"));
|
|
}
|
|
|
|
let resolved = self
|
|
.resolve_verified_openapi_preview(workspace_id, &job, OpenApiUploadLocale::En)
|
|
.await
|
|
.map_err(|resolution| match resolution {
|
|
ImportPreviewResolution::Fatal(error_code, _) => {
|
|
import_plan_repreview_error(error_code)
|
|
}
|
|
ImportPreviewResolution::NonFatal(_) => {
|
|
import_plan_repreview_error("import_preview_revalidation_failed")
|
|
}
|
|
})?;
|
|
|
|
// Applying a new plan requires the current normalizer/projection
|
|
// contract. Historic jobs remain available to the legacy replay path,
|
|
// but a plan created from a historic projection must be previewed
|
|
// again before it can authorize mutations.
|
|
if resolved
|
|
.fingerprint
|
|
.get("normalizer_version")
|
|
.and_then(Value::as_str)
|
|
!= Some(NORMALIZER_VERSION)
|
|
|| resolved
|
|
.fingerprint
|
|
.get("projection_version")
|
|
.and_then(Value::as_str)
|
|
!= Some(PROJECTION_VERSION)
|
|
{
|
|
return Err(import_plan_repreview_error("importer_version_changed"));
|
|
}
|
|
|
|
// Verify every persisted external snapshot again without making a
|
|
// network request. The registry repeats source/dependency checks
|
|
// under its transaction before it mutates operations.
|
|
self.read_import_job_dependencies(
|
|
workspace_id,
|
|
&job.preview_payload,
|
|
OpenApiUploadLocale::En,
|
|
)
|
|
.await
|
|
.map_err(|_| import_plan_repreview_error("import_dependency_verification_failed"))?;
|
|
|
|
let result = self
|
|
.registry
|
|
.apply_import_plan(&ApplyImportPlanRequest {
|
|
workspace_id,
|
|
plan_id,
|
|
plan_digest: &payload.plan_digest,
|
|
idempotency_key: &payload.idempotency_key,
|
|
verified_fingerprint: &resolved.fingerprint,
|
|
applied_at: &now,
|
|
})
|
|
.await
|
|
.map_err(map_apply_registry_error)?;
|
|
Ok(result.report)
|
|
}
|
|
}
|
|
|
|
fn validate_apply_payload(payload: &OpenApiImportPlanApplyPayload) -> Result<(), ApiError> {
|
|
let digest_valid = payload.plan_digest.len() == 64
|
|
&& payload
|
|
.plan_digest
|
|
.bytes()
|
|
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte));
|
|
if !digest_valid {
|
|
return Err(ApiError::validation_with_context(
|
|
"plan_digest must be 64 lowercase hexadecimal characters",
|
|
json!({ "error_code": "import_plan_invalid_input" }),
|
|
));
|
|
}
|
|
if !(1..=MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES).contains(&payload.idempotency_key.len())
|
|
|| payload.idempotency_key.trim() != payload.idempotency_key
|
|
|| payload.idempotency_key.chars().any(char::is_control)
|
|
{
|
|
return Err(ApiError::validation_with_context(
|
|
"idempotency_key must be a non-empty bounded printable value",
|
|
json!({ "error_code": "import_plan_invalid_input" }),
|
|
));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn map_apply_registry_error(error: RegistryError) -> ApiError {
|
|
match error {
|
|
RegistryError::ImportPlanNotFound { .. } => ApiError::not_found_with_context(
|
|
"import plan was not found in this workspace",
|
|
json!({ "error_code": "import_plan_not_found" }),
|
|
),
|
|
RegistryError::ImportPlanExpired { .. } => {
|
|
import_plan_repreview_error("import_plan_expired")
|
|
}
|
|
RegistryError::ImportPlanPayloadCorrupt { .. } => {
|
|
import_plan_repreview_error("import_plan_payload_invalid")
|
|
}
|
|
RegistryError::ImportPlanFingerprintStale { .. } => {
|
|
import_plan_repreview_error("import_plan_fingerprint_stale")
|
|
}
|
|
RegistryError::ImportPlanKeyDigestConflict { .. } => key_digest_conflict(),
|
|
RegistryError::ImportPlanNameConflict { .. } => ApiError::conflict_with_context(
|
|
"an operation name changed after the plan was created",
|
|
json!({
|
|
"error_code": "import_plan_name_conflict",
|
|
"recovery": "confirm_skip_rename_or_new_version"
|
|
}),
|
|
),
|
|
RegistryError::ImportPlanTargetStale { .. } => {
|
|
import_plan_repreview_error("import_plan_target_stale")
|
|
}
|
|
RegistryError::ImportPlanAuthUnavailable { .. } => {
|
|
import_plan_repreview_error("import_plan_auth_unavailable")
|
|
}
|
|
other => ApiError::from(other),
|
|
}
|
|
}
|
|
|
|
fn key_digest_conflict() -> ApiError {
|
|
ApiError::conflict_with_context(
|
|
"idempotency key was already committed for different plan material",
|
|
json!({
|
|
"error_code": "import_plan_key_digest_conflict",
|
|
"recovery": "repreview_required",
|
|
"repreview_required": true
|
|
}),
|
|
)
|
|
}
|