2020-11-30 09:37:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-29 13:08:47 +00:00
|
|
|
"encoding/csv"
|
2020-11-30 09:37:17 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-10-07 09:05:35 +00:00
|
|
|
"io"
|
2022-12-05 14:39:54 +00:00
|
|
|
"strings"
|
2020-11-30 09:37:17 +00:00
|
|
|
"time"
|
|
|
|
|
2022-10-13 10:28:24 +00:00
|
|
|
"github.com/fatih/color"
|
2022-08-18 09:54:01 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-11-30 09:37:17 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
2022-10-07 09:05:35 +00:00
|
|
|
|
|
|
|
middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
2020-11-30 09:37:17 +00:00
|
|
|
)
|
|
|
|
|
2022-10-07 09:05:35 +00:00
|
|
|
func getBouncers(out io.Writer, dbClient *database.Client) error {
|
2022-08-18 09:54:01 +00:00
|
|
|
bouncers, err := dbClient.ListBouncers()
|
|
|
|
if err != nil {
|
2022-10-07 09:05:35 +00:00
|
|
|
return fmt.Errorf("unable to list bouncers: %s", err)
|
2022-08-18 09:54:01 +00:00
|
|
|
}
|
|
|
|
if csConfig.Cscli.Output == "human" {
|
2022-10-07 09:05:35 +00:00
|
|
|
getBouncersTable(out, bouncers)
|
2022-08-18 09:54:01 +00:00
|
|
|
} else if csConfig.Cscli.Output == "json" {
|
2022-10-07 09:05:35 +00:00
|
|
|
enc := json.NewEncoder(out)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
if err := enc.Encode(bouncers); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to unmarshal")
|
2022-08-18 09:54:01 +00:00
|
|
|
}
|
2022-10-07 09:05:35 +00:00
|
|
|
return nil
|
2022-08-18 09:54:01 +00:00
|
|
|
} else if csConfig.Cscli.Output == "raw" {
|
2022-10-07 09:05:35 +00:00
|
|
|
csvwriter := csv.NewWriter(out)
|
2022-08-18 09:54:01 +00:00
|
|
|
err := csvwriter.Write([]string{"name", "ip", "revoked", "last_pull", "type", "version", "auth_type"})
|
|
|
|
if err != nil {
|
2022-10-07 09:05:35 +00:00
|
|
|
return errors.Wrap(err, "failed to write raw header")
|
2022-08-18 09:54:01 +00:00
|
|
|
}
|
|
|
|
for _, b := range bouncers {
|
|
|
|
var revoked string
|
|
|
|
if !b.Revoked {
|
|
|
|
revoked = "validated"
|
|
|
|
} else {
|
|
|
|
revoked = "pending"
|
|
|
|
}
|
|
|
|
err := csvwriter.Write([]string{b.Name, b.IPAddress, revoked, b.LastPull.Format(time.RFC3339), b.Type, b.Version, b.AuthType})
|
|
|
|
if err != nil {
|
2022-10-07 09:05:35 +00:00
|
|
|
return errors.Wrap(err, "failed to write raw")
|
2022-08-18 09:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
csvwriter.Flush()
|
|
|
|
}
|
2022-10-07 09:05:35 +00:00
|
|
|
return nil
|
2022-08-18 09:54:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:13:52 +00:00
|
|
|
func NewBouncersListCmd() *cobra.Command {
|
|
|
|
cmdBouncersList := &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "list",
|
|
|
|
Short: "List bouncers",
|
|
|
|
Long: `List bouncers`,
|
|
|
|
Example: `cscli bouncers list`,
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2020-11-30 09:37:17 +00:00
|
|
|
Run: func(cmd *cobra.Command, arg []string) {
|
2022-10-13 10:28:24 +00:00
|
|
|
err := getBouncers(color.Output, dbClient)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2022-08-18 09:54:01 +00:00
|
|
|
log.Fatalf("unable to list bouncers: %s", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:13:52 +00:00
|
|
|
return cmdBouncersList
|
|
|
|
}
|
|
|
|
|
|
|
|
func runBouncersAdd(cmd *cobra.Command, args []string) error {
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
keyLength, err := flags.GetInt("length")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := flags.GetString("key")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
keyName := args[0]
|
|
|
|
var apiKey string
|
|
|
|
|
|
|
|
if keyName == "" {
|
|
|
|
log.Fatalf("Please provide a name for the api key")
|
|
|
|
}
|
|
|
|
apiKey = key
|
|
|
|
if key == "" {
|
|
|
|
apiKey, err = middlewares.GenerateAPIKey(keyLength)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to generate api key: %s", err)
|
|
|
|
}
|
|
|
|
_, err = dbClient.CreateBouncer(keyName, "", middlewares.HashSHA512(apiKey), types.ApiKeyAuthType)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to create bouncer: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if csConfig.Cscli.Output == "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" {
|
|
|
|
fmt.Printf("%s", apiKey)
|
|
|
|
} else if csConfig.Cscli.Output == "json" {
|
|
|
|
j, err := json.Marshal(apiKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to marshal api key")
|
|
|
|
}
|
|
|
|
fmt.Printf("%s", string(j))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewBouncersAddCmd() *cobra.Command {
|
|
|
|
cmdBouncersAdd := &cobra.Command{
|
2020-11-30 09:37:17 +00:00
|
|
|
Use: "add MyBouncerName [--length 16]",
|
|
|
|
Short: "add bouncer",
|
|
|
|
Long: `add bouncer`,
|
2021-11-02 07:37:51 +00:00
|
|
|
Example: fmt.Sprintf(`cscli bouncers add MyBouncerName
|
|
|
|
cscli bouncers add MyBouncerName -l 24
|
|
|
|
cscli bouncers add MyBouncerName -k %s`, generatePassword(32)),
|
2021-08-31 13:03:47 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
2022-12-30 09:13:52 +00:00
|
|
|
RunE: runBouncersAdd,
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2022-12-30 09:13:52 +00:00
|
|
|
flags := cmdBouncersAdd.Flags()
|
|
|
|
|
|
|
|
flags.IntP("length", "l", 16, "length of the api key")
|
|
|
|
flags.StringP("key", "k", "", "api key for the bouncer")
|
|
|
|
|
|
|
|
return cmdBouncersAdd
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func runBouncersDelete(cmd *cobra.Command, args []string) error {
|
|
|
|
for _, bouncerID := range args {
|
|
|
|
err := dbClient.DeleteBouncer(bouncerID)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to delete bouncer '%s': %s", bouncerID, err)
|
|
|
|
}
|
|
|
|
log.Infof("bouncer '%s' deleted successfully", bouncerID)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:13:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewBouncersDeleteCmd() *cobra.Command {
|
|
|
|
cmdBouncersDelete := &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "delete MyBouncerName",
|
|
|
|
Short: "delete bouncer",
|
2021-09-02 10:23:06 +00:00
|
|
|
Args: cobra.MinimumNArgs(1),
|
2022-04-20 13:44:48 +00:00
|
|
|
Aliases: []string{"remove"},
|
2021-08-31 13:03:47 +00:00
|
|
|
DisableAutoGenTag: true,
|
2022-12-05 14:39:54 +00:00
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
var err error
|
|
|
|
dbClient, err = getDBClient()
|
|
|
|
if err != nil {
|
|
|
|
cobra.CompError("unable to create new database client: " + err.Error())
|
|
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|
|
|
|
bouncers, err := dbClient.ListBouncers()
|
|
|
|
if err != nil {
|
|
|
|
cobra.CompError("unable to list bouncers " + err.Error())
|
|
|
|
}
|
|
|
|
ret := make([]string, 0)
|
|
|
|
for _, bouncer := range bouncers {
|
|
|
|
if strings.Contains(bouncer.Name, toComplete) && !inSlice(bouncer.Name, args) {
|
|
|
|
ret = append(ret, bouncer.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
},
|
2022-12-30 09:13:52 +00:00
|
|
|
RunE: runBouncersDelete,
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmdBouncersDelete
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBouncersCmd() *cobra.Command {
|
|
|
|
var cmdBouncers = &cobra.Command{
|
|
|
|
Use: "bouncers [action]",
|
|
|
|
Short: "Manage bouncers [requires local API]",
|
|
|
|
Long: `To list/add/delete bouncers.
|
|
|
|
Note: This command requires database direct access, so is intended to be run on Local API/master.
|
|
|
|
`,
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
Aliases: []string{"bouncer"},
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
var err error
|
|
|
|
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
|
|
|
log.Fatal("Local API is disabled, please run this command on the local API machine")
|
|
|
|
}
|
|
|
|
dbClient, err = database.NewClient(csConfig.DbConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to create new database client: %s", err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2022-12-30 09:13:52 +00:00
|
|
|
|
|
|
|
cmdBouncers.AddCommand(NewBouncersListCmd())
|
|
|
|
cmdBouncers.AddCommand(NewBouncersAddCmd())
|
|
|
|
cmdBouncers.AddCommand(NewBouncersDeleteCmd())
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return cmdBouncers
|
|
|
|
}
|