github.com/crowdsecurity/crowdsec@v1.6.1/pkg/csconfig/console.go (about)

     1  package csconfig
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  	"gopkg.in/yaml.v3"
     9  
    10  	"github.com/crowdsecurity/go-cs-lib/ptr"
    11  )
    12  
    13  const (
    14  	SEND_CUSTOM_SCENARIOS  = "custom"
    15  	SEND_TAINTED_SCENARIOS = "tainted"
    16  	SEND_MANUAL_SCENARIOS  = "manual"
    17  	CONSOLE_MANAGEMENT     = "console_management"
    18  	SEND_CONTEXT           = "context"
    19  )
    20  
    21  var CONSOLE_CONFIGS = []string{SEND_CUSTOM_SCENARIOS, SEND_MANUAL_SCENARIOS, SEND_TAINTED_SCENARIOS, SEND_CONTEXT, CONSOLE_MANAGEMENT}
    22  var CONSOLE_CONFIGS_HELP = map[string]string{
    23  	SEND_CUSTOM_SCENARIOS:  "Forward alerts from custom scenarios to the console",
    24  	SEND_MANUAL_SCENARIOS:  "Forward manual decisions to the console",
    25  	SEND_TAINTED_SCENARIOS: "Forward alerts from tainted scenarios to the console",
    26  	SEND_CONTEXT:           "Forward context with alerts to the console",
    27  	CONSOLE_MANAGEMENT:     "Receive decisions from console",
    28  }
    29  
    30  var DefaultConsoleConfigFilePath = DefaultConfigPath("console.yaml")
    31  
    32  type ConsoleConfig struct {
    33  	ShareManualDecisions  *bool `yaml:"share_manual_decisions"`
    34  	ShareTaintedScenarios *bool `yaml:"share_tainted"`
    35  	ShareCustomScenarios  *bool `yaml:"share_custom"`
    36  	ConsoleManagement     *bool `yaml:"console_management"`
    37  	ShareContext          *bool `yaml:"share_context"`
    38  }
    39  
    40  func (c *ConsoleConfig) EnabledOptions() []string {
    41  	ret := []string{}
    42  	if c == nil {
    43  		return ret
    44  	}
    45  
    46  	if c.ShareCustomScenarios != nil && *c.ShareCustomScenarios {
    47  		ret = append(ret, SEND_CUSTOM_SCENARIOS)
    48  	}
    49  
    50  	if c.ShareTaintedScenarios != nil && *c.ShareTaintedScenarios {
    51  		ret = append(ret, SEND_TAINTED_SCENARIOS)
    52  	}
    53  
    54  	if c.ShareManualDecisions != nil && *c.ShareManualDecisions {
    55  		ret = append(ret, SEND_MANUAL_SCENARIOS)
    56  	}
    57  
    58  	if c.ConsoleManagement != nil && *c.ConsoleManagement {
    59  		ret = append(ret, CONSOLE_MANAGEMENT)
    60  	}
    61  
    62  	if c.ShareContext != nil && *c.ShareContext {
    63  		ret = append(ret, SEND_CONTEXT)
    64  	}
    65  
    66  	return ret
    67  }
    68  
    69  func (c *ConsoleConfig) IsPAPIEnabled() bool {
    70  	if c == nil || c.ConsoleManagement == nil {
    71  		return false
    72  	}
    73  
    74  	return *c.ConsoleManagement
    75  }
    76  
    77  func (c *LocalApiServerCfg) LoadConsoleConfig() error {
    78  	c.ConsoleConfig = &ConsoleConfig{}
    79  	if _, err := os.Stat(c.ConsoleConfigPath); err != nil && os.IsNotExist(err) {
    80  		log.Debugf("no console configuration to load")
    81  
    82  		c.ConsoleConfig.ShareCustomScenarios = ptr.Of(true)
    83  		c.ConsoleConfig.ShareTaintedScenarios = ptr.Of(true)
    84  		c.ConsoleConfig.ShareManualDecisions = ptr.Of(false)
    85  		c.ConsoleConfig.ConsoleManagement = ptr.Of(false)
    86  		c.ConsoleConfig.ShareContext = ptr.Of(false)
    87  
    88  		return nil
    89  	}
    90  
    91  	yamlFile, err := os.ReadFile(c.ConsoleConfigPath)
    92  	if err != nil {
    93  		return fmt.Errorf("reading console config file '%s': %w", c.ConsoleConfigPath, err)
    94  	}
    95  
    96  	err = yaml.Unmarshal(yamlFile, c.ConsoleConfig)
    97  	if err != nil {
    98  		return fmt.Errorf("unmarshaling console config file '%s': %w", c.ConsoleConfigPath, err)
    99  	}
   100  
   101  	if c.ConsoleConfig.ShareCustomScenarios == nil {
   102  		log.Debugf("no share_custom scenarios found, setting to true")
   103  		c.ConsoleConfig.ShareCustomScenarios = ptr.Of(true)
   104  	}
   105  
   106  	if c.ConsoleConfig.ShareTaintedScenarios == nil {
   107  		log.Debugf("no share_tainted scenarios found, setting to true")
   108  		c.ConsoleConfig.ShareTaintedScenarios = ptr.Of(true)
   109  	}
   110  
   111  	if c.ConsoleConfig.ShareManualDecisions == nil {
   112  		log.Debugf("no share_manual scenarios found, setting to false")
   113  		c.ConsoleConfig.ShareManualDecisions = ptr.Of(false)
   114  	}
   115  
   116  	if c.ConsoleConfig.ConsoleManagement == nil {
   117  		log.Debugf("no console_management found, setting to false")
   118  		c.ConsoleConfig.ConsoleManagement = ptr.Of(false)
   119  	}
   120  
   121  	if c.ConsoleConfig.ShareContext == nil {
   122  		log.Debugf("no 'context' found, setting to false")
   123  		c.ConsoleConfig.ShareContext = ptr.Of(false)
   124  	}
   125  
   126  	log.Debugf("Console configuration '%s' loaded successfully", c.ConsoleConfigPath)
   127  
   128  	return nil
   129  }