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
@@ -1,4 +1,6 @@
|
||||
use super::*;
|
||||
use crank_core::AuthConfig;
|
||||
use crank_import::rest::SecurityRequirementsSource as SecuritySource;
|
||||
|
||||
impl AdminService {
|
||||
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), job_id = %job_id.as_str()))]
|
||||
@@ -204,6 +206,7 @@ impl AdminService {
|
||||
target_operation_id: candidate.target_operation_id.clone(),
|
||||
confirmed_name: candidate.confirmed_name.clone(),
|
||||
auth_profile_id: candidate.auth_profile_id.clone(),
|
||||
security_alternative_index: candidate.security_alternative_index,
|
||||
reason: candidate.reason.clone(),
|
||||
})
|
||||
.collect();
|
||||
@@ -220,6 +223,7 @@ impl AdminService {
|
||||
target_operation_id: None,
|
||||
confirmed_name: None,
|
||||
auth_profile_id: None,
|
||||
security_alternative_index: None,
|
||||
reason: Some("not_selected".to_owned()),
|
||||
}
|
||||
}));
|
||||
@@ -257,9 +261,12 @@ impl AdminService {
|
||||
decision.and_then(|decision| decision.target_operation_id.clone());
|
||||
let confirmed_name = decision.and_then(|decision| decision.confirmed_name.clone());
|
||||
let auth_profile_id = decision.and_then(|decision| decision.auth_profile_id.clone());
|
||||
let requested_security_alternative =
|
||||
decision.and_then(|decision| decision.security_alternative_index);
|
||||
let mut blocker_reason = blocker_reason;
|
||||
let candidate_name = candidate.draft.name.clone();
|
||||
|
||||
let blocked = blocker_reason.is_some();
|
||||
let mut blocked = blocker_reason.is_some();
|
||||
|
||||
// Explicit skip/new_version targets are always resolved in the
|
||||
// workspace: an unknown target, a name mismatch, or an incompatible
|
||||
@@ -353,6 +360,8 @@ impl AdminService {
|
||||
target_operation_id,
|
||||
confirmed_name,
|
||||
auth_profile_id,
|
||||
security_alternative_index: requested_security_alternative,
|
||||
auth_requirement: None,
|
||||
auth_snapshot: None,
|
||||
target: None,
|
||||
operation: None,
|
||||
@@ -368,17 +377,17 @@ impl AdminService {
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// The final server-authored auth profile reference: the explicit
|
||||
// decision, or the target's reference inherited for an actionable
|
||||
// new_version. Only the reference (never a secret value) is stored.
|
||||
// Use an explicit decision or inherit the target reference for new_version.
|
||||
let mut auth_profile_ref = auth_profile_id
|
||||
.as_deref()
|
||||
.map(|id| AuthProfileId::new(id.to_owned()));
|
||||
let mut inherited_auth_profile = false;
|
||||
if let Some(summary) = target_summary.as_ref()
|
||||
&& !blocked
|
||||
&& action == "new_version"
|
||||
&& auth_profile_ref.is_none()
|
||||
&& (candidate.security_requirements.source == SecuritySource::None
|
||||
|| !candidate.security_requirements.alternatives.is_empty())
|
||||
{
|
||||
let record = self
|
||||
.registry
|
||||
@@ -386,8 +395,65 @@ impl AdminService {
|
||||
.await?
|
||||
.ok_or_else(|| ApiError::internal("current operation version is unavailable"))?;
|
||||
auth_profile_ref = record.snapshot.execution_config.auth_profile_ref;
|
||||
inherited_auth_profile = auth_profile_ref.is_some();
|
||||
}
|
||||
let auth_profile_id = auth_profile_ref.as_ref().map(|id| id.as_str().to_owned());
|
||||
let inherited_unconstrained_auth = inherited_auth_profile
|
||||
&& action == "new_version"
|
||||
&& candidate.security_requirements.source
|
||||
== crank_import::rest::SecurityRequirementsSource::None
|
||||
&& candidate.security_requirements.alternatives.is_empty()
|
||||
&& requested_security_alternative.is_none();
|
||||
let security_alternative_index = if blocked {
|
||||
requested_security_alternative
|
||||
} else if inherited_unconstrained_auth {
|
||||
None
|
||||
} else {
|
||||
resolve_security_binding(
|
||||
self,
|
||||
workspace_id,
|
||||
candidate,
|
||||
auth_profile_ref.as_ref(),
|
||||
requested_security_alternative,
|
||||
&mut blocker_reason,
|
||||
)
|
||||
.await?
|
||||
};
|
||||
blocked = blocker_reason.is_some();
|
||||
let auth_requirement = if !blocked && inherited_unconstrained_auth {
|
||||
Some(json!({ "kind": "unconstrained" }))
|
||||
} else if !blocked && auth_profile_ref.is_some() {
|
||||
security_alternative_index
|
||||
.and_then(|index| candidate.security_requirements.alternatives.get(index))
|
||||
.and_then(|alternative| {
|
||||
(alternative.schemes.len() == 1).then(|| &alternative.schemes[0])
|
||||
})
|
||||
.and_then(|scheme_name| candidate.security_schemes.get(scheme_name))
|
||||
.and_then(|scheme| match &scheme.kind {
|
||||
crank_import::rest::NormalizedSecuritySchemeKind::Http { scheme, .. } => {
|
||||
match scheme.to_ascii_lowercase().as_str() {
|
||||
"bearer" => Some(json!({ "kind": "bearer" })),
|
||||
"basic" => Some(json!({ "kind": "basic" })),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
crank_import::rest::NormalizedSecuritySchemeKind::ApiKey {
|
||||
location: crank_import::rest::SecurityParameterLocation::Header,
|
||||
parameter_name,
|
||||
..
|
||||
} => {
|
||||
Some(json!({ "kind": "api_key_header", "parameter_name": parameter_name }))
|
||||
}
|
||||
crank_import::rest::NormalizedSecuritySchemeKind::ApiKey {
|
||||
location: crank_import::rest::SecurityParameterLocation::Query,
|
||||
parameter_name,
|
||||
..
|
||||
} => Some(json!({ "kind": "api_key_query", "parameter_name": parameter_name })),
|
||||
_ => None,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if blocked {
|
||||
return Ok(PlannedCandidate {
|
||||
@@ -399,6 +465,8 @@ impl AdminService {
|
||||
target_operation_id,
|
||||
confirmed_name,
|
||||
auth_profile_id,
|
||||
security_alternative_index,
|
||||
auth_requirement,
|
||||
auth_snapshot: None,
|
||||
target,
|
||||
operation: None,
|
||||
@@ -415,6 +483,8 @@ impl AdminService {
|
||||
target_operation_id,
|
||||
confirmed_name,
|
||||
auth_profile_id,
|
||||
security_alternative_index,
|
||||
auth_requirement: None,
|
||||
auth_snapshot: None,
|
||||
target,
|
||||
operation: None,
|
||||
@@ -479,6 +549,8 @@ impl AdminService {
|
||||
target_operation_id,
|
||||
confirmed_name,
|
||||
auth_profile_id,
|
||||
security_alternative_index,
|
||||
auth_requirement,
|
||||
auth_snapshot,
|
||||
target,
|
||||
operation: Some(operation),
|
||||
@@ -492,7 +564,12 @@ impl AdminService {
|
||||
workspace_id: &WorkspaceId,
|
||||
profile_id: &AuthProfileId,
|
||||
) -> Result<Value, ApiError> {
|
||||
let unavailable = || import_plan_repreview_error("import_plan_auth_unavailable");
|
||||
let unavailable = || {
|
||||
ApiError::unprocessable_with_context(
|
||||
"The selected authentication profile is unavailable for this import.",
|
||||
json!({ "error_code": "import_plan_auth_unavailable" }),
|
||||
)
|
||||
};
|
||||
let profile = self
|
||||
.registry
|
||||
.get_auth_profile(workspace_id, profile_id)
|
||||
@@ -543,6 +620,8 @@ struct PlannedCandidate {
|
||||
target_operation_id: Option<String>,
|
||||
confirmed_name: Option<String>,
|
||||
auth_profile_id: Option<String>,
|
||||
security_alternative_index: Option<usize>,
|
||||
auth_requirement: Option<Value>,
|
||||
auth_snapshot: Option<Value>,
|
||||
target: Option<Value>,
|
||||
operation: Option<RegistryOperation>,
|
||||
@@ -705,6 +784,12 @@ fn validate_decision_fields(decision: &OpenApiImportPlanDecision) -> Result<(),
|
||||
{
|
||||
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() {
|
||||
@@ -714,8 +799,8 @@ fn validate_decision_fields(decision: &OpenApiImportPlanDecision) -> Result<(),
|
||||
}
|
||||
}
|
||||
"skip" => {
|
||||
if decision.auth_profile_id.is_some() {
|
||||
return invalid("auth_profile_id is not allowed for action 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");
|
||||
@@ -745,6 +830,138 @@ fn validate_decision_fields(decision: &OpenApiImportPlanDecision) -> Result<(),
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn resolve_security_binding(
|
||||
service: &AdminService,
|
||||
workspace_id: &WorkspaceId,
|
||||
candidate: &ImportOperationCandidate,
|
||||
profile_ref: Option<&AuthProfileId>,
|
||||
requested_index: Option<usize>,
|
||||
blocker_reason: &mut Option<String>,
|
||||
) -> Result<Option<usize>, ApiError> {
|
||||
let alternatives = &candidate.security_requirements.alternatives;
|
||||
if alternatives.is_empty() {
|
||||
if profile_ref.is_some() || requested_index.is_some() {
|
||||
return Err(ApiError::validation_with_context(
|
||||
"security binding was supplied for an operation with no security requirement",
|
||||
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
|
||||
));
|
||||
}
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let public_index = alternatives
|
||||
.iter()
|
||||
.position(|alternative| alternative.supported && alternative.schemes.is_empty());
|
||||
let compatible = if let Some(profile_ref) = profile_ref {
|
||||
let profile = service
|
||||
.registry
|
||||
.get_auth_profile(workspace_id, profile_ref)
|
||||
.await?
|
||||
.ok_or_else(|| {
|
||||
ApiError::unprocessable_with_context(
|
||||
"selected Auth Profile is unavailable in this workspace",
|
||||
json!({ "error_code": "import_plan_auth_unavailable" }),
|
||||
)
|
||||
})?;
|
||||
let compatible = alternatives
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, alternative)| {
|
||||
alternative.supported
|
||||
&& alternative.schemes.len() == 1
|
||||
&& candidate
|
||||
.security_schemes
|
||||
.get(&alternative.schemes[0])
|
||||
.is_some_and(|scheme| auth_config_matches_scheme(&profile.config, scheme))
|
||||
})
|
||||
.map(|(index, _)| index)
|
||||
.collect::<Vec<_>>();
|
||||
Some((profile, compatible))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if let Some((_, compatible_indices)) = compatible.as_ref() {
|
||||
let selected = match requested_index {
|
||||
Some(index) if compatible_indices.contains(&index) => index,
|
||||
Some(_) => {
|
||||
return Err(ApiError::validation_with_context(
|
||||
"selected Auth Profile is incompatible with the selected security alternative",
|
||||
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
|
||||
));
|
||||
}
|
||||
None if compatible_indices.len() == 1 => compatible_indices[0],
|
||||
None if compatible_indices.is_empty() => {
|
||||
return Err(ApiError::validation_with_context(
|
||||
"selected Auth Profile is incompatible with the source security schemes",
|
||||
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
|
||||
));
|
||||
}
|
||||
None => {
|
||||
return Err(ApiError::validation_with_context(
|
||||
"security_alternative_index is required when a profile matches multiple alternatives",
|
||||
json!({ "operation_key": candidate.key, "error_code": "import_plan_security_alternative_required" }),
|
||||
));
|
||||
}
|
||||
};
|
||||
return Ok(Some(selected));
|
||||
}
|
||||
|
||||
if let Some(index) = requested_index {
|
||||
if alternatives
|
||||
.get(index)
|
||||
.is_none_or(|alternative| !alternative.supported)
|
||||
{
|
||||
return Err(ApiError::validation_with_context(
|
||||
"selected security alternative is unsupported",
|
||||
json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }),
|
||||
));
|
||||
}
|
||||
if alternatives[index].schemes.is_empty() {
|
||||
return Ok(Some(index));
|
||||
}
|
||||
}
|
||||
if let Some(public_index) = public_index
|
||||
&& requested_index.is_none()
|
||||
{
|
||||
return Ok(Some(public_index));
|
||||
}
|
||||
*blocker_reason = Some("auth_profile_required".to_owned());
|
||||
Ok(requested_index)
|
||||
}
|
||||
|
||||
pub(super) fn auth_config_matches_scheme(
|
||||
config: &AuthConfig,
|
||||
scheme: &crank_import::rest::NormalizedSecurityScheme,
|
||||
) -> bool {
|
||||
use crank_import::rest::{NormalizedSecuritySchemeKind, SecurityParameterLocation};
|
||||
match (&scheme.kind, config) {
|
||||
(NormalizedSecuritySchemeKind::Http { scheme, .. }, AuthConfig::Bearer(_)) => {
|
||||
scheme.eq_ignore_ascii_case("bearer")
|
||||
}
|
||||
(NormalizedSecuritySchemeKind::Http { scheme, .. }, AuthConfig::Basic(_)) => {
|
||||
scheme.eq_ignore_ascii_case("basic")
|
||||
}
|
||||
(
|
||||
NormalizedSecuritySchemeKind::ApiKey {
|
||||
location: SecurityParameterLocation::Header,
|
||||
parameter_name,
|
||||
..
|
||||
},
|
||||
AuthConfig::ApiKeyHeader(config),
|
||||
) => parameter_name.eq_ignore_ascii_case(&config.header_name),
|
||||
(
|
||||
NormalizedSecuritySchemeKind::ApiKey {
|
||||
location: SecurityParameterLocation::Query,
|
||||
parameter_name,
|
||||
..
|
||||
},
|
||||
AuthConfig::ApiKeyQuery(config),
|
||||
) => parameter_name == &config.param_name,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Serializes one planned candidate into the versioned plan payload: the
|
||||
/// redacted decision, the bounded rejection reason (if any), the exact
|
||||
/// authorized target with expected state and, for mutating actions, the full
|
||||
@@ -759,6 +976,7 @@ fn plan_candidate_payload(candidate: &PlannedCandidate) -> Result<Value, ApiErro
|
||||
"target_operation_id": candidate.target_operation_id,
|
||||
"confirmed_name": candidate.confirmed_name,
|
||||
"auth_profile_id": candidate.auth_profile_id,
|
||||
"security_alternative_index": candidate.security_alternative_index,
|
||||
},
|
||||
});
|
||||
if let Some(reason) = &candidate.reason {
|
||||
@@ -770,6 +988,9 @@ fn plan_candidate_payload(candidate: &PlannedCandidate) -> Result<Value, ApiErro
|
||||
if let Some(auth_snapshot) = &candidate.auth_snapshot {
|
||||
object["auth_snapshot"] = auth_snapshot.clone();
|
||||
}
|
||||
if let Some(auth_requirement) = &candidate.auth_requirement {
|
||||
object["auth_requirement"] = auth_requirement.clone();
|
||||
}
|
||||
if let Some(operation) = &candidate.operation {
|
||||
let value = serde_json::to_value(operation)
|
||||
.map_err(|error| ApiError::internal(error.to_string()))?;
|
||||
|
||||
Reference in new issue
Block a user