[notifications] Fix bug, list show non active (#2678)
* Fix bug, show non active notifications and sort based on profiles * diff fix
This commit is contained in:
parent
33e3fdabe4
commit
2212c2f847
|
@ -39,7 +39,6 @@ type NotificationsCfg struct {
|
||||||
ids []uint
|
ids []uint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type cliNotifications struct{}
|
type cliNotifications struct{}
|
||||||
|
|
||||||
func NewCLINotifications() *cliNotifications {
|
func NewCLINotifications() *cliNotifications {
|
||||||
|
@ -58,9 +57,6 @@ func (cli cliNotifications) NewCommand() *cobra.Command {
|
||||||
if err := require.LAPI(csConfig); err != nil {
|
if err := require.LAPI(csConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := require.Profiles(csConfig); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := require.Notifications(csConfig); err != nil {
|
if err := require.Notifications(csConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -110,39 +106,28 @@ func getProfilesConfigs() (map[string]NotificationsCfg, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ncfgs := map[string]NotificationsCfg{}
|
ncfgs := map[string]NotificationsCfg{}
|
||||||
|
for _, pc := range pcfgs {
|
||||||
|
ncfgs[pc.Name] = NotificationsCfg{
|
||||||
|
Config: pc,
|
||||||
|
}
|
||||||
|
}
|
||||||
profiles, err := csprofiles.NewProfile(csConfig.API.Server.Profiles)
|
profiles, err := csprofiles.NewProfile(csConfig.API.Server.Profiles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("while extracting profiles from configuration: %w", err)
|
return nil, fmt.Errorf("while extracting profiles from configuration: %w", err)
|
||||||
}
|
}
|
||||||
for profileID, profile := range profiles {
|
for profileID, profile := range profiles {
|
||||||
loop:
|
|
||||||
for _, notif := range profile.Cfg.Notifications {
|
for _, notif := range profile.Cfg.Notifications {
|
||||||
for name, pc := range pcfgs {
|
pc, ok := pcfgs[notif]
|
||||||
if notif == name {
|
if !ok {
|
||||||
if _, ok := ncfgs[pc.Name]; !ok {
|
return nil, fmt.Errorf("notification plugin '%s' does not exist", notif)
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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
|
return ncfgs, nil
|
||||||
|
|
|
@ -2,24 +2,36 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/aquasecurity/table"
|
"github.com/aquasecurity/table"
|
||||||
|
"github.com/enescakir/emoji"
|
||||||
)
|
)
|
||||||
|
|
||||||
func notificationListTable(out io.Writer, ncfgs map[string]NotificationsCfg) {
|
func notificationListTable(out io.Writer, ncfgs map[string]NotificationsCfg) {
|
||||||
t := newLightTable(out)
|
t := newLightTable(out)
|
||||||
t.SetHeaders("Name", "Type", "Profile name")
|
t.SetHeaders("Active", "Name", "Type", "Profile name")
|
||||||
t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
keys := make([]string, 0, len(ncfgs))
|
||||||
for _, b := range 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{}
|
profilesList := []string{}
|
||||||
for _, p := range b.Profiles {
|
for _, p := range b.Profiles {
|
||||||
profilesList = append(profilesList, p.Name)
|
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()
|
t.Render()
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,14 +54,6 @@ func DB(c *csconfig.Config) error {
|
||||||
return nil
|
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 {
|
func Notifications(c *csconfig.Config) error {
|
||||||
if c.ConfigPaths.NotificationDir == "" {
|
if c.ConfigPaths.NotificationDir == "" {
|
||||||
return fmt.Errorf("config_paths.notification_dir is not set in crowdsec config")
|
return fmt.Errorf("config_paths.notification_dir is not set in crowdsec config")
|
||||||
|
|
Loading…
Reference in a new issue