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

refact cscli: decisions, lapi, bouncers, machines #3306

Merged
merged 6 commits into from
Oct 30, 2024
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
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/clialert/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
for _, decision := range alert.Decisions {
k := *decision.Type
if *decision.Simulated {
k = fmt.Sprintf("(simul)%s", k)
k = "(simul)" + k

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clialert/alerts.go#L39

Added line #L39 was not covered by tests
}

v := decMap[k]
Expand Down Expand Up @@ -465,7 +465,7 @@
cscli alerts delete -s crowdsecurity/ssh-bf"`,
DisableAutoGenTag: true,
Aliases: []string{"remove"},
Args: cobra.ExactArgs(0),
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, _ []string) error {
if deleteAll {
return nil
Expand Down
72 changes: 72 additions & 0 deletions cmd/crowdsec-cli/clibouncer/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package clibouncer

import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/spf13/cobra"

middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
"github.com/crowdsecurity/crowdsec/pkg/types"
)

func (cli *cliBouncers) add(ctx context.Context, bouncerName string, key string) error {
var err error

keyLength := 32

if key == "" {
key, err = middlewares.GenerateAPIKey(keyLength)
if err != nil {
return fmt.Errorf("unable to generate api key: %w", err)
}

Check warning on line 24 in cmd/crowdsec-cli/clibouncer/add.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clibouncer/add.go#L23-L24

Added lines #L23 - L24 were not covered by tests
}

_, err = cli.db.CreateBouncer(ctx, bouncerName, "", middlewares.HashSHA512(key), types.ApiKeyAuthType)
if err != nil {
return fmt.Errorf("unable to create bouncer: %w", err)
}

switch cli.cfg().Cscli.Output {
case "human":
fmt.Printf("API key for '%s':\n\n", bouncerName)
fmt.Printf(" %s\n\n", key)
fmt.Print("Please keep this key since you will not be able to retrieve it!\n")
case "raw":
fmt.Print(key)
case "json":
j, err := json.Marshal(key)
if err != nil {
return errors.New("unable to serialize api key")
}

Check warning on line 43 in cmd/crowdsec-cli/clibouncer/add.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clibouncer/add.go#L42-L43

Added lines #L42 - L43 were not covered by tests

fmt.Print(string(j))
}

return nil
}

func (cli *cliBouncers) newAddCmd() *cobra.Command {
var key string

cmd := &cobra.Command{
Use: "add MyBouncerName",
Short: "add a single bouncer to the database",
Example: `cscli bouncers add MyBouncerName
cscli bouncers add MyBouncerName --key <random-key>`,
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
return cli.add(cmd.Context(), args[0], key)
},
}

flags := cmd.Flags()
flags.StringP("length", "l", "", "length of the api key")
_ = flags.MarkDeprecated("length", "use --key instead")
flags.StringVarP(&key, "key", "k", "", "api key for the bouncer")

return cmd
}
Loading