Skip to content

Commit 0e8b557

Browse files
authored
refact alert, decision filters: remove unnecessary pointers (#3607)
1 parent 4527ad0 commit 0e8b557

File tree

8 files changed

+128
-227
lines changed

8 files changed

+128
-227
lines changed

cmd/crowdsec-cli/clialert/alerts.go

Lines changed: 37 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ func (cli *cliAlerts) alertsToTable(alerts *models.GetAlertsResponse, printMachi
104104
if *alerts == nil {
105105
// avoid returning "null" in json
106106
// could be cleaner if we used slice of alerts directly
107-
fmt.Println("[]")
107+
fmt.Fprintln(os.Stdout, "[]")
108108
return nil
109109
}
110110

111111
x, _ := json.MarshalIndent(alerts, "", " ")
112-
fmt.Print(string(x))
112+
fmt.Fprint(os.Stdout, string(x))
113113
case "human":
114114
if len(*alerts) == 0 {
115-
fmt.Println("No active alerts")
115+
fmt.Fprintln(os.Stdout, "No active alerts")
116116
return nil
117117
}
118118

@@ -156,7 +156,7 @@ func (cli *cliAlerts) displayOneAlert(alert *models.Alert, withDetail bool) erro
156156
alertDecisionsTable(color.Output, cfg.Cscli.Color, alert)
157157

158158
if len(alert.Meta) > 0 {
159-
fmt.Printf("\n - Context :\n")
159+
fmt.Fprintf(os.Stdout, "\n - Context :\n")
160160
sort.Slice(alert.Meta, func(i, j int) bool {
161161
return alert.Meta[i].Key < alert.Meta[j].Key
162162
})
@@ -183,7 +183,7 @@ func (cli *cliAlerts) displayOneAlert(alert *models.Alert, withDetail bool) erro
183183
}
184184

185185
if withDetail {
186-
fmt.Printf("\n - Events :\n")
186+
fmt.Fprintf(os.Stdout, "\n - Events :\n")
187187

188188
for _, event := range alert.Events {
189189
alertEventTable(color.Output, cfg.Cscli.Color, event)
@@ -240,7 +240,7 @@ func (cli *cliAlerts) NewCommand() *cobra.Command {
240240
func (cli *cliAlerts) list(ctx context.Context, alertListFilter apiclient.AlertsListOpts, limit *int, contained *bool, printMachine bool) error {
241241
var err error
242242

243-
*alertListFilter.ScopeEquals, err = SanitizeScope(*alertListFilter.ScopeEquals, *alertListFilter.IPEquals, *alertListFilter.RangeEquals)
243+
alertListFilter.ScopeEquals, err = SanitizeScope(alertListFilter.ScopeEquals, alertListFilter.IPEquals, alertListFilter.RangeEquals)
244244
if err != nil {
245245
return err
246246
}
@@ -253,34 +253,6 @@ func (cli *cliAlerts) list(ctx context.Context, alertListFilter apiclient.Alerts
253253
*alertListFilter.Limit = 0
254254
}
255255

256-
if *alertListFilter.TypeEquals == "" {
257-
alertListFilter.TypeEquals = nil
258-
}
259-
260-
if *alertListFilter.ScopeEquals == "" {
261-
alertListFilter.ScopeEquals = nil
262-
}
263-
264-
if *alertListFilter.ValueEquals == "" {
265-
alertListFilter.ValueEquals = nil
266-
}
267-
268-
if *alertListFilter.ScenarioEquals == "" {
269-
alertListFilter.ScenarioEquals = nil
270-
}
271-
272-
if *alertListFilter.IPEquals == "" {
273-
alertListFilter.IPEquals = nil
274-
}
275-
276-
if *alertListFilter.RangeEquals == "" {
277-
alertListFilter.RangeEquals = nil
278-
}
279-
280-
if *alertListFilter.OriginEquals == "" {
281-
alertListFilter.OriginEquals = nil
282-
}
283-
284256
if contained != nil && *contained {
285257
alertListFilter.Contains = new(bool)
286258
}
@@ -299,16 +271,16 @@ func (cli *cliAlerts) list(ctx context.Context, alertListFilter apiclient.Alerts
299271

300272
func (cli *cliAlerts) newListCmd() *cobra.Command {
301273
alertListFilter := apiclient.AlertsListOpts{
302-
ScopeEquals: new(string),
303-
ValueEquals: new(string),
304-
ScenarioEquals: new(string),
305-
IPEquals: new(string),
306-
RangeEquals: new(string),
274+
ScopeEquals: "",
275+
ValueEquals: "",
276+
ScenarioEquals: "",
277+
IPEquals: "",
278+
RangeEquals: "",
307279
Since: cstime.DurationWithDays(0),
308280
Until: cstime.DurationWithDays(0),
309-
TypeEquals: new(string),
281+
TypeEquals: "",
310282
IncludeCAPI: new(bool),
311-
OriginEquals: new(string),
283+
OriginEquals: "",
312284
}
313285

314286
limit := new(int)
@@ -338,13 +310,13 @@ cscli alerts list --type ban`,
338310
flags.BoolVarP(alertListFilter.IncludeCAPI, "all", "a", false, "Include decisions from Central API")
339311
flags.Var(&alertListFilter.Until, "until", "restrict to alerts older than until (ie. 4h, 30d)")
340312
flags.Var(&alertListFilter.Since, "since", "restrict to alerts newer than since (ie. 4h, 30d)")
341-
flags.StringVarP(alertListFilter.IPEquals, "ip", "i", "", "restrict to alerts from this source ip (shorthand for --scope ip --value <IP>)")
342-
flags.StringVarP(alertListFilter.ScenarioEquals, "scenario", "s", "", "the scenario (ie. crowdsecurity/ssh-bf)")
343-
flags.StringVarP(alertListFilter.RangeEquals, "range", "r", "", "restrict to alerts from this range (shorthand for --scope range --value <RANGE/X>)")
344-
flags.StringVar(alertListFilter.TypeEquals, "type", "", "restrict to alerts with given decision type (ie. ban, captcha)")
345-
flags.StringVar(alertListFilter.ScopeEquals, "scope", "", "restrict to alerts of this scope (ie. ip,range)")
346-
flags.StringVarP(alertListFilter.ValueEquals, "value", "v", "", "the value to match for in the specified scope")
347-
flags.StringVar(alertListFilter.OriginEquals, "origin", "", fmt.Sprintf("the value to match for the specified origin (%s ...)", strings.Join(types.GetOrigins(), ",")))
313+
flags.StringVarP(&alertListFilter.IPEquals, "ip", "i", "", "restrict to alerts from this source ip (shorthand for --scope ip --value <IP>)")
314+
flags.StringVarP(&alertListFilter.ScenarioEquals, "scenario", "s", "", "the scenario (ie. crowdsecurity/ssh-bf)")
315+
flags.StringVarP(&alertListFilter.RangeEquals, "range", "r", "", "restrict to alerts from this range (shorthand for --scope range --value <RANGE/X>)")
316+
flags.StringVar(&alertListFilter.TypeEquals, "type", "", "restrict to alerts with given decision type (ie. ban, captcha)")
317+
flags.StringVar(&alertListFilter.ScopeEquals, "scope", "", "restrict to alerts of this scope (ie. ip,range)")
318+
flags.StringVarP(&alertListFilter.ValueEquals, "value", "v", "", "the value to match for in the specified scope")
319+
flags.StringVar(&alertListFilter.OriginEquals, "origin", "", fmt.Sprintf("the value to match for the specified origin (%s ...)", strings.Join(types.GetOrigins(), ",")))
348320
flags.BoolVar(contained, "contained", false, "query decisions contained by range")
349321
flags.BoolVarP(&printMachine, "machine", "m", false, "print machines that sent alerts")
350322
flags.IntVarP(limit, "limit", "l", 50, "limit size of alerts list table (0 to view all alerts)")
@@ -356,7 +328,7 @@ func (cli *cliAlerts) delete(ctx context.Context, delFilter apiclient.AlertsDele
356328
var err error
357329

358330
if !deleteAll {
359-
*delFilter.ScopeEquals, err = SanitizeScope(*delFilter.ScopeEquals, *delFilter.IPEquals, *delFilter.RangeEquals)
331+
delFilter.ScopeEquals, err = SanitizeScope(delFilter.ScopeEquals, delFilter.IPEquals, delFilter.RangeEquals)
360332
if err != nil {
361333
return err
362334
}
@@ -365,26 +337,6 @@ func (cli *cliAlerts) delete(ctx context.Context, delFilter apiclient.AlertsDele
365337
delFilter.ActiveDecisionEquals = activeDecision
366338
}
367339

368-
if *delFilter.ScopeEquals == "" {
369-
delFilter.ScopeEquals = nil
370-
}
371-
372-
if *delFilter.ValueEquals == "" {
373-
delFilter.ValueEquals = nil
374-
}
375-
376-
if *delFilter.ScenarioEquals == "" {
377-
delFilter.ScenarioEquals = nil
378-
}
379-
380-
if *delFilter.IPEquals == "" {
381-
delFilter.IPEquals = nil
382-
}
383-
384-
if *delFilter.RangeEquals == "" {
385-
delFilter.RangeEquals = nil
386-
}
387-
388340
if contained != nil && *contained {
389341
delFilter.Contains = new(bool)
390342
}
@@ -422,11 +374,11 @@ func (cli *cliAlerts) newDeleteCmd() *cobra.Command {
422374
)
423375

424376
delFilter := apiclient.AlertsDeleteOpts{
425-
ScopeEquals: new(string),
426-
ValueEquals: new(string),
427-
ScenarioEquals: new(string),
428-
IPEquals: new(string),
429-
RangeEquals: new(string),
377+
ScopeEquals: "",
378+
ValueEquals: "",
379+
ScenarioEquals: "",
380+
IPEquals: "",
381+
RangeEquals: "",
430382
}
431383

432384
contained := new(bool)
@@ -445,9 +397,9 @@ cscli alerts delete -s crowdsecurity/ssh-bf"`,
445397
if deleteAll {
446398
return nil
447399
}
448-
if *delFilter.ScopeEquals == "" && *delFilter.ValueEquals == "" &&
449-
*delFilter.ScenarioEquals == "" && *delFilter.IPEquals == "" &&
450-
*delFilter.RangeEquals == "" && delAlertByID == "" {
400+
if delFilter.ScopeEquals == "" && delFilter.ValueEquals == "" &&
401+
delFilter.ScenarioEquals == "" && delFilter.IPEquals == "" &&
402+
delFilter.RangeEquals == "" && delAlertByID == "" {
451403
_ = cmd.Usage()
452404
return errors.New("at least one filter or --all must be specified")
453405
}
@@ -461,11 +413,11 @@ cscli alerts delete -s crowdsecurity/ssh-bf"`,
461413

462414
flags := cmd.Flags()
463415
flags.SortFlags = false
464-
flags.StringVar(delFilter.ScopeEquals, "scope", "", "the scope (ie. ip,range)")
465-
flags.StringVarP(delFilter.ValueEquals, "value", "v", "", "the value to match for in the specified scope")
466-
flags.StringVarP(delFilter.ScenarioEquals, "scenario", "s", "", "the scenario (ie. crowdsecurity/ssh-bf)")
467-
flags.StringVarP(delFilter.IPEquals, "ip", "i", "", "Source ip (shorthand for --scope ip --value <IP>)")
468-
flags.StringVarP(delFilter.RangeEquals, "range", "r", "", "Range source ip (shorthand for --scope range --value <RANGE>)")
416+
flags.StringVar(&delFilter.ScopeEquals, "scope", "", "the scope (ie. ip,range)")
417+
flags.StringVarP(&delFilter.ValueEquals, "value", "v", "", "the value to match for in the specified scope")
418+
flags.StringVarP(&delFilter.ScenarioEquals, "scenario", "s", "", "the scenario (ie. crowdsecurity/ssh-bf)")
419+
flags.StringVarP(&delFilter.IPEquals, "ip", "i", "", "Source ip (shorthand for --scope ip --value <IP>)")
420+
flags.StringVarP(&delFilter.RangeEquals, "range", "r", "", "Range source ip (shorthand for --scope range --value <RANGE>)")
469421
flags.StringVar(&delAlertByID, "id", "", "alert ID")
470422
flags.BoolVarP(&deleteAll, "all", "a", false, "delete all alerts")
471423
flags.BoolVar(contained, "contained", false, "query decisions contained by range")
@@ -499,14 +451,14 @@ func (cli *cliAlerts) inspect(ctx context.Context, details bool, alertIDs ...str
499451
return fmt.Errorf("unable to serialize alert with id %s: %w", alertID, err)
500452
}
501453

502-
fmt.Printf("%s\n", string(data))
454+
fmt.Fprintln(os.Stdout, string(data))
503455
case "raw":
504456
data, err := yaml.Marshal(alert)
505457
if err != nil {
506458
return fmt.Errorf("unable to serialize alert with id %s: %w", alertID, err)
507459
}
508460

509-
fmt.Println(string(data))
461+
fmt.Fprintln(os.Stdout, string(data))
510462
}
511463
}
512464

@@ -536,7 +488,7 @@ func (cli *cliAlerts) newInspectCmd() *cobra.Command {
536488
func (cli *cliAlerts) newFlushCmd() *cobra.Command {
537489
var maxItems int
538490

539-
maxAge := cstime.DurationWithDays(7*24*time.Hour)
491+
maxAge := cstime.DurationWithDays(7 * 24 * time.Hour)
540492

541493
cmd := &cobra.Command{
542494
Use: `flush`,

cmd/crowdsec-cli/cliallowlists/allowlists.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"net/url"
11+
"os"
1112
"slices"
1213
"strconv"
1314
"strings"
@@ -283,7 +284,7 @@ func (cli *cliAllowLists) create(ctx context.Context, db *database.Client, name
283284
return err
284285
}
285286

286-
fmt.Printf("allowlist '%s' created successfully\n", name)
287+
fmt.Fprintf(os.Stdout, "allowlist '%s' created successfully\n", name)
287288

288289
return nil
289290
}
@@ -392,7 +393,7 @@ func (cli *cliAllowLists) delete(ctx context.Context, db *database.Client, name
392393
return err
393394
}
394395

395-
fmt.Printf("allowlist '%s' deleted successfully\n", name)
396+
fmt.Fprintf(os.Stdout, "allowlist '%s' deleted successfully\n", name)
396397

397398
return nil
398399
}
@@ -475,7 +476,7 @@ func (cli *cliAllowLists) add(ctx context.Context, db *database.Client, name str
475476
}
476477

477478
if len(toAdd) == 0 {
478-
fmt.Println("no new values for allowlist")
479+
fmt.Fprintln(os.Stdout, "no new values for allowlist")
479480
return nil
480481
}
481482

@@ -485,7 +486,7 @@ func (cli *cliAllowLists) add(ctx context.Context, db *database.Client, name str
485486
}
486487

487488
if added > 0 {
488-
fmt.Printf("added %d values to allowlist %s\n", added, name)
489+
fmt.Fprintf(os.Stdout, "added %d values to allowlist %s\n", added, name)
489490
}
490491

491492
deleted, err := db.ApplyAllowlistsToExistingDecisions(ctx)
@@ -622,7 +623,7 @@ func (cli *cliAllowLists) remove(ctx context.Context, db *database.Client, name
622623
}
623624

624625
if len(toRemove) == 0 {
625-
fmt.Println("no value to remove from allowlist")
626+
fmt.Fprintln(os.Stdout, "no value to remove from allowlist")
626627
return nil
627628
}
628629

@@ -632,7 +633,7 @@ func (cli *cliAllowLists) remove(ctx context.Context, db *database.Client, name
632633
}
633634

634635
if deleted > 0 {
635-
fmt.Printf("removed %d values from allowlist %s", deleted, name)
636+
fmt.Fprintf(os.Stdout, "removed %d values from allowlist %s", deleted, name)
636637
}
637638

638639
return nil

0 commit comments

Comments
 (0)