Files

138 lines
3.4 KiB
Go

package audit
import (
"encoding/json"
"fmt"
"io"
"strings"
)
// WriteJSON emits one object for a single report and an array for several, so a
// one-bundle check pipes straight into jq.
func WriteJSON(w io.Writer, reports []*Report) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
var payload any = reports
if len(reports) == 1 {
payload = reports[0]
}
if err := enc.Encode(payload); err != nil {
return fmt.Errorf("encode report: %w", err)
}
return nil
}
// WriteText renders the human report: verdict, then evidence, then coverage.
func WriteText(w io.Writer, reports []*Report, verbose bool) error {
var p printer
for i, rep := range reports {
if i > 0 {
p.line("")
}
writeReport(&p, rep, verbose)
}
if len(reports) > 1 {
p.line("")
p.line("%s", tally(reports))
}
if _, err := io.WriteString(w, p.b.String()); err != nil {
return fmt.Errorf("write report: %w", err)
}
return nil
}
// printer accumulates the text report. Writing to a strings.Builder cannot fail,
// so the error is discarded once here instead of at every call site.
type printer struct{ b strings.Builder }
func (p *printer) line(format string, args ...any) {
_, _ = fmt.Fprintf(&p.b, format+"\n", args...)
}
func writeReport(p *printer, rep *Report, verbose bool) {
p.line("%s %s", heading(rep), strings.ToUpper(rep.Decision))
p.line(" legitimacy %-13s %s", rep.Legitimacy.Verdict, rep.Legitimacy.Reason)
p.line(" validity %-13s %s", rep.Validity.Verdict, rep.Validity.Reason)
if len(rep.Findings) > 0 {
p.line(" findings")
for _, f := range rep.Findings {
p.line(" %s %s", strings.ToUpper(f.Severity), f.Summary())
for _, ev := range f.Evidence {
p.line(" at %s", ev.Locator)
if ev.Quote != "" {
p.line(" quote: %s", oneLine(ev.Quote))
}
if ev.Detail != "" {
p.line(" detail: %s", ev.Detail)
}
}
if verbose {
p.line(" mechanism: %s", f.Mechanism)
}
p.line(" fix: %s", f.Remediation)
}
} else {
p.line(" findings none")
}
cov := rep.Coverage
p.line(" coverage %d rules evaluated, %d not applicable, contract %s",
cov.RulesEvaluated, len(cov.Skipped), providedWord(cov.ContractProvided))
for _, s := range cov.Skipped {
p.line(" skipped %s: %s", s.RuleID, s.Reason)
}
if verbose {
p.line(" not audited:")
for _, n := range cov.NotAudited {
p.line(" - %s", n)
}
}
}
func heading(rep *Report) string {
switch {
case rep.BundleID != "" && rep.Subject != "":
return rep.BundleID + " (" + rep.Subject + ")"
case rep.BundleID != "":
return rep.BundleID
case rep.Subject != "":
return rep.Subject
default:
return "bundle"
}
}
func providedWord(provided bool) string {
if provided {
return "supplied"
}
return "absent"
}
// oneLine keeps a quoted excerpt on one line so the report stays scannable.
func oneLine(s string) string {
return strings.ReplaceAll(strings.ReplaceAll(s, "\n", "\\n"), "\r", "")
}
func tally(reports []*Report) string {
counts := map[string]int{}
for _, r := range reports {
counts[r.Decision]++
}
return fmt.Sprintf("%d bundles: %d allow, %d review, %d block",
len(reports), counts[DecisionAllow], counts[DecisionReview], counts[DecisionBlock])
}
// DecisionRank orders outcomes so a batch can report its worst case.
func DecisionRank(decision string) int {
switch decision {
case DecisionBlock:
return 2
case DecisionReview:
return 1
default:
return 0
}
}