Skip to content

Commit

Permalink
⭐️ proto report (#1223)
Browse files Browse the repository at this point in the history
* 🧹 refactor reporter format constants
* ⭐️ use proto reporter
* 🧹 update go mod
  • Loading branch information
chris-rock authored Apr 4, 2024
1 parent b3864f0 commit 433ce02
Show file tree
Hide file tree
Showing 21 changed files with 831 additions and 428 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ prep/tools:

# 🌙 cnspec #

cnspec/generate: clean/proto cli/generate policy/generate
cnspec/generate: clean/proto cli/generate policy/generate reporter/generate

.PHONY: cli
cli/generate:
Expand All @@ -78,6 +78,9 @@ policy/generate:
go generate ./policy/scan
go generate ./internal/bundle/yacit

reporter/generate:
go generate ./cli/reporter

# 🏗 Binary #

.PHONY: cnspec/build
Expand Down
17 changes: 8 additions & 9 deletions apps/cnspec/cmd/vuln.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
package cmd

import (
"bytes"
"strings"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -13,10 +14,8 @@ import (
"go.mondoo.com/cnquery/v10/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/upstream/mvd"
"go.mondoo.com/cnquery/v10/sbom"
"go.mondoo.com/cnquery/v10/shared"
"go.mondoo.com/cnspec/v10/cli/reporter"
"go.mondoo.com/cnspec/v10/policy"
"strings"
)

func init() {
Expand Down Expand Up @@ -62,17 +61,17 @@ var vulnCmdRun = func(cmd *cobra.Command, runtime *providers.Runtime, cliRes *pl

report, err := RunScan(conf)
if err != nil {
log.Fatal().Err(err).Msg("error happened during package analysis")
log.Fatal().Err(err).Msg("failed to run scan")
}

buf := bytes.Buffer{}
w := shared.IOWriter{Writer: &buf}
err = reporter.ReportCollectionToJSON(report, &w)
cnspecReport, err := reporter.ConvertToProto(report)
if err == nil {
logger.DebugDumpJSON("mondoo-sbom-report", buf.Bytes())
log.Debug().Msg("converted report to proto")
data, _ := cnspecReport.ToJSON()
logger.DebugDumpJSON("mondoo-sbom-report", data)
}

boms, err := sbom.NewBom(buf.Bytes())
boms, err := sbom.NewBom(cnspecReport.ToCnqueryReport())
if err != nil {
log.Fatal().Err(err).Msg("failed to parse sbom data")
}
Expand Down
4 changes: 2 additions & 2 deletions cli/reporter/aws_sqs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (h *awsSqsHandler) WriteReport(ctx context.Context, report *policy.ReportCo

func (h *awsSqsHandler) convertReport(report *policy.ReportCollection) ([]byte, error) {
switch h.format {
case YAML:
case FormatYAML:
return reportToYaml(report)
case JSON:
case FormatJSON:
return reportToJson(report)
default:
return nil, fmt.Errorf("'%s' is not supported in the aws sqs handler, please use one of the other formats", string(h.format))
Expand Down
6 changes: 3 additions & 3 deletions cli/reporter/azure_service_bus_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (h *azureSbusHandler) WriteReport(ctx context.Context, report *policy.Repor
msg := &azservicebus.Message{
Body: data,
}
if h.format == JSON {
if h.format == FormatJSON {
typ := "application/json"
msg.ContentType = &typ
}
Expand All @@ -74,9 +74,9 @@ func (h *azureSbusHandler) WriteReport(ctx context.Context, report *policy.Repor

func (h *azureSbusHandler) convertReport(report *policy.ReportCollection) ([]byte, error) {
switch h.format {
case YAML:
case FormatYAML:
return reportToYaml(report)
case JSON:
case FormatJSON:
return reportToJson(report)
default:
return nil, fmt.Errorf("'%s' is not supported in the azure service bus handler, please use one of the other formats", string(h.format))
Expand Down
36 changes: 18 additions & 18 deletions cli/reporter/cli_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ func (r *Reporter) WithOutput(out io.Writer) *Reporter {

func (r *Reporter) WriteReport(ctx context.Context, data *policy.ReportCollection) error {
switch r.Format {
case Compact:
case FormatCompact:
rr := &defaultReporter{
Reporter: r,
isCompact: true,
output: r.out,
data: data,
}
return rr.print()
case Summary:
case FormatSummary:
rr := &defaultReporter{
Reporter: r,
isCompact: true,
Expand All @@ -126,22 +126,22 @@ func (r *Reporter) WriteReport(ctx context.Context, data *policy.ReportCollectio
data: data,
}
return rr.print()
case Full:
case FormatFull:
rr := &defaultReporter{
Reporter: r,
isCompact: false,
output: r.out,
data: data,
}
return rr.print()
case Report:
case FormatReport:
rr := &reportRenderer{
printer: r.Printer,
out: r.out,
data: data,
}
return rr.print()
case YAML:
case FormatYAML:
yaml, err := reportToYaml(data)
if err != nil {
return err
Expand All @@ -150,13 +150,13 @@ func (r *Reporter) WriteReport(ctx context.Context, data *policy.ReportCollectio
_, err = r.out.Write(yaml)
return err

case JSON:
case FormatJSON:
writer := shared.IOWriter{Writer: r.out}
return ReportCollectionToJSON(data, &writer)
case JUnit:
return ConvertToJSON(data, &writer)
case FormatJUnit:
writer := shared.IOWriter{Writer: r.out}
return ReportCollectionToJunit(data, &writer)
// case CSV:
return ConvertToJunit(data, &writer)
// case FormatCSV:
// res, err = data.ToCsv()
default:
return errors.New("unknown reporter type, don't recognize this Format")
Expand All @@ -165,7 +165,7 @@ func (r *Reporter) WriteReport(ctx context.Context, data *policy.ReportCollectio

func (r *Reporter) PrintVulns(data *mvd.VulnReport, target string) error {
switch r.Format {
case Compact:
case FormatCompact:
rr := &defaultVulnReporter{
Reporter: r,
isCompact: true,
Expand All @@ -174,7 +174,7 @@ func (r *Reporter) PrintVulns(data *mvd.VulnReport, target string) error {
target: target,
}
return rr.print()
case Summary:
case FormatSummary:
rr := &defaultVulnReporter{
Reporter: r,
isCompact: true,
Expand All @@ -184,7 +184,7 @@ func (r *Reporter) PrintVulns(data *mvd.VulnReport, target string) error {
target: target,
}
return rr.print()
case Full:
case FormatFull:
rr := &defaultVulnReporter{
Reporter: r,
isCompact: false,
Expand All @@ -193,14 +193,14 @@ func (r *Reporter) PrintVulns(data *mvd.VulnReport, target string) error {
target: target,
}
return rr.print()
case Report:
case FormatReport:
return errors.New("'report' is not supported for vuln reports, please use one of the other formats")
case JUnit:
case FormatJUnit:
return errors.New("'junit' is not supported for vuln reports, please use one of the other formats")
case CSV:
case FormatCSV:
writer := shared.IOWriter{Writer: r.out}
return VulnReportToCSV(data, &writer)
case YAML:
case FormatYAML:
raw := bytes.Buffer{}
writer := shared.IOWriter{Writer: &raw}
err := VulnReportToJSON(target, data, &writer)
Expand All @@ -214,7 +214,7 @@ func (r *Reporter) PrintVulns(data *mvd.VulnReport, target string) error {
}
_, err = r.out.Write(json)
return err
case JSON:
case FormatJSON:
writer := shared.IOWriter{Writer: r.out}
return VulnReportToJSON(target, data, &writer)
default:
Expand Down
67 changes: 5 additions & 62 deletions cli/reporter/cli_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package reporter

import (
"bytes"
"context"
"encoding/json"
"os"
"testing"
Expand All @@ -31,7 +30,7 @@ func TestCompactReporter(t *testing.T) {
writer := shared.IOWriter{Writer: &buf}

r := &Reporter{
Format: Compact,
Format: FormatCompact,
Printer: &printer.DefaultPrinter,
Colors: &colors.DefaultColorTheme,
}
Expand Down Expand Up @@ -61,23 +60,23 @@ func TestVulnReporter(t *testing.T) {
buf := bytes.Buffer{}
writer := shared.IOWriter{Writer: &buf}

r := NewReporter(Summary, false)
r := NewReporter(FormatSummary, false)
r.out = &writer
require.NoError(t, err)

target := "index.docker.io/library/ubuntu@669e010b58ba"
err = r.PrintVulns(report, target)
require.NoError(t, err)

r = NewReporter(Compact, false)
r = NewReporter(FormatCompact, false)
r.out = &writer
err = r.PrintVulns(report, target)
require.NoError(t, err)

assert.Contains(t, buf.String(), "5.5 libblkid1 2.34-0.1ubuntu9.1")
assert.NotContains(t, buf.String(), "USN-5279-1")

r = NewReporter(Full, false)
r = NewReporter(FormatFull, false)
r.out = &writer
require.NoError(t, err)

Expand All @@ -87,7 +86,7 @@ func TestVulnReporter(t *testing.T) {
assert.Contains(t, buf.String(), "5.5 libblkid1 2.34-0.1ubuntu9.1")
assert.Contains(t, buf.String(), "USN-5279-1")

r = NewReporter(YAML, false)
r = NewReporter(FormatYAML, false)
r.out = &writer
require.NoError(t, err)

Expand All @@ -99,59 +98,3 @@ func TestVulnReporter(t *testing.T) {
assert.Contains(t, buf.String(), "installed: 2.34-0.1ubuntu9.1")
assert.Contains(t, buf.String(), "advisory: USN-5279-1")
}

func TestJsonOutput(t *testing.T) {
reportCollectionRaw, err := os.ReadFile("./testdata/report-ubuntu.json")
require.NoError(t, err)

yr := &policy.ReportCollection{}
err = json.Unmarshal(reportCollectionRaw, yr)
require.NoError(t, err)

buf := bytes.Buffer{}
writer := shared.IOWriter{Writer: &buf}

r := &Reporter{
Format: JSON,
Printer: &printer.DefaultPrinter,
Colors: &colors.DefaultColorTheme,
out: &writer,
}

err = r.WriteReport(context.Background(), yr)
require.NoError(t, err)
valid := json.Valid(buf.Bytes())
require.True(t, valid)

assert.Contains(t, buf.String(), "//policy.api.mondoo.app/queries/mondoo-linux-security-permissions-on-etcgshadow-are-configured\":{\"score\":100,\"status\":\"pass\"}")
assert.Contains(t, buf.String(), "\"errors\":{}")
}

func TestJsonOutputOnlyErrors(t *testing.T) {
reportCollectionRaw, err := os.ReadFile("./testdata/report-k8s.json")
require.NoError(t, err)

yr := &policy.ReportCollection{}
err = json.Unmarshal(reportCollectionRaw, yr)
require.NoError(t, err)

buf := bytes.Buffer{}
writer := shared.IOWriter{Writer: &buf}

r := &Reporter{
Format: JSON,
Printer: &printer.DefaultPrinter,
Colors: &colors.DefaultColorTheme,
out: &writer,
}

err = r.WriteReport(context.Background(), yr)
require.NoError(t, err)
valid := json.Valid(buf.Bytes())
require.True(t, valid)

assert.NotContains(t, buf.String(), "{\"score\":100,\"status\":\"pass\"}")
assert.NotContains(t, buf.String(), "\"errors\":{}\"")

assert.Contains(t, buf.String(), "\"data\":{},\"scores\":{},\"errors\":{\"//policy")
}
Loading

0 comments on commit 433ce02

Please sign in to comment.