decisions.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. package main
  2. import (
  3. "context"
  4. "encoding/csv"
  5. "encoding/json"
  6. "fmt"
  7. "net/url"
  8. "os"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/crowdsecurity/crowdsec/pkg/apiclient"
  14. "github.com/crowdsecurity/crowdsec/pkg/cwversion"
  15. "github.com/crowdsecurity/crowdsec/pkg/models"
  16. "github.com/crowdsecurity/crowdsec/pkg/types"
  17. "github.com/go-openapi/strfmt"
  18. "github.com/jszwec/csvutil"
  19. "github.com/olekukonko/tablewriter"
  20. "github.com/pkg/errors"
  21. log "github.com/sirupsen/logrus"
  22. "github.com/spf13/cobra"
  23. )
  24. var Client *apiclient.ApiClient
  25. var (
  26. defaultDuration = "4h"
  27. defaultScope = "ip"
  28. defaultType = "ban"
  29. defaultReason = "manual"
  30. )
  31. func DecisionsToTable(alerts *models.GetAlertsResponse, printMachine bool) error {
  32. /*here we cheat a bit : to make it more readable for the user, we dedup some entries*/
  33. var spamLimit map[string]bool = make(map[string]bool)
  34. var skipped = 0
  35. for aIdx := 0; aIdx < len(*alerts); aIdx++ {
  36. alertItem := (*alerts)[aIdx]
  37. newDecisions := make([]*models.Decision, 0)
  38. for _, decisionItem := range alertItem.Decisions {
  39. spamKey := fmt.Sprintf("%t:%s:%s:%s", *decisionItem.Simulated, *decisionItem.Type, *decisionItem.Scope, *decisionItem.Value)
  40. if _, ok := spamLimit[spamKey]; ok {
  41. skipped++
  42. continue
  43. }
  44. spamLimit[spamKey] = true
  45. newDecisions = append(newDecisions, decisionItem)
  46. }
  47. alertItem.Decisions = newDecisions
  48. }
  49. if csConfig.Cscli.Output == "raw" {
  50. csvwriter := csv.NewWriter(os.Stdout)
  51. header := []string{"id", "source", "ip", "reason", "action", "country", "as", "events_count", "expiration", "simulated", "alert_id"}
  52. if printMachine {
  53. header = append(header, "machine")
  54. }
  55. err := csvwriter.Write(header)
  56. if err != nil {
  57. return err
  58. }
  59. for _, alertItem := range *alerts {
  60. for _, decisionItem := range alertItem.Decisions {
  61. raw := []string{
  62. fmt.Sprintf("%d", decisionItem.ID),
  63. *decisionItem.Origin,
  64. *decisionItem.Scope + ":" + *decisionItem.Value,
  65. *decisionItem.Scenario,
  66. *decisionItem.Type,
  67. alertItem.Source.Cn,
  68. alertItem.Source.AsNumber + " " + alertItem.Source.AsName,
  69. fmt.Sprintf("%d", *alertItem.EventsCount),
  70. *decisionItem.Duration,
  71. fmt.Sprintf("%t", *decisionItem.Simulated),
  72. fmt.Sprintf("%d", alertItem.ID),
  73. }
  74. if printMachine {
  75. raw = append(raw, alertItem.MachineID)
  76. }
  77. err := csvwriter.Write(raw)
  78. if err != nil {
  79. return err
  80. }
  81. }
  82. }
  83. csvwriter.Flush()
  84. } else if csConfig.Cscli.Output == "json" {
  85. x, _ := json.MarshalIndent(alerts, "", " ")
  86. fmt.Printf("%s", string(x))
  87. } else if csConfig.Cscli.Output == "human" {
  88. table := tablewriter.NewWriter(os.Stdout)
  89. header := []string{"ID", "Source", "Scope:Value", "Reason", "Action", "Country", "AS", "Events", "expiration", "Alert ID"}
  90. if printMachine {
  91. header = append(header, "Machine")
  92. }
  93. table.SetHeader(header)
  94. if len(*alerts) == 0 {
  95. fmt.Println("No active decisions")
  96. return nil
  97. }
  98. for _, alertItem := range *alerts {
  99. for _, decisionItem := range alertItem.Decisions {
  100. if *alertItem.Simulated {
  101. *decisionItem.Type = fmt.Sprintf("(simul)%s", *decisionItem.Type)
  102. }
  103. raw := []string{
  104. strconv.Itoa(int(decisionItem.ID)),
  105. *decisionItem.Origin,
  106. *decisionItem.Scope + ":" + *decisionItem.Value,
  107. *decisionItem.Scenario,
  108. *decisionItem.Type,
  109. alertItem.Source.Cn,
  110. alertItem.Source.AsNumber + " " + alertItem.Source.AsName,
  111. strconv.Itoa(int(*alertItem.EventsCount)),
  112. *decisionItem.Duration,
  113. strconv.Itoa(int(alertItem.ID)),
  114. }
  115. if printMachine {
  116. raw = append(raw, alertItem.MachineID)
  117. }
  118. table.Append(raw)
  119. }
  120. }
  121. table.Render() // Send output
  122. if skipped > 0 {
  123. fmt.Printf("%d duplicated entries skipped\n", skipped)
  124. }
  125. }
  126. return nil
  127. }
  128. func NewDecisionsCmd() *cobra.Command {
  129. /* ---- DECISIONS COMMAND */
  130. var cmdDecisions = &cobra.Command{
  131. Use: "decisions [action]",
  132. Short: "Manage decisions",
  133. Long: `Add/List/Delete/Import decisions from LAPI`,
  134. Example: `cscli decisions [action] [filter]`,
  135. Aliases: []string{"decision"},
  136. /*TBD example*/
  137. Args: cobra.MinimumNArgs(1),
  138. DisableAutoGenTag: true,
  139. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  140. if err := csConfig.LoadAPIClient(); err != nil {
  141. return errors.Wrap(err, "loading api client")
  142. }
  143. password := strfmt.Password(csConfig.API.Client.Credentials.Password)
  144. apiurl, err := url.Parse(csConfig.API.Client.Credentials.URL)
  145. if err != nil {
  146. return errors.Wrapf(err, "parsing api url %s", csConfig.API.Client.Credentials.URL)
  147. }
  148. Client, err = apiclient.NewClient(&apiclient.Config{
  149. MachineID: csConfig.API.Client.Credentials.Login,
  150. Password: password,
  151. UserAgent: fmt.Sprintf("crowdsec/%s", cwversion.VersionStr()),
  152. URL: apiurl,
  153. VersionPrefix: "v1",
  154. })
  155. if err != nil {
  156. return errors.Wrap(err, "creating api client")
  157. }
  158. return nil
  159. },
  160. }
  161. var filter = apiclient.AlertsListOpts{
  162. ValueEquals: new(string),
  163. ScopeEquals: new(string),
  164. ScenarioEquals: new(string),
  165. OriginEquals: new(string),
  166. IPEquals: new(string),
  167. RangeEquals: new(string),
  168. Since: new(string),
  169. Until: new(string),
  170. TypeEquals: new(string),
  171. IncludeCAPI: new(bool),
  172. Limit: new(int),
  173. }
  174. NoSimu := new(bool)
  175. contained := new(bool)
  176. var printMachine bool
  177. var cmdDecisionsList = &cobra.Command{
  178. Use: "list [options]",
  179. Short: "List decisions from LAPI",
  180. Example: `cscli decisions list -i 1.2.3.4
  181. cscli decisions list -r 1.2.3.0/24
  182. cscli decisions list -s crowdsecurity/ssh-bf
  183. cscli decisions list -t ban
  184. `,
  185. Args: cobra.ExactArgs(0),
  186. DisableAutoGenTag: true,
  187. Run: func(cmd *cobra.Command, args []string) {
  188. var err error
  189. /*take care of shorthand options*/
  190. if err := manageCliDecisionAlerts(filter.IPEquals, filter.RangeEquals, filter.ScopeEquals, filter.ValueEquals); err != nil {
  191. log.Fatalf("%s", err)
  192. }
  193. filter.ActiveDecisionEquals = new(bool)
  194. *filter.ActiveDecisionEquals = true
  195. if NoSimu != nil && *NoSimu {
  196. filter.IncludeSimulated = new(bool)
  197. }
  198. /* nullify the empty entries to avoid bad filter */
  199. if *filter.Until == "" {
  200. filter.Until = nil
  201. } else {
  202. /*time.ParseDuration support hours 'h' as bigger unit, let's make the user's life easier*/
  203. if strings.HasSuffix(*filter.Until, "d") {
  204. realDuration := strings.TrimSuffix(*filter.Until, "d")
  205. days, err := strconv.Atoi(realDuration)
  206. if err != nil {
  207. printHelp(cmd)
  208. log.Fatalf("Can't parse duration %s, valid durations format: 1d, 4h, 4h15m", *filter.Until)
  209. }
  210. *filter.Until = fmt.Sprintf("%d%s", days*24, "h")
  211. }
  212. }
  213. if *filter.Since == "" {
  214. filter.Since = nil
  215. } else {
  216. /*time.ParseDuration support hours 'h' as bigger unit, let's make the user's life easier*/
  217. if strings.HasSuffix(*filter.Since, "d") {
  218. realDuration := strings.TrimSuffix(*filter.Since, "d")
  219. days, err := strconv.Atoi(realDuration)
  220. if err != nil {
  221. printHelp(cmd)
  222. log.Fatalf("Can't parse duration %s, valid durations format: 1d, 4h, 4h15m", *filter.Until)
  223. }
  224. *filter.Since = fmt.Sprintf("%d%s", days*24, "h")
  225. }
  226. }
  227. if *filter.IncludeCAPI {
  228. *filter.Limit = 0
  229. }
  230. if *filter.TypeEquals == "" {
  231. filter.TypeEquals = nil
  232. }
  233. if *filter.ValueEquals == "" {
  234. filter.ValueEquals = nil
  235. }
  236. if *filter.ScopeEquals == "" {
  237. filter.ScopeEquals = nil
  238. }
  239. if *filter.ScenarioEquals == "" {
  240. filter.ScenarioEquals = nil
  241. }
  242. if *filter.IPEquals == "" {
  243. filter.IPEquals = nil
  244. }
  245. if *filter.RangeEquals == "" {
  246. filter.RangeEquals = nil
  247. }
  248. if *filter.OriginEquals == "" {
  249. filter.OriginEquals = nil
  250. }
  251. if contained != nil && *contained {
  252. filter.Contains = new(bool)
  253. }
  254. alerts, _, err := Client.Alerts.List(context.Background(), filter)
  255. if err != nil {
  256. log.Fatalf("Unable to list decisions : %v", err.Error())
  257. }
  258. err = DecisionsToTable(alerts, printMachine)
  259. if err != nil {
  260. log.Fatalf("unable to list decisions : %v", err.Error())
  261. }
  262. },
  263. }
  264. cmdDecisionsList.Flags().SortFlags = false
  265. cmdDecisionsList.Flags().BoolVarP(filter.IncludeCAPI, "all", "a", false, "Include decisions from Central API")
  266. cmdDecisionsList.Flags().StringVar(filter.Since, "since", "", "restrict to alerts newer than since (ie. 4h, 30d)")
  267. cmdDecisionsList.Flags().StringVar(filter.Until, "until", "", "restrict to alerts older than until (ie. 4h, 30d)")
  268. cmdDecisionsList.Flags().StringVarP(filter.TypeEquals, "type", "t", "", "restrict to this decision type (ie. ban,captcha)")
  269. cmdDecisionsList.Flags().StringVar(filter.ScopeEquals, "scope", "", "restrict to this scope (ie. ip,range,session)")
  270. cmdDecisionsList.Flags().StringVar(filter.OriginEquals, "origin", "", "restrict to this origin (ie. lists,CAPI,cscli)")
  271. cmdDecisionsList.Flags().StringVarP(filter.ValueEquals, "value", "v", "", "restrict to this value (ie. 1.2.3.4,userName)")
  272. cmdDecisionsList.Flags().StringVarP(filter.ScenarioEquals, "scenario", "s", "", "restrict to this scenario (ie. crowdsecurity/ssh-bf)")
  273. cmdDecisionsList.Flags().StringVarP(filter.IPEquals, "ip", "i", "", "restrict to alerts from this source ip (shorthand for --scope ip --value <IP>)")
  274. cmdDecisionsList.Flags().StringVarP(filter.RangeEquals, "range", "r", "", "restrict to alerts from this source range (shorthand for --scope range --value <RANGE>)")
  275. cmdDecisionsList.Flags().IntVarP(filter.Limit, "limit", "l", 100, "number of alerts to get (use 0 to remove the limit)")
  276. cmdDecisionsList.Flags().BoolVar(NoSimu, "no-simu", false, "exclude decisions in simulation mode")
  277. cmdDecisionsList.Flags().BoolVarP(&printMachine, "machine", "m", false, "print machines that triggered decisions")
  278. cmdDecisionsList.Flags().BoolVar(contained, "contained", false, "query decisions contained by range")
  279. cmdDecisions.AddCommand(cmdDecisionsList)
  280. var (
  281. addIP string
  282. addRange string
  283. addDuration string
  284. addValue string
  285. addScope string
  286. addReason string
  287. addType string
  288. )
  289. var cmdDecisionsAdd = &cobra.Command{
  290. Use: "add [options]",
  291. Short: "Add decision to LAPI",
  292. Example: `cscli decisions add --ip 1.2.3.4
  293. cscli decisions add --range 1.2.3.0/24
  294. cscli decisions add --ip 1.2.3.4 --duration 24h --type captcha
  295. cscli decisions add --scope username --value foobar
  296. `,
  297. /*TBD : fix long and example*/
  298. Args: cobra.ExactArgs(0),
  299. DisableAutoGenTag: true,
  300. Run: func(cmd *cobra.Command, args []string) {
  301. var err error
  302. var ipRange string
  303. alerts := models.AddAlertsRequest{}
  304. origin := "cscli"
  305. capacity := int32(0)
  306. leakSpeed := "0"
  307. eventsCount := int32(1)
  308. empty := ""
  309. simulated := false
  310. startAt := time.Now().UTC().Format(time.RFC3339)
  311. stopAt := time.Now().UTC().Format(time.RFC3339)
  312. createdAt := time.Now().UTC().Format(time.RFC3339)
  313. /*take care of shorthand options*/
  314. if err := manageCliDecisionAlerts(&addIP, &addRange, &addScope, &addValue); err != nil {
  315. log.Fatalf("%s", err)
  316. }
  317. if addIP != "" {
  318. addValue = addIP
  319. addScope = types.Ip
  320. } else if addRange != "" {
  321. addValue = addRange
  322. addScope = types.Range
  323. } else if addValue == "" {
  324. printHelp(cmd)
  325. log.Fatalf("Missing arguments, a value is required (--ip, --range or --scope and --value)")
  326. }
  327. if addReason == "" {
  328. addReason = fmt.Sprintf("manual '%s' from '%s'", addType, csConfig.API.Client.Credentials.Login)
  329. }
  330. decision := models.Decision{
  331. Duration: &addDuration,
  332. Scope: &addScope,
  333. Value: &addValue,
  334. Type: &addType,
  335. Scenario: &addReason,
  336. Origin: &origin,
  337. }
  338. alert := models.Alert{
  339. Capacity: &capacity,
  340. Decisions: []*models.Decision{&decision},
  341. Events: []*models.Event{},
  342. EventsCount: &eventsCount,
  343. Leakspeed: &leakSpeed,
  344. Message: &addReason,
  345. ScenarioHash: &empty,
  346. Scenario: &addReason,
  347. ScenarioVersion: &empty,
  348. Simulated: &simulated,
  349. Source: &models.Source{
  350. AsName: empty,
  351. AsNumber: empty,
  352. Cn: empty,
  353. IP: addValue,
  354. Range: ipRange,
  355. Scope: &addScope,
  356. Value: &addValue,
  357. },
  358. StartAt: &startAt,
  359. StopAt: &stopAt,
  360. CreatedAt: createdAt,
  361. }
  362. alerts = append(alerts, &alert)
  363. _, _, err = Client.Alerts.Add(context.Background(), alerts)
  364. if err != nil {
  365. log.Fatalf(err.Error())
  366. }
  367. log.Info("Decision successfully added")
  368. },
  369. }
  370. cmdDecisionsAdd.Flags().SortFlags = false
  371. cmdDecisionsAdd.Flags().StringVarP(&addIP, "ip", "i", "", "Source ip (shorthand for --scope ip --value <IP>)")
  372. cmdDecisionsAdd.Flags().StringVarP(&addRange, "range", "r", "", "Range source ip (shorthand for --scope range --value <RANGE>)")
  373. cmdDecisionsAdd.Flags().StringVarP(&addDuration, "duration", "d", "4h", "Decision duration (ie. 1h,4h,30m)")
  374. cmdDecisionsAdd.Flags().StringVarP(&addValue, "value", "v", "", "The value (ie. --scope username --value foobar)")
  375. cmdDecisionsAdd.Flags().StringVar(&addScope, "scope", types.Ip, "Decision scope (ie. ip,range,username)")
  376. cmdDecisionsAdd.Flags().StringVarP(&addReason, "reason", "R", "", "Decision reason (ie. scenario-name)")
  377. cmdDecisionsAdd.Flags().StringVarP(&addType, "type", "t", "ban", "Decision type (ie. ban,captcha,throttle)")
  378. cmdDecisions.AddCommand(cmdDecisionsAdd)
  379. var delFilter = apiclient.DecisionsDeleteOpts{
  380. ScopeEquals: new(string),
  381. ValueEquals: new(string),
  382. TypeEquals: new(string),
  383. IPEquals: new(string),
  384. RangeEquals: new(string),
  385. }
  386. var delDecisionId string
  387. var delDecisionAll bool
  388. var cmdDecisionsDelete = &cobra.Command{
  389. Use: "delete [options]",
  390. Short: "Delete decisions",
  391. DisableAutoGenTag: true,
  392. Aliases: []string{"remove"},
  393. Example: `cscli decisions delete -r 1.2.3.0/24
  394. cscli decisions delete -i 1.2.3.4
  395. cscli decisions delete -s crowdsecurity/ssh-bf
  396. cscli decisions delete --id 42
  397. cscli decisions delete --type captcha
  398. `,
  399. /*TBD : refaire le Long/Example*/
  400. PreRun: func(cmd *cobra.Command, args []string) {
  401. if delDecisionAll {
  402. return
  403. }
  404. if *delFilter.ScopeEquals == "" && *delFilter.ValueEquals == "" &&
  405. *delFilter.TypeEquals == "" && *delFilter.IPEquals == "" &&
  406. *delFilter.RangeEquals == "" && delDecisionId == "" {
  407. cmd.Usage()
  408. log.Fatalln("At least one filter or --all must be specified")
  409. }
  410. },
  411. Run: func(cmd *cobra.Command, args []string) {
  412. var err error
  413. var decisions *models.DeleteDecisionResponse
  414. /*take care of shorthand options*/
  415. if err := manageCliDecisionAlerts(delFilter.IPEquals, delFilter.RangeEquals, delFilter.ScopeEquals, delFilter.ValueEquals); err != nil {
  416. log.Fatalf("%s", err)
  417. }
  418. if *delFilter.ScopeEquals == "" {
  419. delFilter.ScopeEquals = nil
  420. }
  421. if *delFilter.ValueEquals == "" {
  422. delFilter.ValueEquals = nil
  423. }
  424. if *delFilter.TypeEquals == "" {
  425. delFilter.TypeEquals = nil
  426. }
  427. if *delFilter.IPEquals == "" {
  428. delFilter.IPEquals = nil
  429. }
  430. if *delFilter.RangeEquals == "" {
  431. delFilter.RangeEquals = nil
  432. }
  433. if contained != nil && *contained {
  434. delFilter.Contains = new(bool)
  435. }
  436. if delDecisionId == "" {
  437. decisions, _, err = Client.Decisions.Delete(context.Background(), delFilter)
  438. if err != nil {
  439. log.Fatalf("Unable to delete decisions : %v", err.Error())
  440. }
  441. } else {
  442. decisions, _, err = Client.Decisions.DeleteOne(context.Background(), delDecisionId)
  443. if err != nil {
  444. log.Fatalf("Unable to delete decision : %v", err.Error())
  445. }
  446. }
  447. log.Infof("%s decision(s) deleted", decisions.NbDeleted)
  448. },
  449. }
  450. cmdDecisionsDelete.Flags().SortFlags = false
  451. cmdDecisionsDelete.Flags().StringVarP(delFilter.IPEquals, "ip", "i", "", "Source ip (shorthand for --scope ip --value <IP>)")
  452. cmdDecisionsDelete.Flags().StringVarP(delFilter.RangeEquals, "range", "r", "", "Range source ip (shorthand for --scope range --value <RANGE>)")
  453. cmdDecisionsDelete.Flags().StringVar(&delDecisionId, "id", "", "decision id")
  454. cmdDecisionsDelete.Flags().StringVarP(delFilter.TypeEquals, "type", "t", "", "the decision type (ie. ban,captcha)")
  455. cmdDecisionsDelete.Flags().StringVarP(delFilter.ValueEquals, "value", "v", "", "the value to match for in the specified scope")
  456. cmdDecisionsDelete.Flags().BoolVar(&delDecisionAll, "all", false, "delete all decisions")
  457. cmdDecisionsDelete.Flags().BoolVar(contained, "contained", false, "query decisions contained by range")
  458. cmdDecisions.AddCommand(cmdDecisionsDelete)
  459. var (
  460. importDuration string
  461. importScope string
  462. importReason string
  463. importType string
  464. importFile string
  465. )
  466. var cmdDecisionImport = &cobra.Command{
  467. Use: "import [options]",
  468. Short: "Import decisions from json or csv file",
  469. Long: "expected format :\n" +
  470. "csv : any of duration,origin,reason,scope,type,value, with a header line\n" +
  471. `json : {"duration" : "24h", "origin" : "my-list", "reason" : "my_scenario", "scope" : "ip", "type" : "ban", "value" : "x.y.z.z"}`,
  472. DisableAutoGenTag: true,
  473. Example: `decisions.csv :
  474. duration,scope,value
  475. 24h,ip,1.2.3.4
  476. cscsli decisions import -i decisions.csv
  477. decisions.json :
  478. [{"duration" : "4h", "scope" : "ip", "type" : "ban", "value" : "1.2.3.4"}]
  479. `,
  480. Run: func(cmd *cobra.Command, args []string) {
  481. if importFile == "" {
  482. log.Fatalf("Please provide a input file containing decisions with -i flag")
  483. }
  484. csvData, err := os.ReadFile(importFile)
  485. if err != nil {
  486. log.Fatalf("unable to open '%s': %s", importFile, err)
  487. }
  488. type decisionRaw struct {
  489. Duration string `csv:"duration,omitempty" json:"duration,omitempty"`
  490. Origin string `csv:"origin,omitempty" json:"origin,omitempty"`
  491. Scenario string `csv:"reason,omitempty" json:"reason,omitempty"`
  492. Scope string `csv:"scope,omitempty" json:"scope,omitempty"`
  493. Type string `csv:"type,omitempty" json:"type,omitempty"`
  494. Value string `csv:"value" json:"value"`
  495. }
  496. var decisionsListRaw []decisionRaw
  497. switch fileFormat := filepath.Ext(importFile); fileFormat {
  498. case ".json":
  499. if err := json.Unmarshal(csvData, &decisionsListRaw); err != nil {
  500. log.Fatalf("unable to unmarshall json: '%s'", err)
  501. }
  502. case ".csv":
  503. if err := csvutil.Unmarshal(csvData, &decisionsListRaw); err != nil {
  504. log.Fatalf("unable to unmarshall csv: '%s'", err)
  505. }
  506. default:
  507. log.Fatalf("file format not supported for '%s'. supported format are 'json' and 'csv'", importFile)
  508. }
  509. decisionsList := make([]*models.Decision, 0)
  510. for i, decisionLine := range decisionsListRaw {
  511. line := i + 2
  512. if decisionLine.Value == "" {
  513. log.Fatalf("please provide a 'value' in your csv line %d", line)
  514. }
  515. /*deal with defaults and cli-override*/
  516. if decisionLine.Duration == "" {
  517. decisionLine.Duration = defaultDuration
  518. log.Debugf("No 'duration' line %d, using default value: '%s'", line, defaultDuration)
  519. }
  520. if importDuration != "" {
  521. decisionLine.Duration = importDuration
  522. log.Debugf("'duration' line %d, using supplied value: '%s'", line, importDuration)
  523. }
  524. decisionLine.Origin = "cscli-import"
  525. if decisionLine.Scenario == "" {
  526. decisionLine.Scenario = defaultReason
  527. log.Debugf("No 'reason' line %d, using value: '%s'", line, decisionLine.Scenario)
  528. }
  529. if importReason != "" {
  530. decisionLine.Scenario = importReason
  531. log.Debugf("No 'reason' line %d, using supplied value: '%s'", line, importReason)
  532. }
  533. if decisionLine.Type == "" {
  534. decisionLine.Type = defaultType
  535. log.Debugf("No 'type' line %d, using default value: '%s'", line, decisionLine.Type)
  536. }
  537. if importType != "" {
  538. decisionLine.Type = importType
  539. log.Debugf("'type' line %d, using supplied value: '%s'", line, importType)
  540. }
  541. if decisionLine.Scope == "" {
  542. decisionLine.Scope = defaultScope
  543. log.Debugf("No 'scope' line %d, using default value: '%s'", line, decisionLine.Scope)
  544. }
  545. if importScope != "" {
  546. decisionLine.Scope = importScope
  547. log.Debugf("'scope' line %d, using supplied value: '%s'", line, importScope)
  548. }
  549. decision := models.Decision{
  550. Value: types.StrPtr(decisionLine.Value),
  551. Duration: types.StrPtr(decisionLine.Duration),
  552. Origin: types.StrPtr(decisionLine.Origin),
  553. Scenario: types.StrPtr(decisionLine.Scenario),
  554. Type: types.StrPtr(decisionLine.Type),
  555. Scope: types.StrPtr(decisionLine.Scope),
  556. Simulated: new(bool),
  557. }
  558. decisionsList = append(decisionsList, &decision)
  559. }
  560. alerts := models.AddAlertsRequest{}
  561. importAlert := models.Alert{
  562. CreatedAt: time.Now().UTC().Format(time.RFC3339),
  563. Scenario: types.StrPtr(fmt.Sprintf("add: %d IPs", len(decisionsList))),
  564. Message: types.StrPtr(""),
  565. Events: []*models.Event{},
  566. Source: &models.Source{
  567. Scope: types.StrPtr("cscli/manual-import"),
  568. Value: types.StrPtr(""),
  569. },
  570. StartAt: types.StrPtr(time.Now().UTC().Format(time.RFC3339)),
  571. StopAt: types.StrPtr(time.Now().UTC().Format(time.RFC3339)),
  572. Capacity: types.Int32Ptr(0),
  573. Simulated: types.BoolPtr(false),
  574. EventsCount: types.Int32Ptr(int32(len(decisionsList))),
  575. Leakspeed: types.StrPtr(""),
  576. ScenarioHash: types.StrPtr(""),
  577. ScenarioVersion: types.StrPtr(""),
  578. Decisions: decisionsList,
  579. }
  580. alerts = append(alerts, &importAlert)
  581. if len(decisionsList) > 1000 {
  582. log.Infof("You are about to add %d decisions, this may take a while", len(decisionsList))
  583. }
  584. _, _, err = Client.Alerts.Add(context.Background(), alerts)
  585. if err != nil {
  586. log.Fatalf(err.Error())
  587. }
  588. log.Infof("%d decisions successfully imported", len(decisionsList))
  589. },
  590. }
  591. cmdDecisionImport.Flags().SortFlags = false
  592. cmdDecisionImport.Flags().StringVarP(&importFile, "input", "i", "", "Input file")
  593. cmdDecisionImport.Flags().StringVarP(&importDuration, "duration", "d", "", "Decision duration (ie. 1h,4h,30m)")
  594. cmdDecisionImport.Flags().StringVar(&importScope, "scope", types.Ip, "Decision scope (ie. ip,range,username)")
  595. cmdDecisionImport.Flags().StringVarP(&importReason, "reason", "R", "", "Decision reason (ie. scenario-name)")
  596. cmdDecisionImport.Flags().StringVarP(&importType, "type", "t", "", "Decision type (ie. ban,captcha,throttle)")
  597. cmdDecisions.AddCommand(cmdDecisionImport)
  598. return cmdDecisions
  599. }