crowdsec/pkg/csconfig/config.go

119 lines
3 KiB
Go
Raw Normal View History

2020-05-22 15:45:08 +00:00
package csconfig
2020-05-15 09:49:17 +00:00
import (
"fmt"
"io/ioutil"
"os"
2020-05-15 09:49:17 +00:00
"github.com/pkg/errors"
2020-05-15 09:49:17 +00:00
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
/*top-level config : defaults,overriden by cfg file,overriden by cli*/
type Config struct {
//just a path to ourself :p
FilePath *string `yaml:"-"`
Self []byte `yaml:"-"`
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"`
PluginConfig *PluginCfg `yaml:"plugin_config,omitempty"`
DisableAPI bool `yaml:"-"`
DisableAgent bool `yaml:"-"`
Hub *Hub `yaml:"-"`
}
func (c *Config) Dump() error {
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
}
func NewConfig(configFile string, disableAgent bool, disableAPI bool) (*Config, error) {
fcontent, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, errors.Wrap(err, "failed to read config file")
}
configData := os.ExpandEnv(string(fcontent))
cfg := Config{
FilePath: &configFile,
DisableAgent: disableAgent,
DisableAPI: disableAPI,
}
2020-05-15 09:49:17 +00:00
err = yaml.UnmarshalStrict([]byte(configData), &cfg)
if err != nil {
return nil, err
2020-05-15 09:49:17 +00:00
}
return &cfg, nil
}
2020-05-15 09:49:17 +00:00
func NewDefaultConfig() *Config {
logLevel := log.InfoLevel
CommonCfg := CommonCfg{
Daemonize: false,
PidDir: "/tmp/",
LogMedia: "stdout",
//LogDir unneeded
LogLevel: &logLevel,
WorkingDir: ".",
2020-05-15 09:49:17 +00:00
}
prometheus := PrometheusCfg{
Enabled: true,
Level: "full",
2020-05-15 09:49: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
}
crowdsecCfg := CrowdsecServiceCfg{
AcquisitionFilePath: "/etc/crowdsec/config/acquis.yaml",
ParserRoutinesCount: 1,
2020-05-15 09:49:17 +00:00
}
cscliCfg := CscliCfg{
Output: "human",
2020-05-15 09:49:17 +00:00
}
apiCfg := APICfg{
Client: &LocalApiClientCfg{
CredentialsFilePath: "/etc/crowdsec/config/lapi-secrets.yaml",
},
Server: &LocalApiServerCfg{
ListenURI: "127.0.0.1:8080",
UseForwardedForHeaders: false,
OnlineClient: &OnlineApiClientCfg{
CredentialsFilePath: "/etc/crowdsec/config/online-api-secrets.yaml",
},
},
2020-05-15 09:49:17 +00:00
}
dbConfig := DatabaseCfg{
Type: "sqlite",
DbPath: "/var/lib/crowdsec/data/crowdsec.db",
2020-05-15 09:49:17 +00:00
}
globalCfg := Config{
Common: &CommonCfg,
Prometheus: &prometheus,
Crowdsec: &crowdsecCfg,
Cscli: &cscliCfg,
API: &apiCfg,
ConfigPaths: &configPaths,
DbConfig: &dbConfig,
2020-05-15 09:49:17 +00:00
}
return &globalCfg
}