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"
|
2020-05-15 09:49:17 +00:00
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
/*top-level config : defaults,overriden by cfg file,overriden by cli*/
|
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{
|
|
|
|
ConfigDir: "/etc/crowdsec/",
|
|
|
|
DataDir: "/var/lib/crowdsec/data/",
|
|
|
|
SimulationFilePath: "/etc/crowdsec/config/simulation.yaml",
|
|
|
|
HubDir: "/etc/crowdsec/hub",
|
|
|
|
HubIndexFile: "/etc/crowdsec/hub/.index.json",
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
crowdsecCfg := CrowdsecServiceCfg{
|
|
|
|
AcquisitionFilePath: "/etc/crowdsec/config/acquis.yaml",
|
|
|
|
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{
|
|
|
|
CredentialsFilePath: "/etc/crowdsec/config/lapi-secrets.yaml",
|
|
|
|
},
|
|
|
|
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{
|
|
|
|
CredentialsFilePath: "/etc/crowdsec/config/online-api-secrets.yaml",
|
|
|
|
},
|
|
|
|
},
|
2020-05-15 09:49:17 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
dbConfig := DatabaseCfg{
|
|
|
|
Type: "sqlite",
|
|
|
|
DbPath: "/var/lib/crowdsec/data/crowdsec.db",
|
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
|
|
|
|
}
|