(ctx context.Context, db *database.Client, name string, values []string, expiration time.Duration, comment string)
| 457 | } |
| 458 | |
| 459 | func (*cliAllowLists) add(ctx context.Context, db *database.Client, name string, values []string, expiration time.Duration, comment string) error { |
| 460 | allowlist, err := db.GetAllowList(ctx, name, true) |
| 461 | if err != nil { |
| 462 | return err |
| 463 | } |
| 464 | |
| 465 | if allowlist.FromConsole { |
| 466 | 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) |
| 467 | } |
| 468 | |
| 469 | toAdd := make([]*models.AllowlistItem, 0) |
| 470 | |
| 471 | for _, v := range values { |
| 472 | found := false |
| 473 | |
| 474 | for _, item := range allowlist.Edges.AllowlistItems { |
| 475 | if item.Value == v { |
| 476 | found = true |
| 477 | |
| 478 | log.Warnf("value %s already in allowlist", v) |
| 479 | |
| 480 | break |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | if !found { |
| 485 | expTS := time.Time{} |
| 486 | if expiration != 0 { |
| 487 | expTS = time.Now().UTC().Add(expiration) |
| 488 | } |
| 489 | |
| 490 | toAdd = append(toAdd, &models.AllowlistItem{Value: v, Description: comment, Expiration: strfmt.DateTime(expTS)}) |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | if len(toAdd) == 0 { |
| 495 | fmt.Fprintln(os.Stdout, "no new values for allowlist") |
| 496 | return nil |
| 497 | } |
| 498 | |
| 499 | added, err := db.AddToAllowlist(ctx, allowlist, toAdd) |
| 500 | if err != nil { |
| 501 | return fmt.Errorf("unable to add values to allowlist: %w", err) |
| 502 | } |
| 503 | |
| 504 | if added > 0 { |
| 505 | fmt.Fprintf(os.Stdout, "added %d values to allowlist %s\n", added, name) |
| 506 | } |
| 507 | |
| 508 | deleted, err := db.ApplyAllowlistsToExistingDecisions(ctx) |
| 509 | if err != nil { |
| 510 | return fmt.Errorf("unable to apply allowlists to existing decisions: %w", err) |
| 511 | } |
| 512 | if deleted > 0 { |
| 513 | fmt.Fprintf(os.Stdout, "%d decisions deleted by allowlists\n", deleted) |
| 514 | } |
| 515 | |
| 516 | return nil |
no test coverage detected