Skip to content

Commit 332459b

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

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
@@ -27,6 +27,10 @@ use policy::parse_policy;
2727
use queries::run_all_queries;
2828
use report::{render_compact, render_report};
2929

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

69-
let exit_code = if compact {
70-
render_compact(&findings, policy_path, credentials_path)
73+
if compact {
74+
render_compact(&findings);
7175
} else {
72-
render_report(&findings, policy_path, credentials_path)
73-
};
76+
render_report(&findings, policy_path, credentials_path);
77+
}
7478

75-
Ok(exit_code)
79+
Ok(outcome(&findings))
7680
}
7781

7882
// ===========================================================================
@@ -341,4 +345,61 @@ network_policies:
341345
"deny-all policy should produce no findings, got: {findings:?}"
342346
);
343347
}
348+
349+
#[test]
350+
fn test_outcome_empty_findings() {
351+
assert_eq!(outcome(&[]), 0);
352+
}
353+
354+
#[test]
355+
fn test_outcome_accepted_only() {
356+
let f = finding::Finding {
357+
query: finding::category::LINK_LOCAL_REACH.to_owned(),
358+
title: String::new(),
359+
description: String::new(),
360+
paths: vec![],
361+
remediation: vec![],
362+
accepted: true,
363+
accepted_reason: "accepted for test".to_owned(),
364+
};
365+
assert_eq!(outcome(&[f]), 0);
366+
}
367+
368+
#[test]
369+
fn test_outcome_unaccepted_finding() {
370+
let f = finding::Finding {
371+
query: finding::category::LINK_LOCAL_REACH.to_owned(),
372+
title: String::new(),
373+
description: String::new(),
374+
paths: vec![finding::FindingPath::Exfil(finding::ExfilPath {
375+
binary: "/usr/bin/curl".to_owned(),
376+
endpoint_host: "169.254.169.254".to_owned(),
377+
endpoint_port: 80,
378+
mechanism: String::new(),
379+
policy_name: "test".to_owned(),
380+
category: finding::category::LINK_LOCAL_REACH.to_owned(),
381+
method: String::new(),
382+
})],
383+
remediation: vec![],
384+
accepted: false,
385+
accepted_reason: String::new(),
386+
};
387+
assert_eq!(outcome(&[f]), 1);
388+
}
389+
390+
// prove() returns Ok(1) for an unaccepted pathless finding;
391+
// renderers show REVIEW, not PASS, when unaccepted findings exist regardless of path count.
392+
#[test]
393+
fn test_outcome_pathless_unaccepted() {
394+
let f = finding::Finding {
395+
query: finding::category::LINK_LOCAL_REACH.to_owned(),
396+
title: String::new(),
397+
description: String::new(),
398+
paths: vec![],
399+
remediation: vec![],
400+
accepted: false,
401+
accepted_reason: String::new(),
402+
};
403+
assert_eq!(outcome(&[f]), 1);
404+
}
344405
}

‎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)