Skip to content

Commit 6b9d483

Browse files
committed
fix: reinstate original /v2/vulnerability/analyze and introduce /v3/vulnerability/analyze
1 parent 4812166 commit 6b9d483

File tree

6 files changed

+450
-8
lines changed

6 files changed

+450
-8
lines changed

modules/fundamental/src/vulnerability/endpoints/mod.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use crate::{
55
db::DatabaseExt,
66
endpoints::Deprecation,
77
vulnerability::{
8-
model::{AnalysisRequest, AnalysisResponse, VulnerabilityDetails, VulnerabilitySummary},
8+
model::{
9+
AnalysisRequest, AnalysisResponse, VulnerabilityDetails, VulnerabilitySummary,
10+
v2::AnalysisResponseV2,
11+
},
912
service::VulnerabilityService,
1013
},
1114
};
@@ -27,7 +30,8 @@ pub fn configure(config: &mut utoipa_actix_web::service_config::ServiceConfig, d
2730
.app_data(web::Data::new(db))
2831
.service(all)
2932
.service(get)
30-
.service(analyze);
33+
.service(analyze)
34+
.service(analyze_v3);
3135
}
3236

3337
#[allow(dead_code)]
@@ -107,7 +111,7 @@ pub async fn get(
107111
tag = "vulnerability",
108112
request_body = AnalysisRequest,
109113
responses(
110-
(status = 200, description = "Analyze the provided purls to search for known vulnerabilities", body = AnalysisResponse),
114+
(status = 200, description = "Analyze the provided purls to search for known vulnerabilities", body = AnalysisResponseV2),
111115
),
112116
)]
113117
#[post("/v2/vulnerability/analyze")]
@@ -117,6 +121,27 @@ pub async fn analyze(
117121
db: web::Data<Database>,
118122
web::Json(AnalysisRequest { purls }): web::Json<AnalysisRequest>,
119123
_: Require<ReadAdvisory>,
124+
) -> actix_web::Result<impl Responder> {
125+
let tx = db.begin_read().await?;
126+
let details = service.analyze_purls_v2(purls, &tx).await?;
127+
128+
Ok(HttpResponse::Ok().json(details))
129+
}
130+
131+
#[utoipa::path(
132+
operation_id = "analyze_v3",
133+
tag = "vulnerability",
134+
request_body = AnalysisRequest,
135+
responses(
136+
(status = 200, description = "Analyze the provided purls to search for known vulnerabilities", body = AnalysisResponse),
137+
),
138+
)]
139+
#[post("/v3/vulnerability/analyze")]
140+
pub async fn analyze_v3(
141+
service: web::Data<VulnerabilityService>,
142+
db: web::Data<Database>,
143+
web::Json(AnalysisRequest { purls }): web::Json<AnalysisRequest>,
144+
_: Require<ReadAdvisory>,
120145
) -> actix_web::Result<impl Responder> {
121146
let tx = db.begin_read().await?;
122147
let details = service.analyze_purls(purls, &tx).await?;

modules/fundamental/src/vulnerability/model/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod analyze;
22
mod details;
33
mod summary;
4+
pub mod v2;
45

56
pub use analyze::*;
67
pub use details::*;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use crate::{
2+
advisory::model::AdvisoryHead, common::model::Score, vulnerability::model::VulnerabilityHead,
3+
};
4+
use serde::{Deserialize, Serialize};
5+
use std::{collections::BTreeMap, ops::Deref};
6+
use utoipa::ToSchema;
7+
8+
#[derive(Serialize, Deserialize, Debug, ToSchema, Default)]
9+
pub struct AnalysisResultV2 {
10+
pub details: Vec<AnalysisDetailsV2>,
11+
pub warnings: Vec<String>,
12+
}
13+
14+
#[derive(Serialize, Deserialize, Debug, ToSchema)]
15+
pub struct AnalysisDetailsV2 {
16+
#[serde(flatten)]
17+
pub head: VulnerabilityHead,
18+
19+
/// List of purl statuses
20+
pub status: BTreeMap<String, Vec<AnalysisAdvisory>>,
21+
}
22+
23+
#[derive(Serialize, Deserialize, Debug, ToSchema)]
24+
pub struct AnalysisAdvisory {
25+
#[serde(flatten)]
26+
pub advisory: AdvisoryHead,
27+
28+
/// CVSS scores
29+
pub scores: Vec<Score>,
30+
}
31+
32+
#[derive(Serialize, Deserialize, Debug, ToSchema)]
33+
pub struct AnalysisResponseV2(pub BTreeMap<String, AnalysisResultV2>);
34+
35+
impl Deref for AnalysisResponseV2 {
36+
type Target = BTreeMap<String, AnalysisResultV2>;
37+
38+
fn deref(&self) -> &Self::Target {
39+
&self.0
40+
}
41+
}

0 commit comments

Comments
 (0)