2020-11-30 09:37:17 +00:00
|
|
|
package csconfig
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
import (
|
2024-03-04 13:22:53 +00:00
|
|
|
"bytes"
|
2022-06-08 14:05:52 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2023-10-02 09:42:17 +00:00
|
|
|
"errors"
|
2021-03-24 17:16:17 +00:00
|
|
|
"fmt"
|
2023-10-02 09:42:17 +00:00
|
|
|
"io"
|
2022-03-16 16:28:34 +00:00
|
|
|
"net"
|
2022-09-06 11:55:03 +00:00
|
|
|
"os"
|
2021-03-24 17:16:17 +00:00
|
|
|
"strings"
|
2022-06-08 14:05:52 +00:00
|
|
|
"time"
|
2021-03-24 17:16:17 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2024-03-04 13:22:53 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-12-03 07:56:11 +00:00
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
|
|
|
"github.com/crowdsecurity/go-cs-lib/yamlpatch"
|
2023-05-23 08:52:47 +00:00
|
|
|
|
2022-12-03 07:56:11 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
2021-03-24 17:16:17 +00:00
|
|
|
)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
type APICfg struct {
|
|
|
|
Client *LocalApiClientCfg `yaml:"client"`
|
|
|
|
Server *LocalApiServerCfg `yaml:"server"`
|
2023-01-19 07:45:50 +00:00
|
|
|
CTI *CTICfg `yaml:"cti"`
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ApiCredentialsCfg struct {
|
2023-01-31 13:47:44 +00:00
|
|
|
PapiURL string `yaml:"papi_url,omitempty" json:"papi_url,omitempty"`
|
2022-06-08 14:05:52 +00:00
|
|
|
URL string `yaml:"url,omitempty" json:"url,omitempty"`
|
|
|
|
Login string `yaml:"login,omitempty" json:"login,omitempty"`
|
|
|
|
Password string `yaml:"password,omitempty" json:"-"`
|
|
|
|
CACertPath string `yaml:"ca_cert_path,omitempty"`
|
|
|
|
KeyPath string `yaml:"key_path,omitempty"`
|
|
|
|
CertPath string `yaml:"cert_path,omitempty"`
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*global api config (for lapi->oapi)*/
|
|
|
|
type OnlineApiClientCfg struct {
|
2022-12-03 07:56:11 +00:00
|
|
|
CredentialsFilePath string `yaml:"credentials_path,omitempty"` // credz will be edited by software, store in diff file
|
2020-11-30 09:37:17 +00:00
|
|
|
Credentials *ApiCredentialsCfg `yaml:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
/*local api config (for crowdsec/cscli->lapi)*/
|
|
|
|
type LocalApiClientCfg struct {
|
2022-12-03 07:56:11 +00:00
|
|
|
CredentialsFilePath string `yaml:"credentials_path,omitempty"` // credz will be edited by software, store in diff file
|
2020-11-30 09:37:17 +00:00
|
|
|
Credentials *ApiCredentialsCfg `yaml:"-"`
|
|
|
|
InsecureSkipVerify *bool `yaml:"insecure_skip_verify"` // check if api certificate is bad or not
|
|
|
|
}
|
|
|
|
|
2023-01-19 07:45:50 +00:00
|
|
|
type CTICfg struct {
|
|
|
|
Key *string `yaml:"key,omitempty"`
|
|
|
|
CacheTimeout *time.Duration `yaml:"cache_timeout,omitempty"`
|
|
|
|
CacheSize *int `yaml:"cache_size,omitempty"`
|
|
|
|
Enabled *bool `yaml:"enabled,omitempty"`
|
|
|
|
LogLevel *log.Level `yaml:"log_level,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *CTICfg) Load() error {
|
|
|
|
if a.Key == nil {
|
|
|
|
*a.Enabled = false
|
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-01-19 07:45:50 +00:00
|
|
|
if a.Key != nil && *a.Key == "" {
|
2024-03-04 13:22:53 +00:00
|
|
|
return errors.New("empty cti key")
|
2023-01-19 07:45:50 +00:00
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-01-19 07:45:50 +00:00
|
|
|
if a.Enabled == nil {
|
|
|
|
a.Enabled = new(bool)
|
|
|
|
*a.Enabled = true
|
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-01-19 07:45:50 +00:00
|
|
|
if a.CacheTimeout == nil {
|
|
|
|
a.CacheTimeout = new(time.Duration)
|
|
|
|
*a.CacheTimeout = 10 * time.Minute
|
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-01-19 07:45:50 +00:00
|
|
|
if a.CacheSize == nil {
|
|
|
|
a.CacheSize = new(int)
|
|
|
|
*a.CacheSize = 100
|
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-01-19 07:45:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
func (o *OnlineApiClientCfg) Load() error {
|
|
|
|
o.Credentials = new(ApiCredentialsCfg)
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2022-09-06 11:55:03 +00:00
|
|
|
fcontent, err := os.ReadFile(o.CredentialsFilePath)
|
2021-03-24 17:16:17 +00:00
|
|
|
if err != nil {
|
2024-01-12 13:41:36 +00:00
|
|
|
return err
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2024-03-04 13:22:53 +00:00
|
|
|
dec := yaml.NewDecoder(bytes.NewReader(fcontent))
|
|
|
|
dec.KnownFields(true)
|
|
|
|
|
|
|
|
err = dec.Decode(o.Credentials)
|
2021-03-24 17:16:17 +00:00
|
|
|
if err != nil {
|
2024-03-04 13:22:53 +00:00
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
return fmt.Errorf("failed unmarshaling api server credentials configuration file '%s': %w", o.CredentialsFilePath, err)
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case o.Credentials.Login == "":
|
|
|
|
log.Warningf("can't load CAPI credentials from '%s' (missing login field)", o.CredentialsFilePath)
|
|
|
|
o.Credentials = nil
|
|
|
|
case o.Credentials.Password == "":
|
|
|
|
log.Warningf("can't load CAPI credentials from '%s' (missing password field)", o.CredentialsFilePath)
|
|
|
|
o.Credentials = nil
|
|
|
|
case o.Credentials.URL == "":
|
|
|
|
log.Warningf("can't load CAPI credentials from '%s' (missing url field)", o.CredentialsFilePath)
|
2021-03-24 17:16:17 +00:00
|
|
|
o.Credentials = nil
|
|
|
|
}
|
2023-01-31 13:47:44 +00:00
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LocalApiClientCfg) Load() error {
|
2022-05-18 08:08:37 +00:00
|
|
|
patcher := yamlpatch.NewPatcher(l.CredentialsFilePath, ".local")
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2022-05-18 08:08:37 +00:00
|
|
|
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
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2024-03-04 13:22:53 +00:00
|
|
|
dec := yaml.NewDecoder(bytes.NewReader(fcontent))
|
|
|
|
dec.KnownFields(true)
|
|
|
|
|
|
|
|
err = dec.Decode(&l.Credentials)
|
2021-03-24 17:16:17 +00:00
|
|
|
if err != nil {
|
2024-03-04 13:22:53 +00:00
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
return fmt.Errorf("failed unmarshaling api client credential configuration file '%s': %w", l.CredentialsFilePath, err)
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
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, "/") {
|
2022-11-07 09:36:50 +00:00
|
|
|
l.Credentials.URL += "/"
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-08 14:05:52 +00:00
|
|
|
|
2022-12-29 21:00:11 +00:00
|
|
|
if l.Credentials.Login != "" && (l.Credentials.CertPath != "" || l.Credentials.KeyPath != "") {
|
2024-03-04 13:22:53 +00:00
|
|
|
return errors.New("user/password authentication and TLS authentication are mutually exclusive")
|
2022-06-08 14:05:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
if l.InsecureSkipVerify == nil {
|
|
|
|
apiclient.InsecureSkipVerify = false
|
|
|
|
} else {
|
|
|
|
apiclient.InsecureSkipVerify = *l.InsecureSkipVerify
|
|
|
|
}
|
2022-06-08 14:05:52 +00:00
|
|
|
|
2023-01-19 07:45:50 +00:00
|
|
|
if l.Credentials.CACertPath != "" {
|
2022-09-06 11:55:03 +00:00
|
|
|
caCert, err := os.ReadFile(l.Credentials.CACertPath)
|
2022-06-08 14:05:52 +00:00
|
|
|
if err != nil {
|
2023-06-01 14:31:56 +00:00
|
|
|
return fmt.Errorf("failed to load cacert: %w", err)
|
2022-06-08 14:05:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 08:10:58 +00:00
|
|
|
caCertPool, err := x509.SystemCertPool()
|
|
|
|
if err != nil {
|
|
|
|
log.Warningf("Error loading system CA certificates: %s", err)
|
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-05-25 08:10:58 +00:00
|
|
|
if caCertPool == nil {
|
|
|
|
caCertPool = x509.NewCertPool()
|
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2022-06-08 14:05:52 +00:00
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
2022-12-29 21:00:11 +00:00
|
|
|
apiclient.CaCertPool = caCertPool
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Credentials.CertPath != "" && l.Credentials.KeyPath != "" {
|
|
|
|
cert, err := tls.LoadX509KeyPair(l.Credentials.CertPath, l.Credentials.KeyPath)
|
|
|
|
if err != nil {
|
2023-06-01 14:31:56 +00:00
|
|
|
return fmt.Errorf("failed to load api client certificate: %w", err)
|
2022-12-29 21:00:11 +00:00
|
|
|
}
|
2022-06-08 14:05:52 +00:00
|
|
|
|
|
|
|
apiclient.Cert = &cert
|
|
|
|
}
|
2022-12-29 21:00:11 +00:00
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-16 16:28:34 +00:00
|
|
|
func (lapiCfg *LocalApiServerCfg) GetTrustedIPs() ([]net.IPNet, error) {
|
|
|
|
trustedIPs := make([]net.IPNet, 0)
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2022-03-16 16:28:34 +00:00
|
|
|
for _, ip := range lapiCfg.TrustedIPs {
|
|
|
|
cidr := toValidCIDR(ip)
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2022-03-16 16:28:34 +00:00
|
|
|
_, ipNet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2022-03-16 16:28:34 +00:00
|
|
|
trustedIPs = append(trustedIPs, *ipNet)
|
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2022-03-16 16:28:34 +00:00
|
|
|
return trustedIPs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toValidCIDR(ip string) string {
|
|
|
|
if strings.Contains(ip, "/") {
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(ip, ":") {
|
|
|
|
return ip + "/128"
|
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2022-03-16 16:28:34 +00:00
|
|
|
return ip + "/32"
|
|
|
|
}
|
|
|
|
|
2023-03-21 10:50:10 +00:00
|
|
|
type CapiWhitelist struct {
|
|
|
|
Ips []net.IP `yaml:"ips,omitempty"`
|
|
|
|
Cidrs []*net.IPNet `yaml:"cidrs,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
/*local api service configuration*/
|
|
|
|
type LocalApiServerCfg struct {
|
2023-02-24 13:44:21 +00:00
|
|
|
Enable *bool `yaml:"enable"`
|
|
|
|
ListenURI string `yaml:"listen_uri,omitempty"` // 127.0.0.1:8080
|
|
|
|
TLS *TLSCfg `yaml:"tls"`
|
|
|
|
DbConfig *DatabaseCfg `yaml:"-"`
|
|
|
|
LogDir string `yaml:"-"`
|
|
|
|
LogMedia string `yaml:"-"`
|
|
|
|
OnlineClient *OnlineApiClientCfg `yaml:"online_client"`
|
|
|
|
ProfilesPath string `yaml:"profiles_path,omitempty"`
|
|
|
|
ConsoleConfigPath string `yaml:"console_path,omitempty"`
|
|
|
|
ConsoleConfig *ConsoleConfig `yaml:"-"`
|
|
|
|
Profiles []*ProfileCfg `yaml:"-"`
|
|
|
|
LogLevel *log.Level `yaml:"log_level"`
|
|
|
|
UseForwardedForHeaders bool `yaml:"use_forwarded_for_headers,omitempty"`
|
|
|
|
TrustedProxies *[]string `yaml:"trusted_proxies,omitempty"`
|
|
|
|
CompressLogs *bool `yaml:"-"`
|
|
|
|
LogMaxSize int `yaml:"-"`
|
|
|
|
LogMaxAge int `yaml:"-"`
|
|
|
|
LogMaxFiles int `yaml:"-"`
|
|
|
|
TrustedIPs []string `yaml:"trusted_ips,omitempty"`
|
|
|
|
PapiLogLevel *log.Level `yaml:"papi_log_level"`
|
|
|
|
DisableRemoteLapiRegistration bool `yaml:"disable_remote_lapi_registration,omitempty"`
|
2023-03-21 10:50:10 +00:00
|
|
|
CapiWhitelistsPath string `yaml:"capi_whitelists_path,omitempty"`
|
|
|
|
CapiWhitelists *CapiWhitelist `yaml:"-"`
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 10:07:53 +00:00
|
|
|
func (c *Config) LoadAPIServer(inCli bool) error {
|
2022-09-12 12:38:29 +00:00
|
|
|
if c.DisableAPI {
|
|
|
|
log.Warning("crowdsec local API is disabled from flag")
|
|
|
|
}
|
|
|
|
|
2023-05-25 09:28:08 +00:00
|
|
|
if c.API.Server == nil {
|
|
|
|
log.Warning("crowdsec local API is disabled")
|
|
|
|
c.DisableAPI = true
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-31 13:47:44 +00:00
|
|
|
|
2023-07-27 15:02:20 +00:00
|
|
|
if c.API.Server.Enable == nil {
|
|
|
|
// if the option is not present, it is enabled by default
|
|
|
|
c.API.Server.Enable = ptr.Of(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !*c.API.Server.Enable {
|
|
|
|
log.Warning("crowdsec local API is disabled because 'enable' is set to false")
|
|
|
|
c.DisableAPI = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DisableAPI {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-14 13:54:11 +00:00
|
|
|
if c.API.Server.ListenURI == "" {
|
2024-03-04 13:22:53 +00:00
|
|
|
return errors.New("no listen_uri specified")
|
2023-12-14 13:54:11 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 21:36:21 +00:00
|
|
|
// inherit log level from common, then api->server
|
2023-05-25 09:28:08 +00:00
|
|
|
var logLevel log.Level
|
|
|
|
if c.API.Server.LogLevel != nil {
|
|
|
|
logLevel = *c.API.Server.LogLevel
|
|
|
|
} else if c.Common.LogLevel != nil {
|
|
|
|
logLevel = *c.Common.LogLevel
|
|
|
|
} else {
|
|
|
|
logLevel = log.InfoLevel
|
|
|
|
}
|
2023-01-31 13:47:44 +00:00
|
|
|
|
2023-05-25 09:28:08 +00:00
|
|
|
if c.API.Server.PapiLogLevel == nil {
|
|
|
|
c.API.Server.PapiLogLevel = &logLevel
|
|
|
|
}
|
2023-03-21 10:50:10 +00:00
|
|
|
|
2023-05-25 09:28:08 +00:00
|
|
|
if c.API.Server.OnlineClient != nil && c.API.Server.OnlineClient.CredentialsFilePath != "" {
|
|
|
|
if err := c.API.Server.OnlineClient.Load(); err != nil {
|
2023-06-01 14:31:56 +00:00
|
|
|
return fmt.Errorf("loading online client credentials: %w", err)
|
2023-03-21 10:50:10 +00:00
|
|
|
}
|
2023-05-25 09:28:08 +00:00
|
|
|
}
|
2023-07-28 07:56:20 +00:00
|
|
|
|
2024-01-31 11:40:41 +00:00
|
|
|
if (c.API.Server.OnlineClient == nil || c.API.Server.OnlineClient.Credentials == nil) && !inCli {
|
2023-05-25 09:28:08 +00:00
|
|
|
log.Printf("push and pull to Central API disabled")
|
|
|
|
}
|
2023-07-28 07:56:20 +00:00
|
|
|
|
2024-01-30 10:07:53 +00:00
|
|
|
if err := c.LoadDBConfig(inCli); err != nil {
|
2023-05-25 09:28:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-03-21 10:50:10 +00:00
|
|
|
|
2023-05-25 09:28:08 +00:00
|
|
|
if err := c.API.Server.LoadCapiWhitelists(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-31 11:40:41 +00:00
|
|
|
if c.API.Server.CapiWhitelistsPath != "" && !inCli {
|
2023-05-25 09:28:08 +00:00
|
|
|
log.Infof("loaded capi whitelist from %s: %d IPs, %d CIDRs", c.API.Server.CapiWhitelistsPath, len(c.API.Server.CapiWhitelists.Ips), len(c.API.Server.CapiWhitelists.Cidrs))
|
2022-09-12 12:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.API.Server.LogDir = c.Common.LogDir
|
|
|
|
c.API.Server.LogMedia = c.Common.LogMedia
|
|
|
|
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
|
2023-07-28 07:56:20 +00:00
|
|
|
|
2022-09-12 12:38:29 +00:00
|
|
|
if c.API.Server.UseForwardedForHeaders && c.API.Server.TrustedProxies == nil {
|
|
|
|
c.API.Server.TrustedProxies = &[]string{"0.0.0.0/0"}
|
|
|
|
}
|
2023-07-28 07:56:20 +00:00
|
|
|
|
2022-09-12 12:38:29 +00:00
|
|
|
if c.API.Server.TrustedProxies != nil {
|
|
|
|
c.API.Server.UseForwardedForHeaders = true
|
|
|
|
}
|
2023-07-28 07:56:20 +00:00
|
|
|
|
2022-09-12 12:38:29 +00:00
|
|
|
if err := c.API.Server.LoadProfiles(); err != nil {
|
2023-06-01 14:31:56 +00:00
|
|
|
return fmt.Errorf("while loading profiles for LAPI: %w", err)
|
2022-09-12 12:38:29 +00:00
|
|
|
}
|
2023-07-28 07:56:20 +00:00
|
|
|
|
2022-09-12 12:38:29 +00:00
|
|
|
if c.API.Server.ConsoleConfigPath == "" {
|
|
|
|
c.API.Server.ConsoleConfigPath = DefaultConsoleConfigFilePath
|
|
|
|
}
|
2023-07-28 07:56:20 +00:00
|
|
|
|
2022-09-12 12:38:29 +00:00
|
|
|
if err := c.API.Server.LoadConsoleConfig(); err != nil {
|
2023-06-01 14:31:56 +00:00
|
|
|
return fmt.Errorf("while loading console options: %w", err)
|
2022-09-12 12:38:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 07:45:50 +00:00
|
|
|
if c.API.CTI != nil {
|
|
|
|
if err := c.API.CTI.Load(); err != nil {
|
2023-06-01 14:31:56 +00:00
|
|
|
return fmt.Errorf("loading CTI configuration: %w", err)
|
2023-01-19 07:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-03 07:56:11 +00:00
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-21 10:50:10 +00:00
|
|
|
// we cannot unmarshal to type net.IPNet, so we need to do it manually
|
|
|
|
type capiWhitelists struct {
|
|
|
|
Ips []string `yaml:"ips"`
|
|
|
|
Cidrs []string `yaml:"cidrs"`
|
|
|
|
}
|
|
|
|
|
2023-10-02 09:42:17 +00:00
|
|
|
func parseCapiWhitelists(fd io.Reader) (*CapiWhitelist, error) {
|
|
|
|
fromCfg := capiWhitelists{}
|
2023-03-21 10:50:10 +00:00
|
|
|
|
|
|
|
decoder := yaml.NewDecoder(fd)
|
|
|
|
if err := decoder.Decode(&fromCfg); err != nil {
|
2023-10-02 09:42:17 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2024-03-04 13:22:53 +00:00
|
|
|
return nil, errors.New("empty file")
|
2023-10-02 09:42:17 +00:00
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-10-02 09:42:17 +00:00
|
|
|
return nil, err
|
2023-03-21 10:50:10 +00:00
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-10-02 09:42:17 +00:00
|
|
|
ret := &CapiWhitelist{
|
2023-08-10 11:03:47 +00:00
|
|
|
Ips: make([]net.IP, len(fromCfg.Ips)),
|
|
|
|
Cidrs: make([]*net.IPNet, len(fromCfg.Cidrs)),
|
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-10-02 09:42:17 +00:00
|
|
|
for idx, v := range fromCfg.Ips {
|
2023-03-21 10:50:10 +00:00
|
|
|
ip := net.ParseIP(v)
|
|
|
|
if ip == nil {
|
2023-10-02 09:42:17 +00:00
|
|
|
return nil, fmt.Errorf("invalid IP address: %s", v)
|
2023-03-21 10:50:10 +00:00
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-10-02 09:42:17 +00:00
|
|
|
ret.Ips[idx] = ip
|
2023-03-21 10:50:10 +00:00
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-10-02 09:42:17 +00:00
|
|
|
for idx, v := range fromCfg.Cidrs {
|
2023-03-21 10:50:10 +00:00
|
|
|
_, tnet, err := net.ParseCIDR(v)
|
|
|
|
if err != nil {
|
2023-10-02 09:42:17 +00:00
|
|
|
return nil, err
|
2023-03-21 10:50:10 +00:00
|
|
|
}
|
2024-01-12 13:41:36 +00:00
|
|
|
|
2023-10-02 09:42:17 +00:00
|
|
|
ret.Cidrs[idx] = tnet
|
2023-03-21 10:50:10 +00:00
|
|
|
}
|
2023-10-02 09:42:17 +00:00
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LocalApiServerCfg) LoadCapiWhitelists() error {
|
|
|
|
if s.CapiWhitelistsPath == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := os.Open(s.CapiWhitelistsPath)
|
|
|
|
if err != nil {
|
2024-03-04 13:22:53 +00:00
|
|
|
return fmt.Errorf("while opening capi whitelist file: %w", err)
|
2023-10-02 09:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
s.CapiWhitelists, err = parseCapiWhitelists(fd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("while parsing capi whitelist file '%s': %w", s.CapiWhitelistsPath, err)
|
|
|
|
}
|
|
|
|
|
2023-03-21 10:50:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-24 17:16:17 +00:00
|
|
|
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 {
|
2024-03-04 13:22:53 +00:00
|
|
|
return errors.New("no API client section in configuration")
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|