|
| 1 | +package cliallowlists |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/go-openapi/strfmt" |
| 12 | + "github.com/jszwec/csvutil" |
| 13 | + log "github.com/sirupsen/logrus" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + |
| 16 | + "github.com/crowdsecurity/go-cs-lib/cstime" |
| 17 | + |
| 18 | + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/core/args" |
| 19 | + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/core/require" |
| 20 | + "github.com/crowdsecurity/crowdsec/pkg/database" |
| 21 | + "github.com/crowdsecurity/crowdsec/pkg/models" |
| 22 | +) |
| 23 | + |
| 24 | +type allowlistItemRaw struct { |
| 25 | + Value string `csv:"value"` |
| 26 | + Expiration string `csv:"expiration,omitempty"` |
| 27 | + Comment string `csv:"comment,omitempty"` |
| 28 | +} |
| 29 | + |
| 30 | +func (cli *cliAllowLists) newImportCmd() *cobra.Command { |
| 31 | + var input string |
| 32 | + |
| 33 | + cmd := &cobra.Command{ |
| 34 | + Use: "import [allowlist_name] -i <file>", |
| 35 | + Short: "Import values to an allowlist from a CSV file", |
| 36 | + Long: "Import values to an allowlist from a CSV file.\n\n" + |
| 37 | + "The CSV file must have a header line with at least a 'value' column.\n" + |
| 38 | + "Optional columns: 'expiration' (duration like 1h, 1d), 'comment'.", |
| 39 | + Example: `csv file: |
| 40 | +value,expiration,comment |
| 41 | +1.2.3.4,24h,my comment |
| 42 | +2.3.4.5,,another comment |
| 43 | +10.0.0.0/8,1d, |
| 44 | +
|
| 45 | +$ cscli allowlists import my_allowlist -i allowlist.csv |
| 46 | +
|
| 47 | +From standard input: |
| 48 | +
|
| 49 | +$ cat allowlist.csv | cscli allowlists import my_allowlist -i -`, |
| 50 | + Args: args.ExactArgs(1), |
| 51 | + ValidArgsFunction: cli.validAllowlists, |
| 52 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 53 | + cfg := cli.cfg() |
| 54 | + |
| 55 | + if err := require.LAPI(cfg); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + ctx := cmd.Context() |
| 60 | + |
| 61 | + db, err := require.DBClient(ctx, cfg.DbConfig) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + name := args[0] |
| 67 | + |
| 68 | + return cli.import_allowlist(ctx, db, name, input) |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + flags := cmd.Flags() |
| 73 | + flags.StringVarP(&input, "input", "i", "", "Input file (use - for stdin)") |
| 74 | + |
| 75 | + _ = cmd.MarkFlagRequired("input") |
| 76 | + |
| 77 | + return cmd |
| 78 | +} |
| 79 | + |
| 80 | +func (*cliAllowLists) import_allowlist(ctx context.Context, db *database.Client, name string, input string) error { |
| 81 | + var ( |
| 82 | + fin *os.File |
| 83 | + err error |
| 84 | + ) |
| 85 | + |
| 86 | + if input == "-" { |
| 87 | + fin = os.Stdin |
| 88 | + } else { |
| 89 | + fin, err = os.Open(input) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("unable to open %s: %w", input, err) |
| 92 | + } |
| 93 | + defer fin.Close() |
| 94 | + } |
| 95 | + |
| 96 | + content, err := io.ReadAll(fin) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("unable to read from %s: %w", input, err) |
| 99 | + } |
| 100 | + |
| 101 | + var items []allowlistItemRaw |
| 102 | + |
| 103 | + if err := csvutil.Unmarshal(content, &items); err != nil { |
| 104 | + return fmt.Errorf("unable to parse CSV: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + if len(items) == 0 { |
| 108 | + return errors.New("no values to import") |
| 109 | + } |
| 110 | + |
| 111 | + allowlist, err := db.GetAllowList(ctx, name, true) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + if allowlist.FromConsole { |
| 117 | + return fmt.Errorf("allowlist %s is managed by console, cannot update with cscli. Please visit https://app.crowdsec.net/allowlists/%s to update", name, allowlist.AllowlistID) |
| 118 | + } |
| 119 | + |
| 120 | + toAdd := make([]*models.AllowlistItem, 0) |
| 121 | + |
| 122 | + for i, item := range items { |
| 123 | + if item.Value == "" { |
| 124 | + return fmt.Errorf("row %d: missing 'value'", i+1) |
| 125 | + } |
| 126 | + |
| 127 | + found := false |
| 128 | + |
| 129 | + for _, existing := range allowlist.Edges.AllowlistItems { |
| 130 | + if existing.Value == item.Value { |
| 131 | + found = true |
| 132 | + |
| 133 | + log.Warnf("value %s already in allowlist", item.Value) |
| 134 | + |
| 135 | + break |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if found { |
| 140 | + continue |
| 141 | + } |
| 142 | + |
| 143 | + expTS := time.Time{} |
| 144 | + |
| 145 | + if item.Expiration != "" { |
| 146 | + duration, err := cstime.ParseDurationWithDays(item.Expiration) |
| 147 | + if err != nil { |
| 148 | + return fmt.Errorf("row %d: invalid expiration %q: %w", i+1, item.Expiration, err) |
| 149 | + } |
| 150 | + |
| 151 | + expTS = time.Now().UTC().Add(duration) |
| 152 | + } |
| 153 | + |
| 154 | + toAdd = append(toAdd, &models.AllowlistItem{ |
| 155 | + Value: item.Value, |
| 156 | + Description: item.Comment, |
| 157 | + Expiration: strfmt.DateTime(expTS), |
| 158 | + }) |
| 159 | + } |
| 160 | + |
| 161 | + if len(toAdd) == 0 { |
| 162 | + fmt.Fprintln(os.Stdout, "no new values for allowlist") |
| 163 | + return nil |
| 164 | + } |
| 165 | + |
| 166 | + added, err := db.AddToAllowlist(ctx, allowlist, toAdd) |
| 167 | + if err != nil { |
| 168 | + return fmt.Errorf("unable to add values to allowlist: %w", err) |
| 169 | + } |
| 170 | + |
| 171 | + if added > 0 { |
| 172 | + fmt.Fprintf(os.Stdout, "added %d values to allowlist %s\n", added, name) |
| 173 | + } |
| 174 | + |
| 175 | + deleted, err := db.ApplyAllowlistsToExistingDecisions(ctx) |
| 176 | + if err != nil { |
| 177 | + return fmt.Errorf("unable to apply allowlists to existing decisions: %w", err) |
| 178 | + } |
| 179 | + |
| 180 | + if deleted > 0 { |
| 181 | + fmt.Fprintf(os.Stdout, "%d decisions deleted by allowlists\n", deleted) |
| 182 | + } |
| 183 | + |
| 184 | + return nil |
| 185 | +} |
0 commit comments