Skip to content

Commit c2019bb

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 c4def37 commit c2019bb

3 files changed

Lines changed: 454 additions & 4 deletions

File tree

examples/regorus/main.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fn rego_eval(
4646
non_strict: bool,
4747
#[cfg(feature = "coverage")] coverage: bool,
4848
v0: bool,
49+
format: &str,
4950
) -> Result<()> {
5051
// Create engine.
5152
let mut engine = regorus::Engine::new();
@@ -117,8 +118,17 @@ fn rego_eval(
117118
// to use.
118119
let results = engine.eval_query(query, enable_tracing)?;
119120

120-
println!("{}", serde_json::to_string_pretty(&results)?);
121-
121+
match format {
122+
"sarif" => {
123+
let config = regorus::sarif::SarifConfig::default();
124+
let report = regorus::sarif::SarifReport::from_query_results(&results, &config)
125+
.map_err(|e| anyhow!("SARIF generation failed: {e}"))?;
126+
println!("{}", report.to_json().map_err(|e| anyhow!("{e}"))?);
127+
}
128+
"json" | _ => {
129+
println!("{}", serde_json::to_string_pretty(&results)?);
130+
}
131+
}
122132
#[cfg(feature = "coverage")]
123133
if coverage {
124134
let report = engine.get_coverage_report()?;
@@ -249,9 +259,11 @@ enum RegorusCommand {
249259
/// Turn on Rego language v0.
250260
#[arg(long)]
251261
v0: bool,
252-
},
253262

254-
/// Tokenize a Rego policy.
263+
/// Output format (json or sarif).
264+
#[arg(long, short = 'F', default_value = "json")]
265+
format: String,
266+
}, /// Tokenize a Rego policy.
255267
Lex {
256268
/// Rego policy file.
257269
file: String,
@@ -331,6 +343,7 @@ fn main() -> Result<()> {
331343
#[cfg(feature = "coverage")]
332344
coverage,
333345
v0,
346+
format,
334347
} => rego_eval(
335348
&bundles,
336349
&data,
@@ -341,6 +354,7 @@ fn main() -> Result<()> {
341354
#[cfg(feature = "coverage")]
342355
coverage,
343356
v0,
357+
&format,
344358
),
345359
RegorusCommand::Lex { file, verbose } => rego_lex(file, verbose),
346360
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)