Skip to content

Commit

Permalink
🧹 Add bytes reporter to support the packer plugin
Browse files Browse the repository at this point in the history
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: mondoohq/packer-plugin-cnspec#170

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker committed Jan 10, 2024
1 parent 491537b commit 4fe1dd2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
5 changes: 5 additions & 0 deletions cli/reporter/aws_sqs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package reporter

import (
"bytes"
"context"
"fmt"
"regexp"
Expand Down Expand Up @@ -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:
Expand Down
32 changes: 32 additions & 0 deletions cli/reporter/bytes_handler.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions cli/reporter/cli_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions cli/reporter/file_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package reporter

import (
"bytes"
"context"
"os"
"strings"
Expand All @@ -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 {
Expand All @@ -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
}
8 changes: 8 additions & 0 deletions cli/reporter/output_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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:
Expand All @@ -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
}
Expand Down

0 comments on commit 4fe1dd2

Please sign in to comment.