Skip to content

Commit b1dbd85

Browse files
committed
refactor(prover): separate outcome evaluation from rendering
Signed-off-by: Evie Howard <[email protected]>
1 parent 6e6e649 commit b1dbd85

2 files changed

Lines changed: 80 additions & 26 deletions

File tree

‎crates/openshell-prover/src/lib.rs‎

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ use policy::parse_policy;
2626
use queries::run_all_queries;
2727
use report::{render_compact, render_report};
2828

29+
fn outcome(findings: &[finding::Finding]) -> i32 {
30+
i32::from(findings.iter().any(|finding| !finding.accepted))
31+
}
32+
2933
/// Run the prover end-to-end and return a result containing an exit code.
3034
///
3135
/// - `Ok(0)` — pass (no findings, or all accepted)
@@ -65,13 +69,13 @@ pub fn prove(
6569
findings = apply_accepted_risks(findings, &accepted);
6670
}
6771

68-
let exit_code = if compact {
69-
render_compact(&findings, policy_path, credentials_path)
72+
if compact {
73+
render_compact(&findings);
7074
} else {
71-
render_report(&findings, policy_path, credentials_path)
72-
};
75+
render_report(&findings, policy_path, credentials_path);
76+
}
7377

74-
Ok(exit_code)
78+
Ok(outcome(&findings))
7579
}
7680

7781
// ===========================================================================
@@ -311,4 +315,61 @@ network_policies:
311315
"deny-all policy should produce no findings, got: {findings:?}"
312316
);
313317
}
318+
319+
#[test]
320+
fn test_outcome_empty_findings() {
321+
assert_eq!(outcome(&[]), 0);
322+
}
323+
324+
#[test]
325+
fn test_outcome_accepted_only() {
326+
let f = finding::Finding {
327+
query: finding::category::LINK_LOCAL_REACH.to_owned(),
328+
title: String::new(),
329+
description: String::new(),
330+
paths: vec![],
331+
remediation: vec![],
332+
accepted: true,
333+
accepted_reason: "accepted for test".to_owned(),
334+
};
335+
assert_eq!(outcome(&[f]), 0);
336+
}
337+
338+
#[test]
339+
fn test_outcome_unaccepted_finding() {
340+
let f = finding::Finding {
341+
query: finding::category::LINK_LOCAL_REACH.to_owned(),
342+
title: String::new(),
343+
description: String::new(),
344+
paths: vec![finding::FindingPath::Exfil(finding::ExfilPath {
345+
binary: "/usr/bin/curl".to_owned(),
346+
endpoint_host: "169.254.169.254".to_owned(),
347+
endpoint_port: 80,
348+
mechanism: String::new(),
349+
policy_name: "test".to_owned(),
350+
category: finding::category::LINK_LOCAL_REACH.to_owned(),
351+
method: String::new(),
352+
})],
353+
remediation: vec![],
354+
accepted: false,
355+
accepted_reason: String::new(),
356+
};
357+
assert_eq!(outcome(&[f]), 1);
358+
}
359+
360+
// prove() returns Ok(1) for an unaccepted pathless finding;
361+
// renderers show REVIEW, not PASS, when unaccepted findings exist regardless of path count.
362+
#[test]
363+
fn test_outcome_pathless_unaccepted() {
364+
let f = finding::Finding {
365+
query: finding::category::LINK_LOCAL_REACH.to_owned(),
366+
title: String::new(),
367+
description: String::new(),
368+
paths: vec![],
369+
remediation: vec![],
370+
accepted: false,
371+
accepted_reason: String::new(),
372+
};
373+
assert_eq!(outcome(&[f]), 1);
374+
}
314375
}

‎crates/openshell-prover/src/report.rs‎

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ fn format_path_line(query: &str, p: &crate::finding::ExfilPath) -> String {
7676
// ---------------------------------------------------------------------------
7777

7878
/// Render compact output (one-line-per-finding-line for demos and CI).
79-
/// Returns exit code: 0 = pass, 1 = any findings present.
80-
pub fn render_compact(findings: &[Finding], _policy_path: &str, _credentials_path: &str) -> i32 {
79+
pub fn render_compact(findings: &[Finding]) {
8180
let active: Vec<&Finding> = findings.iter().filter(|f| !f.accepted).collect();
8281
let accepted: Vec<&Finding> = findings.iter().filter(|f| f.accepted).collect();
8382

@@ -108,19 +107,17 @@ pub fn render_compact(findings: &[Finding], _policy_path: &str, _credentials_pat
108107
format!(", {} accepted", accepted.len())
109108
};
110109

111-
let path_count: usize = active.iter().map(|f| f.paths.len()).sum();
112-
if path_count > 0 {
110+
if active.is_empty() {
113111
println!(
114-
" {} {path_count} finding path(s) require review{accepted_note}",
115-
" REVIEW ".black().bold().on_yellow()
112+
" {} no findings{accepted_note}",
113+
" PASS ".white().bold().on_green()
116114
);
117-
1
118115
} else {
119116
println!(
120-
" {} no findings{accepted_note}",
121-
" PASS ".white().bold().on_green()
117+
" {} {} finding(s) require review{accepted_note}",
118+
" REVIEW ".black().bold().on_yellow(),
119+
active.len()
122120
);
123-
0
124121
}
125122
}
126123

@@ -129,8 +126,7 @@ pub fn render_compact(findings: &[Finding], _policy_path: &str, _credentials_pat
129126
// ---------------------------------------------------------------------------
130127

131128
/// Render a full terminal report with finding panels.
132-
/// Returns exit code: 0 = pass, 1 = any findings present.
133-
pub fn render_report(findings: &[Finding], policy_path: &str, credentials_path: &str) -> i32 {
129+
pub fn render_report(findings: &[Finding], policy_path: &str, credentials_path: &str) {
134130
let policy_name = Path::new(policy_path)
135131
.file_name()
136132
.map_or("policy.yaml", |n| n.to_str().unwrap_or("policy.yaml"));
@@ -160,7 +156,7 @@ pub fn render_report(findings: &[Finding], policy_path: &str, credentials_path:
160156

161157
if active.is_empty() && accepted.is_empty() {
162158
println!("{}", "No findings. Policy posture is clean.".green().bold());
163-
return 0;
159+
return;
164160
}
165161

166162
println!("{}", "Finding Summary".bold().underline());
@@ -207,26 +203,23 @@ pub fn render_report(findings: &[Finding], policy_path: &str, credentials_path:
207203
}
208204
}
209205

210-
let path_count: usize = active.iter().map(|f| f.paths.len()).sum();
211206
let accepted_note = if accepted.is_empty() {
212207
String::new()
213208
} else {
214209
format!(" ({} accepted)", accepted.len())
215210
};
216-
if path_count > 0 {
211+
if active.is_empty() {
217212
println!(
218213
"{}{accepted_note}",
219-
"REVIEW \u{2014} prover findings require human attention."
220-
.bold()
221-
.yellow()
214+
"PASS \u{2014} All findings accepted.".bold().green()
222215
);
223-
1
224216
} else {
225217
println!(
226218
"{}{accepted_note}",
227-
"PASS \u{2014} All findings accepted.".bold().green()
219+
"REVIEW \u{2014} prover findings require human attention."
220+
.bold()
221+
.yellow()
228222
);
229-
0
230223
}
231224
}
232225

0 commit comments

Comments
 (0)