2020-11-30 09:37:17 +00:00
package main
import (
"context"
2021-12-29 13:08:47 +00:00
"encoding/csv"
2020-11-30 09:37:17 +00:00
"encoding/json"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"
2022-10-13 10:28:24 +00:00
"github.com/fatih/color"
2020-11-30 09:37:17 +00:00
"github.com/go-openapi/strfmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
2022-10-07 09:05:35 +00:00
2023-07-28 14:35:08 +00:00
"github.com/crowdsecurity/go-cs-lib/version"
2023-05-23 08:52:47 +00:00
2022-10-07 09:05:35 +00:00
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
"github.com/crowdsecurity/crowdsec/pkg/models"
"github.com/crowdsecurity/crowdsec/pkg/types"
2020-11-30 09:37:17 +00:00
)
var Client * apiclient . ApiClient
2024-02-01 21:36:21 +00:00
func ( cli * cliDecisions ) decisionsToTable ( alerts * models . GetAlertsResponse , printMachine bool ) error {
2020-11-30 09:37:17 +00:00
/*here we cheat a bit : to make it more readable for the user, we dedup some entries*/
2023-03-09 10:56:02 +00:00
spamLimit := make ( map [ string ] bool )
skipped := 0
2020-11-30 09:37:17 +00:00
2022-01-13 15:46:16 +00:00
for aIdx := 0 ; aIdx < len ( * alerts ) ; aIdx ++ {
2020-11-30 09:37:17 +00:00
alertItem := ( * alerts ) [ aIdx ]
newDecisions := make ( [ ] * models . Decision , 0 )
2024-01-03 09:55:41 +00:00
2020-11-30 09:37:17 +00:00
for _ , decisionItem := range alertItem . Decisions {
spamKey := fmt . Sprintf ( "%t:%s:%s:%s" , * decisionItem . Simulated , * decisionItem . Type , * decisionItem . Scope , * decisionItem . Value )
if _ , ok := spamLimit [ spamKey ] ; ok {
2021-12-22 14:45:41 +00:00
skipped ++
2020-11-30 09:37:17 +00:00
continue
}
2024-01-03 09:55:41 +00:00
2020-11-30 09:37:17 +00:00
spamLimit [ spamKey ] = true
2024-01-03 09:55:41 +00:00
2020-11-30 09:37:17 +00:00
newDecisions = append ( newDecisions , decisionItem )
}
2024-01-03 09:55:41 +00:00
2020-11-30 09:37:17 +00:00
alertItem . Decisions = newDecisions
}
2024-01-03 09:55:41 +00:00
2024-02-01 21:36:21 +00:00
switch cli . cfg ( ) . Cscli . Output {
case "raw" :
2021-12-29 13:08:47 +00:00
csvwriter := csv . NewWriter ( os . Stdout )
2022-03-16 16:29:31 +00:00
header := [ ] string { "id" , "source" , "ip" , "reason" , "action" , "country" , "as" , "events_count" , "expiration" , "simulated" , "alert_id" }
2024-01-03 09:55:41 +00:00
2022-03-16 16:29:31 +00:00
if printMachine {
header = append ( header , "machine" )
}
2024-01-03 09:55:41 +00:00
2022-03-16 16:29:31 +00:00
err := csvwriter . Write ( header )
2021-12-29 13:08:47 +00:00
if err != nil {
return err
}
2024-01-03 09:55:41 +00:00
2020-11-30 09:37:17 +00:00
for _ , alertItem := range * alerts {
for _ , decisionItem := range alertItem . Decisions {
2022-03-16 16:29:31 +00:00
raw := [ ] string {
2021-12-29 13:08:47 +00:00
fmt . Sprintf ( "%d" , decisionItem . ID ) ,
2020-11-30 09:37:17 +00:00
* decisionItem . Origin ,
2021-12-29 13:08:47 +00:00
* decisionItem . Scope + ":" + * decisionItem . Value ,
2020-11-30 09:37:17 +00:00
* decisionItem . Scenario ,
* decisionItem . Type ,
alertItem . Source . Cn ,
2022-11-14 08:55:53 +00:00
alertItem . Source . GetAsNumberName ( ) ,
2021-12-29 13:08:47 +00:00
fmt . Sprintf ( "%d" , * alertItem . EventsCount ) ,
2020-11-30 09:37:17 +00:00
* decisionItem . Duration ,
2021-12-29 13:08:47 +00:00
fmt . Sprintf ( "%t" , * decisionItem . Simulated ) ,
fmt . Sprintf ( "%d" , alertItem . ID ) ,
2022-03-16 16:29:31 +00:00
}
if printMachine {
raw = append ( raw , alertItem . MachineID )
}
err := csvwriter . Write ( raw )
2021-12-29 13:08:47 +00:00
if err != nil {
return err
}
2020-11-30 09:37:17 +00:00
}
}
2024-01-03 09:55:41 +00:00
2021-12-29 13:08:47 +00:00
csvwriter . Flush ( )
2024-02-01 21:36:21 +00:00
case "json" :
2023-08-03 10:51:50 +00:00
if * alerts == nil {
// avoid returning "null" in `json"
// could be cleaner if we used slice of alerts directly
fmt . Println ( "[]" )
return nil
}
2024-02-01 21:36:21 +00:00
2020-11-30 09:37:17 +00:00
x , _ := json . MarshalIndent ( alerts , "" , " " )
fmt . Printf ( "%s" , string ( x ) )
2024-02-01 21:36:21 +00:00
case "human" :
2020-11-30 09:37:17 +00:00
if len ( * alerts ) == 0 {
fmt . Println ( "No active decisions" )
return nil
}
2024-02-01 21:36:21 +00:00
cli . decisionsTable ( color . Output , alerts , printMachine )
2021-12-22 14:45:41 +00:00
if skipped > 0 {
fmt . Printf ( "%d duplicated entries skipped\n" , skipped )
}
2020-11-30 09:37:17 +00:00
}
2024-01-03 09:55:41 +00:00
2020-11-30 09:37:17 +00:00
return nil
}
2024-02-01 21:36:21 +00:00
type cliDecisions struct {
cfg configGetter
}
2023-12-11 09:32:54 +00:00
2024-02-06 09:50:28 +00:00
func NewCLIDecisions ( cfg configGetter ) * cliDecisions {
2024-02-01 21:36:21 +00:00
return & cliDecisions {
2024-02-06 09:50:28 +00:00
cfg : cfg ,
2024-02-01 21:36:21 +00:00
}
2023-12-11 09:32:54 +00:00
}
2024-02-01 21:36:21 +00:00
func ( cli * cliDecisions ) NewCommand ( ) * cobra . Command {
2023-12-11 09:32:54 +00:00
cmd := & cobra . Command {
2020-11-30 09:37:17 +00:00
Use : "decisions [action]" ,
Short : "Manage decisions" ,
2021-12-15 10:39:37 +00:00
Long : ` Add/List/Delete/Import decisions from LAPI ` ,
2020-11-30 09:37:17 +00:00
Example : ` cscli decisions [action] [filter] ` ,
2022-04-20 13:44:48 +00:00
Aliases : [ ] string { "decision" } ,
2020-11-30 09:37:17 +00:00
/*TBD example*/
2021-08-31 13:03:47 +00:00
Args : cobra . MinimumNArgs ( 1 ) ,
DisableAutoGenTag : true ,
2024-01-03 09:55:41 +00:00
PersistentPreRunE : func ( _ * cobra . Command , _ [ ] string ) error {
2024-02-01 21:36:21 +00:00
cfg := cli . cfg ( )
if err := cfg . LoadAPIClient ( ) ; err != nil {
2023-06-22 13:01:34 +00:00
return fmt . Errorf ( "loading api client: %w" , err )
2020-11-30 09:37:17 +00:00
}
2024-02-01 21:36:21 +00:00
password := strfmt . Password ( cfg . API . Client . Credentials . Password )
apiurl , err := url . Parse ( cfg . API . Client . Credentials . URL )
2020-11-30 09:37:17 +00:00
if err != nil {
2024-02-01 21:36:21 +00:00
return fmt . Errorf ( "parsing api url %s: %w" , cfg . API . Client . Credentials . URL , err )
2020-11-30 09:37:17 +00:00
}
Client , err = apiclient . NewClient ( & apiclient . Config {
2024-02-01 21:36:21 +00:00
MachineID : cfg . API . Client . Credentials . Login ,
2020-11-30 09:37:17 +00:00
Password : password ,
2023-05-23 08:52:47 +00:00
UserAgent : fmt . Sprintf ( "crowdsec/%s" , version . String ( ) ) ,
2020-11-30 09:37:17 +00:00
URL : apiurl ,
VersionPrefix : "v1" ,
} )
if err != nil {
2023-06-22 13:01:34 +00:00
return fmt . Errorf ( "creating api client: %w" , err )
2020-11-30 09:37:17 +00:00
}
2024-02-06 09:50:28 +00:00
2022-05-19 08:48:08 +00:00
return nil
2020-11-30 09:37:17 +00:00
} ,
}
2024-02-01 21:36:21 +00:00
cmd . AddCommand ( cli . newListCmd ( ) )
cmd . AddCommand ( cli . newAddCmd ( ) )
cmd . AddCommand ( cli . newDeleteCmd ( ) )
cmd . AddCommand ( cli . newImportCmd ( ) )
2023-01-19 10:02:00 +00:00
2023-12-11 09:32:54 +00:00
return cmd
2023-01-19 10:02:00 +00:00
}
2024-02-01 21:36:21 +00:00
func ( cli * cliDecisions ) newListCmd ( ) * cobra . Command {
2020-11-30 09:37:17 +00:00
var filter = apiclient . AlertsListOpts {
ValueEquals : new ( string ) ,
ScopeEquals : new ( string ) ,
ScenarioEquals : new ( string ) ,
2022-01-11 13:31:51 +00:00
OriginEquals : new ( string ) ,
2020-11-30 09:37:17 +00:00
IPEquals : new ( string ) ,
RangeEquals : new ( string ) ,
Since : new ( string ) ,
Until : new ( string ) ,
TypeEquals : new ( string ) ,
IncludeCAPI : new ( bool ) ,
2021-10-26 11:33:59 +00:00
Limit : new ( int ) ,
2020-11-30 09:37:17 +00:00
}
2024-01-03 09:55:41 +00:00
2020-11-30 09:37:17 +00:00
NoSimu := new ( bool )
2021-01-15 08:48:39 +00:00
contained := new ( bool )
2024-01-03 09:55:41 +00:00
2022-03-16 16:29:31 +00:00
var printMachine bool
2023-01-19 10:02:00 +00:00
2023-12-11 09:32:54 +00:00
cmd := & cobra . Command {
2020-11-30 09:37:17 +00:00
Use : "list [options]" ,
Short : "List decisions from LAPI" ,
Example : ` cscli decisions list - i 1.2 .3 .4
cscli decisions list - r 1.2 .3 .0 / 24
cscli decisions list - s crowdsecurity / ssh - bf
2024-03-20 13:02:29 +00:00
cscli decisions list -- origin lists -- scenario list_name
2020-11-30 09:37:17 +00:00
` ,
2021-08-31 13:03:47 +00:00
Args : cobra . ExactArgs ( 0 ) ,
DisableAutoGenTag : true ,
2024-01-03 09:55:41 +00:00
RunE : func ( cmd * cobra . Command , _ [ ] string ) error {
2020-11-30 09:37:17 +00:00
var err error
/*take care of shorthand options*/
2023-06-27 12:29:42 +00:00
if err = manageCliDecisionAlerts ( filter . IPEquals , filter . RangeEquals , filter . ScopeEquals , filter . ValueEquals ) ; err != nil {
return err
2020-11-30 09:37:17 +00:00
}
filter . ActiveDecisionEquals = new ( bool )
* filter . ActiveDecisionEquals = true
if NoSimu != nil && * NoSimu {
2021-01-15 08:48:39 +00:00
filter . IncludeSimulated = new ( bool )
2020-11-30 09:37:17 +00:00
}
2022-02-14 12:47:15 +00:00
/* nullify the empty entries to avoid bad filter */
2020-11-30 09:37:17 +00:00
if * filter . Until == "" {
filter . Until = nil
2022-11-07 09:36:50 +00:00
} else if strings . HasSuffix ( * filter . Until , "d" ) {
2020-11-30 09:37:17 +00:00
/*time.ParseDuration support hours 'h' as bigger unit, let's make the user's life easier*/
2022-11-07 09:36:50 +00:00
realDuration := strings . TrimSuffix ( * filter . Until , "d" )
days , err := strconv . Atoi ( realDuration )
if err != nil {
printHelp ( cmd )
2023-06-27 12:29:42 +00:00
return fmt . Errorf ( "can't parse duration %s, valid durations format: 1d, 4h, 4h15m" , * filter . Until )
2020-11-30 09:37:17 +00:00
}
2022-11-07 09:36:50 +00:00
* filter . Until = fmt . Sprintf ( "%d%s" , days * 24 , "h" )
2020-11-30 09:37:17 +00:00
}
2022-11-07 09:36:50 +00:00
2020-11-30 09:37:17 +00:00
if * filter . Since == "" {
filter . Since = nil
2022-11-07 09:36:50 +00:00
} else if strings . HasSuffix ( * filter . Since , "d" ) {
2020-11-30 09:37:17 +00:00
/*time.ParseDuration support hours 'h' as bigger unit, let's make the user's life easier*/
2022-11-07 09:36:50 +00:00
realDuration := strings . TrimSuffix ( * filter . Since , "d" )
days , err := strconv . Atoi ( realDuration )
if err != nil {
printHelp ( cmd )
2023-06-27 12:29:42 +00:00
return fmt . Errorf ( "can't parse duration %s, valid durations format: 1d, 4h, 4h15m" , * filter . Since )
2020-11-30 09:37:17 +00:00
}
2022-11-07 09:36:50 +00:00
* filter . Since = fmt . Sprintf ( "%d%s" , days * 24 , "h" )
2020-11-30 09:37:17 +00:00
}
2021-10-26 11:33:59 +00:00
if * filter . IncludeCAPI {
* filter . Limit = 0
}
2020-11-30 09:37:17 +00:00
if * filter . TypeEquals == "" {
filter . TypeEquals = nil
}
if * filter . ValueEquals == "" {
filter . ValueEquals = nil
}
if * filter . ScopeEquals == "" {
filter . ScopeEquals = nil
}
if * filter . ScenarioEquals == "" {
filter . ScenarioEquals = nil
}
if * filter . IPEquals == "" {
filter . IPEquals = nil
}
if * filter . RangeEquals == "" {
filter . RangeEquals = nil
}
2021-01-15 08:48:39 +00:00
2022-01-11 13:31:51 +00:00
if * filter . OriginEquals == "" {
filter . OriginEquals = nil
}
2021-01-15 08:48:39 +00:00
if contained != nil && * contained {
filter . Contains = new ( bool )
}
2020-11-30 09:37:17 +00:00
alerts , _ , err := Client . Alerts . List ( context . Background ( ) , filter )
if err != nil {
2023-06-27 12:29:42 +00:00
return fmt . Errorf ( "unable to retrieve decisions: %w" , err )
2020-11-30 09:37:17 +00:00
}
2024-02-01 21:36:21 +00:00
err = cli . decisionsToTable ( alerts , printMachine )
2020-11-30 09:37:17 +00:00
if err != nil {
2023-06-27 12:29:42 +00:00
return fmt . Errorf ( "unable to print decisions: %w" , err )
2020-11-30 09:37:17 +00:00
}
2023-06-27 12:29:42 +00:00
return nil
2020-11-30 09:37:17 +00:00
} ,
}
2023-12-11 09:32:54 +00:00
cmd . Flags ( ) . SortFlags = false
cmd . Flags ( ) . BoolVarP ( filter . IncludeCAPI , "all" , "a" , false , "Include decisions from Central API" )
cmd . Flags ( ) . StringVar ( filter . Since , "since" , "" , "restrict to alerts newer than since (ie. 4h, 30d)" )
cmd . Flags ( ) . StringVar ( filter . Until , "until" , "" , "restrict to alerts older than until (ie. 4h, 30d)" )
cmd . Flags ( ) . StringVarP ( filter . TypeEquals , "type" , "t" , "" , "restrict to this decision type (ie. ban,captcha)" )
cmd . Flags ( ) . StringVar ( filter . ScopeEquals , "scope" , "" , "restrict to this scope (ie. ip,range,session)" )
cmd . Flags ( ) . StringVar ( filter . OriginEquals , "origin" , "" , fmt . Sprintf ( "the value to match for the specified origin (%s ...)" , strings . Join ( types . GetOrigins ( ) , "," ) ) )
cmd . Flags ( ) . StringVarP ( filter . ValueEquals , "value" , "v" , "" , "restrict to this value (ie. 1.2.3.4,userName)" )
cmd . Flags ( ) . StringVarP ( filter . ScenarioEquals , "scenario" , "s" , "" , "restrict to this scenario (ie. crowdsecurity/ssh-bf)" )
cmd . Flags ( ) . StringVarP ( filter . IPEquals , "ip" , "i" , "" , "restrict to alerts from this source ip (shorthand for --scope ip --value <IP>)" )
cmd . Flags ( ) . StringVarP ( filter . RangeEquals , "range" , "r" , "" , "restrict to alerts from this source range (shorthand for --scope range --value <RANGE>)" )
cmd . Flags ( ) . IntVarP ( filter . Limit , "limit" , "l" , 100 , "number of alerts to get (use 0 to remove the limit)" )
cmd . Flags ( ) . BoolVar ( NoSimu , "no-simu" , false , "exclude decisions in simulation mode" )
cmd . Flags ( ) . BoolVarP ( & printMachine , "machine" , "m" , false , "print machines that triggered decisions" )
cmd . Flags ( ) . BoolVar ( contained , "contained" , false , "query decisions contained by range" )
return cmd
2023-01-19 10:02:00 +00:00
}
2024-02-01 21:36:21 +00:00
func ( cli * cliDecisions ) newAddCmd ( ) * cobra . Command {
2020-11-30 09:37:17 +00:00
var (
addIP string
addRange string
addDuration string
addValue string
addScope string
addReason string
addType string
)
2023-12-11 09:32:54 +00:00
cmd := & cobra . Command {
2020-11-30 09:37:17 +00:00
Use : "add [options]" ,
Short : "Add decision to LAPI" ,
Example : ` cscli decisions add -- ip 1.2 .3 .4
cscli decisions add -- range 1.2 .3 .0 / 24
cscli decisions add -- ip 1.2 .3 .4 -- duration 24 h -- type captcha
cscli decisions add -- scope username -- value foobar
` ,
/*TBD : fix long and example*/
2021-08-31 13:03:47 +00:00
Args : cobra . ExactArgs ( 0 ) ,
DisableAutoGenTag : true ,
2024-01-03 09:55:41 +00:00
RunE : func ( cmd * cobra . Command , _ [ ] string ) error {
2020-11-30 09:37:17 +00:00
var err error
alerts := models . AddAlertsRequest { }
2023-01-31 13:47:44 +00:00
origin := types . CscliOrigin
2020-11-30 09:37:17 +00:00
capacity := int32 ( 0 )
leakSpeed := "0"
eventsCount := int32 ( 1 )
empty := ""
simulated := false
2022-01-19 13:56:05 +00:00
startAt := time . Now ( ) . UTC ( ) . Format ( time . RFC3339 )
stopAt := time . Now ( ) . UTC ( ) . Format ( time . RFC3339 )
createdAt := time . Now ( ) . UTC ( ) . Format ( time . RFC3339 )
2020-11-30 09:37:17 +00:00
/*take care of shorthand options*/
2024-02-01 21:36:21 +00:00
if err = manageCliDecisionAlerts ( & addIP , & addRange , & addScope , & addValue ) ; err != nil {
2023-06-27 12:29:42 +00:00
return err
2020-11-30 09:37:17 +00:00
}
if addIP != "" {
addValue = addIP
addScope = types . Ip
} else if addRange != "" {
addValue = addRange
addScope = types . Range
} else if addValue == "" {
2022-03-10 12:55:25 +00:00
printHelp ( cmd )
2024-01-03 09:55:41 +00:00
return fmt . Errorf ( "missing arguments, a value is required (--ip, --range or --scope and --value)" )
2020-11-30 09:37:17 +00:00
}
if addReason == "" {
2024-02-01 21:36:21 +00:00
addReason = fmt . Sprintf ( "manual '%s' from '%s'" , addType , cli . cfg ( ) . API . Client . Credentials . Login )
2020-11-30 09:37:17 +00:00
}
decision := models . Decision {
Duration : & addDuration ,
Scope : & addScope ,
Value : & addValue ,
Type : & addType ,
Scenario : & addReason ,
Origin : & origin ,
}
alert := models . Alert {
Capacity : & capacity ,
Decisions : [ ] * models . Decision { & decision } ,
Events : [ ] * models . Event { } ,
EventsCount : & eventsCount ,
Leakspeed : & leakSpeed ,
Message : & addReason ,
ScenarioHash : & empty ,
Scenario : & addReason ,
ScenarioVersion : & empty ,
Simulated : & simulated ,
2023-01-31 13:47:44 +00:00
//setting empty scope/value broke plugins, and it didn't seem to be needed anymore w/ latest papi changes
2020-11-30 09:37:17 +00:00
Source : & models . Source {
AsName : empty ,
AsNumber : empty ,
Cn : empty ,
2022-01-13 15:46:16 +00:00
IP : addValue ,
2023-01-31 13:47:44 +00:00
Range : "" ,
2020-11-30 09:37:17 +00:00
Scope : & addScope ,
Value : & addValue ,
} ,
StartAt : & startAt ,
StopAt : & stopAt ,
CreatedAt : createdAt ,
}
alerts = append ( alerts , & alert )
_ , _ , err = Client . Alerts . Add ( context . Background ( ) , alerts )
if err != nil {
2023-06-27 12:29:42 +00:00
return err
2020-11-30 09:37:17 +00:00
}
log . Info ( "Decision successfully added" )
2024-02-06 09:50:28 +00:00
2023-06-27 12:29:42 +00:00
return nil
2020-11-30 09:37:17 +00:00
} ,
}
2023-12-11 09:32:54 +00:00
cmd . Flags ( ) . SortFlags = false
cmd . Flags ( ) . StringVarP ( & addIP , "ip" , "i" , "" , "Source ip (shorthand for --scope ip --value <IP>)" )
cmd . Flags ( ) . StringVarP ( & addRange , "range" , "r" , "" , "Range source ip (shorthand for --scope range --value <RANGE>)" )
cmd . Flags ( ) . StringVarP ( & addDuration , "duration" , "d" , "4h" , "Decision duration (ie. 1h,4h,30m)" )
cmd . Flags ( ) . StringVarP ( & addValue , "value" , "v" , "" , "The value (ie. --scope username --value foobar)" )
cmd . Flags ( ) . StringVar ( & addScope , "scope" , types . Ip , "Decision scope (ie. ip,range,username)" )
cmd . Flags ( ) . StringVarP ( & addReason , "reason" , "R" , "" , "Decision reason (ie. scenario-name)" )
cmd . Flags ( ) . StringVarP ( & addType , "type" , "t" , "ban" , "Decision type (ie. ban,captcha,throttle)" )
2020-11-30 09:37:17 +00:00
2023-12-11 09:32:54 +00:00
return cmd
2023-01-19 10:02:00 +00:00
}
2024-02-01 21:36:21 +00:00
func ( cli * cliDecisions ) newDeleteCmd ( ) * cobra . Command {
2020-11-30 09:37:17 +00:00
var delFilter = apiclient . DecisionsDeleteOpts {
2022-10-19 12:37:27 +00:00
ScopeEquals : new ( string ) ,
ValueEquals : new ( string ) ,
TypeEquals : new ( string ) ,
IPEquals : new ( string ) ,
RangeEquals : new ( string ) ,
ScenarioEquals : new ( string ) ,
2023-03-08 17:29:20 +00:00
OriginEquals : new ( string ) ,
2020-11-30 09:37:17 +00:00
}
2024-01-03 09:55:41 +00:00
var delDecisionID string
2020-11-30 09:37:17 +00:00
var delDecisionAll bool
2024-01-03 09:55:41 +00:00
2023-01-19 10:02:00 +00:00
contained := new ( bool )
2023-12-11 09:32:54 +00:00
cmd := & cobra . Command {
2021-08-31 13:03:47 +00:00
Use : "delete [options]" ,
Short : "Delete decisions" ,
DisableAutoGenTag : true ,
2022-04-20 13:44:48 +00:00
Aliases : [ ] string { "remove" } ,
2020-11-30 09:37:17 +00:00
Example : ` cscli decisions delete - r 1.2 .3 .0 / 24
cscli decisions delete - i 1.2 .3 .4
cscli decisions delete -- id 42
cscli decisions delete -- type captcha
2024-03-20 13:02:29 +00:00
cscli decisions delete -- origin lists -- scenario list_name
2020-11-30 09:37:17 +00:00
` ,
/*TBD : refaire le Long/Example*/
2024-01-03 09:55:41 +00:00
PreRunE : func ( cmd * cobra . Command , _ [ ] string ) error {
2020-11-30 09:37:17 +00:00
if delDecisionAll {
2023-06-27 12:29:42 +00:00
return nil
2020-11-30 09:37:17 +00:00
}
if * delFilter . ScopeEquals == "" && * delFilter . ValueEquals == "" &&
* delFilter . TypeEquals == "" && * delFilter . IPEquals == "" &&
2023-03-08 17:29:20 +00:00
* delFilter . RangeEquals == "" && * delFilter . ScenarioEquals == "" &&
2024-01-03 09:55:41 +00:00
* delFilter . OriginEquals == "" && delDecisionID == "" {
2020-11-30 09:37:17 +00:00
cmd . Usage ( )
2023-06-27 12:29:42 +00:00
return fmt . Errorf ( "at least one filter or --all must be specified" )
2020-11-30 09:37:17 +00:00
}
2023-06-27 12:29:42 +00:00
return nil
2020-11-30 09:37:17 +00:00
} ,
2024-01-03 09:55:41 +00:00
RunE : func ( _ * cobra . Command , _ [ ] string ) error {
2020-11-30 09:37:17 +00:00
var err error
var decisions * models . DeleteDecisionResponse
/*take care of shorthand options*/
2023-06-27 12:29:42 +00:00
if err = manageCliDecisionAlerts ( delFilter . IPEquals , delFilter . RangeEquals , delFilter . ScopeEquals , delFilter . ValueEquals ) ; err != nil {
return err
2020-11-30 09:37:17 +00:00
}
if * delFilter . ScopeEquals == "" {
delFilter . ScopeEquals = nil
}
2023-03-08 17:29:20 +00:00
if * delFilter . OriginEquals == "" {
delFilter . OriginEquals = nil
}
2020-11-30 09:37:17 +00:00
if * delFilter . ValueEquals == "" {
delFilter . ValueEquals = nil
}
2022-10-19 12:37:27 +00:00
if * delFilter . ScenarioEquals == "" {
delFilter . ScenarioEquals = nil
}
2020-11-30 09:37:17 +00:00
if * delFilter . TypeEquals == "" {
delFilter . TypeEquals = nil
}
if * delFilter . IPEquals == "" {
delFilter . IPEquals = nil
}
if * delFilter . RangeEquals == "" {
delFilter . RangeEquals = nil
}
2021-01-15 08:48:39 +00:00
if contained != nil && * contained {
delFilter . Contains = new ( bool )
}
2024-01-03 09:55:41 +00:00
if delDecisionID == "" {
2020-11-30 09:37:17 +00:00
decisions , _ , err = Client . Decisions . Delete ( context . Background ( ) , delFilter )
if err != nil {
2024-01-03 09:55:41 +00:00
return fmt . Errorf ( "unable to delete decisions: %v" , err )
2020-11-30 09:37:17 +00:00
}
} else {
2024-01-03 09:55:41 +00:00
if _ , err = strconv . Atoi ( delDecisionID ) ; err != nil {
return fmt . Errorf ( "id '%s' is not an integer: %v" , delDecisionID , err )
2022-08-30 10:38:28 +00:00
}
2024-01-03 09:55:41 +00:00
decisions , _ , err = Client . Decisions . DeleteOne ( context . Background ( ) , delDecisionID )
2020-11-30 09:37:17 +00:00
if err != nil {
2024-01-03 09:55:41 +00:00
return fmt . Errorf ( "unable to delete decision: %v" , err )
2020-11-30 09:37:17 +00:00
}
}
log . Infof ( "%s decision(s) deleted" , decisions . NbDeleted )
2024-02-06 09:50:28 +00:00
2023-06-27 12:29:42 +00:00
return nil
2020-11-30 09:37:17 +00:00
} ,
}
2023-12-11 09:32:54 +00:00
cmd . Flags ( ) . SortFlags = false
cmd . Flags ( ) . StringVarP ( delFilter . IPEquals , "ip" , "i" , "" , "Source ip (shorthand for --scope ip --value <IP>)" )
cmd . Flags ( ) . StringVarP ( delFilter . RangeEquals , "range" , "r" , "" , "Range source ip (shorthand for --scope range --value <RANGE>)" )
cmd . Flags ( ) . StringVarP ( delFilter . TypeEquals , "type" , "t" , "" , "the decision type (ie. ban,captcha)" )
cmd . Flags ( ) . StringVarP ( delFilter . ValueEquals , "value" , "v" , "" , "the value to match for in the specified scope" )
cmd . Flags ( ) . StringVarP ( delFilter . ScenarioEquals , "scenario" , "s" , "" , "the scenario name (ie. crowdsecurity/ssh-bf)" )
cmd . Flags ( ) . StringVar ( delFilter . OriginEquals , "origin" , "" , fmt . Sprintf ( "the value to match for the specified origin (%s ...)" , strings . Join ( types . GetOrigins ( ) , "," ) ) )
2023-03-08 17:29:20 +00:00
2024-01-03 09:55:41 +00:00
cmd . Flags ( ) . StringVar ( & delDecisionID , "id" , "" , "decision id" )
2023-12-11 09:32:54 +00:00
cmd . Flags ( ) . BoolVar ( & delDecisionAll , "all" , false , "delete all decisions" )
cmd . Flags ( ) . BoolVar ( contained , "contained" , false , "query decisions contained by range" )
2021-01-14 15:27:45 +00:00
2023-12-11 09:32:54 +00:00
return cmd
2023-01-19 10:02:00 +00:00
}