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,6 +1,7 @@
|
||||
use crank_core::HttpMethod;
|
||||
use serde_json::Value;
|
||||
|
||||
use super::parser_findings::append_unused_security_scheme_findings as append_unused;
|
||||
use crate::rest::{
|
||||
model::{
|
||||
ImportFinding, RestImportDocument, RestImportOperation, RestImportParameter,
|
||||
@@ -15,6 +16,7 @@ pub fn parse_document(root: &Value) -> Result<RestImportDocument, ImportParseErr
|
||||
}
|
||||
|
||||
fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseError> {
|
||||
let security_schemes = super::security::schemes(root, false);
|
||||
let version = root
|
||||
.get("openapi")
|
||||
.and_then(Value::as_str)
|
||||
@@ -25,20 +27,16 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
|
||||
.unwrap_or("Imported API")
|
||||
.to_owned();
|
||||
let mut findings = Vec::new();
|
||||
let mut internal_finding_locations = Vec::new();
|
||||
let mut locations = Vec::new();
|
||||
let servers = parse_servers(root.get("servers"), "/servers");
|
||||
append_findings_with_locations(
|
||||
&mut findings,
|
||||
&mut internal_finding_locations,
|
||||
servers.findings,
|
||||
);
|
||||
super::parser_findings::append_located(&mut findings, &mut locations, servers.findings);
|
||||
let servers = servers.servers;
|
||||
if servers.is_empty() {
|
||||
findings.push(document_finding(
|
||||
"missing_servers",
|
||||
"В документе не указаны servers, base URL нужно будет выбрать вручную.",
|
||||
));
|
||||
internal_finding_locations.push(Some(crate::rest::model::SourceLocation {
|
||||
locations.push(Some(crate::rest::model::SourceLocation {
|
||||
pointer: String::new(),
|
||||
}));
|
||||
} else if servers.len() > 1 {
|
||||
@@ -46,7 +44,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
|
||||
"multiple_servers",
|
||||
"В документе несколько servers, при импорте нужно выбрать нужный base URL.",
|
||||
));
|
||||
internal_finding_locations.push(Some(crate::rest::model::SourceLocation {
|
||||
locations.push(Some(crate::rest::model::SourceLocation {
|
||||
pointer: "/servers".to_owned(),
|
||||
}));
|
||||
}
|
||||
@@ -60,7 +58,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
|
||||
for (path, path_item) in paths {
|
||||
if !path_item.is_object() {
|
||||
findings.push(super::http_methods::invalid_path_item());
|
||||
internal_finding_locations.push(Some(crate::rest::model::SourceLocation {
|
||||
locations.push(Some(crate::rest::model::SourceLocation {
|
||||
pointer: format!("/paths/{}", path.replace('~', "~0").replace('/', "~1")),
|
||||
}));
|
||||
continue;
|
||||
@@ -71,7 +69,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
|
||||
"unsupported_http_method",
|
||||
format!("Метод {method_name} для пути {path} пока не поддерживается."),
|
||||
));
|
||||
internal_finding_locations.push(Some(crate::rest::model::SourceLocation {
|
||||
locations.push(Some(crate::rest::model::SourceLocation {
|
||||
pointer: format!(
|
||||
"/paths/{}/{}",
|
||||
path.replace('~', "~0").replace('/', "~1"),
|
||||
@@ -85,17 +83,17 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
|
||||
path_item.get("parameters"),
|
||||
&format!("{path_pointer}/parameters"),
|
||||
);
|
||||
append_findings_with_locations(
|
||||
super::parser_findings::append_located(
|
||||
&mut findings,
|
||||
&mut internal_finding_locations,
|
||||
&mut locations,
|
||||
path_parameters.findings,
|
||||
);
|
||||
let path_parameters = path_parameters.parameters;
|
||||
let path_servers =
|
||||
parse_servers(path_item.get("servers"), &format!("{path_pointer}/servers"));
|
||||
append_findings_with_locations(
|
||||
super::parser_findings::append_located(
|
||||
&mut findings,
|
||||
&mut internal_finding_locations,
|
||||
&mut locations,
|
||||
path_servers.findings,
|
||||
);
|
||||
let path_servers = path_servers.servers;
|
||||
@@ -108,7 +106,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
|
||||
"invalid_operation",
|
||||
"Операция имеет неверную структуру и не была интерпретирована.",
|
||||
));
|
||||
internal_finding_locations.push(Some(crate::rest::model::SourceLocation {
|
||||
locations.push(Some(crate::rest::model::SourceLocation {
|
||||
pointer: format!(
|
||||
"/paths/{}/{}",
|
||||
path.replace('~', "~0").replace('/', "~1"),
|
||||
@@ -127,9 +125,9 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
|
||||
operation_value.get("parameters"),
|
||||
&format!("{path_pointer}/{method_name}/parameters"),
|
||||
);
|
||||
append_findings_with_locations(
|
||||
super::parser_findings::append_located(
|
||||
&mut findings,
|
||||
&mut internal_finding_locations,
|
||||
&mut locations,
|
||||
parsed_parameters.findings,
|
||||
);
|
||||
operation_parameters.extend(parsed_parameters.parameters);
|
||||
@@ -138,9 +136,9 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
|
||||
operation_value.get("servers"),
|
||||
&format!("{operation_pointer}/servers"),
|
||||
);
|
||||
append_findings_with_locations(
|
||||
super::parser_findings::append_located(
|
||||
&mut findings,
|
||||
&mut internal_finding_locations,
|
||||
&mut locations,
|
||||
operation_servers.findings,
|
||||
);
|
||||
let operation_servers = operation_servers.servers;
|
||||
@@ -148,20 +146,23 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
|
||||
operation_value.get("tags"),
|
||||
&format!("{operation_pointer}/tags"),
|
||||
);
|
||||
append_findings_with_locations(
|
||||
&mut findings,
|
||||
&mut internal_finding_locations,
|
||||
tags.findings,
|
||||
);
|
||||
super::parser_findings::append_located(&mut findings, &mut locations, tags.findings);
|
||||
|
||||
let mut located_operation_findings =
|
||||
super::openapi3_findings::operation(operation_value, &operation_pointer);
|
||||
let (security_requirements, security_findings) =
|
||||
super::security::collect_operation_requirements(
|
||||
root,
|
||||
operation_value,
|
||||
&operation_pointer,
|
||||
&security_schemes,
|
||||
);
|
||||
located_operation_findings.extend(security_findings);
|
||||
located_operation_findings.extend(super::openapi3_findings::path_parameters(
|
||||
path,
|
||||
&operation_parameters,
|
||||
));
|
||||
let (operation_findings, internal_finding_locations) =
|
||||
located_operation_findings.into_iter().unzip();
|
||||
let (operation_findings, locations) = located_operation_findings.into_iter().unzip();
|
||||
let response_schema = (method != HttpMethod::Head)
|
||||
.then(|| response_schema(operation_value, &operation_pointer))
|
||||
.flatten();
|
||||
@@ -169,6 +170,7 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
|
||||
key: format!("{} {}", method_name.to_uppercase(), path),
|
||||
method,
|
||||
path: path.clone(),
|
||||
security_requirements,
|
||||
operation_id: operation_value
|
||||
.get("operationId")
|
||||
.and_then(Value::as_str)
|
||||
@@ -200,19 +202,20 @@ fn parse_document_v2(root: &Value) -> Result<RestImportDocument, ImportParseErro
|
||||
operation_servers
|
||||
},
|
||||
findings: operation_findings,
|
||||
internal_finding_locations,
|
||||
internal_finding_locations: locations,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
append_unused(root, &security_schemes, &mut findings, &mut locations);
|
||||
Ok(RestImportDocument {
|
||||
format: "openapi".to_owned(),
|
||||
version,
|
||||
title,
|
||||
servers,
|
||||
security_schemes,
|
||||
operations,
|
||||
findings,
|
||||
internal_finding_locations,
|
||||
internal_finding_locations: locations,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -277,6 +280,7 @@ pub fn parse_document_legacy_v1(root: &Value) -> Result<RestImportDocument, Impo
|
||||
key: format!("{} {}", method_name.to_uppercase(), path),
|
||||
method,
|
||||
path: path.clone(),
|
||||
security_requirements: Default::default(),
|
||||
operation_id: operation
|
||||
.get("operationId")
|
||||
.and_then(Value::as_str)
|
||||
@@ -308,6 +312,7 @@ pub fn parse_document_legacy_v1(root: &Value) -> Result<RestImportDocument, Impo
|
||||
version,
|
||||
title,
|
||||
servers,
|
||||
security_schemes: Default::default(),
|
||||
operations,
|
||||
findings,
|
||||
internal_finding_locations: Vec::new(),
|
||||
@@ -700,17 +705,6 @@ fn parameter_blocker(
|
||||
)
|
||||
}
|
||||
|
||||
fn append_findings_with_locations(
|
||||
findings: &mut Vec<ImportFinding>,
|
||||
locations: &mut Vec<Option<crate::rest::model::SourceLocation>>,
|
||||
parameter_findings: Vec<(ImportFinding, crate::rest::model::SourceLocation)>,
|
||||
) {
|
||||
for (finding, location) in parameter_findings {
|
||||
findings.push(finding);
|
||||
locations.push(Some(location));
|
||||
}
|
||||
}
|
||||
|
||||
fn request_body_schema(
|
||||
operation: &Value,
|
||||
operation_pointer: &str,
|
||||
|
||||
Reference in new issue
Block a user