story 2-12-preview-partial-mode: implemented and reviewed via bmad-loop
This commit is contained in:
1 parent
d1ba0bd0e4
commit
ab35d67177
56 files changed
+5801
-376
No files matched your search
@@ -29,7 +29,7 @@ use crate::{
|
||||
capabilities::get_capabilities,
|
||||
imports::{
|
||||
apply_openapi_import_plan, create_openapi_import, create_openapi_import_plan,
|
||||
preview_openapi_import,
|
||||
preview_openapi_import, revalidate_openapi_import_plan,
|
||||
},
|
||||
observability::{
|
||||
approve_approval, deny_approval, export_logs_csv, export_usage_csv, get_agent_usage,
|
||||
@@ -65,6 +65,10 @@ pub fn build_app(state: AppState) -> Router {
|
||||
"/imports/openapi/{job_id}/plans",
|
||||
post(create_openapi_import_plan),
|
||||
)
|
||||
.route(
|
||||
"/imports/openapi/plans/{plan_id}",
|
||||
get(revalidate_openapi_import_plan),
|
||||
)
|
||||
.route(
|
||||
"/imports/openapi/{plan_id}/apply",
|
||||
post(apply_openapi_import_plan).layer(DefaultBodyLimit::max(16 * 1024)),
|
||||
|
||||
@@ -656,12 +656,25 @@ pub struct OpenApiImportPlanCandidateView {
|
||||
pub struct OpenApiImportPlanCreateResponse {
|
||||
pub plan_reference: String,
|
||||
pub plan_digest: String,
|
||||
pub catalog_revision: String,
|
||||
pub support_matrix_version: String,
|
||||
pub expires_at: String,
|
||||
pub selection_mode: String,
|
||||
pub counts: OpenApiImportPlanCounts,
|
||||
pub candidates: Vec<OpenApiImportPlanCandidateView>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct OpenApiImportPlanRevalidationResponse {
|
||||
pub plan_reference: String,
|
||||
pub plan_digest: String,
|
||||
pub revalidation_status: String,
|
||||
pub revalidation_required: bool,
|
||||
pub reason: Option<String>,
|
||||
pub counts: OpenApiImportPlanCounts,
|
||||
pub candidates: Vec<OpenApiImportPlanCandidateView>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct CreatedOperationResponse {
|
||||
pub operation_id: String,
|
||||
|
||||
@@ -223,6 +223,20 @@ pub async fn create_openapi_import_plan(
|
||||
Ok(Json(json!(plan)))
|
||||
}
|
||||
|
||||
pub async fn revalidate_openapi_import_plan(
|
||||
Path(path): Path<WorkspacePlanPath>,
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<Value>, ApiError> {
|
||||
let plan = state
|
||||
.service
|
||||
.revalidate_openapi_import_plan(
|
||||
&path.workspace_id.as_str().into(),
|
||||
&path.plan_id.as_str().into(),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(json!(plan)))
|
||||
}
|
||||
|
||||
pub async fn apply_openapi_import_plan(
|
||||
Path(path): Path<WorkspacePlanPath>,
|
||||
State(state): State<AppState>,
|
||||
|
||||
@@ -6,8 +6,9 @@ use crank_core::{
|
||||
SecretStatus, ToolQualityFinding, ToolQualitySeverity, WorkspaceId,
|
||||
};
|
||||
use crank_import::rest::{
|
||||
ImportFinding, ImportFindingSeverity, ImportOperationCandidate, NORMALIZER_VERSION,
|
||||
PROJECTION_VERSION, operation_draft_from_candidate, validate_document_source_safe,
|
||||
ImportFinding, ImportFindingSeverity, ImportNameConflict, ImportOperationCandidate,
|
||||
NORMALIZER_VERSION, PROJECTION_VERSION, operation_draft_from_candidate,
|
||||
validate_document_source_safe,
|
||||
};
|
||||
use crank_registry::{
|
||||
ApplyImportJobRequest, ArtifactSourceId, ArtifactSourceSensitivity,
|
||||
@@ -27,7 +28,8 @@ use crate::{
|
||||
AdminService, OpenApiImportAuthProfileChoice, OpenApiImportCreatePayload,
|
||||
OpenApiImportCreateResponse, OpenApiImportCreatedOperation, OpenApiImportPlanApplyPayload,
|
||||
OpenApiImportPlanCandidateView, OpenApiImportPlanCounts, OpenApiImportPlanCreatePayload,
|
||||
OpenApiImportPlanCreateResponse, OpenApiImportPlanDecision, OpenApiImportPreviewResponse,
|
||||
OpenApiImportPlanCreateResponse, OpenApiImportPlanDecision,
|
||||
OpenApiImportPlanRevalidationResponse, OpenApiImportPreviewResponse,
|
||||
OpenApiImportSecurityProfileChoice, OpenApiImportSkippedOperation, OpenApiUpload,
|
||||
OpenApiUploadLocale, OperationPayload, new_prefixed_id,
|
||||
},
|
||||
@@ -52,7 +54,8 @@ use verified_preview::{
|
||||
const IMPORT_JOB_TTL_HOURS: i64 = 24;
|
||||
const OPENAPI_PARSE_DEADLINE: std::time::Duration = std::time::Duration::from_secs(30);
|
||||
const OPENAPI_PARSE_CONCURRENCY: usize = 4;
|
||||
const MAX_IMPORT_PLAN_KEYS: usize = 512;
|
||||
const MAX_IMPORT_PLAN_KEYS: usize = 2_000;
|
||||
const OPENAPI_SUPPORT_MATRIX_VERSION: &str = "1.0.2";
|
||||
const MAX_IMPORT_PLAN_KEY_BYTES: usize = 4_096;
|
||||
const MAX_IMPORT_PLAN_SERVER_URL_BYTES: usize = 8_192;
|
||||
const MAX_IMPORT_PLAN_NAME_BYTES: usize = 256;
|
||||
@@ -60,6 +63,132 @@ const MAX_IMPORT_PLAN_ID_REF_BYTES: usize = 128;
|
||||
static OPENAPI_PARSE_SLOTS: tokio::sync::Semaphore =
|
||||
tokio::sync::Semaphore::const_new(OPENAPI_PARSE_CONCURRENCY);
|
||||
|
||||
fn preview_name_conflicts(
|
||||
preview: &crank_import::rest::ImportPreview,
|
||||
catalog_names: &[String],
|
||||
) -> Vec<ImportNameConflict> {
|
||||
let operations = preview
|
||||
.groups
|
||||
.iter()
|
||||
.flat_map(|group| group.operations.iter())
|
||||
.collect::<Vec<_>>();
|
||||
let mut operation_ids = BTreeMap::<&str, usize>::new();
|
||||
for candidate in &operations {
|
||||
if let Some(id) = candidate.operation_id.as_deref() {
|
||||
*operation_ids.entry(id).or_default() += 1;
|
||||
}
|
||||
}
|
||||
let occupied = catalog_names
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.collect::<BTreeSet<_>>();
|
||||
let mut proposals = catalog_names.iter().cloned().collect::<BTreeSet<_>>();
|
||||
proposals.extend(
|
||||
operations
|
||||
.iter()
|
||||
.map(|candidate| candidate.suggested_name.clone()),
|
||||
);
|
||||
let mut conflicts = Vec::new();
|
||||
for candidate in operations {
|
||||
let duplicate = candidate
|
||||
.operation_id
|
||||
.as_deref()
|
||||
.is_some_and(|id| operation_ids.get(id).is_some_and(|count| *count > 1));
|
||||
let invalid = candidate.operation_id.as_deref().is_some_and(|id| {
|
||||
id.is_empty()
|
||||
|| id.len() > MAX_IMPORT_PLAN_NAME_BYTES
|
||||
|| !id.bytes().enumerate().all(|(index, ch)| {
|
||||
ch.is_ascii_alphabetic() || ch == b'_' || (index > 0 && ch.is_ascii_digit())
|
||||
})
|
||||
});
|
||||
let taken = occupied.contains(candidate.suggested_name.as_str());
|
||||
if !duplicate && !invalid && !taken {
|
||||
continue;
|
||||
}
|
||||
let base = candidate
|
||||
.suggested_name
|
||||
.chars()
|
||||
.take(240)
|
||||
.collect::<String>();
|
||||
let proposal = next_available_name(&base, &mut proposals);
|
||||
for conflict_type in [
|
||||
duplicate.then_some("duplicate_name"),
|
||||
invalid.then_some("invalid_name"),
|
||||
taken.then_some("taken_name"),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
conflicts.push(ImportNameConflict {
|
||||
operation_key: candidate.key.clone(),
|
||||
conflict_type: conflict_type.to_owned(),
|
||||
proposed_name: proposal.clone(),
|
||||
source_location: candidate.source_location.as_ref().map(|location| {
|
||||
crank_import::rest::SourceLocation {
|
||||
pointer: if candidate.operation_id.is_some() {
|
||||
format!("{}/operationId", location.pointer)
|
||||
} else {
|
||||
location.pointer.clone()
|
||||
},
|
||||
}
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
conflicts
|
||||
}
|
||||
|
||||
fn next_available_name(base: &str, occupied: &mut BTreeSet<String>) -> String {
|
||||
// A catalog may contain more names than the preview candidate cap. With
|
||||
// occupied.len() + 1 suffixes, at least one candidate must be free.
|
||||
(2..=occupied.len() + 2)
|
||||
.map(|index| format!("{base}_{index}"))
|
||||
.find(|name| occupied.insert(name.clone()))
|
||||
.expect("finite occupied names always leave a free suffix")
|
||||
}
|
||||
|
||||
fn attach_preview_readiness(
|
||||
preview: &mut crank_import::rest::ImportPreview,
|
||||
choices: &[OpenApiImportSecurityProfileChoice],
|
||||
) {
|
||||
let document_blocked = preview.findings.iter().any(|finding| {
|
||||
finding.severity == ImportFindingSeverity::Error && finding.operation_key.is_none()
|
||||
});
|
||||
for candidate in preview
|
||||
.groups
|
||||
.iter_mut()
|
||||
.flat_map(|group| group.operations.iter_mut())
|
||||
{
|
||||
let blocked = document_blocked
|
||||
|| candidate
|
||||
.findings
|
||||
.iter()
|
||||
.any(|finding| finding.severity == ImportFindingSeverity::Error);
|
||||
let alternatives = &candidate.security_requirements.alternatives;
|
||||
let public = alternatives.is_empty()
|
||||
|| alternatives
|
||||
.iter()
|
||||
.any(|alternative| alternative.supported && alternative.schemes.is_empty());
|
||||
candidate.status = if blocked {
|
||||
"blocked"
|
||||
} else if public {
|
||||
"actionable"
|
||||
} else if alternatives.iter().enumerate().any(|(index, alternative)| {
|
||||
alternative.supported
|
||||
&& choices.iter().any(|choice| {
|
||||
choice.operation_key == candidate.key
|
||||
&& choice.alternative_index == index
|
||||
&& !choice.profiles.is_empty()
|
||||
})
|
||||
}) {
|
||||
"unresolved"
|
||||
} else {
|
||||
"blocked"
|
||||
}
|
||||
.to_owned();
|
||||
}
|
||||
}
|
||||
|
||||
impl AdminService {
|
||||
#[instrument(skip(self, upload), fields(workspace_id = %workspace_id.as_str()))]
|
||||
pub async fn preview_openapi_import(
|
||||
@@ -186,6 +315,20 @@ impl AdminService {
|
||||
detach_guard.detach_now().await;
|
||||
return Err(ApiError::openapi_upload(locale, "no_methods"));
|
||||
}
|
||||
let candidate_count: usize = parsed
|
||||
.preview
|
||||
.groups
|
||||
.iter()
|
||||
.map(|group| group.operations.len())
|
||||
.sum();
|
||||
if candidate_count > MAX_IMPORT_PLAN_KEYS {
|
||||
dependencies.detach_all().await;
|
||||
detach_guard.detach_now().await;
|
||||
return Err(ApiError::validation_with_context(
|
||||
"OpenAPI preview has too many candidates",
|
||||
json!({ "error_code": "openapi_preview_too_many_candidates", "limit": MAX_IMPORT_PLAN_KEYS }),
|
||||
));
|
||||
}
|
||||
let source_envelope = ImportJobSourceEnvelope {
|
||||
source_id,
|
||||
digest: artifact.artifact_ref().clone(),
|
||||
@@ -236,12 +379,19 @@ impl AdminService {
|
||||
let security_profile_choices = self
|
||||
.security_profile_choices(workspace_id, &parsed.preview)
|
||||
.await?;
|
||||
let catalog_names = self
|
||||
.registry
|
||||
.list_import_catalog_names(workspace_id)
|
||||
.await?;
|
||||
let mut response_preview = parsed.preview;
|
||||
response_preview.conflicts = preview_name_conflicts(&response_preview, &catalog_names);
|
||||
attach_preview_readiness(&mut response_preview, &security_profile_choices);
|
||||
Ok(OpenApiImportPreviewResponse {
|
||||
job_id: job_id.as_str().to_owned(),
|
||||
expires_at: expires_at
|
||||
.format(&Rfc3339)
|
||||
.map_err(|error| ApiError::internal(error.to_string()))?,
|
||||
preview: parsed.preview,
|
||||
preview: response_preview,
|
||||
security_profile_choices,
|
||||
})
|
||||
}
|
||||
@@ -542,6 +692,7 @@ fn openapi_import_response(applied: ImportJobApplyResult) -> OpenApiImportCreate
|
||||
operation.name
|
||||
),
|
||||
operation_key: Some(operation.operation_key.clone()),
|
||||
source_location: None,
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
@@ -560,6 +711,7 @@ fn openapi_import_response(applied: ImportJobApplyResult) -> OpenApiImportCreate
|
||||
format!("Операция {name} уже существует и была пропущена.")
|
||||
},
|
||||
operation_key: Some(operation.operation_key.clone()),
|
||||
source_location: None,
|
||||
});
|
||||
OpenApiImportSkippedOperation {
|
||||
operation_key: operation.operation_key.clone(),
|
||||
@@ -658,3 +810,20 @@ fn tool_quality_finding_from_import(finding: &ImportFinding) -> ToolQualityFindi
|
||||
field_path: finding.operation_key.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod preview_name_tests {
|
||||
use super::next_available_name;
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
#[test]
|
||||
fn crowded_catalog_still_gets_a_free_deterministic_proposal() {
|
||||
let mut occupied = (2..=2_002)
|
||||
.map(|index| format!("shared_{index}"))
|
||||
.collect::<BTreeSet<_>>();
|
||||
occupied.insert("shared".to_owned());
|
||||
|
||||
assert_eq!(next_available_name("shared", &mut occupied), "shared_2003");
|
||||
assert_eq!(next_available_name("shared", &mut occupied), "shared_2004");
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
mod validation;
|
||||
|
||||
use super::*;
|
||||
use crank_core::AuthConfig;
|
||||
use crank_import::rest::SecurityRequirementsSource as SecuritySource;
|
||||
@@ -61,6 +63,7 @@ impl AdminService {
|
||||
ImportPreviewResolution::NonFatal(error) => error,
|
||||
})?;
|
||||
let preview = &resolved.preview;
|
||||
let catalog_revision = self.registry.import_catalog_revision(workspace_id).await?;
|
||||
|
||||
// Stored external dependency snapshots are re-verified for every
|
||||
// replay variant (including historically persisted ones) without a
|
||||
@@ -81,6 +84,12 @@ impl AdminService {
|
||||
.map(|operation| (operation.key.clone(), operation))
|
||||
.collect();
|
||||
let all_set: BTreeSet<String> = candidates.keys().cloned().collect();
|
||||
if all_set.len() > MAX_IMPORT_PLAN_KEYS {
|
||||
return Err(ApiError::validation_with_context(
|
||||
"too many candidates for an import plan",
|
||||
json!({ "error_code": "import_plan_invalid_input" }),
|
||||
));
|
||||
}
|
||||
let selected = resolve_plan_selection(
|
||||
&payload.selection_mode,
|
||||
&payload.selected_operation_keys,
|
||||
@@ -100,7 +109,31 @@ impl AdminService {
|
||||
.filter(|key| !selected.contains(*key))
|
||||
.cloned()
|
||||
.collect();
|
||||
let decisions = validate_plan_decisions(&payload.decisions, &selected)?;
|
||||
let excluded_reasons = excluded
|
||||
.iter()
|
||||
.map(|key| {
|
||||
let reason = preview
|
||||
.findings
|
||||
.iter()
|
||||
.find(|finding| {
|
||||
finding.severity == ImportFindingSeverity::Error
|
||||
&& finding.operation_key.is_none()
|
||||
})
|
||||
.map(|finding| finding.code.clone())
|
||||
.or_else(|| {
|
||||
candidates.get(key).and_then(|candidate| {
|
||||
candidate
|
||||
.findings
|
||||
.iter()
|
||||
.find(|finding| finding.severity == ImportFindingSeverity::Error)
|
||||
.map(|finding| finding.code.clone())
|
||||
})
|
||||
})
|
||||
.unwrap_or_else(|| "not_selected".to_owned());
|
||||
(key.clone(), reason)
|
||||
})
|
||||
.collect::<BTreeMap<_, _>>();
|
||||
let decisions = validation::validate_plan_decisions(&payload.decisions, &selected)?;
|
||||
|
||||
let global_blocker = preview
|
||||
.findings
|
||||
@@ -144,6 +177,15 @@ impl AdminService {
|
||||
.count(),
|
||||
blocked: planned.iter().filter(|candidate| candidate.blocked).count(),
|
||||
};
|
||||
if payload.selection_mode == "partial" && counts.blocked != 0 {
|
||||
return Err(ApiError::validation_with_context(
|
||||
"partial selection includes blocked candidates",
|
||||
json!({ "error_code": "import_plan_partial_blocked" }),
|
||||
));
|
||||
}
|
||||
if self.registry.import_catalog_revision(workspace_id).await? != catalog_revision {
|
||||
return Err(import_plan_repreview_error("import_plan_catalog_stale"));
|
||||
}
|
||||
|
||||
// Server-authored immutable plan. Client-supplied candidate material,
|
||||
// digests or payload fields are not read: every value below derives
|
||||
@@ -157,7 +199,7 @@ impl AdminService {
|
||||
});
|
||||
let plan_reference = new_prefixed_id("plan");
|
||||
let plan_payload = json!({
|
||||
"schema_version": "import-plan-v1",
|
||||
"schema_version": "import-plan-v2",
|
||||
"plan_reference": plan_reference,
|
||||
"job_id": job_id.as_str(),
|
||||
"server_url": payload.server_url,
|
||||
@@ -167,12 +209,25 @@ impl AdminService {
|
||||
},
|
||||
"dependencies": job.preview_payload.get("dependencies").cloned().unwrap_or(json!([])),
|
||||
"fingerprint": resolved.fingerprint,
|
||||
"catalog_revision": catalog_revision,
|
||||
"support_matrix_version": OPENAPI_SUPPORT_MATRIX_VERSION,
|
||||
"findings": {
|
||||
"document": preview.findings,
|
||||
"candidates": candidates.iter().map(|(key, candidate)| json!({
|
||||
"key": key,
|
||||
"findings": candidate.findings,
|
||||
})).collect::<Vec<_>>(),
|
||||
},
|
||||
"selection": {
|
||||
"mode": payload.selection_mode,
|
||||
"all": all_keys,
|
||||
"selected": selected_keys,
|
||||
"excluded": excluded,
|
||||
},
|
||||
"excluded_reasons": excluded_reasons.iter().map(|(key, reason)| json!({
|
||||
"key": key,
|
||||
"reason": reason,
|
||||
})).collect::<Vec<_>>(),
|
||||
"counts": plan_counts_value,
|
||||
"candidates": planned.iter().map(plan_candidate_payload).collect::<Result<Vec<_>, _>>()?,
|
||||
});
|
||||
@@ -224,13 +279,15 @@ impl AdminService {
|
||||
confirmed_name: None,
|
||||
auth_profile_id: None,
|
||||
security_alternative_index: None,
|
||||
reason: Some("not_selected".to_owned()),
|
||||
reason: excluded_reasons.get(key).cloned(),
|
||||
}
|
||||
}));
|
||||
|
||||
Ok(OpenApiImportPlanCreateResponse {
|
||||
plan_reference: created.id.as_str().to_owned(),
|
||||
plan_digest: created.digest,
|
||||
catalog_revision,
|
||||
support_matrix_version: OPENAPI_SUPPORT_MATRIX_VERSION.to_owned(),
|
||||
expires_at: created
|
||||
.expires_at
|
||||
.format(&Rfc3339)
|
||||
@@ -703,133 +760,6 @@ fn resolve_plan_selection(
|
||||
}
|
||||
}
|
||||
|
||||
/// Binds decisions to selected candidates with action-specific field rules:
|
||||
/// unknown/duplicate keys, foreign fields and oversized references are
|
||||
/// rejected before the plan is materialized.
|
||||
fn validate_plan_decisions<'input>(
|
||||
decisions: &'input [OpenApiImportPlanDecision],
|
||||
selected: &BTreeSet<String>,
|
||||
) -> Result<BTreeMap<String, &'input OpenApiImportPlanDecision>, ApiError> {
|
||||
let invalid = |context: Value| {
|
||||
Err(ApiError::validation_with_context(
|
||||
"import plan decision is invalid",
|
||||
context,
|
||||
))
|
||||
};
|
||||
let mut mapping = BTreeMap::new();
|
||||
for decision in decisions {
|
||||
if decision.operation_key.is_empty()
|
||||
|| decision.operation_key.len() > MAX_IMPORT_PLAN_KEY_BYTES
|
||||
{
|
||||
return invalid(json!({
|
||||
"operation_key": decision.operation_key,
|
||||
"error_code": "import_plan_invalid_input"
|
||||
}));
|
||||
}
|
||||
if !matches!(
|
||||
decision.action.as_str(),
|
||||
"create" | "skip" | "rename" | "new_version"
|
||||
) {
|
||||
return invalid(json!({
|
||||
"operation_key": decision.operation_key,
|
||||
"error_code": "import_plan_invalid_input"
|
||||
}));
|
||||
}
|
||||
validate_decision_fields(decision)?;
|
||||
if !selected.contains(&decision.operation_key) {
|
||||
return invalid(json!({
|
||||
"operation_key": decision.operation_key,
|
||||
"error_code": "import_plan_decision_unselected"
|
||||
}));
|
||||
}
|
||||
if mapping
|
||||
.insert(decision.operation_key.clone(), decision)
|
||||
.is_some()
|
||||
{
|
||||
return invalid(json!({
|
||||
"operation_key": decision.operation_key,
|
||||
"error_code": "import_plan_invalid_input"
|
||||
}));
|
||||
}
|
||||
}
|
||||
Ok(mapping)
|
||||
}
|
||||
|
||||
fn validate_decision_fields(decision: &OpenApiImportPlanDecision) -> Result<(), ApiError> {
|
||||
let context = json!({
|
||||
"operation_key": decision.operation_key,
|
||||
"error_code": "import_plan_invalid_input"
|
||||
});
|
||||
let invalid = |message: &str| -> Result<(), ApiError> {
|
||||
Err(ApiError::validation_with_context(message, context.clone()))
|
||||
};
|
||||
if decision
|
||||
.target_operation_id
|
||||
.as_deref()
|
||||
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_ID_REF_BYTES)
|
||||
{
|
||||
return invalid("decision target_operation_id is empty or too long");
|
||||
}
|
||||
if decision
|
||||
.confirmed_name
|
||||
.as_deref()
|
||||
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_NAME_BYTES)
|
||||
{
|
||||
return invalid("decision confirmed_name is empty or too long");
|
||||
}
|
||||
if decision
|
||||
.auth_profile_id
|
||||
.as_deref()
|
||||
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_ID_REF_BYTES)
|
||||
{
|
||||
return invalid("decision auth_profile_id is empty or too long");
|
||||
}
|
||||
if decision
|
||||
.security_alternative_index
|
||||
.is_some_and(|index| index > MAX_IMPORT_PLAN_KEYS)
|
||||
{
|
||||
return invalid("decision security_alternative_index is too large");
|
||||
}
|
||||
match decision.action.as_str() {
|
||||
"create" => {
|
||||
if decision.target_operation_id.is_some() || decision.confirmed_name.is_some() {
|
||||
return invalid(
|
||||
"target_operation_id and confirmed_name are not allowed for action create",
|
||||
);
|
||||
}
|
||||
}
|
||||
"skip" => {
|
||||
if decision.auth_profile_id.is_some() || decision.security_alternative_index.is_some() {
|
||||
return invalid("security binding is not allowed for action skip");
|
||||
}
|
||||
if decision.target_operation_id.is_none() {
|
||||
return invalid("action skip requires the exact target_operation_id");
|
||||
}
|
||||
if decision.confirmed_name.is_some() {
|
||||
return invalid("confirmed_name is not allowed for action skip");
|
||||
}
|
||||
}
|
||||
"rename" => {
|
||||
if decision.target_operation_id.is_some() {
|
||||
return invalid("target_operation_id is not allowed for action rename");
|
||||
}
|
||||
if decision.confirmed_name.is_none() {
|
||||
return invalid("action rename requires the exact confirmed_name");
|
||||
}
|
||||
}
|
||||
"new_version" => {
|
||||
if decision.target_operation_id.is_none() {
|
||||
return invalid("action new_version requires the exact target_operation_id");
|
||||
}
|
||||
if decision.confirmed_name.is_some() {
|
||||
return invalid("confirmed_name is not allowed for action new_version");
|
||||
}
|
||||
}
|
||||
_ => unreachable!("decision action is validated before the field rules"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn resolve_security_binding(
|
||||
service: &AdminService,
|
||||
workspace_id: &WorkspaceId,
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
use super::*;
|
||||
|
||||
/// Binds decisions to selected candidates with action-specific field rules:
|
||||
/// unknown/duplicate keys, foreign fields and oversized references are
|
||||
/// rejected before the plan is materialized.
|
||||
pub(super) fn validate_plan_decisions<'input>(
|
||||
decisions: &'input [OpenApiImportPlanDecision],
|
||||
selected: &BTreeSet<String>,
|
||||
) -> Result<BTreeMap<String, &'input OpenApiImportPlanDecision>, ApiError> {
|
||||
let invalid = |context: Value| {
|
||||
Err(ApiError::validation_with_context(
|
||||
"import plan decision is invalid",
|
||||
context,
|
||||
))
|
||||
};
|
||||
let mut mapping = BTreeMap::new();
|
||||
for decision in decisions {
|
||||
if decision.operation_key.is_empty()
|
||||
|| decision.operation_key.len() > MAX_IMPORT_PLAN_KEY_BYTES
|
||||
{
|
||||
return invalid(json!({
|
||||
"operation_key": decision.operation_key,
|
||||
"error_code": "import_plan_invalid_input"
|
||||
}));
|
||||
}
|
||||
if !matches!(
|
||||
decision.action.as_str(),
|
||||
"create" | "skip" | "rename" | "new_version"
|
||||
) {
|
||||
return invalid(json!({
|
||||
"operation_key": decision.operation_key,
|
||||
"error_code": "import_plan_invalid_input"
|
||||
}));
|
||||
}
|
||||
validate_decision_fields(decision)?;
|
||||
if !selected.contains(&decision.operation_key) {
|
||||
return invalid(json!({
|
||||
"operation_key": decision.operation_key,
|
||||
"error_code": "import_plan_decision_unselected"
|
||||
}));
|
||||
}
|
||||
if mapping
|
||||
.insert(decision.operation_key.clone(), decision)
|
||||
.is_some()
|
||||
{
|
||||
return invalid(json!({
|
||||
"operation_key": decision.operation_key,
|
||||
"error_code": "import_plan_invalid_input"
|
||||
}));
|
||||
}
|
||||
}
|
||||
Ok(mapping)
|
||||
}
|
||||
|
||||
fn validate_decision_fields(decision: &OpenApiImportPlanDecision) -> Result<(), ApiError> {
|
||||
let context = json!({
|
||||
"operation_key": decision.operation_key,
|
||||
"error_code": "import_plan_invalid_input"
|
||||
});
|
||||
let invalid = |message: &str| -> Result<(), ApiError> {
|
||||
Err(ApiError::validation_with_context(message, context.clone()))
|
||||
};
|
||||
if decision
|
||||
.target_operation_id
|
||||
.as_deref()
|
||||
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_ID_REF_BYTES)
|
||||
{
|
||||
return invalid("decision target_operation_id is empty or too long");
|
||||
}
|
||||
if decision
|
||||
.confirmed_name
|
||||
.as_deref()
|
||||
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_NAME_BYTES)
|
||||
{
|
||||
return invalid("decision confirmed_name is empty or too long");
|
||||
}
|
||||
if decision
|
||||
.auth_profile_id
|
||||
.as_deref()
|
||||
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_ID_REF_BYTES)
|
||||
{
|
||||
return invalid("decision auth_profile_id is empty or too long");
|
||||
}
|
||||
if decision
|
||||
.security_alternative_index
|
||||
.is_some_and(|index| index > MAX_IMPORT_PLAN_KEYS)
|
||||
{
|
||||
return invalid("decision security_alternative_index is too large");
|
||||
}
|
||||
match decision.action.as_str() {
|
||||
"create" => {
|
||||
if decision.target_operation_id.is_some() || decision.confirmed_name.is_some() {
|
||||
return invalid(
|
||||
"target_operation_id and confirmed_name are not allowed for action create",
|
||||
);
|
||||
}
|
||||
}
|
||||
"skip" => {
|
||||
if decision.auth_profile_id.is_some() || decision.security_alternative_index.is_some() {
|
||||
return invalid("security binding is not allowed for action skip");
|
||||
}
|
||||
if decision.target_operation_id.is_none() {
|
||||
return invalid("action skip requires the exact target_operation_id");
|
||||
}
|
||||
if decision.confirmed_name.is_some() {
|
||||
return invalid("confirmed_name is not allowed for action skip");
|
||||
}
|
||||
}
|
||||
"rename" => {
|
||||
if decision.target_operation_id.is_some() {
|
||||
return invalid("target_operation_id is not allowed for action rename");
|
||||
}
|
||||
if decision.confirmed_name.is_none() {
|
||||
return invalid("action rename requires the exact confirmed_name");
|
||||
}
|
||||
}
|
||||
"new_version" => {
|
||||
if decision.target_operation_id.is_none() {
|
||||
return invalid("action new_version requires the exact target_operation_id");
|
||||
}
|
||||
if decision.confirmed_name.is_some() {
|
||||
return invalid("confirmed_name is not allowed for action new_version");
|
||||
}
|
||||
}
|
||||
_ => unreachable!("decision action is validated before the field rules"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -4,6 +4,160 @@ use crank_registry::{ApplyImportPlanRequest, ImportPlanApplyReport};
|
||||
const MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES: usize = 128;
|
||||
|
||||
impl AdminService {
|
||||
/// Reads an immutable plan and checks its current authority without Apply mutations.
|
||||
pub async fn revalidate_openapi_import_plan(
|
||||
&self,
|
||||
workspace_id: &WorkspaceId,
|
||||
plan_id: &ImportPlanId,
|
||||
) -> Result<OpenApiImportPlanRevalidationResponse, ApiError> {
|
||||
self.ensure_workspace_exists(workspace_id).await?;
|
||||
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" }),
|
||||
)
|
||||
})?;
|
||||
let mut reason = None;
|
||||
let now = OffsetDateTime::now_utc();
|
||||
if plan.expires_at <= now {
|
||||
reason = Some("import_plan_expired");
|
||||
} else if let Some(job) = self
|
||||
.registry
|
||||
.get_import_job(workspace_id, &plan.job_id)
|
||||
.await?
|
||||
{
|
||||
if job.kind != ImportJobKind::OpenApi
|
||||
|| job.status != ImportJobStatus::Pending
|
||||
|| job.expires_at <= now
|
||||
{
|
||||
reason = Some("import_plan_job_terminal");
|
||||
} else {
|
||||
match self
|
||||
.resolve_verified_openapi_preview(workspace_id, &job, OpenApiUploadLocale::En)
|
||||
.await
|
||||
{
|
||||
Ok(resolved) => {
|
||||
let v2 = plan.payload.get("schema_version").and_then(Value::as_str)
|
||||
== Some("import-plan-v2");
|
||||
if self
|
||||
.read_import_job_dependencies(
|
||||
workspace_id,
|
||||
&job.preview_payload,
|
||||
OpenApiUploadLocale::En,
|
||||
)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
reason = Some("import_dependency_verification_failed");
|
||||
} else if !v2 {
|
||||
// Historical plans stay readable for audit, but lack the
|
||||
// catalog evidence required for current Apply authority.
|
||||
reason = Some("importer_version_changed");
|
||||
} else 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)
|
||||
|| plan
|
||||
.payload
|
||||
.get("support_matrix_version")
|
||||
.and_then(Value::as_str)
|
||||
!= Some(OPENAPI_SUPPORT_MATRIX_VERSION)
|
||||
{
|
||||
reason = Some("importer_version_changed");
|
||||
} else if self
|
||||
.registry
|
||||
.revalidate_import_plan(workspace_id, plan_id, &resolved.fingerprint)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
reason = Some("import_plan_revalidation_failed");
|
||||
}
|
||||
}
|
||||
Err(_) => reason = Some("import_preview_revalidation_failed"),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
reason = Some("import_plan_job_unavailable");
|
||||
}
|
||||
|
||||
let counts = &plan.payload["counts"];
|
||||
let count = |key: &str| {
|
||||
counts
|
||||
.get(key)
|
||||
.and_then(Value::as_u64)
|
||||
.and_then(|value| usize::try_from(value).ok())
|
||||
.unwrap_or(0)
|
||||
};
|
||||
let selected = plan.payload["candidates"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(|candidate| {
|
||||
let decision = &candidate["decision"];
|
||||
let string = |value: &Value| value.as_str().map(str::to_owned);
|
||||
OpenApiImportPlanCandidateView {
|
||||
key: string(&candidate["key"]).unwrap_or_default(),
|
||||
name: string(&candidate["name"]).unwrap_or_default(),
|
||||
state: string(&candidate["state"]).unwrap_or_default(),
|
||||
action: string(&decision["action"]),
|
||||
target_operation_id: string(&decision["target_operation_id"]),
|
||||
confirmed_name: string(&decision["confirmed_name"]),
|
||||
auth_profile_id: string(&decision["auth_profile_id"]),
|
||||
security_alternative_index: decision["security_alternative_index"]
|
||||
.as_u64()
|
||||
.and_then(|value| usize::try_from(value).ok()),
|
||||
reason: string(&candidate["reason"]),
|
||||
}
|
||||
});
|
||||
let excluded = plan
|
||||
.payload
|
||||
.pointer("/selection/excluded")
|
||||
.and_then(Value::as_array)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(Value::as_str)
|
||||
.map(|key| OpenApiImportPlanCandidateView {
|
||||
key: key.to_owned(),
|
||||
name: key.to_owned(),
|
||||
state: "excluded".to_owned(),
|
||||
action: None,
|
||||
target_operation_id: None,
|
||||
confirmed_name: None,
|
||||
auth_profile_id: None,
|
||||
security_alternative_index: None,
|
||||
reason: plan.payload["excluded_reasons"]
|
||||
.as_array()
|
||||
.and_then(|items| items.iter().find(|item| item["key"].as_str() == Some(key)))
|
||||
.and_then(|item| item["reason"].as_str())
|
||||
.map(str::to_owned)
|
||||
.or_else(|| Some("not_selected".to_owned())),
|
||||
});
|
||||
Ok(OpenApiImportPlanRevalidationResponse {
|
||||
plan_reference: plan.id.as_str().to_owned(),
|
||||
plan_digest: plan.digest,
|
||||
revalidation_status: if reason.is_some() { "stale" } else { "current" }.to_owned(),
|
||||
revalidation_required: reason.is_some(),
|
||||
reason: reason.map(str::to_owned),
|
||||
counts: OpenApiImportPlanCounts {
|
||||
selected: count("selected"),
|
||||
excluded: count("excluded"),
|
||||
actionable: count("actionable"),
|
||||
blocked: count("blocked"),
|
||||
},
|
||||
candidates: selected.chain(excluded).collect(),
|
||||
})
|
||||
}
|
||||
|
||||
#[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,
|
||||
@@ -85,6 +239,13 @@ impl AdminService {
|
||||
.get("projection_version")
|
||||
.and_then(Value::as_str)
|
||||
!= Some(PROJECTION_VERSION)
|
||||
|| (plan.payload.get("schema_version").and_then(Value::as_str)
|
||||
== Some("import-plan-v2")
|
||||
&& plan
|
||||
.payload
|
||||
.get("support_matrix_version")
|
||||
.and_then(Value::as_str)
|
||||
!= Some(OPENAPI_SUPPORT_MATRIX_VERSION))
|
||||
{
|
||||
return Err(import_plan_repreview_error("importer_version_changed"));
|
||||
}
|
||||
|
||||
@@ -209,6 +209,7 @@ fn import_replay_contract(
|
||||
("normalized-ir-v2", "preview-v2")
|
||||
| ("normalized-ir-v3", "preview-v3")
|
||||
| ("normalized-ir-v4", "preview-v4")
|
||||
| ("normalized-ir-v5", "preview-v5")
|
||||
) {
|
||||
return Err(ApiError::openapi_upload(locale, "source_integrity"));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user