|
| 1 | +package output |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/open-policy-agent/opa/tester" |
| 10 | + "github.com/owenrumney/go-sarif/v2/sarif" |
| 11 | + "golang.org/x/exp/slices" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + // Tool information |
| 16 | + toolName = "conftest" |
| 17 | + toolURI = "https://github.com/open-policy-agent/conftest" |
| 18 | + sarifVersion = sarif.Version210 |
| 19 | + |
| 20 | + // Result descriptions |
| 21 | + successDesc = "Policy was satisfied successfully" |
| 22 | + skippedDesc = "Policy check was skipped" |
| 23 | + failureDesc = "Policy violation" |
| 24 | + warningDesc = "Policy warning" |
| 25 | + exceptionDesc = "Policy exception" |
| 26 | + |
| 27 | + // Exit code descriptions |
| 28 | + exitNoViolations = "No policy violations found" |
| 29 | + exitViolations = "Policy violations found" |
| 30 | + exitWarnings = "Policy warnings found" |
| 31 | +) |
| 32 | + |
| 33 | +// SARIF represents an Outputter that outputs results in SARIF format. |
| 34 | +type SARIF struct { |
| 35 | + writer io.Writer |
| 36 | +} |
| 37 | + |
| 38 | +// NewSARIF creates a new SARIF with the given writer. |
| 39 | +func NewSARIF(w io.Writer) *SARIF { |
| 40 | + return &SARIF{ |
| 41 | + writer: w, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// getRuleID generates a stable rule ID based on namespace and rule type |
| 46 | +func getRuleID(namespace string, ruleType string) string { |
| 47 | + return fmt.Sprintf("%s/%s", namespace, ruleType) |
| 48 | +} |
| 49 | + |
| 50 | +// getRuleDescription returns the appropriate description based on the rule type |
| 51 | +func getRuleDescription(ruleID string) string { |
| 52 | + switch { |
| 53 | + case strings.HasSuffix(ruleID, "/success"): |
| 54 | + return successDesc |
| 55 | + case strings.HasSuffix(ruleID, "/skip"): |
| 56 | + return skippedDesc |
| 57 | + case strings.HasSuffix(ruleID, "/allow"): |
| 58 | + return exceptionDesc |
| 59 | + case strings.HasSuffix(ruleID, "/warn"): |
| 60 | + return warningDesc |
| 61 | + default: |
| 62 | + return failureDesc |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// addRuleIndex adds a new rule to the SARIF run and returns its index. |
| 67 | +func addRuleIndex(run *sarif.Run, ruleID string, result Result, indices map[string]int) int { |
| 68 | + addRule(run, ruleID, result) |
| 69 | + idx := len(run.Tool.Driver.Rules) - 1 |
| 70 | + indices[ruleID] = idx |
| 71 | + return idx |
| 72 | +} |
| 73 | + |
| 74 | +// addRule adds a new rule to the SARIF run with the given ID and result metadata. |
| 75 | +func addRule(run *sarif.Run, ruleID string, result Result) { |
| 76 | + desc := getRuleDescription(ruleID) |
| 77 | + rule := run.AddRule(ruleID). |
| 78 | + WithDescription(desc). |
| 79 | + WithShortDescription(&sarif.MultiformatMessageString{ |
| 80 | + Text: &desc, |
| 81 | + }) |
| 82 | + |
| 83 | + if result.Metadata != nil { |
| 84 | + props := sarif.NewPropertyBag() |
| 85 | + for k, v := range result.Metadata { |
| 86 | + props.Add(k, v) |
| 87 | + } |
| 88 | + rule.WithProperties(props.Properties) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// addResult adds a result to the SARIF run |
| 93 | +func addResult(run *sarif.Run, result Result, namespace, ruleType, level, fileName string, indices map[string]int) { |
| 94 | + ruleID := getRuleID(namespace, ruleType) |
| 95 | + idx, ok := indices[ruleID] |
| 96 | + if !ok { |
| 97 | + idx = addRuleIndex(run, ruleID, result, indices) |
| 98 | + } |
| 99 | + |
| 100 | + run.CreateResultForRule(ruleID). |
| 101 | + WithRuleIndex(idx). |
| 102 | + WithLevel(level). |
| 103 | + WithMessage(sarif.NewTextMessage(result.Message)). |
| 104 | + AddLocation( |
| 105 | + sarif.NewLocationWithPhysicalLocation( |
| 106 | + sarif.NewPhysicalLocation(). |
| 107 | + WithArtifactLocation( |
| 108 | + sarif.NewSimpleArtifactLocation(filepath.ToSlash(fileName)), |
| 109 | + ), |
| 110 | + ), |
| 111 | + ) |
| 112 | +} |
| 113 | + |
| 114 | +// Output outputs the results in SARIF format. |
| 115 | +func (s *SARIF) Output(results []CheckResult) error { |
| 116 | + report, err := sarif.New(sarifVersion) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("create sarif report: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + run := sarif.NewRunWithInformationURI(toolName, toolURI) |
| 122 | + indices := make(map[string]int) |
| 123 | + |
| 124 | + for _, result := range results { |
| 125 | + // Process failures |
| 126 | + for _, failure := range result.Failures { |
| 127 | + addResult(run, failure, result.Namespace, "deny", "error", result.FileName, indices) |
| 128 | + } |
| 129 | + |
| 130 | + // Process warnings |
| 131 | + for _, warning := range result.Warnings { |
| 132 | + addResult(run, warning, result.Namespace, "warn", "warning", result.FileName, indices) |
| 133 | + } |
| 134 | + |
| 135 | + // Process exceptions (treated as successes) |
| 136 | + hasSuccesses := result.Successes > 0 |
| 137 | + for _, exception := range result.Exceptions { |
| 138 | + addResult(run, exception, result.Namespace, "allow", "note", result.FileName, indices) |
| 139 | + hasSuccesses = true |
| 140 | + } |
| 141 | + |
| 142 | + // Don't add success/skip results if there are failures or warnings |
| 143 | + hasErrors := len(result.Failures) > 0 || len(result.Warnings) > 0 |
| 144 | + if hasErrors { |
| 145 | + continue |
| 146 | + } |
| 147 | + |
| 148 | + // Add success/exception results if there are no failures or warnings |
| 149 | + if hasSuccesses { |
| 150 | + statusResult := Result{ |
| 151 | + Message: successDesc, |
| 152 | + Metadata: map[string]interface{}{ |
| 153 | + "description": successDesc, |
| 154 | + }, |
| 155 | + } |
| 156 | + addResult(run, statusResult, result.Namespace, "success", "none", result.FileName, indices) |
| 157 | + } else { |
| 158 | + statusResult := Result{ |
| 159 | + Message: skippedDesc, |
| 160 | + Metadata: map[string]interface{}{ |
| 161 | + "description": skippedDesc, |
| 162 | + }, |
| 163 | + } |
| 164 | + addResult(run, statusResult, result.Namespace, "skip", "none", result.FileName, indices) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + // Add run metadata |
| 169 | + exitCode := 0 |
| 170 | + exitDesc := exitNoViolations |
| 171 | + if hasFailures(results) { |
| 172 | + exitCode = 1 |
| 173 | + exitDesc = exitViolations |
| 174 | + } else if hasWarnings(results) { |
| 175 | + exitDesc = exitWarnings |
| 176 | + } |
| 177 | + |
| 178 | + successful := true |
| 179 | + invocation := sarif.NewInvocation() |
| 180 | + invocation.ExecutionSuccessful = &successful |
| 181 | + invocation.ExitCode = &exitCode |
| 182 | + invocation.ExitCodeDescription = &exitDesc |
| 183 | + |
| 184 | + run.Invocations = []*sarif.Invocation{invocation} |
| 185 | + |
| 186 | + // Add the run to the report |
| 187 | + report.AddRun(run) |
| 188 | + |
| 189 | + // Write the report |
| 190 | + return report.Write(s.writer) |
| 191 | +} |
| 192 | + |
| 193 | +// Report is not supported in SARIF output |
| 194 | +func (s *SARIF) Report(_ []*tester.Result, _ string) error { |
| 195 | + return fmt.Errorf("report is not supported in SARIF output") |
| 196 | +} |
| 197 | + |
| 198 | +// hasFailures returns true if any of the results contain failures |
| 199 | +func hasFailures(results []CheckResult) bool { |
| 200 | + return slices.ContainsFunc(results, func(r CheckResult) bool { |
| 201 | + return len(r.Failures) > 0 |
| 202 | + }) |
| 203 | +} |
| 204 | + |
| 205 | +// hasWarnings returns true if any of the results contain warnings |
| 206 | +func hasWarnings(results []CheckResult) bool { |
| 207 | + return slices.ContainsFunc(results, func(r CheckResult) bool { |
| 208 | + return len(r.Warnings) > 0 |
| 209 | + }) |
| 210 | +} |
0 commit comments