-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐ Add output handler to store asset reports (#982)
* ⭐ Add output handler to store asset reports. * ✨ Rework output target handling. --------- Signed-off-by: Preslav <[email protected]>
- Loading branch information
1 parent
c6825f1
commit 2034124
Showing
15 changed files
with
427 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package reporter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/rs/zerolog/log" | ||
"go.mondoo.com/cnspec/v9/policy" | ||
"gocloud.dev/pubsub" | ||
_ "gocloud.dev/pubsub/awssnssqs" | ||
) | ||
|
||
var sqsRegex = regexp.MustCompile(`(https:\/\/|http:\/\/)?(sqs)[.][a-z]{2}[-][a-z]{3,}[-][0-9]{1}[.](amazonaws.com)[\/][0-9]{12}[\/]{1}[a-zA-Z0-9-_]*`) | ||
|
||
type awsSqsHandler struct { | ||
sqsQueueUrl string | ||
format Format | ||
} | ||
|
||
func (h *awsSqsHandler) WriteReport(ctx context.Context, report *policy.ReportCollection) error { | ||
// the url may be passed in with a https:// or an http:// prefix, we can trim those | ||
trimmedUrl := strings.TrimPrefix(h.sqsQueueUrl, "https://") | ||
trimmedUrl = strings.TrimPrefix(trimmedUrl, "http://") | ||
topic, err := pubsub.OpenTopic(ctx, "awssqs://"+trimmedUrl) | ||
if err != nil { | ||
return err | ||
} | ||
defer topic.Shutdown(ctx) //nolint: errcheck | ||
data, err := h.convertReport(report) | ||
if err != nil { | ||
return err | ||
} | ||
err = topic.Send(ctx, &pubsub.Message{ | ||
Body: data, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
log.Info().Str("url", h.sqsQueueUrl).Msg("sent report to SQS queue") | ||
return nil | ||
} | ||
|
||
func (h *awsSqsHandler) convertReport(report *policy.ReportCollection) ([]byte, error) { | ||
switch h.format { | ||
case YAML: | ||
return reportToYaml(report) | ||
case JSON: | ||
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)) | ||
} | ||
} |
Oops, something went wrong.