Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"github.com/spf13/cobra"
)

var dryrun bool

var notifyCmd = &cobra.Command{
Use: "notify [CONFIG_FILE]",
Short: "notify",
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
14 changes: 12 additions & 2 deletions sechub/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion sechub/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down