Skip to content

Commit fcc0029

Browse files
committed
feat(scanresult): Use global evaluation result from scanner
1 parent 5fbf085 commit fcc0029

File tree

5 files changed

+47
-21
lines changed

5 files changed

+47
-21
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ repos:
1212
- id: trailing-whitespace
1313
- id: end-of-file-fixer
1414
- id: check-yaml
15+
- id: check-toml
16+
- id: no-commit-to-branch

src/domain/scanresult/evaluation_result.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ impl EvaluationResult {
1313
matches!(self, Self::Passed)
1414
}
1515
}
16+
17+
impl From<&str> for EvaluationResult {
18+
fn from(value: &str) -> Self {
19+
if value.eq_ignore_ascii_case("failed") {
20+
EvaluationResult::Failed
21+
} else {
22+
EvaluationResult::Passed
23+
}
24+
}
25+
}

src/domain/scanresult/scan_result.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct ScanResult {
2727
policies: HashMap<String, Arc<Policy>>,
2828
policy_bundles: HashMap<String, Arc<PolicyBundle>>,
2929
accepted_risks: HashMap<String, Arc<AcceptedRisk>>,
30+
global_evaluation: EvaluationResult,
3031
}
3132

3233
impl ScanResult {
@@ -41,6 +42,7 @@ impl ScanResult {
4142
architecture: Architecture,
4243
labels: HashMap<String, String>,
4344
created_at: DateTime<Utc>,
45+
global_evaluation: EvaluationResult,
4446
) -> Self {
4547
Self {
4648
scan_type,
@@ -60,6 +62,7 @@ impl ScanResult {
6062
policies: HashMap::new(),
6163
policy_bundles: HashMap::new(),
6264
accepted_risks: HashMap::new(),
65+
global_evaluation,
6366
}
6467
}
6568

@@ -242,15 +245,7 @@ impl ScanResult {
242245
}
243246

244247
pub fn evaluation_result(&self) -> EvaluationResult {
245-
if self
246-
.policies()
247-
.iter()
248-
.all(|p| p.evaluation_result().is_passed())
249-
{
250-
EvaluationResult::Passed
251-
} else {
252-
EvaluationResult::Failed
253-
}
248+
self.global_evaluation
254249
}
255250
}
256251

@@ -277,6 +272,7 @@ mod tests {
277272
Architecture::Amd64,
278273
HashMap::new(),
279274
Utc::now(),
275+
EvaluationResult::Failed,
280276
)
281277
}
282278

@@ -505,7 +501,18 @@ mod tests {
505501

506502
#[test]
507503
fn evaluation_result_passed() {
508-
let mut scan_result = create_scan_result();
504+
let mut scan_result = ScanResult::new(
505+
ScanType::Docker,
506+
"alpine:latest".to_string(),
507+
"sha256:12345".to_string(),
508+
Some("sha256:67890".to_string()),
509+
OperatingSystem::new(Family::Linux, "alpine:3.18".to_string()),
510+
123456,
511+
Architecture::Amd64,
512+
HashMap::new(),
513+
Utc::now(),
514+
EvaluationResult::Passed,
515+
);
509516
let now = Utc::now();
510517
let policy =
511518
scan_result.add_policy("policy-1".to_string(), "My Policy".to_string(), now, now);
@@ -758,7 +765,11 @@ mod tests {
758765

759766
assert_eq!(bundle.evaluation_result(), EvaluationResult::Passed);
760767
assert_eq!(policy.evaluation_result(), EvaluationResult::Passed);
761-
assert_eq!(scan_result.evaluation_result(), EvaluationResult::Passed);
768+
assert_eq!(
769+
scan_result.evaluation_result(),
770+
EvaluationResult::Failed,
771+
"Global evaluation should remain Failed"
772+
);
762773

763774
let failed_rule = bundle.add_rule(
764775
"rule-failed".to_string(),
@@ -780,6 +791,10 @@ mod tests {
780791

781792
assert_eq!(bundle.evaluation_result(), EvaluationResult::Failed);
782793
assert_eq!(policy.evaluation_result(), EvaluationResult::Failed);
783-
assert_eq!(scan_result.evaluation_result(), EvaluationResult::Failed);
794+
assert_eq!(
795+
scan_result.evaluation_result(),
796+
EvaluationResult::Failed,
797+
"Global evaluation should remain Failed"
798+
);
784799
}
785800
}

src/infra/sysdig_image_scanner_json_scan_result_v1.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::collections::HashMap;
77
use crate::domain::scanresult::{
88
accepted_risk_reason::AcceptedRiskReason,
99
architecture::Architecture,
10-
evaluation_result::EvaluationResult,
1110
operating_system::{Family, OperatingSystem},
1211
package_type::PackageType,
1312
scan_result::ScanResult,
@@ -17,7 +16,7 @@ use crate::domain::scanresult::{
1716

1817
impl From<JsonScanResultV1> for ScanResult {
1918
fn from(report: JsonScanResultV1) -> Self {
20-
let mut scan_result = ScanResult::from(&report.result.metadata);
19+
let mut scan_result = ScanResult::from(&report.result);
2120

2221
add_layers(&report.result, &mut scan_result);
2322
add_risk_accepts(&report.result, &mut scan_result);
@@ -145,11 +144,7 @@ fn add_policies(result: &JsonResult, scan_result: &mut ScanResult) {
145144
let rule = policy_bundle.add_rule(
146145
json_rule.rule_id.clone(),
147146
json_rule.description.clone(),
148-
if json_rule.evaluation_result.eq_ignore_ascii_case("failed") {
149-
EvaluationResult::Failed
150-
} else {
151-
EvaluationResult::Passed
152-
},
147+
json_rule.evaluation_result.as_str().into(),
153148
);
154149

155150
for json_failure in json_rule.failures.as_deref().unwrap_or_default() {
@@ -188,8 +183,9 @@ fn failure_message_for(result: &JsonResult, package_ref: &str, vulnerability_ref
188183
}
189184
}
190185

191-
impl From<&JsonMetadata> for ScanResult {
192-
fn from(metadata: &JsonMetadata) -> Self {
186+
impl From<&JsonResult> for ScanResult {
187+
fn from(result: &JsonResult) -> Self {
188+
let metadata = &result.metadata;
193189
ScanResult::new(
194190
ScanType::Docker,
195191
metadata.pull_string.clone(),
@@ -200,6 +196,7 @@ impl From<&JsonMetadata> for ScanResult {
200196
arch_from_str(&metadata.architecture),
201197
metadata.labels.clone(),
202198
metadata.created_at,
199+
result.policies.global_evaluation.as_str().into(),
203200
)
204201
}
205202
}

tests/general.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rstest::{fixture, rstest};
55
use serde_json::json;
66
use std::collections::HashMap;
77
use sysdig_lsp::domain::scanresult::architecture::Architecture;
8+
use sysdig_lsp::domain::scanresult::evaluation_result::EvaluationResult;
89
use sysdig_lsp::domain::scanresult::operating_system::{Family, OperatingSystem};
910
use sysdig_lsp::domain::scanresult::scan_result::ScanResult;
1011
use sysdig_lsp::domain::scanresult::scan_type::ScanType;
@@ -120,6 +121,7 @@ fn scan_result() -> ScanResult {
120121
Architecture::Amd64,
121122
HashMap::new(),
122123
chrono::Utc::now(),
124+
EvaluationResult::Passed,
123125
);
124126

125127
let layer = result.add_layer(

0 commit comments

Comments
 (0)