diff --git a/cmd/notify.go b/cmd/notify.go index 14fa468..4231ba3 100644 --- a/cmd/notify.go +++ b/cmd/notify.go @@ -33,6 +33,8 @@ import ( "github.com/spf13/cobra" ) +var dryrun bool + var notifyCmd = &cobra.Command{ Use: "notify [CONFIG_FILE]", Short: "notify", @@ -68,7 +70,7 @@ var notifyCmd = &cobra.Command{ if err != nil { return err } - if err := hub.Notify(ctx, cfg, findings); err != nil { + if err := hub.Notify(ctx, cfg, findings, dryrun); err != nil { return err } return nil @@ -78,6 +80,8 @@ var notifyCmd = &cobra.Command{ func init() { rootCmd.AddCommand(notifyCmd) notifyCmd.Flags().StringSliceVarP(&overlays, "overlay", "", []string{}, "patch file or directory for overlaying") + notifyCmd.Flags().BoolVarP(&dryrun, "dryrun", "s", false, "output notifications to stdout") + } func collectActiveFindings(ctx context.Context, cfg aws.Config) ([]sechub.NotifyFinding, error) { diff --git a/sechub/notify.go b/sechub/notify.go index c37e5f9..8413f76 100644 --- a/sechub/notify.go +++ b/sechub/notify.go @@ -80,7 +80,7 @@ type NotifyFinding struct { WorkflowStatus types.WorkflowStatus } -func (sh *SecHub) Notify(ctx context.Context, cfg aws.Config, findings []NotifyFinding) error { +func (sh *SecHub) Notify(ctx context.Context, cfg aws.Config, findings []NotifyFinding, dryrun bool) error { urep := strings.NewReplacer("ap-northeast-1", cfg.Region) now := time.Now() env := map[string]interface{}{ @@ -119,7 +119,7 @@ func (sh *SecHub) Notify(ctx context.Context, cfg aws.Config, findings []NotifyF if n.If == "" { return errors.New("no cond") } - if n.WebhookURL == "" { + if !dryrun && n.WebhookURL == "" { return errors.New("no webhookURL") } env["header"] = n.Header @@ -139,6 +139,16 @@ func (sh *SecHub) Notify(ctx context.Context, cfg aws.Config, findings []NotifyF if err != nil { return err } + + if dryrun { + var out bytes.Buffer + if err := json.Indent(&out, b, "", " "); err != nil { + return err + } + fmt.Println(out.String()) + continue + } + req, err := http.NewRequest( http.MethodPost, n.WebhookURL, diff --git a/sechub/notify_test.go b/sechub/notify_test.go index fcd4785..b7506b5 100644 --- a/sechub/notify_test.go +++ b/sechub/notify_test.go @@ -116,6 +116,19 @@ func TestNotify(t *testing.T) { }, true, }, + { + "dryrun true", + &Notification{ + If: "true", + }, + []NotifyFinding{ + { + SeverityLabel: types.SeverityLabelCritical, + WorkflowStatus: types.WorkflowStatusNew, + }, + }, + false, + }, } ctx := context.Background() cfg, err := config.LoadDefaultConfig(ctx) @@ -135,7 +148,8 @@ func TestNotify(t *testing.T) { tt.notification.WebhookURL = ts.URL sh := New(region) sh.Notifications = append(sh.Notifications, tt.notification) - if err := sh.Notify(ctx, cfg, tt.findings); err != nil { + dryrun := tt.name == "dryrun true" + if err := sh.Notify(ctx, cfg, tt.findings, dryrun); err != nil { t.Error(err) } if len(r.Requests()) == 0 {