separate cscli cobra constructors: lapi, machines, bouncers, postoverflows (#1945)
This commit is contained in:
parent
72c1753fb7
commit
59f6610721
|
@ -18,10 +18,6 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var keyIP string
|
|
||||||
var keyLength int
|
|
||||||
var key string
|
|
||||||
|
|
||||||
func getBouncers(out io.Writer, dbClient *database.Client) error {
|
func getBouncers(out io.Writer, dbClient *database.Client) error {
|
||||||
bouncers, err := dbClient.ListBouncers()
|
bouncers, err := dbClient.ListBouncers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -59,6 +55,141 @@ func getBouncers(out io.Writer, dbClient *database.Client) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewBouncersListCmd() *cobra.Command {
|
||||||
|
cmdBouncersList := &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List bouncers",
|
||||||
|
Long: `List bouncers`,
|
||||||
|
Example: `cscli bouncers list`,
|
||||||
|
Args: cobra.ExactArgs(0),
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
Run: func(cmd *cobra.Command, arg []string) {
|
||||||
|
err := getBouncers(color.Output, dbClient)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("unable to list bouncers: %s", err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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{
|
||||||
|
Use: "add MyBouncerName [--length 16]",
|
||||||
|
Short: "add bouncer",
|
||||||
|
Long: `add bouncer`,
|
||||||
|
Example: fmt.Sprintf(`cscli bouncers add MyBouncerName
|
||||||
|
cscli bouncers add MyBouncerName -l 24
|
||||||
|
cscli bouncers add MyBouncerName -k %s`, generatePassword(32)),
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
RunE: runBouncersAdd,
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func NewBouncersDeleteCmd() *cobra.Command {
|
||||||
|
cmdBouncersDelete := &cobra.Command{
|
||||||
|
Use: "delete MyBouncerName",
|
||||||
|
Short: "delete bouncer",
|
||||||
|
Args: cobra.MinimumNArgs(1),
|
||||||
|
Aliases: []string{"remove"},
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
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
|
||||||
|
},
|
||||||
|
RunE: runBouncersDelete,
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmdBouncersDelete
|
||||||
|
}
|
||||||
|
|
||||||
func NewBouncersCmd() *cobra.Command {
|
func NewBouncersCmd() *cobra.Command {
|
||||||
/* ---- DECISIONS COMMAND */
|
/* ---- DECISIONS COMMAND */
|
||||||
var cmdBouncers = &cobra.Command{
|
var cmdBouncers = &cobra.Command{
|
||||||
|
@ -85,104 +216,9 @@ Note: This command requires database direct access, so is intended to be run on
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmdBouncersList = &cobra.Command{
|
cmdBouncers.AddCommand(NewBouncersListCmd())
|
||||||
Use: "list",
|
cmdBouncers.AddCommand(NewBouncersAddCmd())
|
||||||
Short: "List bouncers",
|
cmdBouncers.AddCommand(NewBouncersDeleteCmd())
|
||||||
Long: `List bouncers`,
|
|
||||||
Example: `cscli bouncers list`,
|
|
||||||
Args: cobra.ExactArgs(0),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
Run: func(cmd *cobra.Command, arg []string) {
|
|
||||||
err := getBouncers(color.Output, dbClient)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("unable to list bouncers: %s", err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmdBouncers.AddCommand(cmdBouncersList)
|
|
||||||
|
|
||||||
var cmdBouncersAdd = &cobra.Command{
|
|
||||||
Use: "add MyBouncerName [--length 16]",
|
|
||||||
Short: "add bouncer",
|
|
||||||
Long: `add bouncer`,
|
|
||||||
Example: fmt.Sprintf(`cscli bouncers add MyBouncerName
|
|
||||||
cscli bouncers add MyBouncerName -l 24
|
|
||||||
cscli bouncers add MyBouncerName -k %s`, generatePassword(32)),
|
|
||||||
Args: cobra.ExactArgs(1),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
Run: func(cmd *cobra.Command, arg []string) {
|
|
||||||
keyName := arg[0]
|
|
||||||
var apiKey string
|
|
||||||
var err error
|
|
||||||
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, keyIP, 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))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmdBouncersAdd.Flags().IntVarP(&keyLength, "length", "l", 16, "length of the api key")
|
|
||||||
cmdBouncersAdd.Flags().StringVarP(&key, "key", "k", "", "api key for the bouncer")
|
|
||||||
cmdBouncers.AddCommand(cmdBouncersAdd)
|
|
||||||
|
|
||||||
var cmdBouncersDelete = &cobra.Command{
|
|
||||||
Use: "delete MyBouncerName",
|
|
||||||
Short: "delete bouncer",
|
|
||||||
Args: cobra.MinimumNArgs(1),
|
|
||||||
Aliases: []string{"remove"},
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
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
|
|
||||||
},
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmdBouncers.AddCommand(cmdBouncersDelete)
|
|
||||||
return cmdBouncers
|
return cmdBouncers
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import (
|
||||||
var CAPIURLPrefix string = "v2"
|
var CAPIURLPrefix string = "v2"
|
||||||
var CAPIBaseURL string = "https://api.crowdsec.net/"
|
var CAPIBaseURL string = "https://api.crowdsec.net/"
|
||||||
var capiUserPrefix string
|
var capiUserPrefix string
|
||||||
|
var outputFile string
|
||||||
|
|
||||||
func NewCapiCmd() *cobra.Command {
|
func NewCapiCmd() *cobra.Command {
|
||||||
var cmdCapi = &cobra.Command{
|
var cmdCapi = &cobra.Command{
|
||||||
|
|
|
@ -21,31 +21,74 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var LAPIURLPrefix string = "v1"
|
var LAPIURLPrefix string = "v1"
|
||||||
var lapiUser string
|
|
||||||
|
|
||||||
func NewLapiCmd() *cobra.Command {
|
func runLapiStatus (cmd *cobra.Command, args []string) error {
|
||||||
var cmdLapi = &cobra.Command{
|
|
||||||
Use: "lapi [action]",
|
|
||||||
Short: "Manage interaction with Local API (LAPI)",
|
|
||||||
Args: cobra.MinimumNArgs(1),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
if err := csConfig.LoadAPIClient(); err != nil {
|
|
||||||
return errors.Wrap(err, "loading api client")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var cmdLapiRegister = &cobra.Command{
|
|
||||||
Use: "register",
|
|
||||||
Short: "Register a machine to Local API (LAPI)",
|
|
||||||
Long: `Register you machine to the Local API (LAPI).
|
|
||||||
Keep in mind the machine needs to be validated by an administrator on LAPI side to be effective.`,
|
|
||||||
Args: cobra.MinimumNArgs(0),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
password := strfmt.Password(csConfig.API.Client.Credentials.Password)
|
||||||
|
apiurl, err := url.Parse(csConfig.API.Client.Credentials.URL)
|
||||||
|
login := csConfig.API.Client.Credentials.Login
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("parsing api url ('%s'): %s", apiurl, err)
|
||||||
|
}
|
||||||
|
if err := csConfig.LoadHub(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
|
||||||
|
log.Info("Run 'sudo cscli hub update' to get the hub index")
|
||||||
|
log.Fatalf("Failed to load hub index : %s", err)
|
||||||
|
}
|
||||||
|
scenarios, err := cwhub.GetInstalledScenariosAsString()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to get scenarios : %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
Client, err = apiclient.NewDefaultClient(apiurl,
|
||||||
|
LAPIURLPrefix,
|
||||||
|
fmt.Sprintf("crowdsec/%s", cwversion.VersionStr()),
|
||||||
|
nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("init default client: %s", err)
|
||||||
|
}
|
||||||
|
t := models.WatcherAuthRequest{
|
||||||
|
MachineID: &login,
|
||||||
|
Password: &password,
|
||||||
|
Scenarios: scenarios,
|
||||||
|
}
|
||||||
|
log.Infof("Loaded credentials from %s", csConfig.API.Client.CredentialsFilePath)
|
||||||
|
log.Infof("Trying to authenticate with username %s on %s", login, apiurl)
|
||||||
|
_, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to authenticate to Local API (LAPI) : %s", err)
|
||||||
|
} else {
|
||||||
|
log.Infof("You can successfully interact with Local API (LAPI)")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func runLapiRegister(cmd *cobra.Command, args []string) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
apiURL, err := flags.GetString("url")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
outputFile, err := flags.GetString("file")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
lapiUser, err := flags.GetString("machine")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if lapiUser == "" {
|
if lapiUser == "" {
|
||||||
lapiUser, err = generateID("")
|
lapiUser, err = generateID("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -113,62 +156,60 @@ Keep in mind the machine needs to be validated by an administrator on LAPI side
|
||||||
fmt.Printf("%s\n", string(apiConfigDump))
|
fmt.Printf("%s\n", string(apiConfigDump))
|
||||||
}
|
}
|
||||||
log.Warning(ReloadMessage())
|
log.Warning(ReloadMessage())
|
||||||
},
|
|
||||||
}
|
|
||||||
cmdLapiRegister.Flags().StringVarP(&apiURL, "url", "u", "", "URL of the API (ie. http://127.0.0.1)")
|
|
||||||
cmdLapiRegister.Flags().StringVarP(&outputFile, "file", "f", "", "output file destination")
|
|
||||||
cmdLapiRegister.Flags().StringVar(&lapiUser, "machine", "", "Name of the machine to register with")
|
|
||||||
cmdLapi.AddCommand(cmdLapiRegister)
|
|
||||||
|
|
||||||
var cmdLapiStatus = &cobra.Command{
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func NewLapiStatusCmd() *cobra.Command {
|
||||||
|
cmdLapiStatus := &cobra.Command{
|
||||||
Use: "status",
|
Use: "status",
|
||||||
Short: "Check authentication to Local API (LAPI)",
|
Short: "Check authentication to Local API (LAPI)",
|
||||||
Args: cobra.MinimumNArgs(0),
|
Args: cobra.MinimumNArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
RunE: runLapiStatus,
|
||||||
var err error
|
|
||||||
|
|
||||||
password := strfmt.Password(csConfig.API.Client.Credentials.Password)
|
|
||||||
apiurl, err := url.Parse(csConfig.API.Client.Credentials.URL)
|
|
||||||
login := csConfig.API.Client.Credentials.Login
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("parsing api url ('%s'): %s", apiurl, err)
|
|
||||||
}
|
|
||||||
if err := csConfig.LoadHub(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
|
return cmdLapiStatus
|
||||||
log.Info("Run 'sudo cscli hub update' to get the hub index")
|
}
|
||||||
log.Fatalf("Failed to load hub index : %s", err)
|
|
||||||
}
|
|
||||||
scenarios, err := cwhub.GetInstalledScenariosAsString()
|
func NewLapiRegisterCmd() *cobra.Command {
|
||||||
if err != nil {
|
cmdLapiRegister := &cobra.Command{
|
||||||
log.Fatalf("failed to get scenarios : %s", err)
|
Use: "register",
|
||||||
|
Short: "Register a machine to Local API (LAPI)",
|
||||||
|
Long: `Register you machine to the Local API (LAPI).
|
||||||
|
Keep in mind the machine needs to be validated by an administrator on LAPI side to be effective.`,
|
||||||
|
Args: cobra.MinimumNArgs(0),
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
RunE: runLapiRegister,
|
||||||
}
|
}
|
||||||
|
|
||||||
Client, err = apiclient.NewDefaultClient(apiurl,
|
flags := cmdLapiRegister.Flags()
|
||||||
LAPIURLPrefix,
|
flags.StringP("url", "u", "", "URL of the API (ie. http://127.0.0.1)")
|
||||||
fmt.Sprintf("crowdsec/%s", cwversion.VersionStr()),
|
flags.StringP("file", "f", "", "output file destination")
|
||||||
nil)
|
flags.String("machine", "", "Name of the machine to register with")
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("init default client: %s", err)
|
return cmdLapiRegister
|
||||||
}
|
}
|
||||||
t := models.WatcherAuthRequest{
|
|
||||||
MachineID: &login,
|
|
||||||
Password: &password,
|
func NewLapiCmd() *cobra.Command {
|
||||||
Scenarios: scenarios,
|
var cmdLapi = &cobra.Command{
|
||||||
}
|
Use: "lapi [action]",
|
||||||
log.Infof("Loaded credentials from %s", csConfig.API.Client.CredentialsFilePath)
|
Short: "Manage interaction with Local API (LAPI)",
|
||||||
log.Infof("Trying to authenticate with username %s on %s", login, apiurl)
|
Args: cobra.MinimumNArgs(1),
|
||||||
_, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
|
DisableAutoGenTag: true,
|
||||||
if err != nil {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
log.Fatalf("Failed to authenticate to Local API (LAPI) : %s", err)
|
if err := csConfig.LoadAPIClient(); err != nil {
|
||||||
} else {
|
return errors.Wrap(err, "loading api client")
|
||||||
log.Infof("You can successfully interact with Local API (LAPI)")
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmdLapi.AddCommand(cmdLapiStatus)
|
|
||||||
|
cmdLapi.AddCommand(NewLapiRegisterCmd())
|
||||||
|
cmdLapi.AddCommand(NewLapiStatusCmd())
|
||||||
|
|
||||||
return cmdLapi
|
return cmdLapi
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,22 +29,15 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var machineID string
|
|
||||||
var machinePassword string
|
|
||||||
var interactive bool
|
|
||||||
var apiURL string
|
|
||||||
var outputFile string
|
|
||||||
var forceAdd bool
|
|
||||||
var autoAdd bool
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
passwordLength = 64
|
passwordLength = 64
|
||||||
upper = "ABCDEFGHIJKLMNOPQRSTUVWXY"
|
|
||||||
lower = "abcdefghijklmnopqrstuvwxyz"
|
|
||||||
digits = "0123456789"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func generatePassword(length int) string {
|
func generatePassword(length int) string {
|
||||||
|
upper := "ABCDEFGHIJKLMNOPQRSTUVWXY"
|
||||||
|
lower := "abcdefghijklmnopqrstuvwxyz"
|
||||||
|
digits := "0123456789"
|
||||||
|
|
||||||
charset := upper + lower + digits
|
charset := upper + lower + digits
|
||||||
charsetLength := len(charset)
|
charsetLength := len(charset)
|
||||||
|
|
||||||
|
@ -149,32 +142,8 @@ func getAgents(out io.Writer, dbClient *database.Client) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMachinesCmd() *cobra.Command {
|
func NewMachinesListCmd() *cobra.Command {
|
||||||
/* ---- DECISIONS COMMAND */
|
cmdMachinesList := &cobra.Command{
|
||||||
var cmdMachines = &cobra.Command{
|
|
||||||
Use: "machines [action]",
|
|
||||||
Short: "Manage local API machines [requires local API]",
|
|
||||||
Long: `To list/add/delete/validate machines.
|
|
||||||
Note: This command requires database direct access, so is intended to be run on the local API machine.
|
|
||||||
`,
|
|
||||||
Example: `cscli machines [action]`,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
Aliases: []string{"machine"},
|
|
||||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
||||||
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("local api : %s", err)
|
|
||||||
}
|
|
||||||
log.Fatal("Local API is disabled, please run this command on the local API machine")
|
|
||||||
}
|
|
||||||
if err := csConfig.LoadDBConfig(); err != nil {
|
|
||||||
log.Errorf("This command requires direct database access (must be run on the local API machine)")
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var cmdMachinesList = &cobra.Command{
|
|
||||||
Use: "list",
|
Use: "list",
|
||||||
Short: "List machines",
|
Short: "List machines",
|
||||||
Long: `List `,
|
Long: `List `,
|
||||||
|
@ -195,9 +164,12 @@ Note: This command requires database direct access, so is intended to be run on
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmdMachines.AddCommand(cmdMachinesList)
|
|
||||||
|
|
||||||
var cmdMachinesAdd = &cobra.Command{
|
return cmdMachinesList
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMachinesAddCmd() *cobra.Command {
|
||||||
|
cmdMachinesAdd := &cobra.Command{
|
||||||
Use: "add",
|
Use: "add",
|
||||||
Short: "add machine to the database.",
|
Short: "add machine to the database.",
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
|
@ -214,15 +186,63 @@ cscli machines add MyTestMachine --password MyPassword
|
||||||
log.Fatalf("unable to create new database client: %s", err)
|
log.Fatalf("unable to create new database client: %s", err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
RunE: runMachinesAdd,
|
||||||
|
}
|
||||||
|
|
||||||
|
flags := cmdMachinesAdd.Flags()
|
||||||
|
flags.StringP("password", "p", "", "machine password to login to the API")
|
||||||
|
flags.StringP("file", "f", "", "output file destination (defaults to "+csconfig.DefaultConfigPath("local_api_credentials.yaml"))
|
||||||
|
flags.StringP("url", "u", "", "URL of the local API")
|
||||||
|
flags.BoolP("interactive", "i", false, "interfactive mode to enter the password")
|
||||||
|
flags.BoolP("auto", "a", false, "automatically generate password (and username if not provided)")
|
||||||
|
flags.Bool("force", false, "will force add the machine if it already exist")
|
||||||
|
|
||||||
|
return cmdMachinesAdd
|
||||||
|
}
|
||||||
|
|
||||||
|
func runMachinesAdd(cmd *cobra.Command, args []string) error {
|
||||||
var dumpFile string
|
var dumpFile string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
machinePassword, err := flags.GetString("password")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
outputFile, err := flags.GetString("file")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
apiURL, err := flags.GetString("url")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
interactive, err := flags.GetBool("interactive")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
autoAdd, err := flags.GetBool("auto")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
forceAdd, err := flags.GetBool("force")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var machineID string
|
||||||
|
|
||||||
// create machineID if not specified by user
|
// create machineID if not specified by user
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
if !autoAdd {
|
if !autoAdd {
|
||||||
printHelp(cmd)
|
printHelp(cmd)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
machineID, err = generateID("")
|
machineID, err = generateID("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -243,7 +263,7 @@ cscli machines add MyTestMachine --password MyPassword
|
||||||
if machinePassword == "" && !interactive {
|
if machinePassword == "" && !interactive {
|
||||||
if !autoAdd {
|
if !autoAdd {
|
||||||
printHelp(cmd)
|
printHelp(cmd)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
machinePassword = generatePassword(passwordLength)
|
machinePassword = generatePassword(passwordLength)
|
||||||
} else if machinePassword == "" && interactive {
|
} else if machinePassword == "" && interactive {
|
||||||
|
@ -286,18 +306,12 @@ cscli machines add MyTestMachine --password MyPassword
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("%s\n", string(apiConfigDump))
|
fmt.Printf("%s\n", string(apiConfigDump))
|
||||||
}
|
}
|
||||||
},
|
|
||||||
}
|
|
||||||
cmdMachinesAdd.Flags().StringVarP(&machinePassword, "password", "p", "", "machine password to login to the API")
|
|
||||||
cmdMachinesAdd.Flags().StringVarP(&outputFile, "file", "f", "",
|
|
||||||
"output file destination (defaults to "+csconfig.DefaultConfigPath("local_api_credentials.yaml"))
|
|
||||||
cmdMachinesAdd.Flags().StringVarP(&apiURL, "url", "u", "", "URL of the local API")
|
|
||||||
cmdMachinesAdd.Flags().BoolVarP(&interactive, "interactive", "i", false, "interfactive mode to enter the password")
|
|
||||||
cmdMachinesAdd.Flags().BoolVarP(&autoAdd, "auto", "a", false, "automatically generate password (and username if not provided)")
|
|
||||||
cmdMachinesAdd.Flags().BoolVar(&forceAdd, "force", false, "will force add the machine if it already exist")
|
|
||||||
cmdMachines.AddCommand(cmdMachinesAdd)
|
|
||||||
|
|
||||||
var cmdMachinesDelete = &cobra.Command{
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMachinesDeleteCmd() *cobra.Command {
|
||||||
|
cmdMachinesDelete := &cobra.Command{
|
||||||
Use: "delete [machine_name]...",
|
Use: "delete [machine_name]...",
|
||||||
Short: "delete machines",
|
Short: "delete machines",
|
||||||
Example: `cscli machines delete "machine1" "machine2"`,
|
Example: `cscli machines delete "machine1" "machine2"`,
|
||||||
|
@ -330,20 +344,27 @@ cscli machines add MyTestMachine --password MyPassword
|
||||||
}
|
}
|
||||||
return ret, cobra.ShellCompDirectiveNoFileComp
|
return ret, cobra.ShellCompDirectiveNoFileComp
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
RunE: runMachinesDelete,
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmdMachinesDelete
|
||||||
|
}
|
||||||
|
|
||||||
|
func runMachinesDelete(cmd *cobra.Command, args []string) error {
|
||||||
for _, machineID := range args {
|
for _, machineID := range args {
|
||||||
err := dbClient.DeleteWatcher(machineID)
|
err := dbClient.DeleteWatcher(machineID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("unable to delete machine '%s': %s", machineID, err)
|
log.Errorf("unable to delete machine '%s': %s", machineID, err)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
log.Infof("machine '%s' deleted successfully", machineID)
|
log.Infof("machine '%s' deleted successfully", machineID)
|
||||||
}
|
}
|
||||||
},
|
|
||||||
}
|
|
||||||
cmdMachines.AddCommand(cmdMachinesDelete)
|
|
||||||
|
|
||||||
var cmdMachinesValidate = &cobra.Command{
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMachinesValidateCmd() *cobra.Command {
|
||||||
|
cmdMachinesValidate := &cobra.Command{
|
||||||
Use: "validate",
|
Use: "validate",
|
||||||
Short: "validate a machine to access the local API",
|
Short: "validate a machine to access the local API",
|
||||||
Long: `validate a machine to access the local API.`,
|
Long: `validate a machine to access the local API.`,
|
||||||
|
@ -358,14 +379,45 @@ cscli machines add MyTestMachine --password MyPassword
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
machineID = args[0]
|
machineID := args[0]
|
||||||
if err := dbClient.ValidateMachine(machineID); err != nil {
|
if err := dbClient.ValidateMachine(machineID); err != nil {
|
||||||
log.Fatalf("unable to validate machine '%s': %s", machineID, err)
|
log.Fatalf("unable to validate machine '%s': %s", machineID, err)
|
||||||
}
|
}
|
||||||
log.Infof("machine '%s' validated successfully", machineID)
|
log.Infof("machine '%s' validated successfully", machineID)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmdMachines.AddCommand(cmdMachinesValidate)
|
|
||||||
|
return cmdMachinesValidate
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMachinesCmd() *cobra.Command {
|
||||||
|
var cmdMachines = &cobra.Command{
|
||||||
|
Use: "machines [action]",
|
||||||
|
Short: "Manage local API machines [requires local API]",
|
||||||
|
Long: `To list/add/delete/validate machines.
|
||||||
|
Note: This command requires database direct access, so is intended to be run on the local API machine.
|
||||||
|
`,
|
||||||
|
Example: `cscli machines [action]`,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
Aliases: []string{"machine"},
|
||||||
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||||
|
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("local api : %s", err)
|
||||||
|
}
|
||||||
|
log.Fatal("Local API is disabled, please run this command on the local API machine")
|
||||||
|
}
|
||||||
|
if err := csConfig.LoadDBConfig(); err != nil {
|
||||||
|
log.Errorf("This command requires direct database access (must be run on the local API machine)")
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdMachines.AddCommand(NewMachinesListCmd())
|
||||||
|
cmdMachines.AddCommand(NewMachinesAddCmd())
|
||||||
|
cmdMachines.AddCommand(NewMachinesDeleteCmd())
|
||||||
|
cmdMachines.AddCommand(NewMachinesValidateCmd())
|
||||||
|
|
||||||
return cmdMachines
|
return cmdMachines
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,8 +10,150 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func NewPostOverflowsInstallCmd() *cobra.Command {
|
||||||
|
var ignoreError bool
|
||||||
|
|
||||||
|
cmdPostOverflowsInstall := &cobra.Command{
|
||||||
|
Use: "install [config]",
|
||||||
|
Short: "Install given postoverflow(s)",
|
||||||
|
Long: `Fetch and install given postoverflow(s) from hub`,
|
||||||
|
Example: `cscli postoverflows install crowdsec/xxx crowdsec/xyz`,
|
||||||
|
Args: cobra.MinimumNArgs(1),
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
|
return compAllItems(cwhub.PARSERS_OVFLW, args, toComplete)
|
||||||
|
},
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
for _, name := range args {
|
||||||
|
t := cwhub.GetItem(cwhub.PARSERS_OVFLW, name)
|
||||||
|
if t == nil {
|
||||||
|
nearestItem, score := GetDistance(cwhub.PARSERS_OVFLW, name)
|
||||||
|
Suggest(cwhub.PARSERS_OVFLW, name, nearestItem.Name, score, ignoreError)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := cwhub.InstallItem(csConfig, name, cwhub.PARSERS_OVFLW, forceAction, downloadOnly); err != nil {
|
||||||
|
if ignoreError {
|
||||||
|
log.Errorf("Error while installing '%s': %s", name, err)
|
||||||
|
} else {
|
||||||
|
log.Fatalf("Error while installing '%s': %s", name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdPostOverflowsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
|
||||||
|
cmdPostOverflowsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
|
||||||
|
cmdPostOverflowsInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple postoverflows")
|
||||||
|
|
||||||
|
return cmdPostOverflowsInstall
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPostOverflowsRemoveCmd() *cobra.Command {
|
||||||
|
cmdPostOverflowsRemove := &cobra.Command{
|
||||||
|
Use: "remove [config]",
|
||||||
|
Short: "Remove given postoverflow(s)",
|
||||||
|
Long: `remove given postoverflow(s)`,
|
||||||
|
Example: `cscli postoverflows remove crowdsec/xxx crowdsec/xyz`,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
Aliases: []string{"delete"},
|
||||||
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
|
return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
|
||||||
|
},
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if all {
|
||||||
|
cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, "", all, purge, forceAction)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) == 0 {
|
||||||
|
log.Fatalf("Specify at least one postoverflow to remove or '--all' flag.")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range args {
|
||||||
|
cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, name, all, purge, forceAction)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdPostOverflowsRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
|
||||||
|
cmdPostOverflowsRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
|
||||||
|
cmdPostOverflowsRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the postoverflows")
|
||||||
|
|
||||||
|
return cmdPostOverflowsRemove
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPostOverflowsUpgradeCmd() *cobra.Command {
|
||||||
|
cmdPostOverflowsUpgrade := &cobra.Command{
|
||||||
|
Use: "upgrade [config]",
|
||||||
|
Short: "Upgrade given postoverflow(s)",
|
||||||
|
Long: `Fetch and Upgrade given postoverflow(s) from hub`,
|
||||||
|
Example: `cscli postoverflows upgrade crowdsec/xxx crowdsec/xyz`,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
|
return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
|
||||||
|
},
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if all {
|
||||||
|
cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, "", forceAction)
|
||||||
|
} else {
|
||||||
|
if len(args) == 0 {
|
||||||
|
log.Fatalf("no target postoverflow to upgrade")
|
||||||
|
}
|
||||||
|
for _, name := range args {
|
||||||
|
cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, name, forceAction)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdPostOverflowsUpgrade.PersistentFlags().BoolVarP(&all, "all", "a", false, "Upgrade all the postoverflows")
|
||||||
|
cmdPostOverflowsUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
|
||||||
|
|
||||||
|
return cmdPostOverflowsUpgrade
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPostOverflowsInspectCmd() *cobra.Command {
|
||||||
|
cmdPostOverflowsInspect := &cobra.Command{
|
||||||
|
Use: "inspect [config]",
|
||||||
|
Short: "Inspect given postoverflow",
|
||||||
|
Long: `Inspect given postoverflow`,
|
||||||
|
Example: `cscli postoverflows inspect crowdsec/xxx crowdsec/xyz`,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
|
return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
|
||||||
|
},
|
||||||
|
Args: cobra.MinimumNArgs(1),
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
InspectItem(args[0], cwhub.PARSERS_OVFLW)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmdPostOverflowsInspect
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPostOverflowsListCmd() *cobra.Command {
|
||||||
|
cmdPostOverflowsList := &cobra.Command{
|
||||||
|
Use: "list [config]",
|
||||||
|
Short: "List all postoverflows or given one",
|
||||||
|
Long: `List all postoverflows or given one`,
|
||||||
|
Example: `cscli postoverflows list
|
||||||
|
cscli postoverflows list crowdsecurity/xxx`,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
ListItems(color.Output, []string{cwhub.PARSERS_OVFLW}, args, false, true, all)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdPostOverflowsList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well")
|
||||||
|
|
||||||
|
return cmdPostOverflowsList
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func NewPostOverflowsCmd() *cobra.Command {
|
func NewPostOverflowsCmd() *cobra.Command {
|
||||||
var cmdPostOverflows = &cobra.Command{
|
cmdPostOverflows := &cobra.Command{
|
||||||
Use: "postoverflows [action] [config]",
|
Use: "postoverflows [action] [config]",
|
||||||
Short: "Install/Remove/Upgrade/Inspect postoverflow(s) from hub",
|
Short: "Install/Remove/Upgrade/Inspect postoverflow(s) from hub",
|
||||||
Example: `cscli postoverflows install crowdsecurity/cdn-whitelist
|
Example: `cscli postoverflows install crowdsecurity/cdn-whitelist
|
||||||
|
@ -48,125 +190,11 @@ func NewPostOverflowsCmd() *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var ignoreError bool
|
cmdPostOverflows.AddCommand(NewPostOverflowsInstallCmd())
|
||||||
var cmdPostOverflowsInstall = &cobra.Command{
|
cmdPostOverflows.AddCommand(NewPostOverflowsRemoveCmd())
|
||||||
Use: "install [config]",
|
cmdPostOverflows.AddCommand(NewPostOverflowsUpgradeCmd())
|
||||||
Short: "Install given postoverflow(s)",
|
cmdPostOverflows.AddCommand(NewPostOverflowsInspectCmd())
|
||||||
Long: `Fetch and install given postoverflow(s) from hub`,
|
cmdPostOverflows.AddCommand(NewPostOverflowsListCmd())
|
||||||
Example: `cscli postoverflows install crowdsec/xxx crowdsec/xyz`,
|
|
||||||
Args: cobra.MinimumNArgs(1),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
||||||
return compAllItems(cwhub.PARSERS_OVFLW, args, toComplete)
|
|
||||||
},
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
for _, name := range args {
|
|
||||||
t := cwhub.GetItem(cwhub.PARSERS_OVFLW, name)
|
|
||||||
if t == nil {
|
|
||||||
nearestItem, score := GetDistance(cwhub.PARSERS_OVFLW, name)
|
|
||||||
Suggest(cwhub.PARSERS_OVFLW, name, nearestItem.Name, score, ignoreError)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := cwhub.InstallItem(csConfig, name, cwhub.PARSERS_OVFLW, forceAction, downloadOnly); err != nil {
|
|
||||||
if ignoreError {
|
|
||||||
log.Errorf("Error while installing '%s': %s", name, err)
|
|
||||||
} else {
|
|
||||||
log.Fatalf("Error while installing '%s': %s", name, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmdPostOverflowsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
|
|
||||||
cmdPostOverflowsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
|
|
||||||
cmdPostOverflowsInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple postoverflows")
|
|
||||||
cmdPostOverflows.AddCommand(cmdPostOverflowsInstall)
|
|
||||||
|
|
||||||
var cmdPostOverflowsRemove = &cobra.Command{
|
|
||||||
Use: "remove [config]",
|
|
||||||
Short: "Remove given postoverflow(s)",
|
|
||||||
Long: `remove given postoverflow(s)`,
|
|
||||||
Example: `cscli postoverflows remove crowdsec/xxx crowdsec/xyz`,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
Aliases: []string{"delete"},
|
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
||||||
return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
|
|
||||||
},
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
if all {
|
|
||||||
cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, "", all, purge, forceAction)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(args) == 0 {
|
|
||||||
log.Fatalf("Specify at least one postoverflow to remove or '--all' flag.")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, name := range args {
|
|
||||||
cwhub.RemoveMany(csConfig, cwhub.PARSERS_OVFLW, name, all, purge, forceAction)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmdPostOverflowsRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
|
|
||||||
cmdPostOverflowsRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
|
|
||||||
cmdPostOverflowsRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the postoverflows")
|
|
||||||
cmdPostOverflows.AddCommand(cmdPostOverflowsRemove)
|
|
||||||
|
|
||||||
var cmdPostOverflowsUpgrade = &cobra.Command{
|
|
||||||
Use: "upgrade [config]",
|
|
||||||
Short: "Upgrade given postoverflow(s)",
|
|
||||||
Long: `Fetch and Upgrade given postoverflow(s) from hub`,
|
|
||||||
Example: `cscli postoverflows upgrade crowdsec/xxx crowdsec/xyz`,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
||||||
return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
|
|
||||||
},
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
if all {
|
|
||||||
cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, "", forceAction)
|
|
||||||
} else {
|
|
||||||
if len(args) == 0 {
|
|
||||||
log.Fatalf("no target postoverflow to upgrade")
|
|
||||||
}
|
|
||||||
for _, name := range args {
|
|
||||||
cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, name, forceAction)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmdPostOverflowsUpgrade.PersistentFlags().BoolVarP(&all, "all", "a", false, "Upgrade all the postoverflows")
|
|
||||||
cmdPostOverflowsUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
|
|
||||||
cmdPostOverflows.AddCommand(cmdPostOverflowsUpgrade)
|
|
||||||
|
|
||||||
var cmdPostOverflowsInspect = &cobra.Command{
|
|
||||||
Use: "inspect [config]",
|
|
||||||
Short: "Inspect given postoverflow",
|
|
||||||
Long: `Inspect given postoverflow`,
|
|
||||||
Example: `cscli postoverflows inspect crowdsec/xxx crowdsec/xyz`,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
||||||
return compInstalledItems(cwhub.PARSERS_OVFLW, args, toComplete)
|
|
||||||
},
|
|
||||||
Args: cobra.MinimumNArgs(1),
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
InspectItem(args[0], cwhub.PARSERS_OVFLW)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmdPostOverflows.AddCommand(cmdPostOverflowsInspect)
|
|
||||||
|
|
||||||
var cmdPostOverflowsList = &cobra.Command{
|
|
||||||
Use: "list [config]",
|
|
||||||
Short: "List all postoverflows or given one",
|
|
||||||
Long: `List all postoverflows or given one`,
|
|
||||||
Example: `cscli postoverflows list
|
|
||||||
cscli postoverflows list crowdsecurity/xxx`,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
ListItems(color.Output, []string{cwhub.PARSERS_OVFLW}, args, false, true, all)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmdPostOverflowsList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well")
|
|
||||||
cmdPostOverflows.AddCommand(cmdPostOverflowsList)
|
|
||||||
|
|
||||||
return cmdPostOverflows
|
return cmdPostOverflows
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue