2021-06-28 15:34:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-13 15:46:16 +00:00
|
|
|
"encoding/csv"
|
|
|
|
"encoding/json"
|
2024-04-23 10:28:38 +00:00
|
|
|
"errors"
|
2021-06-28 15:34:19 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2022-01-13 15:46:16 +00:00
|
|
|
"os"
|
2024-04-23 10:28:38 +00:00
|
|
|
"strconv"
|
2024-01-19 14:49:00 +00:00
|
|
|
"strings"
|
2021-06-28 15:34:19 +00:00
|
|
|
|
2022-10-13 10:28:24 +00:00
|
|
|
"github.com/fatih/color"
|
2022-10-07 09:05:35 +00:00
|
|
|
"github.com/go-openapi/strfmt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
2023-03-03 16:03:21 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-10-07 09:05:35 +00:00
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
|
|
|
"github.com/crowdsecurity/go-cs-lib/version"
|
2023-05-23 08:52:47 +00:00
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
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"
|
2022-01-13 15:46:16 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
2021-06-28 15:34:19 +00:00
|
|
|
)
|
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
type cliConsole struct {
|
|
|
|
cfg configGetter
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCLIConsole(cfg configGetter) *cliConsole {
|
|
|
|
return &cliConsole{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *cliConsole) NewCommand() *cobra.Command {
|
2024-04-23 10:28:38 +00:00
|
|
|
cmd := &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,
|
2024-02-12 10:45:58 +00:00
|
|
|
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
|
|
|
cfg := cli.cfg()
|
|
|
|
if err := require.LAPI(cfg); err != nil {
|
2023-07-27 15:02:20 +00:00
|
|
|
return err
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
if err := require.CAPI(cfg); err != nil {
|
2023-07-27 15:02:20 +00:00
|
|
|
return err
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
if err := require.CAPIRegistered(cfg); err != nil {
|
2023-07-27 15:02:20 +00:00
|
|
|
return err
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
|
2021-06-28 15:34:19 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
cmd.AddCommand(cli.newEnrollCmd())
|
|
|
|
cmd.AddCommand(cli.newEnableCmd())
|
|
|
|
cmd.AddCommand(cli.newDisableCmd())
|
|
|
|
cmd.AddCommand(cli.newStatusCmd())
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *cliConsole) newEnrollCmd() *cobra.Command {
|
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{}
|
2024-01-19 14:49:00 +00:00
|
|
|
opts := []string{}
|
2021-10-26 13:33:17 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
cmd := &cobra.Command{
|
2021-06-28 15:34:19 +00:00
|
|
|
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.`,
|
2024-01-19 14:49:00 +00:00
|
|
|
Example: fmt.Sprintf(`cscli console enroll YOUR-ENROLL-KEY
|
2021-10-26 13:33:17 +00:00
|
|
|
cscli console enroll --name [instance_name] YOUR-ENROLL-KEY
|
|
|
|
cscli console enroll --name [instance_name] --tags [tag_1] --tags [tag_2] YOUR-ENROLL-KEY
|
2024-01-19 14:49:00 +00:00
|
|
|
cscli console enroll --enable context,manual YOUR-ENROLL-KEY
|
|
|
|
|
|
|
|
valid options are : %s,all (see 'cscli console status' for details)`, strings.Join(csconfig.CONSOLE_CONFIGS, ",")),
|
2021-08-31 13:03:47 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
2024-02-12 10:45:58 +00:00
|
|
|
RunE: func(_ *cobra.Command, args []string) error {
|
|
|
|
cfg := cli.cfg()
|
|
|
|
password := strfmt.Password(cfg.API.Server.OnlineClient.Credentials.Password)
|
|
|
|
|
|
|
|
apiURL, err := url.Parse(cfg.API.Server.OnlineClient.Credentials.URL)
|
2021-06-28 15:34:19 +00:00
|
|
|
if err != nil {
|
2024-02-12 10:45:58 +00:00
|
|
|
return fmt.Errorf("could not parse CAPI URL: %w", err)
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
hub, err := require.Hub(cfg, nil, nil)
|
2023-11-24 14:57:32 +00:00
|
|
|
if err != nil {
|
2023-08-29 09:44:23 +00:00
|
|
|
return err
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
scenarios, err := hub.GetInstalledItemNames(cwhub.SCENARIOS)
|
2021-06-28 15:34:19 +00:00
|
|
|
if err != nil {
|
2024-02-12 10:45:58 +00:00
|
|
|
return fmt.Errorf("failed to get installed scenarios: %w", err)
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(scenarios) == 0 {
|
|
|
|
scenarios = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
enableOpts := []string{csconfig.SEND_MANUAL_SCENARIOS, csconfig.SEND_TAINTED_SCENARIOS}
|
2024-01-19 14:49:00 +00:00
|
|
|
if len(opts) != 0 {
|
|
|
|
for _, opt := range opts {
|
|
|
|
valid := false
|
|
|
|
if opt == "all" {
|
2024-02-12 10:45:58 +00:00
|
|
|
enableOpts = csconfig.CONSOLE_CONFIGS
|
2024-01-19 14:49:00 +00:00
|
|
|
break
|
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
for _, availableOpt := range csconfig.CONSOLE_CONFIGS {
|
|
|
|
if opt == availableOpt {
|
2024-01-19 14:49:00 +00:00
|
|
|
valid = true
|
|
|
|
enable := true
|
2024-02-12 10:45:58 +00:00
|
|
|
for _, enabledOpt := range enableOpts {
|
|
|
|
if opt == enabledOpt {
|
2024-01-19 14:49:00 +00:00
|
|
|
enable = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if enable {
|
2024-02-12 10:45:58 +00:00
|
|
|
enableOpts = append(enableOpts, opt)
|
2024-01-19 14:49:00 +00:00
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
|
2024-01-19 14:49:00 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
return fmt.Errorf("option %s doesn't exist", opt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 15:34:19 +00:00
|
|
|
c, _ := apiclient.NewClient(&apiclient.Config{
|
2024-02-12 10:45:58 +00:00
|
|
|
MachineID: cli.cfg().API.Server.OnlineClient.Credentials.Login,
|
2021-06-28 15:34:19 +00:00
|
|
|
Password: password,
|
|
|
|
Scenarios: scenarios,
|
2023-05-23 08:52:47 +00:00
|
|
|
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
2021-06-28 15:34:19 +00:00
|
|
|
URL: apiURL,
|
2023-02-06 13:06:14 +00:00
|
|
|
VersionPrefix: "v3",
|
2021-06-28 15:34:19 +00:00
|
|
|
})
|
2024-02-12 10:45:58 +00:00
|
|
|
|
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 {
|
2024-02-12 10:45:58 +00:00
|
|
|
return fmt.Errorf("could not enroll instance: %w", err)
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
|
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")
|
2023-08-29 09:44:23 +00:00
|
|
|
return nil
|
2022-04-20 11:34:17 +00:00
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
if err := cli.setConsoleOpts(enableOpts, true); err != nil {
|
2023-08-29 09:44:23 +00:00
|
|
|
return err
|
2022-01-24 16:22:16 +00:00
|
|
|
}
|
2023-08-29 09:44:23 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
for _, opt := range enableOpts {
|
2024-01-19 14:49:00 +00:00
|
|
|
log.Infof("Enabled %s : %s", opt, csconfig.CONSOLE_CONFIGS_HELP[opt])
|
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
|
2023-08-29 09:44:23 +00:00
|
|
|
log.Info("Watcher successfully enrolled. Visit https://app.crowdsec.net to accept it.")
|
|
|
|
log.Info("Please restart crowdsec after accepting the enrollment.")
|
2024-02-12 10:45:58 +00:00
|
|
|
|
2023-08-29 09:44:23 +00:00
|
|
|
return nil
|
2021-06-28 15:34:19 +00:00
|
|
|
},
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.StringVarP(&name, "name", "n", "", "Name to display in the console")
|
|
|
|
flags.BoolVarP(&overwrite, "overwrite", "", false, "Force enroll the instance")
|
|
|
|
flags.StringSliceVarP(&tags, "tags", "t", tags, "Tags to display in the console")
|
|
|
|
flags.StringSliceVarP(&opts, "enable", "e", opts, "Enable console options")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *cliConsole) newEnableCmd() *cobra.Command {
|
|
|
|
var enableAll bool
|
2022-01-13 15:46:16 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
cmd := &cobra.Command{
|
2023-01-04 15:50:02 +00:00
|
|
|
Use: "enable [option]",
|
|
|
|
Short: "Enable a console option",
|
|
|
|
Example: "sudo cscli console 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,
|
2024-02-12 10:45:58 +00:00
|
|
|
RunE: func(_ *cobra.Command, args []string) error {
|
2022-01-13 15:46:16 +00:00
|
|
|
if enableAll {
|
2024-02-12 10:45:58 +00:00
|
|
|
if err := cli.setConsoleOpts(csconfig.CONSOLE_CONFIGS, true); err != nil {
|
2023-08-29 09:44:23 +00:00
|
|
|
return err
|
|
|
|
}
|
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 {
|
2024-04-23 10:28:38 +00:00
|
|
|
return errors.New("you must specify at least one feature to enable")
|
2023-08-29 09:44:23 +00:00
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
if err := cli.setConsoleOpts(args, true); err != nil {
|
2023-08-29 09:44:23 +00:00
|
|
|
return err
|
2022-02-24 09:24:04 +00:00
|
|
|
}
|
|
|
|
log.Infof("%v have been enabled", args)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
|
2022-01-13 15:46:16 +00:00
|
|
|
log.Infof(ReloadMessage())
|
2024-02-12 10:45:58 +00:00
|
|
|
|
2023-08-29 09:44:23 +00:00
|
|
|
return nil
|
2022-01-13 15:46:16 +00:00
|
|
|
},
|
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
cmd.Flags().BoolVarP(&enableAll, "all", "a", false, "Enable all console options")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
func (cli *cliConsole) newDisableCmd() *cobra.Command {
|
|
|
|
var disableAll bool
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2023-01-04 15:50:02 +00:00
|
|
|
Use: "disable [option]",
|
|
|
|
Short: "Disable a console option",
|
|
|
|
Example: "sudo cscli console disable tainted",
|
2022-01-13 15:46:16 +00:00
|
|
|
Long: `
|
|
|
|
Disable given information push to the central API.`,
|
|
|
|
ValidArgs: csconfig.CONSOLE_CONFIGS,
|
|
|
|
DisableAutoGenTag: true,
|
2024-02-12 10:45:58 +00:00
|
|
|
RunE: func(_ *cobra.Command, args []string) error {
|
2022-01-13 15:46:16 +00:00
|
|
|
if disableAll {
|
2024-02-12 10:45:58 +00:00
|
|
|
if err := cli.setConsoleOpts(csconfig.CONSOLE_CONFIGS, false); err != nil {
|
2023-08-29 09:44:23 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
log.Infof("All features have been disabled")
|
|
|
|
} else {
|
2024-02-12 10:45:58 +00:00
|
|
|
if err := cli.setConsoleOpts(args, false); err != nil {
|
2023-08-29 09:44:23 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
log.Infof("%v have been disabled", args)
|
|
|
|
}
|
2023-08-29 09:44:23 +00:00
|
|
|
|
2022-01-13 15:46:16 +00:00
|
|
|
log.Infof(ReloadMessage())
|
2024-02-12 10:45:58 +00:00
|
|
|
|
2023-08-29 09:44:23 +00:00
|
|
|
return nil
|
2022-01-13 15:46:16 +00:00
|
|
|
},
|
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
cmd.Flags().BoolVarP(&disableAll, "all", "a", false, "Disable all console options")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
func (cli *cliConsole) newStatusCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2023-08-29 09:44:23 +00:00
|
|
|
Use: "status",
|
|
|
|
Short: "Shows status of the console options",
|
2023-01-04 15:50:02 +00:00
|
|
|
Example: `sudo cscli console status`,
|
2022-01-13 15:46:16 +00:00
|
|
|
DisableAutoGenTag: true,
|
2024-02-12 10:45:58 +00:00
|
|
|
RunE: func(_ *cobra.Command, _ []string) error {
|
|
|
|
cfg := cli.cfg()
|
|
|
|
consoleCfg := cfg.API.Server.ConsoleConfig
|
|
|
|
switch cfg.Cscli.Output {
|
2022-01-13 15:46:16 +00:00
|
|
|
case "human":
|
2024-02-12 10:45:58 +00:00
|
|
|
cmdConsoleStatusTable(color.Output, *consoleCfg)
|
2022-01-13 15:46:16 +00:00
|
|
|
case "json":
|
2023-08-29 09:44:23 +00:00
|
|
|
out := map[string](*bool){
|
2024-02-12 10:45:58 +00:00
|
|
|
csconfig.SEND_MANUAL_SCENARIOS: consoleCfg.ShareManualDecisions,
|
|
|
|
csconfig.SEND_CUSTOM_SCENARIOS: consoleCfg.ShareCustomScenarios,
|
|
|
|
csconfig.SEND_TAINTED_SCENARIOS: consoleCfg.ShareTaintedScenarios,
|
|
|
|
csconfig.SEND_CONTEXT: consoleCfg.ShareContext,
|
|
|
|
csconfig.CONSOLE_MANAGEMENT: consoleCfg.ConsoleManagement,
|
2023-08-29 09:44:23 +00:00
|
|
|
}
|
|
|
|
data, err := json.MarshalIndent(out, "", " ")
|
2022-01-13 15:46:16 +00:00
|
|
|
if err != nil {
|
2024-02-12 10:45:58 +00:00
|
|
|
return fmt.Errorf("failed to marshal configuration: %w", err)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
2023-08-29 09:44:23 +00:00
|
|
|
fmt.Println(string(data))
|
2022-01-13 15:46:16 +00:00
|
|
|
case "raw":
|
|
|
|
csvwriter := csv.NewWriter(os.Stdout)
|
|
|
|
err := csvwriter.Write([]string{"option", "enabled"})
|
|
|
|
if err != nil {
|
2023-08-29 09:44:23 +00:00
|
|
|
return err
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rows := [][]string{
|
2024-04-23 10:28:38 +00:00
|
|
|
{csconfig.SEND_MANUAL_SCENARIOS, strconv.FormatBool(*consoleCfg.ShareManualDecisions)},
|
|
|
|
{csconfig.SEND_CUSTOM_SCENARIOS, strconv.FormatBool(*consoleCfg.ShareCustomScenarios)},
|
|
|
|
{csconfig.SEND_TAINTED_SCENARIOS, strconv.FormatBool(*consoleCfg.ShareTaintedScenarios)},
|
|
|
|
{csconfig.SEND_CONTEXT, strconv.FormatBool(*consoleCfg.ShareContext)},
|
|
|
|
{csconfig.CONSOLE_MANAGEMENT, strconv.FormatBool(*consoleCfg.ConsoleManagement)},
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
for _, row := range rows {
|
|
|
|
err = csvwriter.Write(row)
|
|
|
|
if err != nil {
|
2023-08-29 09:44:23 +00:00
|
|
|
return err
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
csvwriter.Flush()
|
|
|
|
}
|
2024-02-12 10:45:58 +00:00
|
|
|
|
2023-08-29 09:44:23 +00:00
|
|
|
return nil
|
2022-01-13 15:46:16 +00:00
|
|
|
},
|
|
|
|
}
|
2023-01-04 15:50:02 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
return cmd
|
2021-06-28 15:34:19 +00:00
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
func (cli *cliConsole) dumpConfig() error {
|
|
|
|
serverCfg := cli.cfg().API.Server
|
|
|
|
|
|
|
|
out, err := yaml.Marshal(serverCfg.ConsoleConfig)
|
2023-10-09 09:10:51 +00:00
|
|
|
if err != nil {
|
2024-02-12 10:45:58 +00:00
|
|
|
return fmt.Errorf("while marshaling ConsoleConfig (for %s): %w", serverCfg.ConsoleConfigPath, err)
|
2023-10-09 09:10:51 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
if serverCfg.ConsoleConfigPath == "" {
|
|
|
|
serverCfg.ConsoleConfigPath = csconfig.DefaultConsoleConfigFilePath
|
|
|
|
log.Debugf("Empty console_path, defaulting to %s", serverCfg.ConsoleConfigPath)
|
2023-10-09 09:10:51 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
if err := os.WriteFile(serverCfg.ConsoleConfigPath, out, 0o600); err != nil {
|
|
|
|
return fmt.Errorf("while dumping console config to %s: %w", serverCfg.ConsoleConfigPath, err)
|
2023-10-09 09:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
func (cli *cliConsole) setConsoleOpts(args []string, wanted bool) error {
|
|
|
|
cfg := cli.cfg()
|
|
|
|
consoleCfg := cfg.API.Server.ConsoleConfig
|
|
|
|
|
2022-01-13 15:46:16 +00:00
|
|
|
for _, arg := range args {
|
|
|
|
switch arg {
|
2023-01-31 13:47:44 +00:00
|
|
|
case csconfig.CONSOLE_MANAGEMENT:
|
|
|
|
/*for each flag check if it's already set before setting it*/
|
2024-02-12 10:45:58 +00:00
|
|
|
if consoleCfg.ConsoleManagement != nil {
|
|
|
|
if *consoleCfg.ConsoleManagement == wanted {
|
2023-02-08 09:35:21 +00:00
|
|
|
log.Debugf("%s already set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
|
2023-01-31 13:47:44 +00:00
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
|
2024-02-12 10:45:58 +00:00
|
|
|
*consoleCfg.ConsoleManagement = wanted
|
2023-01-31 13:47:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
|
2024-02-12 10:45:58 +00:00
|
|
|
consoleCfg.ConsoleManagement = ptr.Of(wanted)
|
2023-01-31 13:47:44 +00:00
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
if cfg.API.Server.OnlineClient.Credentials != nil {
|
2023-03-03 16:03:21 +00:00
|
|
|
changed := false
|
2024-02-12 10:45:58 +00:00
|
|
|
if wanted && cfg.API.Server.OnlineClient.Credentials.PapiURL == "" {
|
2023-03-03 16:03:21 +00:00
|
|
|
changed = true
|
2024-02-12 10:45:58 +00:00
|
|
|
cfg.API.Server.OnlineClient.Credentials.PapiURL = types.PAPIBaseURL
|
|
|
|
} else if !wanted && cfg.API.Server.OnlineClient.Credentials.PapiURL != "" {
|
2023-03-03 16:03:21 +00:00
|
|
|
changed = true
|
2024-02-12 10:45:58 +00:00
|
|
|
cfg.API.Server.OnlineClient.Credentials.PapiURL = ""
|
2023-03-03 16:03:21 +00:00
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-03-03 16:03:21 +00:00
|
|
|
if changed {
|
2024-02-12 10:45:58 +00:00
|
|
|
fileContent, err := yaml.Marshal(cfg.API.Server.OnlineClient.Credentials)
|
2023-03-03 16:03:21 +00:00
|
|
|
if err != nil {
|
2024-02-12 10:45:58 +00:00
|
|
|
return fmt.Errorf("cannot marshal credentials: %w", err)
|
2023-03-03 16:03:21 +00:00
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
log.Infof("Updating credentials file: %s", cfg.API.Server.OnlineClient.CredentialsFilePath)
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
err = os.WriteFile(cfg.API.Server.OnlineClient.CredentialsFilePath, fileContent, 0o600)
|
2023-03-03 16:03:21 +00:00
|
|
|
if err != nil {
|
2024-02-12 10:45:58 +00:00
|
|
|
return fmt.Errorf("cannot write credentials file: %w", err)
|
2023-03-03 16:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
case csconfig.SEND_CUSTOM_SCENARIOS:
|
|
|
|
/*for each flag check if it's already set before setting it*/
|
2024-02-12 10:45:58 +00:00
|
|
|
if consoleCfg.ShareCustomScenarios != nil {
|
|
|
|
if *consoleCfg.ShareCustomScenarios == wanted {
|
2023-02-08 09:35:21 +00:00
|
|
|
log.Debugf("%s already set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
2022-01-13 15:46:16 +00:00
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
2024-02-12 10:45:58 +00:00
|
|
|
*consoleCfg.ShareCustomScenarios = wanted
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
2024-02-12 10:45:58 +00:00
|
|
|
consoleCfg.ShareCustomScenarios = ptr.Of(wanted)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
case csconfig.SEND_TAINTED_SCENARIOS:
|
|
|
|
/*for each flag check if it's already set before setting it*/
|
2024-02-12 10:45:58 +00:00
|
|
|
if consoleCfg.ShareTaintedScenarios != nil {
|
|
|
|
if *consoleCfg.ShareTaintedScenarios == wanted {
|
2023-02-08 09:35:21 +00:00
|
|
|
log.Debugf("%s already set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
2022-01-13 15:46:16 +00:00
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
2024-02-12 10:45:58 +00:00
|
|
|
*consoleCfg.ShareTaintedScenarios = wanted
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
2024-02-12 10:45:58 +00:00
|
|
|
consoleCfg.ShareTaintedScenarios = ptr.Of(wanted)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
case csconfig.SEND_MANUAL_SCENARIOS:
|
|
|
|
/*for each flag check if it's already set before setting it*/
|
2024-02-12 10:45:58 +00:00
|
|
|
if consoleCfg.ShareManualDecisions != nil {
|
|
|
|
if *consoleCfg.ShareManualDecisions == wanted {
|
2023-02-08 09:35:21 +00:00
|
|
|
log.Debugf("%s already set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
2022-01-13 15:46:16 +00:00
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
2024-02-12 10:45:58 +00:00
|
|
|
*consoleCfg.ShareManualDecisions = wanted
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
2024-02-12 10:45:58 +00:00
|
|
|
consoleCfg.ShareManualDecisions = ptr.Of(wanted)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
2023-01-04 15:50:02 +00:00
|
|
|
case csconfig.SEND_CONTEXT:
|
|
|
|
/*for each flag check if it's already set before setting it*/
|
2024-02-12 10:45:58 +00:00
|
|
|
if consoleCfg.ShareContext != nil {
|
|
|
|
if *consoleCfg.ShareContext == wanted {
|
2023-02-08 09:35:21 +00:00
|
|
|
log.Debugf("%s already set to %t", csconfig.SEND_CONTEXT, wanted)
|
2023-01-04 15:50:02 +00:00
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_CONTEXT, wanted)
|
2024-02-12 10:45:58 +00:00
|
|
|
*consoleCfg.ShareContext = wanted
|
2023-01-04 15:50:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Infof("%s set to %t", csconfig.SEND_CONTEXT, wanted)
|
2024-02-12 10:45:58 +00:00
|
|
|
consoleCfg.ShareContext = ptr.Of(wanted)
|
2023-01-04 15:50:02 +00:00
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
default:
|
2023-08-29 09:44:23 +00:00
|
|
|
return fmt.Errorf("unknown flag %s", arg)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 10:45:58 +00:00
|
|
|
if err := cli.dumpConfig(); err != nil {
|
|
|
|
return fmt.Errorf("failed writing console config: %w", err)
|
2023-08-29 09:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|