Skip to content

Commit

Permalink
Merge branch 'master' into hub-1.5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Nov 24, 2023
2 parents f8a61b1 + 76d4bc7 commit c07a3b6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
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 @@ import (
"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":
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 @@ func getBouncers(out io.Writer, dbClient *database.Client) error {
}
csvwriter.Flush()
}

return nil
}

Expand All @@ -78,12 +80,9 @@ func NewBouncersListCmd() *cobra.Command {
}

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 @@ func runBouncersAdd(cmd *cobra.Command, args []string) error {
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 runBouncersAdd(cmd *cobra.Command, args []string) error {

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
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var (

func main() {
// set the formatter asap and worry about level later
logFormatter := &log.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true}
logFormatter := &log.TextFormatter{TimestampFormat: "2006-01-02 15:04:05", FullTimestamp: true}
log.SetFormatter(logFormatter)

if err := fflag.RegisterAllFeatures(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level
}
logLevel = cfgLevel
log.SetLevel(logLevel)
logFormatter = &log.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true, ForceColors: forceColors}
logFormatter = &log.TextFormatter{TimestampFormat: "2006-01-02 15:04:05", FullTimestamp: true, ForceColors: forceColors}
log.SetFormatter(logFormatter)
return nil
}
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

0 comments on commit c07a3b6

Please sign in to comment.