From 4fe1dd299b2798901a7cbd744c10871b7c96190c Mon Sep 17 00:00:00 2001 From: Christian Zunker Date: Wed, 10 Jan 2024 10:42:35 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20Add=20bytes=20reporter=20to=20su?= =?UTF-8?q?pport=20the=20packer=20plugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds a reporter which allows to access the rendered report as bytes.Buffer. This is needed, so we can output it inside the packer plugin. For reference: https://github.com/mondoohq/packer-plugin-cnspec/pull/170 Signed-off-by: Christian Zunker --- cli/reporter/aws_sqs_handler.go | 5 +++++ cli/reporter/bytes_handler.go | 32 ++++++++++++++++++++++++++++++++ cli/reporter/cli_reporter.go | 4 ++++ cli/reporter/file_handler.go | 8 +++++--- cli/reporter/output_handler.go | 8 ++++++++ 5 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 cli/reporter/bytes_handler.go diff --git a/cli/reporter/aws_sqs_handler.go b/cli/reporter/aws_sqs_handler.go index 920f013f..f67a48bd 100644 --- a/cli/reporter/aws_sqs_handler.go +++ b/cli/reporter/aws_sqs_handler.go @@ -4,6 +4,7 @@ package reporter import ( + "bytes" "context" "fmt" "regexp" @@ -45,6 +46,10 @@ func (h *awsSqsHandler) WriteReport(ctx context.Context, report *policy.ReportCo return nil } +func (h *awsSqsHandler) OutputReport(ctx context.Context) *bytes.Buffer { + return nil +} + func (h *awsSqsHandler) convertReport(report *policy.ReportCollection) ([]byte, error) { switch h.format { case YAML: diff --git a/cli/reporter/bytes_handler.go b/cli/reporter/bytes_handler.go new file mode 100644 index 00000000..3596be2a --- /dev/null +++ b/cli/reporter/bytes_handler.go @@ -0,0 +1,32 @@ +// Copyright (c) Mondoo, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package reporter + +import ( + "bytes" + "context" + + "go.mondoo.com/cnspec/v9/policy" + _ "gocloud.dev/pubsub/awssnssqs" +) + +type byteWriterHandler struct { + data *bytes.Buffer + format Format +} + +func (h *byteWriterHandler) WriteReport(ctx context.Context, report *policy.ReportCollection) error { + reporter := NewReporter(h.format, false) + h.data = &bytes.Buffer{} + reporter.out = h.data + err := reporter.WriteReport(ctx, report) + if err != nil { + return err + } + return nil +} + +func (h *byteWriterHandler) OutputReport(ctx context.Context) *bytes.Buffer { + return h.data +} diff --git a/cli/reporter/cli_reporter.go b/cli/reporter/cli_reporter.go index 3d0bbc47..6029927c 100644 --- a/cli/reporter/cli_reporter.go +++ b/cli/reporter/cli_reporter.go @@ -157,6 +157,10 @@ func (r *Reporter) WriteReport(ctx context.Context, data *policy.ReportCollectio } } +func (r *Reporter) OutputReport(ctx context.Context) *bytes.Buffer { + return nil +} + func (r *Reporter) PrintVulns(data *mvd.VulnReport, target string) error { switch r.Format { case Compact: diff --git a/cli/reporter/file_handler.go b/cli/reporter/file_handler.go index 60d2e5c9..5b0e637e 100644 --- a/cli/reporter/file_handler.go +++ b/cli/reporter/file_handler.go @@ -4,6 +4,7 @@ package reporter import ( + "bytes" "context" "os" "strings" @@ -27,9 +28,6 @@ func (h *localFileHandler) WriteReport(ctx context.Context, report *policy.Repor } defer f.Close() //nolint: errcheck reporter := NewReporter(h.format, false) - if err != nil { - return err - } reporter.out = f err = reporter.WriteReport(ctx, report) if err != nil { @@ -38,3 +36,7 @@ func (h *localFileHandler) WriteReport(ctx context.Context, report *policy.Repor log.Info().Str("file", trimmedFile).Msg("wrote report to file") return nil } + +func (h *localFileHandler) OutputReport(ctx context.Context) *bytes.Buffer { + return nil +} diff --git a/cli/reporter/output_handler.go b/cli/reporter/output_handler.go index 6aa12f19..87243043 100644 --- a/cli/reporter/output_handler.go +++ b/cli/reporter/output_handler.go @@ -27,10 +27,13 @@ const ( CLI OutputTarget = iota + 1 LOCAL_FILE AWS_SQS + BYTE_WRITER ) type OutputHandler interface { WriteReport(ctx context.Context, report *policy.ReportCollection) error + // Return the saved report as a byte array. Only suported for the byte writer type + OutputReport(ctx context.Context) *bytes.Buffer } func NewOutputHandler(config HandlerConfig) (OutputHandler, error) { @@ -44,6 +47,8 @@ func NewOutputHandler(config HandlerConfig) (OutputHandler, error) { return &localFileHandler{file: config.OutputTarget, format: format}, nil case AWS_SQS: return &awsSqsHandler{sqsQueueUrl: config.OutputTarget, format: format}, nil + case BYTE_WRITER: + return &byteWriterHandler{format: format}, nil case CLI: fallthrough default: @@ -61,6 +66,9 @@ func determineOutputType(target string) OutputTarget { if sqsRegex.MatchString(target) { return AWS_SQS } + if target == "bytes" { + return BYTE_WRITER + } return LOCAL_FILE }