|
@@ -2,13 +2,22 @@ package main
|
|
|
|
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
|
|
+ "encoding/csv"
|
|
|
|
+ "encoding/json"
|
|
|
|
+ "errors"
|
|
"fmt"
|
|
"fmt"
|
|
|
|
+ "io/fs"
|
|
"net/url"
|
|
"net/url"
|
|
|
|
+ "os"
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
|
|
|
+ "github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
|
|
|
+ "github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
+ "github.com/enescakir/emoji"
|
|
"github.com/go-openapi/strfmt"
|
|
"github.com/go-openapi/strfmt"
|
|
|
|
+ "github.com/olekukonko/tablewriter"
|
|
log "github.com/sirupsen/logrus"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
)
|
|
@@ -21,12 +30,24 @@ func NewConsoleCmd() *cobra.Command {
|
|
DisableAutoGenTag: true,
|
|
DisableAutoGenTag: true,
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
|
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
|
|
|
+ var fdErr *fs.PathError
|
|
|
|
+ if errors.As(err, &fdErr) {
|
|
|
|
+ log.Fatalf("Unable to load Local API : %s", fdErr)
|
|
|
|
+ } else if err != nil {
|
|
|
|
+ log.Fatalf("Unable to load required Local API Configuration : %s", err)
|
|
|
|
+ } else {
|
|
|
|
+ log.Fatal("Local API is disabled, please run this command on the local API machine")
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if csConfig.DisableAPI {
|
|
log.Fatal("Local API is disabled, please run this command on the local API machine")
|
|
log.Fatal("Local API is disabled, please run this command on the local API machine")
|
|
}
|
|
}
|
|
if csConfig.API.Server.OnlineClient == nil {
|
|
if csConfig.API.Server.OnlineClient == nil {
|
|
- log.Fatalf("no configuration for Central API (CAPI) in '%s'", *csConfig.FilePath)
|
|
|
|
|
|
+ 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")
|
|
}
|
|
}
|
|
-
|
|
|
|
return nil
|
|
return nil
|
|
},
|
|
},
|
|
}
|
|
}
|
|
@@ -48,18 +69,6 @@ After running this command your will need to validate the enrollment in the weba
|
|
`,
|
|
`,
|
|
Args: cobra.ExactArgs(1),
|
|
Args: cobra.ExactArgs(1),
|
|
DisableAutoGenTag: true,
|
|
DisableAutoGenTag: true,
|
|
- PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
- if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
|
|
|
- log.Fatal("Local API is disabled, please run this command on the local API machine")
|
|
|
|
- }
|
|
|
|
- if csConfig.API.Server.OnlineClient == nil {
|
|
|
|
- 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")
|
|
|
|
- }
|
|
|
|
- return nil
|
|
|
|
- },
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
password := strfmt.Password(csConfig.API.Server.OnlineClient.Credentials.Password)
|
|
password := strfmt.Password(csConfig.API.Server.OnlineClient.Credentials.Password)
|
|
apiURL, err := url.Parse(csConfig.API.Server.OnlineClient.Credentials.URL)
|
|
apiURL, err := url.Parse(csConfig.API.Server.OnlineClient.Credentials.URL)
|
|
@@ -97,11 +106,193 @@ After running this command your will need to validate the enrollment in the weba
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Fatalf("Could not enroll instance: %s", err)
|
|
log.Fatalf("Could not enroll instance: %s", err)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ SetConsoleOpts(csconfig.CONSOLE_CONFIGS, true)
|
|
|
|
+ log.Infof("Enabled tainted&manual alerts sharing, see 'cscli console status'.")
|
|
log.Infof("Watcher successfully enrolled. Visit https://app.crowdsec.net to accept it.")
|
|
log.Infof("Watcher successfully enrolled. Visit https://app.crowdsec.net to accept it.")
|
|
|
|
+ log.Infof("Please restart crowdsec after accepting the enrollment.")
|
|
},
|
|
},
|
|
}
|
|
}
|
|
cmdEnroll.Flags().StringVarP(&name, "name", "n", "", "Name to display in the console")
|
|
cmdEnroll.Flags().StringVarP(&name, "name", "n", "", "Name to display in the console")
|
|
cmdEnroll.Flags().StringSliceVarP(&tags, "tags", "t", tags, "Tags to display in the console")
|
|
cmdEnroll.Flags().StringSliceVarP(&tags, "tags", "t", tags, "Tags to display in the console")
|
|
cmdConsole.AddCommand(cmdEnroll)
|
|
cmdConsole.AddCommand(cmdEnroll)
|
|
|
|
+
|
|
|
|
+ var enableAll, disableAll bool
|
|
|
|
+
|
|
|
|
+ cmdEnable := &cobra.Command{
|
|
|
|
+ Use: "enable [feature-flag]",
|
|
|
|
+ Short: "Enable a feature flag",
|
|
|
|
+ Example: "enable alerts-tainted",
|
|
|
|
+ Long: `
|
|
|
|
+Enable given information push to the central API. Allows to empower the console`,
|
|
|
|
+ ValidArgs: csconfig.CONSOLE_CONFIGS,
|
|
|
|
+ Args: cobra.MinimumNArgs(1),
|
|
|
|
+ DisableAutoGenTag: true,
|
|
|
|
+ Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
+ if enableAll {
|
|
|
|
+ SetConsoleOpts(csconfig.CONSOLE_CONFIGS, true)
|
|
|
|
+ } else {
|
|
|
|
+ SetConsoleOpts(args, true)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if err := csConfig.API.Server.DumpConsoleConfig(); err != nil {
|
|
|
|
+ log.Fatalf("failed writing console config : %s", err)
|
|
|
|
+ }
|
|
|
|
+ if enableAll {
|
|
|
|
+ log.Infof("All features have been enabled successfully")
|
|
|
|
+ } else {
|
|
|
|
+ log.Infof("%v have been enabled", args)
|
|
|
|
+ }
|
|
|
|
+ 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",
|
|
|
|
+ Example: "disable alerts-tainted",
|
|
|
|
+ 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",
|
|
|
|
+ Example: "status alerts-tainted",
|
|
|
|
+ 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)
|
|
return cmdConsole
|
|
return cmdConsole
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+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)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|