Skip to content

Commit e3dc55b

Browse files
anakrishCopilot
andcommitted
feat: add SARIF output format for policy violations
Add a new `sarif` module that converts policy evaluation results into SARIF v2.1.0 format, enabling integration with GitHub Advanced Security, Azure DevOps, and other SARIF-consuming tools. - New `src/sarif.rs` module with `SarifReport` and `SarifConfig` - CLI: `--format sarif` option on the `eval` command - Configurable field mapping (msg, severity, file, rule_id) - Max results limit support - Severity mapping (OPA-style → SARIF levels) - Proper URI handling with base URI support Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7ce1bbb commit e3dc55b

3 files changed

Lines changed: 454 additions & 4 deletions

File tree

examples/regorus.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ fn rego_eval(
4343
non_strict: bool,
4444
#[cfg(feature = "coverage")] coverage: bool,
4545
v0: bool,
46+
format: &str,
4647
) -> Result<()> {
4748
// Create engine.
4849
let mut engine = regorus::Engine::new();
@@ -114,8 +115,17 @@ fn rego_eval(
114115
// to use.
115116
let results = engine.eval_query(query, enable_tracing)?;
116117

117-
println!("{}", serde_json::to_string_pretty(&results)?);
118-
118+
match format {
119+
"sarif" => {
120+
let config = regorus::sarif::SarifConfig::default();
121+
let report = regorus::sarif::SarifReport::from_query_results(&results, &config)
122+
.map_err(|e| anyhow!("SARIF generation failed: {e}"))?;
123+
println!("{}", report.to_json().map_err(|e| anyhow!("{e}"))?);
124+
}
125+
"json" | _ => {
126+
println!("{}", serde_json::to_string_pretty(&results)?);
127+
}
128+
}
119129
#[cfg(feature = "coverage")]
120130
if coverage {
121131
let report = engine.get_coverage_report()?;
@@ -246,9 +256,11 @@ enum RegorusCommand {
246256
/// Turn on Rego language v0.
247257
#[arg(long)]
248258
v0: bool,
249-
},
250259

251-
/// Tokenize a Rego policy.
260+
/// Output format (json or sarif).
261+
#[arg(long, short = 'F', default_value = "json")]
262+
format: String,
263+
}, /// Tokenize a Rego policy.
252264
Lex {
253265
/// Rego policy file.
254266
file: String,
@@ -292,6 +304,7 @@ fn main() -> Result<()> {
292304
#[cfg(feature = "coverage")]
293305
coverage,
294306
v0,
307+
format,
295308
} => rego_eval(
296309
&bundles,
297310
&data,
@@ -302,6 +315,7 @@ fn main() -> Result<()> {
302315
#[cfg(feature = "coverage")]
303316
coverage,
304317
v0,
318+
&format,
305319
),
306320
RegorusCommand::Lex { file, verbose } => rego_lex(file, verbose),
307321
RegorusCommand::Parse { file, v0 } => rego_parse(file, v0),

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ mod schema;
154154
pub mod target;
155155
#[cfg(any(test, all(feature = "yaml", feature = "std")))]
156156
pub mod test_utils;
157+
pub mod sarif;
157158
pub mod utils;
158159
mod value;
159160

0 commit comments

Comments
 (0)