Skip to content

Commit

Permalink
refact: function SanitizeScope()
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Sep 2, 2024
1 parent 9f86e69 commit 4a2f019
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 70 deletions.
10 changes: 6 additions & 4 deletions cmd/crowdsec-cli/clialert/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,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 @@ -381,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
35 changes: 35 additions & 0 deletions cmd/crowdsec-cli/clialert/sanitize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package clialert

import (
"fmt"
"net"

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

// SanitizeScope validates ip and range and sets the scope accordingly if it's not already set.
// The return value has consistent case.
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 scope == "" {
scope = types.Range
}
}

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

if scope == "" {
scope = types.Ip
}
}

return types.NormalizeScope(scope), nil
}
File renamed without changes.
42 changes: 0 additions & 42 deletions cmd/crowdsec-cli/clialert/utils.go

This file was deleted.

16 changes: 10 additions & 6 deletions cmd/crowdsec-cli/clidecision/decisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,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 = clialert.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 @@ -330,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 := clialert.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 @@ -385,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 @@ -439,7 +442,8 @@ func (cli *cliDecisions) delete(delFilter apiclient.DecisionsDeleteOpts, delDeci
var err error

/*take care of shorthand options*/
if err = clialert.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
20 changes: 2 additions & 18 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
17 changes: 17 additions & 0 deletions pkg/types/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"net"
"strings"
"time"

"github.com/expr-lang/expr/vm"
Expand Down Expand Up @@ -143,3 +144,19 @@ func (r RuntimeAlert) GetSources() []string {
}
return ret
}

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

0 comments on commit 4a2f019

Please sign in to comment.