Skip to content

Commit

Permalink
cscli refact: package clialert, clidecision (#3203)
Browse files Browse the repository at this point in the history
* cscli refact: package clialert, clidecision

* refact: function SanitizeScope()

* lint
  • Loading branch information
mmetc authored Sep 3, 2024
1 parent 5a50fd0 commit bc6be99
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 89 deletions.
17 changes: 11 additions & 6 deletions cmd/crowdsec-cli/alerts.go → cmd/crowdsec-cli/clialert/alerts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clialert

import (
"context"
Expand All @@ -24,6 +24,7 @@ import (
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
"github.com/crowdsecurity/crowdsec/pkg/models"
"github.com/crowdsecurity/crowdsec/pkg/types"
Expand Down Expand Up @@ -183,12 +184,14 @@ func (cli *cliAlerts) displayOneAlert(alert *models.Alert, withDetail bool) erro
return nil
}

type configGetter func() *csconfig.Config

type cliAlerts struct {
client *apiclient.ApiClient
cfg configGetter
}

func NewCLIAlerts(getconfig configGetter) *cliAlerts {
func New(getconfig configGetter) *cliAlerts {
return &cliAlerts{
cfg: getconfig,
}
Expand Down Expand Up @@ -235,8 +238,10 @@ func (cli *cliAlerts) NewCommand() *cobra.Command {
}

func (cli *cliAlerts) list(alertListFilter apiclient.AlertsListOpts, limit *int, contained *bool, printMachine bool) error {
if err := manageCliDecisionAlerts(alertListFilter.IPEquals, alertListFilter.RangeEquals,
alertListFilter.ScopeEquals, alertListFilter.ValueEquals); err != nil {
var err error

*alertListFilter.ScopeEquals, err = SanitizeScope(*alertListFilter.ScopeEquals, *alertListFilter.IPEquals, *alertListFilter.RangeEquals)
if err != nil {
return err
}

Expand Down Expand Up @@ -378,8 +383,8 @@ func (cli *cliAlerts) delete(alertDeleteFilter apiclient.AlertsDeleteOpts, Activ
var err error

if !AlertDeleteAll {
if err = manageCliDecisionAlerts(alertDeleteFilter.IPEquals, alertDeleteFilter.RangeEquals,
alertDeleteFilter.ScopeEquals, alertDeleteFilter.ValueEquals); err != nil {
*alertDeleteFilter.ScopeEquals, err = SanitizeScope(*alertDeleteFilter.ScopeEquals, *alertDeleteFilter.IPEquals, *alertDeleteFilter.RangeEquals)
if err != nil {
return err
}

Expand Down
26 changes: 26 additions & 0 deletions cmd/crowdsec-cli/clialert/sanitize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package clialert

import (
"fmt"
"net"

"github.com/crowdsecurity/crowdsec/pkg/types"
)

// SanitizeScope validates ip and range and sets the scope accordingly to our case convention.
func SanitizeScope(scope, ip, ipRange string) (string, error) {
if ipRange != "" {
_, _, err := net.ParseCIDR(ipRange)
if err != nil {
return "", fmt.Errorf("%s is not a valid range", ipRange)
}
}

if ip != "" {
if net.ParseIP(ip) == nil {
return "", fmt.Errorf("%s is not a valid ip", ip)
}
}

return types.NormalizeScope(scope), nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clialert

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clidecision

import (
"context"
Expand All @@ -17,7 +17,9 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clialert"
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
"github.com/crowdsecurity/crowdsec/pkg/models"
"github.com/crowdsecurity/crowdsec/pkg/types"
Expand Down Expand Up @@ -114,12 +116,14 @@ func (cli *cliDecisions) decisionsToTable(alerts *models.GetAlertsResponse, prin
return nil
}

type configGetter func() *csconfig.Config

type cliDecisions struct {
client *apiclient.ApiClient
cfg configGetter
}

func NewCLIDecisions(cfg configGetter) *cliDecisions {
func New(cfg configGetter) *cliDecisions {
return &cliDecisions{
cfg: cfg,
}
Expand Down Expand Up @@ -170,8 +174,9 @@ func (cli *cliDecisions) NewCommand() *cobra.Command {

func (cli *cliDecisions) list(filter apiclient.AlertsListOpts, NoSimu *bool, contained *bool, printMachine bool) error {
var err error
/*take care of shorthand options*/
if err = manageCliDecisionAlerts(filter.IPEquals, filter.RangeEquals, filter.ScopeEquals, filter.ValueEquals); err != nil {

*filter.ScopeEquals, err = clialert.SanitizeScope(*filter.ScopeEquals, *filter.IPEquals, *filter.RangeEquals)
if err != nil {
return err
}

Expand Down Expand Up @@ -326,8 +331,10 @@ func (cli *cliDecisions) add(addIP, addRange, addDuration, addValue, addScope, a
stopAt := time.Now().UTC().Format(time.RFC3339)
createdAt := time.Now().UTC().Format(time.RFC3339)

/*take care of shorthand options*/
if err := manageCliDecisionAlerts(&addIP, &addRange, &addScope, &addValue); err != nil {
var err error

addScope, err = clialert.SanitizeScope(addScope, addIP, addRange)
if err != nil {
return err
}

Expand Down Expand Up @@ -381,7 +388,7 @@ func (cli *cliDecisions) add(addIP, addRange, addDuration, addValue, addScope, a
}
alerts = append(alerts, &alert)

_, _, err := cli.client.Alerts.Add(context.Background(), alerts)
_, _, err = cli.client.Alerts.Add(context.Background(), alerts)
if err != nil {
return err
}
Expand Down Expand Up @@ -435,7 +442,8 @@ func (cli *cliDecisions) delete(delFilter apiclient.DecisionsDeleteOpts, delDeci
var err error

/*take care of shorthand options*/
if err = manageCliDecisionAlerts(delFilter.IPEquals, delFilter.RangeEquals, delFilter.ScopeEquals, delFilter.ValueEquals); err != nil {
*delFilter.ScopeEquals, err = clialert.SanitizeScope(*delFilter.ScopeEquals, *delFilter.IPEquals, *delFilter.RangeEquals)
if err != nil {
return err
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clidecision

import (
"bufio"
Expand Down Expand Up @@ -122,8 +122,8 @@ func (cli *cliDecisions) runImport(cmd *cobra.Command, args []string) error {
}

var (
content []byte
fin *os.File
content []byte
fin *os.File
)

// set format if the file has a json or csv extension
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package clidecision

import (
"fmt"
Expand Down
6 changes: 4 additions & 2 deletions cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (

"github.com/crowdsecurity/go-cs-lib/trace"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clialert"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clibouncer"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clicapi"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliconsole"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clidecision"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliexplain"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clihub"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clihubtest"
Expand Down Expand Up @@ -257,8 +259,8 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
cmd.AddCommand(clihub.New(cli.cfg).NewCommand())
cmd.AddCommand(climetrics.New(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIDashboard(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIDecisions(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIAlerts(cli.cfg).NewCommand())
cmd.AddCommand(clidecision.New(cli.cfg).NewCommand())
cmd.AddCommand(clialert.New(cli.cfg).NewCommand())
cmd.AddCommand(clisimulation.New(cli.cfg).NewCommand())
cmd.AddCommand(clibouncer.New(cli.cfg).NewCommand())
cmd.AddCommand(climachine.New(cli.cfg).NewCommand())
Expand Down
40 changes: 0 additions & 40 deletions cmd/crowdsec-cli/utils.go

This file was deleted.

22 changes: 3 additions & 19 deletions pkg/apiserver/controllers/v1/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net"
"net/http"
"strconv"
"strings"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -124,21 +123,6 @@ func (c *Controller) sendAlertToPluginChannel(alert *models.Alert, profileID uin
}
}

func normalizeScope(scope string) string {
switch strings.ToLower(scope) {
case "ip":
return types.Ip
case "range":
return types.Range
case "as":
return types.AS
case "country":
return types.Country
default:
return scope
}
}

// CreateAlert writes the alerts received in the body to the database
func (c *Controller) CreateAlert(gctx *gin.Context) {
var input models.AddAlertsRequest
Expand All @@ -160,12 +144,12 @@ func (c *Controller) CreateAlert(gctx *gin.Context) {
for _, alert := range input {
// normalize scope for alert.Source and decisions
if alert.Source.Scope != nil {
*alert.Source.Scope = normalizeScope(*alert.Source.Scope)
*alert.Source.Scope = types.NormalizeScope(*alert.Source.Scope)
}

for _, decision := range alert.Decisions {
if decision.Scope != nil {
*decision.Scope = normalizeScope(*decision.Scope)
*decision.Scope = types.NormalizeScope(*decision.Scope)
}
}

Expand Down Expand Up @@ -296,8 +280,8 @@ func (c *Controller) FindAlerts(gctx *gin.Context) {
// FindAlertByID returns the alert associated with the ID
func (c *Controller) FindAlertByID(gctx *gin.Context) {
alertIDStr := gctx.Param("alert_id")
alertID, err := strconv.Atoi(alertIDStr)

alertID, err := strconv.Atoi(alertIDStr)
if err != nil {
gctx.JSON(http.StatusBadRequest, gin.H{"message": "alert_id must be valid integer"})
return
Expand Down
Loading

0 comments on commit bc6be99

Please sign in to comment.