Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement SARIF output and add query metadata in engine #1042

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ As of today Conftest supports the following output types:
- JUnit `--output=junit`
- GitHub `--output=github`
- AzureDevOps `--output=azuredevops`
- SARIF `--output=sarif`

### Plaintext

Expand Down Expand Up @@ -322,6 +323,71 @@ success file=examples/kubernetes/deployment.yaml 1
5 tests, 1 passed, 0 warnings, 4 failures, 0 exceptions
```

### SARIF

```console
$ conftest test --proto-file-dirs examples/textproto/protos -p examples/textproto/policy examples/textproto/fail.textproto -o sarif
{
"$schema": "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json",
"version": "2.1.0",
"runs": [
{
"tool": {
"driver": {
"name": "conftest",
"informationUri": "https://github.com/open-policy-agent/conftest",
"rules": [
{
"id": "conftest-failure-main-deny",
"shortDescription": {
"text": "fail: Power level must be over 9000"
},
"properties": {
"namespace": "main",
"query": "data.main.deny"
}
}
]
}
},
"results": [
{
"ruleId": "conftest-failure-main-deny",
"ruleIndex": 0,
"kind": "fail",
"level": "error",
"message": {
"text": "fail: Power level must be over 9000"
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": "examples/textproto/fail.textproto"
}
}
}
],
"properties": {
"namespace": "main",
"query": "data.main.deny"
}
}
],
"invocations": [
{
"executionSuccessful": true,
"exitCode": 1,
"exitCodeDescription": "Policy violations found",
"startTimeUtc": "2025-01-19T13:14:11Z",
"endTimeUtc": "2025-01-19T13:14:11Z"
}
]
}
]
}
```

## `--parser`

Conftest normally detects which parser to used based on the file extension of the file, even when multiple input files are passed in. However, it is possible force a specific parser to be used with the `--parser` flag.
Expand Down
4 changes: 4 additions & 0 deletions output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
OutputJUnit = "junit"
OutputGitHub = "github"
OutputAzureDevOps = "azuredevops"
OutputSARIF = "sarif"
)

// Get returns a type that can render output in the given format.
Expand All @@ -57,6 +58,8 @@ func Get(format string, options Options) Outputter {
return NewGitHub(options.File)
case OutputAzureDevOps:
return NewAzureDevOps(options.File)
case OutputSARIF:
return NewSARIF(options.File)
default:
return NewStandard(options.File)
}
Expand All @@ -71,5 +74,6 @@ func Outputs() []string {
OutputTable,
OutputJUnit,
OutputGitHub,
OutputSARIF,
}
}
4 changes: 4 additions & 0 deletions output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func TestGetOutputter(t *testing.T) {
input: OutputJUnit,
expected: NewJUnit(os.Stdout, false),
},
{
input: OutputSARIF,
expected: NewSARIF(os.Stdout),
},
{
input: "unknown_format",
expected: NewStandard(os.Stdout),
Expand Down
Loading
Loading