2021-06-28 15:34:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-13 15:46:16 +00:00
|
|
|
"encoding/csv"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2021-06-28 15:34:19 +00:00
|
|
|
"fmt"
|
2022-01-13 15:46:16 +00:00
|
|
|
"io/fs"
|
2021-06-28 15:34:19 +00:00
|
|
|
"net/url"
|
2022-01-13 15:46:16 +00:00
|
|
|
"os"
|
2021-06-28 15:34:19 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
2022-01-13 15:46:16 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
2021-06-28 15:34:19 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
2022-01-13 15:46:16 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
"github.com/enescakir/emoji"
|
2021-06-28 15:34:19 +00:00
|
|
|
"github.com/go-openapi/strfmt"
|
2022-01-13 15:46:16 +00:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2021-06-28 15:34:19 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewConsoleCmd() *cobra.Command {
|
|
|
|
var cmdConsole = &cobra.Command{
|
2021-09-03 10:56:17 +00:00
|
|
|
Use: "console [action]",
|
|
|
|
Short: "Manage interaction with Crowdsec console (https://app.crowdsec.net)",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
2021-06-28 15:34:19 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
2022-01-13 15:46:16 +00:00
|
|
|
var fdErr *fs.PathError
|
|
|
|
if errors.As(err, &fdErr) {
|
|
|
|
log.Fatalf("Unable to load Local API : %s", fdErr)
|
2022-02-01 21:08:06 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-01-13 15:46:16 +00:00
|
|
|
log.Fatalf("Unable to load required Local API Configuration : %s", err)
|
|
|
|
}
|
2022-02-01 21:08:06 +00:00
|
|
|
log.Fatal("Local API is disabled, please run this command on the local API machine")
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
if csConfig.DisableAPI {
|
2021-06-28 15:34:19 +00:00
|
|
|
log.Fatal("Local API is disabled, please run this command on the local API machine")
|
|
|
|
}
|
|
|
|
if csConfig.API.Server.OnlineClient == nil {
|
2022-01-13 15:46:16 +00:00
|
|
|
log.Fatalf("No configuration for Central API (CAPI) in '%s'", *csConfig.FilePath)
|
|
|
|
}
|
|
|
|
if csConfig.API.Server.OnlineClient.Credentials == nil {
|
|
|
|
log.Fatal("You must configure Central API (CAPI) with `cscli capi register` before enrolling your instance")
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-26 13:33:17 +00:00
|
|
|
name := ""
|
2022-04-20 11:34:17 +00:00
|
|
|
overwrite := false
|
2021-10-26 13:33:17 +00:00
|
|
|
tags := []string{}
|
|
|
|
|
2021-06-28 15:34:19 +00:00
|
|
|
cmdEnroll := &cobra.Command{
|
|
|
|
Use: "enroll [enroll-key]",
|
|
|
|
Short: "Enroll this instance to https://app.crowdsec.net [requires local API]",
|
|
|
|
Long: `
|
|
|
|
Enroll this instance to https://app.crowdsec.net
|
|
|
|
|
|
|
|
You can get your enrollment key by creating an account on https://app.crowdsec.net.
|
|
|
|
After running this command your will need to validate the enrollment in the webapp.`,
|
2021-10-26 13:33:17 +00:00
|
|
|
Example: `cscli console enroll YOUR-ENROLL-KEY
|
|
|
|
cscli console enroll --name [instance_name] YOUR-ENROLL-KEY
|
|
|
|
cscli console enroll --name [instance_name] --tags [tag_1] --tags [tag_2] YOUR-ENROLL-KEY
|
|
|
|
`,
|
2021-08-31 13:03:47 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
2021-06-28 15:34:19 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
password := strfmt.Password(csConfig.API.Server.OnlineClient.Credentials.Password)
|
|
|
|
apiURL, err := url.Parse(csConfig.API.Server.OnlineClient.Credentials.URL)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not parse CAPI URL : %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := csConfig.LoadHub(); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
|
|
|
|
log.Fatalf("Failed to load hub index : %s", err)
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Info("Run 'sudo cscli hub update' to get the hub index")
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 15:48:29 +00:00
|
|
|
scenarios, err := cwhub.GetInstalledScenariosAsString()
|
2021-06-28 15:34:19 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to get scenarios : %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(scenarios) == 0 {
|
|
|
|
scenarios = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, _ := apiclient.NewClient(&apiclient.Config{
|
|
|
|
MachineID: csConfig.API.Server.OnlineClient.Credentials.Login,
|
|
|
|
Password: password,
|
|
|
|
Scenarios: scenarios,
|
|
|
|
UserAgent: fmt.Sprintf("crowdsec/%s", cwversion.VersionStr()),
|
|
|
|
URL: apiURL,
|
|
|
|
VersionPrefix: "v2",
|
|
|
|
})
|
2022-04-20 11:34:17 +00:00
|
|
|
resp, err := c.Auth.EnrollWatcher(context.Background(), args[0], name, tags, overwrite)
|
2021-06-28 15:34:19 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not enroll instance: %s", err)
|
|
|
|
}
|
2022-04-20 11:34:17 +00:00
|
|
|
if resp.Response.StatusCode == 200 && !overwrite {
|
2022-06-22 07:38:23 +00:00
|
|
|
log.Warning("Instance already enrolled. You can use '--overwrite' to force enroll")
|
2022-04-20 11:34:17 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
|
|
|
|
SetConsoleOpts(csconfig.CONSOLE_CONFIGS, true)
|
2022-01-24 16:22:16 +00:00
|
|
|
if err := csConfig.API.Server.DumpConsoleConfig(); err != nil {
|
|
|
|
log.Fatalf("failed writing console config : %s", err)
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
log.Infof("Enabled tainted&manual alerts sharing, see 'cscli console status'.")
|
2021-06-28 15:34:19 +00:00
|
|
|
log.Infof("Watcher successfully enrolled. Visit https://app.crowdsec.net to accept it.")
|
2022-01-13 15:46:16 +00:00
|
|
|
log.Infof("Please restart crowdsec after accepting the enrollment.")
|
2021-06-28 15:34:19 +00:00
|
|
|
},
|
|
|
|
}
|
2021-10-26 13:33:17 +00:00
|
|
|
cmdEnroll.Flags().StringVarP(&name, "name", "n", "", "Name to display in the console")
|
2022-04-20 11:34:17 +00:00
|
|
|
cmdEnroll.Flags().BoolVarP(&overwrite, "overwrite", "", false, "Force enroll the instance")
|
2021-10-26 13:33:17 +00:00
|
|
|
cmdEnroll.Flags().StringSliceVarP(&tags, "tags", "t", tags, "Tags to display in the console")
|
2021-06-28 15:34:19 +00:00
|
|
|
cmdConsole.AddCommand(cmdEnroll)
|
2022-01-13 15:46:16 +00:00
|
|
|
|
|
|
|
var enableAll, disableAll bool
|
|
|
|
|
|
|
|
cmdEnable := &cobra.Command{
|
|
|
|
Use: "enable [feature-flag]",
|
|
|
|
Short: "Enable a feature flag",
|
2022-05-19 07:55:49 +00:00
|
|
|
Example: "enable tainted",
|
2022-01-13 15:46:16 +00:00
|
|
|
Long: `
|
|
|
|
Enable given information push to the central API. Allows to empower the console`,
|
|
|
|
ValidArgs: csconfig.CONSOLE_CONFIGS,
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if enableAll {
|
|
|
|
SetConsoleOpts(csconfig.CONSOLE_CONFIGS, true)
|
2022-02-24 09:24:04 +00:00
|
|
|
log.Infof("All features have been enabled successfully")
|
2022-01-13 15:46:16 +00:00
|
|
|
} else {
|
2022-02-24 09:24:04 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
log.Fatalf("You must specify at least one feature to enable")
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
SetConsoleOpts(args, true)
|
2022-02-24 09:24:04 +00:00
|
|
|
log.Infof("%v have been enabled", args)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
if err := csConfig.API.Server.DumpConsoleConfig(); err != nil {
|
|
|
|
log.Fatalf("failed writing console config : %s", err)
|
|
|
|
}
|
|
|
|
log.Infof(ReloadMessage())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmdEnable.Flags().BoolVarP(&enableAll, "all", "a", false, "Enable all feature flags")
|
|
|
|
cmdConsole.AddCommand(cmdEnable)
|
|
|
|
|
|
|
|
cmdDisable := &cobra.Command{
|
|
|
|
Use: "disable [feature-flag]",
|
|
|
|
Short: "Disable a feature flag",
|
2022-05-19 07:55:49 +00:00
|
|
|
Example: "disable tainted",
|
2022-01-13 15:46:16 +00:00
|
|
|
Long: `
|
|
|
|
Disable given information push to the central API.`,
|
|
|
|
ValidArgs: csconfig.CONSOLE_CONFIGS,
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if disableAll {
|
|
|
|
SetConsoleOpts(csconfig.CONSOLE_CONFIGS, false)
|
|
|
|
} else {
|
|
|
|
SetConsoleOpts(args, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := csConfig.API.Server.DumpConsoleConfig(); err != nil {
|
|
|
|
log.Fatalf("failed writing console config : %s", err)
|
|
|
|
}
|
|
|
|
if disableAll {
|
|
|
|
log.Infof("All features have been disabled")
|
|
|
|
} else {
|
|
|
|
log.Infof("%v have been disabled", args)
|
|
|
|
}
|
|
|
|
log.Infof(ReloadMessage())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmdDisable.Flags().BoolVarP(&disableAll, "all", "a", false, "Enable all feature flags")
|
|
|
|
cmdConsole.AddCommand(cmdDisable)
|
|
|
|
|
|
|
|
cmdConsoleStatus := &cobra.Command{
|
|
|
|
Use: "status [feature-flag]",
|
|
|
|
Short: "Shows status of one or all feature flags",
|
2022-05-19 07:55:49 +00:00
|
|
|
Example: "status tainted",
|
2022-01-13 15:46:16 +00:00
|
|
|
DisableAutoGenTag: true,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
switch csConfig.Cscli.Output {
|
|
|
|
case "human":
|
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
|
|
|
|
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
|
|
|
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
|
|
|
table.SetHeader([]string{"Option Name", "Activated", "Description"})
|
|
|
|
for _, option := range csconfig.CONSOLE_CONFIGS {
|
|
|
|
switch option {
|
|
|
|
case csconfig.SEND_CUSTOM_SCENARIOS:
|
|
|
|
activated := string(emoji.CrossMark)
|
|
|
|
if *csConfig.API.Server.ConsoleConfig.ShareCustomScenarios {
|
|
|
|
activated = string(emoji.CheckMarkButton)
|
|
|
|
}
|
|
|
|
table.Append([]string{option, activated, "Send alerts from custom scenarios to the console"})
|
|
|
|
case csconfig.SEND_MANUAL_SCENARIOS:
|
|
|
|
activated := string(emoji.CrossMark)
|
|
|
|
if *csConfig.API.Server.ConsoleConfig.ShareManualDecisions {
|
|
|
|
activated = string(emoji.CheckMarkButton)
|
|
|
|
}
|
|
|
|
table.Append([]string{option, activated, "Send manual decisions to the console"})
|
|
|
|
case csconfig.SEND_TAINTED_SCENARIOS:
|
|
|
|
activated := string(emoji.CrossMark)
|
|
|
|
if *csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios {
|
|
|
|
activated = string(emoji.CheckMarkButton)
|
|
|
|
}
|
|
|
|
table.Append([]string{option, activated, "Send alerts from tainted scenarios to the console"})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
table.Render()
|
|
|
|
case "json":
|
|
|
|
data, err := json.MarshalIndent(csConfig.API.Server.ConsoleConfig, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to marshal configuration: %s", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("%s\n", string(data))
|
|
|
|
case "raw":
|
|
|
|
csvwriter := csv.NewWriter(os.Stdout)
|
|
|
|
err := csvwriter.Write([]string{"option", "enabled"})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows := [][]string{
|
|
|
|
{"share_manual_decisions", fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ShareManualDecisions)},
|
|
|
|
{"share_custom", fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ShareCustomScenarios)},
|
|
|
|
{"share_tainted", fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios)},
|
|
|
|
}
|
|
|
|
for _, row := range rows {
|
|
|
|
err = csvwriter.Write(row)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
csvwriter.Flush()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdConsole.AddCommand(cmdConsoleStatus)
|
2021-06-28 15:34:19 +00:00
|
|
|
return cmdConsole
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
|
|
|
|
func SetConsoleOpts(args []string, wanted bool) {
|
|
|
|
for _, arg := range args {
|
|
|
|
switch arg {
|
|
|
|
case csconfig.SEND_CUSTOM_SCENARIOS:
|
|
|
|
/*for each flag check if it's already set before setting it*/
|
|
|
|
if csConfig.API.Server.ConsoleConfig.ShareCustomScenarios != nil {
|
|
|
|
if *csConfig.API.Server.ConsoleConfig.ShareCustomScenarios == wanted {
|
|
|
|
log.Infof("%s already set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
|
|
|
*csConfig.API.Server.ConsoleConfig.ShareCustomScenarios = wanted
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
|
|
|
csConfig.API.Server.ConsoleConfig.ShareCustomScenarios = types.BoolPtr(wanted)
|
|
|
|
}
|
|
|
|
case csconfig.SEND_TAINTED_SCENARIOS:
|
|
|
|
/*for each flag check if it's already set before setting it*/
|
|
|
|
if csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios != nil {
|
|
|
|
if *csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios == wanted {
|
|
|
|
log.Infof("%s already set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
|
|
|
*csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios = wanted
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
|
|
|
csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios = types.BoolPtr(wanted)
|
|
|
|
}
|
|
|
|
case csconfig.SEND_MANUAL_SCENARIOS:
|
|
|
|
/*for each flag check if it's already set before setting it*/
|
|
|
|
if csConfig.API.Server.ConsoleConfig.ShareManualDecisions != nil {
|
|
|
|
if *csConfig.API.Server.ConsoleConfig.ShareManualDecisions == wanted {
|
|
|
|
log.Infof("%s already set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
|
|
|
*csConfig.API.Server.ConsoleConfig.ShareManualDecisions = wanted
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
|
|
|
csConfig.API.Server.ConsoleConfig.ShareManualDecisions = types.BoolPtr(wanted)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Fatalf("unknown flag %s", arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|