2020-05-22 15:45:08 +00:00
|
|
|
package csconfig
|
2020-05-15 09:49:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2021-02-04 16:17:01 +00:00
|
|
|
"os"
|
2022-02-01 09:34:53 +00:00
|
|
|
"path/filepath"
|
2020-05-15 09:49:17 +00:00
|
|
|
|
2022-02-17 16:52:04 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
2020-11-30 09:37:17 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-05-15 09:49:17 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2022-02-01 09:34:53 +00:00
|
|
|
// defaultConfigDir is the base path to all configuration files, to be overridden in the Makefile */
|
|
|
|
var defaultConfigDir = "/etc/crowdsec"
|
|
|
|
|
|
|
|
// defaultDataDir is the base path to all data files, to be overridden in the Makefile */
|
|
|
|
var defaultDataDir = "/var/lib/crowdsec/data/"
|
|
|
|
|
|
|
|
// Config contains top-level defaults -> overridden by configuration file -> overridden by CLI flags
|
2021-03-24 17:16:17 +00:00
|
|
|
type Config struct {
|
2020-11-30 09:37:17 +00:00
|
|
|
//just a path to ourself :p
|
2021-03-24 17:16:17 +00:00
|
|
|
FilePath *string `yaml:"-"`
|
|
|
|
Self []byte `yaml:"-"`
|
2021-02-09 16:59:35 +00:00
|
|
|
Common *CommonCfg `yaml:"common,omitempty"`
|
|
|
|
Prometheus *PrometheusCfg `yaml:"prometheus,omitempty"`
|
|
|
|
Crowdsec *CrowdsecServiceCfg `yaml:"crowdsec_service,omitempty"`
|
|
|
|
Cscli *CscliCfg `yaml:"cscli,omitempty"`
|
|
|
|
DbConfig *DatabaseCfg `yaml:"db_config,omitempty"`
|
|
|
|
API *APICfg `yaml:"api,omitempty"`
|
|
|
|
ConfigPaths *ConfigurationPaths `yaml:"config_paths,omitempty"`
|
2021-09-08 09:36:42 +00:00
|
|
|
PluginConfig *PluginCfg `yaml:"plugin_config,omitempty"`
|
2021-02-09 16:59:35 +00:00
|
|
|
DisableAPI bool `yaml:"-"`
|
|
|
|
DisableAgent bool `yaml:"-"`
|
2021-03-24 17:16:17 +00:00
|
|
|
Hub *Hub `yaml:"-"`
|
2020-07-16 13:59:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
func (c *Config) Dump() error {
|
2020-11-30 09:37:17 +00:00
|
|
|
out, err := yaml.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed marshaling config")
|
|
|
|
}
|
|
|
|
fmt.Printf("%s", string(out))
|
|
|
|
return nil
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
func NewConfig(configFile string, disableAgent bool, disableAPI bool) (*Config, error) {
|
|
|
|
fcontent, err := ioutil.ReadFile(configFile)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2021-03-24 17:16:17 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to read config file")
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2021-02-04 16:17:01 +00:00
|
|
|
configData := os.ExpandEnv(string(fcontent))
|
2021-03-24 17:16:17 +00:00
|
|
|
cfg := Config{
|
|
|
|
FilePath: &configFile,
|
|
|
|
DisableAgent: disableAgent,
|
|
|
|
DisableAPI: disableAPI,
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2020-05-15 09:49:17 +00:00
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
err = yaml.UnmarshalStrict([]byte(configData), &cfg)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2021-03-24 17:16:17 +00:00
|
|
|
return nil, err
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
return &cfg, nil
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2020-05-15 09:49:17 +00:00
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
2020-11-30 09:37:17 +00:00
|
|
|
logLevel := log.InfoLevel
|
|
|
|
CommonCfg := CommonCfg{
|
|
|
|
Daemonize: false,
|
|
|
|
PidDir: "/tmp/",
|
|
|
|
LogMedia: "stdout",
|
|
|
|
//LogDir unneeded
|
|
|
|
LogLevel: &logLevel,
|
|
|
|
WorkingDir: ".",
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
prometheus := PrometheusCfg{
|
|
|
|
Enabled: true,
|
|
|
|
Level: "full",
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
configPaths := ConfigurationPaths{
|
2022-02-01 09:34:53 +00:00
|
|
|
ConfigDir: DefaultConfigPath("."),
|
|
|
|
DataDir: DefaultDataPath("."),
|
|
|
|
SimulationFilePath: DefaultConfigPath("simulation.yaml"),
|
|
|
|
HubDir: DefaultConfigPath("hub"),
|
|
|
|
HubIndexFile: DefaultConfigPath("hub", ".index.json"),
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
crowdsecCfg := CrowdsecServiceCfg{
|
2022-02-01 09:34:53 +00:00
|
|
|
AcquisitionFilePath: DefaultConfigPath("acquis.yaml"),
|
2020-11-30 09:37:17 +00:00
|
|
|
ParserRoutinesCount: 1,
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
cscliCfg := CscliCfg{
|
|
|
|
Output: "human",
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
apiCfg := APICfg{
|
|
|
|
Client: &LocalApiClientCfg{
|
2022-02-01 09:34:53 +00:00
|
|
|
CredentialsFilePath: DefaultConfigPath("lapi-secrets.yaml"),
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
|
|
|
Server: &LocalApiServerCfg{
|
2021-02-09 18:10:14 +00:00
|
|
|
ListenURI: "127.0.0.1:8080",
|
|
|
|
UseForwardedForHeaders: false,
|
2020-11-30 09:37:17 +00:00
|
|
|
OnlineClient: &OnlineApiClientCfg{
|
2022-02-01 09:34:53 +00:00
|
|
|
CredentialsFilePath: DefaultConfigPath("config", "online-api-secrets.yaml"),
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
|
|
|
},
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
dbConfig := DatabaseCfg{
|
2022-02-17 16:52:04 +00:00
|
|
|
Type: "sqlite",
|
|
|
|
DbPath: DefaultDataPath("crowdsec.db"),
|
|
|
|
MaxOpenConns: types.IntPtr(DEFAULT_MAX_OPEN_CONNS),
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
globalCfg := Config{
|
2020-11-30 09:37:17 +00:00
|
|
|
Common: &CommonCfg,
|
|
|
|
Prometheus: &prometheus,
|
|
|
|
Crowdsec: &crowdsecCfg,
|
|
|
|
Cscli: &cscliCfg,
|
|
|
|
API: &apiCfg,
|
|
|
|
ConfigPaths: &configPaths,
|
|
|
|
DbConfig: &dbConfig,
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
return &globalCfg
|
|
|
|
}
|
2022-02-01 09:34:53 +00:00
|
|
|
|
|
|
|
// DefaultConfigPath returns the default path for a configuration resource
|
|
|
|
// "elem" parameters are path components relative to the default cfg directory.
|
|
|
|
func DefaultConfigPath(elem ...string) string {
|
|
|
|
elem = append([]string{defaultConfigDir}, elem...)
|
|
|
|
return filepath.Join(elem...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultDataPath returns the the default path for a data resource.
|
|
|
|
// "elem" parameters are path components relative to the default data directory.
|
|
|
|
func DefaultDataPath(elem ...string) string {
|
|
|
|
elem = append([]string{defaultDataDir}, elem...)
|
|
|
|
return filepath.Join(elem...)
|
|
|
|
}
|