Browse Source

[notifications] Fix bug, list show non active (#2678)

* Fix bug, show non active notifications and sort based on profiles

* diff fix
Laurence Jones 1 năm trước cách đây
mục cha
commit
2212c2f847

+ 16 - 31
cmd/crowdsec-cli/notifications.go

@@ -39,8 +39,7 @@ type NotificationsCfg struct {
 	ids      []uint
 }
 
-
-type cliNotifications struct {}
+type cliNotifications struct{}
 
 func NewCLINotifications() *cliNotifications {
 	return &cliNotifications{}
@@ -58,9 +57,6 @@ func (cli cliNotifications) NewCommand() *cobra.Command {
 			if err := require.LAPI(csConfig); err != nil {
 				return err
 			}
-			if err := require.Profiles(csConfig); err != nil {
-				return err
-			}
 			if err := require.Notifications(csConfig); err != nil {
 				return err
 			}
@@ -110,39 +106,28 @@ func getProfilesConfigs() (map[string]NotificationsCfg, error) {
 		return nil, err
 	}
 	ncfgs := map[string]NotificationsCfg{}
+	for _, pc := range pcfgs {
+		ncfgs[pc.Name] = NotificationsCfg{
+			Config: pc,
+		}
+	}
 	profiles, err := csprofiles.NewProfile(csConfig.API.Server.Profiles)
 	if err != nil {
 		return nil, fmt.Errorf("while extracting profiles from configuration: %w", err)
 	}
 	for profileID, profile := range profiles {
-	loop:
 		for _, notif := range profile.Cfg.Notifications {
-			for name, pc := range pcfgs {
-				if notif == name {
-					if _, ok := ncfgs[pc.Name]; !ok {
-						ncfgs[pc.Name] = NotificationsCfg{
-							Config:   pc,
-							Profiles: []*csconfig.ProfileCfg{profile.Cfg},
-							ids:      []uint{uint(profileID)},
-						}
-						continue loop
-					}
-					tmp := ncfgs[pc.Name]
-					for _, pr := range tmp.Profiles {
-						var profiles []*csconfig.ProfileCfg
-						if pr.Name == profile.Cfg.Name {
-							continue
-						}
-						profiles = append(tmp.Profiles, profile.Cfg)
-						ids := append(tmp.ids, uint(profileID))
-						ncfgs[pc.Name] = NotificationsCfg{
-							Config:   tmp.Config,
-							Profiles: profiles,
-							ids:      ids,
-						}
-					}
-				}
+			pc, ok := pcfgs[notif]
+			if !ok {
+				return nil, fmt.Errorf("notification plugin '%s' does not exist", notif)
+			}
+			tmp, ok := ncfgs[pc.Name]
+			if !ok {
+				return nil, fmt.Errorf("notification plugin '%s' does not exist", pc.Name)
 			}
+			tmp.Profiles = append(tmp.Profiles, profile.Cfg)
+			tmp.ids = append(tmp.ids, uint(profileID))
+			ncfgs[pc.Name] = tmp
 		}
 	}
 	return ncfgs, nil

+ 19 - 7
cmd/crowdsec-cli/notifications_table.go

@@ -2,24 +2,36 @@ package main
 
 import (
 	"io"
+	"sort"
 	"strings"
 
 	"github.com/aquasecurity/table"
+	"github.com/enescakir/emoji"
 )
 
 func notificationListTable(out io.Writer, ncfgs map[string]NotificationsCfg) {
 	t := newLightTable(out)
-	t.SetHeaders("Name", "Type", "Profile name")
-	t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
-	t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
-
-	for _, b := range ncfgs {
+	t.SetHeaders("Active", "Name", "Type", "Profile name")
+	t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
+	t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
+	keys := make([]string, 0, len(ncfgs))
+	for k := range ncfgs {
+		keys = append(keys, k)
+	}
+	sort.Slice(keys, func(i, j int) bool {
+		return len(ncfgs[keys[i]].Profiles) > len(ncfgs[keys[j]].Profiles)
+	})
+	for _, k := range keys {
+		b := ncfgs[k]
 		profilesList := []string{}
 		for _, p := range b.Profiles {
 			profilesList = append(profilesList, p.Name)
 		}
-		t.AddRow(b.Config.Name, b.Config.Type, strings.Join(profilesList, ", "))
+		active := emoji.CheckMark.String()
+		if len(profilesList) == 0 {
+			active = emoji.Prohibited.String()
+		}
+		t.AddRow(active, b.Config.Name, b.Config.Type, strings.Join(profilesList, ", "))
 	}
-
 	t.Render()
 }

+ 0 - 8
cmd/crowdsec-cli/require/require.go

@@ -54,14 +54,6 @@ func DB(c *csconfig.Config) error {
 	return nil
 }
 
-func Profiles(c *csconfig.Config) error {
-	if err := c.API.Server.LoadProfiles(); err != nil {
-		return fmt.Errorf("while loading profiles: %w", err)
-	}
-
-	return nil
-}
-
 func Notifications(c *csconfig.Config) error {
 	if c.ConfigPaths.NotificationDir == "" {
 		return fmt.Errorf("config_paths.notification_dir is not set in crowdsec config")