Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"cscli bouncers add": increase key size, deprecate and ignore --length option #2531

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions cmd/crowdsec-cli/bouncers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,38 @@
"encoding/json"
"fmt"
"io"
"slices"
"strings"
"time"

"github.com/AlecAivazis/survey/v2"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"slices"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
"github.com/crowdsecurity/crowdsec/pkg/database"
"github.com/crowdsecurity/crowdsec/pkg/types"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
)

func getBouncers(out io.Writer, dbClient *database.Client) error {
bouncers, err := dbClient.ListBouncers()
if err != nil {
return fmt.Errorf("unable to list bouncers: %s", err)
}
if csConfig.Cscli.Output == "human" {

switch csConfig.Cscli.Output {
case "human":
getBouncersTable(out, bouncers)
} else if csConfig.Cscli.Output == "json" {
case "json":
enc := json.NewEncoder(out)
enc.SetIndent("", " ")
if err := enc.Encode(bouncers); err != nil {
return fmt.Errorf("failed to unmarshal: %w", err)
}
return nil
} else if csConfig.Cscli.Output == "raw" {
case "raw":

Check warning on line 39 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L39

Added line #L39 was not covered by tests
csvwriter := csv.NewWriter(out)
err := csvwriter.Write([]string{"name", "ip", "revoked", "last_pull", "type", "version", "auth_type"})
if err != nil {
Expand All @@ -55,6 +56,7 @@
}
csvwriter.Flush()
}

return nil
}

Expand All @@ -78,12 +80,9 @@
}

func runBouncersAdd(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
keyLength := 32

keyLength, err := flags.GetInt("length")
if err != nil {
return err
}
flags := cmd.Flags()

key, err := flags.GetString("key")
if err != nil {
Expand All @@ -108,13 +107,14 @@
return fmt.Errorf("unable to create bouncer: %s", err)
}

if csConfig.Cscli.Output == "human" {
switch csConfig.Cscli.Output {
case "human":
fmt.Printf("API key for '%s':\n\n", keyName)
fmt.Printf(" %s\n\n", apiKey)
fmt.Print("Please keep this key since you will not be able to retrieve it!\n")
} else if csConfig.Cscli.Output == "raw" {
case "raw":
fmt.Printf("%s", apiKey)
} else if csConfig.Cscli.Output == "json" {
case "json":
j, err := json.Marshal(apiKey)
if err != nil {
return fmt.Errorf("unable to marshal api key")
Expand All @@ -127,19 +127,18 @@

func NewBouncersAddCmd() *cobra.Command {
cmdBouncersAdd := &cobra.Command{
Use: "add MyBouncerName [--length 16]",
Use: "add MyBouncerName",
Short: "add a single bouncer to the database",
Example: `cscli bouncers add MyBouncerName
cscli bouncers add MyBouncerName -l 24
cscli bouncers add MyBouncerName -k <random-key>`,
cscli bouncers add MyBouncerName --key <random-key>`,
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
RunE: runBouncersAdd,
}

flags := cmdBouncersAdd.Flags()

flags.IntP("length", "l", 16, "length of the api key")
flags.StringP("length", "l", "", "length of the api key")
flags.MarkDeprecated("length", "use --key instead")
flags.StringP("key", "k", "", "api key for the bouncer")

return cmdBouncersAdd
Expand Down
12 changes: 12 additions & 0 deletions test/bats/10_bouncers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ teardown() {
assert_output '[]'
}

@test "we can create a bouncer with a known key" {
# also test the output formats since we know the key
rune -0 cscli bouncers add ciTestBouncer --key "foobarbaz" -o human
assert_output --partial 'foobarbaz'
rune -0 cscli bouncers delete ciTestBouncer
rune -0 cscli bouncers add ciTestBouncer --key "foobarbaz" -o json
assert_output '"foobarbaz"'
rune -0 cscli bouncers delete ciTestBouncer
rune -0 cscli bouncers add ciTestBouncer --key "foobarbaz" -o raw
assert_output foobarbaz
}

@test "we can't add the same bouncer twice" {
rune -0 cscli bouncers add ciTestBouncer
rune -1 cscli bouncers add ciTestBouncer -o json
Expand Down
Loading