story 2-10-security-schemes-secrets: implemented and reviewed via bmad-loop
This commit is contained in:
1 parent
f5f40cb167
commit
ecfb4e788f
45 files changed
+6025
-829
No files matched your search
@@ -3,11 +3,11 @@ use std::collections::{BTreeMap, BTreeSet};
|
||||
use crank_artifacts::{ArtifactError, MAX_ARTIFACT_BYTES};
|
||||
use crank_core::{
|
||||
AuthProfileId, ExecutionConfig, OperationId, OperationSecurityLevel, OperationStatus, Protocol,
|
||||
ToolQualityFinding, ToolQualitySeverity, WorkspaceId,
|
||||
SecretStatus, ToolQualityFinding, ToolQualitySeverity, WorkspaceId,
|
||||
};
|
||||
use crank_import::rest::{
|
||||
ImportFinding, ImportFindingSeverity, ImportOperationCandidate, NORMALIZER_VERSION,
|
||||
PROJECTION_VERSION, operation_draft_from_candidate,
|
||||
PROJECTION_VERSION, operation_draft_from_candidate, validate_document_source_safe,
|
||||
};
|
||||
use crank_registry::{
|
||||
ApplyImportJobRequest, ArtifactSourceId, ArtifactSourceSensitivity,
|
||||
@@ -24,12 +24,12 @@ use tracing::{info, instrument};
|
||||
use crate::{
|
||||
error::ApiError,
|
||||
service::{
|
||||
AdminService, OpenApiImportCreatePayload, OpenApiImportCreateResponse,
|
||||
OpenApiImportCreatedOperation, OpenApiImportPlanApplyPayload,
|
||||
AdminService, OpenApiImportAuthProfileChoice, OpenApiImportCreatePayload,
|
||||
OpenApiImportCreateResponse, OpenApiImportCreatedOperation, OpenApiImportPlanApplyPayload,
|
||||
OpenApiImportPlanCandidateView, OpenApiImportPlanCounts, OpenApiImportPlanCreatePayload,
|
||||
OpenApiImportPlanCreateResponse, OpenApiImportPlanDecision, OpenApiImportPreviewResponse,
|
||||
OpenApiImportSkippedOperation, OpenApiUpload, OpenApiUploadLocale, OperationPayload,
|
||||
new_prefixed_id,
|
||||
OpenApiImportSecurityProfileChoice, OpenApiImportSkippedOperation, OpenApiUpload,
|
||||
OpenApiUploadLocale, OperationPayload, new_prefixed_id,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -71,20 +71,52 @@ impl AdminService {
|
||||
|
||||
validate_openapi_upload(&upload)?;
|
||||
let OpenApiUpload {
|
||||
bytes,
|
||||
bytes: raw_bytes,
|
||||
mime_type,
|
||||
locale,
|
||||
} = upload;
|
||||
let store = self.artifact_store.clone();
|
||||
let artifact = tokio::task::spawn_blocking(move || store.put_registered(&bytes))
|
||||
.await
|
||||
.map_err(|_| ApiError::openapi_upload(locale, "storage_unavailable"))?
|
||||
.map_err(|error| artifact_error(locale, error))?;
|
||||
let raw_document = std::str::from_utf8(&raw_bytes)
|
||||
.map_err(|_| ApiError::openapi_upload(locale, "invalid_utf8"))?;
|
||||
validate_document_source_safe(raw_document, &self.external_reference_normalization)
|
||||
.map_err(|_| ApiError::openapi_upload(locale, "invalid_document"))?;
|
||||
let now = OffsetDateTime::now_utc();
|
||||
let mut dependencies = self
|
||||
.materialize_external_reference_snapshots(workspace_id, &raw_bytes, now)
|
||||
.await;
|
||||
if dependencies.unsafe_source {
|
||||
return Err(ApiError::openapi_upload(locale, "invalid_document"));
|
||||
}
|
||||
let source_digest =
|
||||
crank_import::rest::SourceDigest::parse(format!("{:x}", Sha256::digest(&raw_bytes)))
|
||||
.map_err(|_| ApiError::openapi_upload(locale, "invalid_document"))?;
|
||||
if crank_import::rest::validate_resolved_bundle_source_safe(
|
||||
raw_document,
|
||||
source_digest,
|
||||
&dependencies.snapshots,
|
||||
&self.external_reference_normalization,
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
dependencies.detach_all().await;
|
||||
return Err(ApiError::openapi_upload(locale, "invalid_document"));
|
||||
}
|
||||
let bytes = raw_bytes;
|
||||
let store = self.artifact_store.clone();
|
||||
let artifact_result = tokio::task::spawn_blocking(move || store.put_registered(&bytes))
|
||||
.await
|
||||
.map_err(|_| ApiError::openapi_upload(locale, "storage_unavailable"))
|
||||
.and_then(|result| result.map_err(|error| artifact_error(locale, error)));
|
||||
let artifact = match artifact_result {
|
||||
Ok(artifact) => artifact,
|
||||
Err(error) => {
|
||||
dependencies.detach_all().await;
|
||||
return Err(error);
|
||||
}
|
||||
};
|
||||
let expires_at = now + Duration::hours(IMPORT_JOB_TTL_HOURS);
|
||||
let job_id = ImportJobId::new(new_prefixed_id("imp"));
|
||||
let source_id = ArtifactSourceId::new(new_prefixed_id("src_openapi"));
|
||||
let source = self
|
||||
let source_result = self
|
||||
.registry
|
||||
.create_artifact_source(CreateArtifactSourceRequest {
|
||||
workspace_id,
|
||||
@@ -94,7 +126,14 @@ impl AdminService {
|
||||
sensitivity: ArtifactSourceSensitivity::Internal,
|
||||
created_at: now,
|
||||
})
|
||||
.await?;
|
||||
.await;
|
||||
let source = match source_result {
|
||||
Ok(source) => source,
|
||||
Err(error) => {
|
||||
dependencies.detach_all().await;
|
||||
return Err(ApiError::from(error));
|
||||
}
|
||||
};
|
||||
let mut detach_guard = SourceDetachGuard::new(
|
||||
self.registry.clone(),
|
||||
workspace_id.clone(),
|
||||
@@ -120,9 +159,6 @@ impl AdminService {
|
||||
detach_guard.detach_now().await;
|
||||
return Err(ApiError::openapi_upload(locale, "source_integrity"));
|
||||
}
|
||||
let mut dependencies = self
|
||||
.materialize_external_reference_snapshots(workspace_id, &verified.bytes, now)
|
||||
.await;
|
||||
let parsed = match parse_verified_preview(
|
||||
verified.bytes,
|
||||
artifact.artifact_ref().digest_hex().to_owned(),
|
||||
@@ -197,15 +233,92 @@ impl AdminService {
|
||||
dependencies.disarm();
|
||||
detach_guard.disarm();
|
||||
|
||||
let security_profile_choices = self
|
||||
.security_profile_choices(workspace_id, &parsed.preview)
|
||||
.await?;
|
||||
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,
|
||||
security_profile_choices,
|
||||
})
|
||||
}
|
||||
|
||||
async fn security_profile_choices(
|
||||
&self,
|
||||
workspace_id: &WorkspaceId,
|
||||
preview: &crank_import::rest::ImportPreview,
|
||||
) -> Result<Vec<OpenApiImportSecurityProfileChoice>, ApiError> {
|
||||
let profiles = self.registry.list_auth_profiles(workspace_id).await?;
|
||||
let mut available = Vec::new();
|
||||
for profile in profiles {
|
||||
if profile.kind != profile.config.kind() {
|
||||
continue;
|
||||
}
|
||||
let mut usable = true;
|
||||
for secret_id in profile.config.secret_ids() {
|
||||
let Some(secret) = self.registry.get_secret(workspace_id, secret_id).await? else {
|
||||
usable = false;
|
||||
break;
|
||||
};
|
||||
if secret.secret.status != SecretStatus::Active
|
||||
|| self
|
||||
.registry
|
||||
.get_current_secret_version(workspace_id, secret_id)
|
||||
.await?
|
||||
.is_none()
|
||||
{
|
||||
usable = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if usable {
|
||||
available.push(profile);
|
||||
}
|
||||
}
|
||||
|
||||
let mut choices = Vec::new();
|
||||
for operation in preview
|
||||
.groups
|
||||
.iter()
|
||||
.flat_map(|group| group.operations.iter())
|
||||
{
|
||||
for (alternative_index, alternative) in operation
|
||||
.security_requirements
|
||||
.alternatives
|
||||
.iter()
|
||||
.enumerate()
|
||||
{
|
||||
if !alternative.supported || alternative.schemes.len() != 1 {
|
||||
continue;
|
||||
}
|
||||
let Some(scheme) = operation.security_schemes.get(&alternative.schemes[0]) else {
|
||||
continue;
|
||||
};
|
||||
let profiles = available
|
||||
.iter()
|
||||
.filter(|profile| {
|
||||
import_plan::auth_config_matches_scheme(&profile.config, scheme)
|
||||
})
|
||||
.map(|profile| OpenApiImportAuthProfileChoice {
|
||||
id: profile.id.as_str().to_owned(),
|
||||
name: profile.name.clone(),
|
||||
kind: profile.kind,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
choices.push(OpenApiImportSecurityProfileChoice {
|
||||
operation_key: operation.key.clone(),
|
||||
alternative_index,
|
||||
schemes: alternative.schemes.clone(),
|
||||
profiles,
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(choices)
|
||||
}
|
||||
|
||||
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), job_id = %job_id.as_str()))]
|
||||
pub async fn create_openapi_import(
|
||||
&self,
|
||||
|
||||
Reference in new issue
Block a user