2020-11-30 09:37:17 +00:00
|
|
|
package csconfig
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2022-03-16 16:28:34 +00:00
|
|
|
"net"
|
2021-03-24 17:16:17 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
2022-05-18 08:08:37 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/yamlpatch"
|
2021-03-24 17:16:17 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
type APICfg struct {
|
|
|
|
Client *LocalApiClientCfg `yaml:"client"`
|
|
|
|
Server *LocalApiServerCfg `yaml:"server"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApiCredentialsCfg struct {
|
|
|
|
URL string `yaml:"url,omitempty" json:"url,omitempty"`
|
|
|
|
Login string `yaml:"login,omitempty" json:"login,omitempty"`
|
|
|
|
Password string `yaml:"password,omitempty" json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
/*global api config (for lapi->oapi)*/
|
|
|
|
type OnlineApiClientCfg struct {
|
|
|
|
CredentialsFilePath string `yaml:"credentials_path,omitempty"` //credz will be edited by software, store in diff file
|
|
|
|
Credentials *ApiCredentialsCfg `yaml:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
/*local api config (for crowdsec/cscli->lapi)*/
|
|
|
|
type LocalApiClientCfg struct {
|
|
|
|
CredentialsFilePath string `yaml:"credentials_path,omitempty"` //credz will be edited by software, store in diff file
|
|
|
|
Credentials *ApiCredentialsCfg `yaml:"-"`
|
|
|
|
InsecureSkipVerify *bool `yaml:"insecure_skip_verify"` // check if api certificate is bad or not
|
|
|
|
}
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
func (o *OnlineApiClientCfg) Load() error {
|
|
|
|
o.Credentials = new(ApiCredentialsCfg)
|
|
|
|
fcontent, err := ioutil.ReadFile(o.CredentialsFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to read api server credentials configuration file '%s'", o.CredentialsFilePath)
|
|
|
|
}
|
|
|
|
err = yaml.UnmarshalStrict(fcontent, o.Credentials)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed unmarshaling api server credentials configuration file '%s'", o.CredentialsFilePath)
|
|
|
|
}
|
|
|
|
if o.Credentials.Login == "" || o.Credentials.Password == "" || o.Credentials.URL == "" {
|
|
|
|
log.Warningf("can't load CAPI credentials from '%s' (missing field)", o.CredentialsFilePath)
|
|
|
|
o.Credentials = nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LocalApiClientCfg) Load() error {
|
2022-05-18 08:08:37 +00:00
|
|
|
patcher := yamlpatch.NewPatcher(l.CredentialsFilePath, ".local")
|
|
|
|
fcontent, err := patcher.MergedPatchContent()
|
2021-03-24 17:16:17 +00:00
|
|
|
if err != nil {
|
2022-05-19 08:48:08 +00:00
|
|
|
return err
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
err = yaml.UnmarshalStrict(fcontent, &l.Credentials)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed unmarshaling api client credential configuration file '%s'", l.CredentialsFilePath)
|
|
|
|
}
|
2022-05-19 08:48:08 +00:00
|
|
|
if l.Credentials == nil || l.Credentials.URL == "" {
|
|
|
|
return fmt.Errorf("no credentials or URL found in api client configuration '%s'", l.CredentialsFilePath)
|
|
|
|
}
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
if l.Credentials != nil && l.Credentials.URL != "" {
|
|
|
|
if !strings.HasSuffix(l.Credentials.URL, "/") {
|
|
|
|
l.Credentials.URL = l.Credentials.URL + "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if l.InsecureSkipVerify == nil {
|
|
|
|
apiclient.InsecureSkipVerify = false
|
|
|
|
} else {
|
|
|
|
apiclient.InsecureSkipVerify = *l.InsecureSkipVerify
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-16 16:28:34 +00:00
|
|
|
func (lapiCfg *LocalApiServerCfg) GetTrustedIPs() ([]net.IPNet, error) {
|
|
|
|
trustedIPs := make([]net.IPNet, 0)
|
|
|
|
for _, ip := range lapiCfg.TrustedIPs {
|
|
|
|
cidr := toValidCIDR(ip)
|
|
|
|
_, ipNet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
trustedIPs = append(trustedIPs, *ipNet)
|
|
|
|
}
|
|
|
|
return trustedIPs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toValidCIDR(ip string) string {
|
|
|
|
if strings.Contains(ip, "/") {
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(ip, ":") {
|
|
|
|
return ip + "/128"
|
|
|
|
}
|
|
|
|
return ip + "/32"
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
/*local api service configuration*/
|
|
|
|
type LocalApiServerCfg struct {
|
2021-02-09 18:10:14 +00:00
|
|
|
ListenURI string `yaml:"listen_uri,omitempty"` //127.0.0.1:8080
|
|
|
|
TLS *TLSCfg `yaml:"tls"`
|
|
|
|
DbConfig *DatabaseCfg `yaml:"-"`
|
|
|
|
LogDir string `yaml:"-"`
|
2021-02-11 17:28:01 +00:00
|
|
|
LogMedia string `yaml:"-"`
|
2021-02-09 18:10:14 +00:00
|
|
|
OnlineClient *OnlineApiClientCfg `yaml:"online_client"`
|
|
|
|
ProfilesPath string `yaml:"profiles_path,omitempty"`
|
2022-01-13 15:46:16 +00:00
|
|
|
ConsoleConfigPath string `yaml:"console_path,omitempty"`
|
|
|
|
ConsoleConfig *ConsoleConfig `yaml:"-"`
|
2021-02-09 18:10:14 +00:00
|
|
|
Profiles []*ProfileCfg `yaml:"-"`
|
|
|
|
LogLevel *log.Level `yaml:"log_level"`
|
|
|
|
UseForwardedForHeaders bool `yaml:"use_forwarded_for_headers,omitempty"`
|
2022-01-17 16:18:12 +00:00
|
|
|
TrustedProxies *[]string `yaml:"trusted_proxies,omitempty"`
|
2021-12-28 10:59:03 +00:00
|
|
|
CompressLogs *bool `yaml:"-"`
|
|
|
|
LogMaxSize int `yaml:"-"`
|
|
|
|
LogMaxAge int `yaml:"-"`
|
|
|
|
LogMaxFiles int `yaml:"-"`
|
2022-03-16 16:28:34 +00:00
|
|
|
TrustedIPs []string `yaml:"trusted_ips,omitempty"`
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TLSCfg struct {
|
|
|
|
CertFilePath string `yaml:"cert_file"`
|
|
|
|
KeyFilePath string `yaml:"key_file"`
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
|
|
|
|
func (c *Config) LoadAPIServer() error {
|
|
|
|
if c.API.Server != nil && !c.DisableAPI {
|
|
|
|
if err := c.LoadCommon(); err != nil {
|
|
|
|
return fmt.Errorf("loading common configuration: %s", err.Error())
|
|
|
|
}
|
|
|
|
c.API.Server.LogDir = c.Common.LogDir
|
|
|
|
c.API.Server.LogMedia = c.Common.LogMedia
|
2021-12-28 10:59:03 +00:00
|
|
|
c.API.Server.CompressLogs = c.Common.CompressLogs
|
|
|
|
c.API.Server.LogMaxSize = c.Common.LogMaxSize
|
|
|
|
c.API.Server.LogMaxAge = c.Common.LogMaxAge
|
|
|
|
c.API.Server.LogMaxFiles = c.Common.LogMaxFiles
|
2022-01-17 16:18:12 +00:00
|
|
|
if c.API.Server.UseForwardedForHeaders && c.API.Server.TrustedProxies == nil {
|
|
|
|
c.API.Server.TrustedProxies = &[]string{"0.0.0.0/0"}
|
|
|
|
}
|
|
|
|
if c.API.Server.TrustedProxies != nil {
|
|
|
|
c.API.Server.UseForwardedForHeaders = true
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
if err := c.API.Server.LoadProfiles(); err != nil {
|
|
|
|
return errors.Wrap(err, "while loading profiles for LAPI")
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
if c.API.Server.ConsoleConfigPath == "" {
|
2022-02-01 09:34:53 +00:00
|
|
|
c.API.Server.ConsoleConfigPath = DefaultConsoleConfigFilePath
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
if err := c.API.Server.LoadConsoleConfig(); err != nil {
|
|
|
|
return errors.Wrap(err, "while loading console options")
|
|
|
|
}
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
if c.API.Server.OnlineClient != nil && c.API.Server.OnlineClient.CredentialsFilePath != "" {
|
|
|
|
if err := c.API.Server.OnlineClient.Load(); err != nil {
|
|
|
|
return errors.Wrap(err, "loading online client credentials")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.API.Server.OnlineClient == nil || c.API.Server.OnlineClient.Credentials == nil {
|
2021-08-25 16:30:05 +00:00
|
|
|
log.Printf("push and pull to Central API disabled")
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
if err := c.LoadDBConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warningf("crowdsec local API is disabled")
|
|
|
|
c.DisableAPI = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) LoadAPIClient() error {
|
2022-05-19 08:48:08 +00:00
|
|
|
if c.API == nil || c.API.Client == nil || c.API.Client.CredentialsFilePath == "" || c.DisableAgent {
|
2021-03-24 17:16:17 +00:00
|
|
|
return fmt.Errorf("no API client section in configuration")
|
|
|
|
}
|
|
|
|
|
2022-05-19 08:48:08 +00:00
|
|
|
if err := c.API.Client.Load(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
return nil
|
|
|
|
}
|