api.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package csconfig
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net"
  6. "strings"
  7. "github.com/crowdsecurity/crowdsec/pkg/apiclient"
  8. "github.com/crowdsecurity/crowdsec/pkg/yamlpatch"
  9. "github.com/pkg/errors"
  10. log "github.com/sirupsen/logrus"
  11. "gopkg.in/yaml.v2"
  12. )
  13. type APICfg struct {
  14. Client *LocalApiClientCfg `yaml:"client"`
  15. Server *LocalApiServerCfg `yaml:"server"`
  16. }
  17. type ApiCredentialsCfg struct {
  18. URL string `yaml:"url,omitempty" json:"url,omitempty"`
  19. Login string `yaml:"login,omitempty" json:"login,omitempty"`
  20. Password string `yaml:"password,omitempty" json:"-"`
  21. }
  22. /*global api config (for lapi->oapi)*/
  23. type OnlineApiClientCfg struct {
  24. CredentialsFilePath string `yaml:"credentials_path,omitempty"` //credz will be edited by software, store in diff file
  25. Credentials *ApiCredentialsCfg `yaml:"-"`
  26. }
  27. /*local api config (for crowdsec/cscli->lapi)*/
  28. type LocalApiClientCfg struct {
  29. CredentialsFilePath string `yaml:"credentials_path,omitempty"` //credz will be edited by software, store in diff file
  30. Credentials *ApiCredentialsCfg `yaml:"-"`
  31. InsecureSkipVerify *bool `yaml:"insecure_skip_verify"` // check if api certificate is bad or not
  32. }
  33. func (o *OnlineApiClientCfg) Load() error {
  34. o.Credentials = new(ApiCredentialsCfg)
  35. fcontent, err := ioutil.ReadFile(o.CredentialsFilePath)
  36. if err != nil {
  37. return errors.Wrapf(err, "failed to read api server credentials configuration file '%s'", o.CredentialsFilePath)
  38. }
  39. err = yaml.UnmarshalStrict(fcontent, o.Credentials)
  40. if err != nil {
  41. return errors.Wrapf(err, "failed unmarshaling api server credentials configuration file '%s'", o.CredentialsFilePath)
  42. }
  43. if o.Credentials.Login == "" || o.Credentials.Password == "" || o.Credentials.URL == "" {
  44. log.Warningf("can't load CAPI credentials from '%s' (missing field)", o.CredentialsFilePath)
  45. o.Credentials = nil
  46. }
  47. return nil
  48. }
  49. func (l *LocalApiClientCfg) Load() error {
  50. patcher := yamlpatch.NewPatcher(l.CredentialsFilePath, ".local")
  51. fcontent, err := patcher.MergedPatchContent()
  52. if err != nil {
  53. return err
  54. }
  55. err = yaml.UnmarshalStrict(fcontent, &l.Credentials)
  56. if err != nil {
  57. return errors.Wrapf(err, "failed unmarshaling api client credential configuration file '%s'", l.CredentialsFilePath)
  58. }
  59. if l.Credentials == nil || l.Credentials.URL == "" {
  60. return fmt.Errorf("no credentials or URL found in api client configuration '%s'", l.CredentialsFilePath)
  61. }
  62. if l.Credentials != nil && l.Credentials.URL != "" {
  63. if !strings.HasSuffix(l.Credentials.URL, "/") {
  64. l.Credentials.URL = l.Credentials.URL + "/"
  65. }
  66. }
  67. if l.InsecureSkipVerify == nil {
  68. apiclient.InsecureSkipVerify = false
  69. } else {
  70. apiclient.InsecureSkipVerify = *l.InsecureSkipVerify
  71. }
  72. return nil
  73. }
  74. func (lapiCfg *LocalApiServerCfg) GetTrustedIPs() ([]net.IPNet, error) {
  75. trustedIPs := make([]net.IPNet, 0)
  76. for _, ip := range lapiCfg.TrustedIPs {
  77. cidr := toValidCIDR(ip)
  78. _, ipNet, err := net.ParseCIDR(cidr)
  79. if err != nil {
  80. return nil, err
  81. }
  82. trustedIPs = append(trustedIPs, *ipNet)
  83. }
  84. return trustedIPs, nil
  85. }
  86. func toValidCIDR(ip string) string {
  87. if strings.Contains(ip, "/") {
  88. return ip
  89. }
  90. if strings.Contains(ip, ":") {
  91. return ip + "/128"
  92. }
  93. return ip + "/32"
  94. }
  95. /*local api service configuration*/
  96. type LocalApiServerCfg struct {
  97. ListenURI string `yaml:"listen_uri,omitempty"` //127.0.0.1:8080
  98. TLS *TLSCfg `yaml:"tls"`
  99. DbConfig *DatabaseCfg `yaml:"-"`
  100. LogDir string `yaml:"-"`
  101. LogMedia string `yaml:"-"`
  102. OnlineClient *OnlineApiClientCfg `yaml:"online_client"`
  103. ProfilesPath string `yaml:"profiles_path,omitempty"`
  104. ConsoleConfigPath string `yaml:"console_path,omitempty"`
  105. ConsoleConfig *ConsoleConfig `yaml:"-"`
  106. Profiles []*ProfileCfg `yaml:"-"`
  107. LogLevel *log.Level `yaml:"log_level"`
  108. UseForwardedForHeaders bool `yaml:"use_forwarded_for_headers,omitempty"`
  109. TrustedProxies *[]string `yaml:"trusted_proxies,omitempty"`
  110. CompressLogs *bool `yaml:"-"`
  111. LogMaxSize int `yaml:"-"`
  112. LogMaxAge int `yaml:"-"`
  113. LogMaxFiles int `yaml:"-"`
  114. TrustedIPs []string `yaml:"trusted_ips,omitempty"`
  115. }
  116. type TLSCfg struct {
  117. CertFilePath string `yaml:"cert_file"`
  118. KeyFilePath string `yaml:"key_file"`
  119. }
  120. func (c *Config) LoadAPIServer() error {
  121. if c.API.Server != nil && !c.DisableAPI {
  122. if err := c.LoadCommon(); err != nil {
  123. return fmt.Errorf("loading common configuration: %s", err.Error())
  124. }
  125. c.API.Server.LogDir = c.Common.LogDir
  126. c.API.Server.LogMedia = c.Common.LogMedia
  127. c.API.Server.CompressLogs = c.Common.CompressLogs
  128. c.API.Server.LogMaxSize = c.Common.LogMaxSize
  129. c.API.Server.LogMaxAge = c.Common.LogMaxAge
  130. c.API.Server.LogMaxFiles = c.Common.LogMaxFiles
  131. if c.API.Server.UseForwardedForHeaders && c.API.Server.TrustedProxies == nil {
  132. c.API.Server.TrustedProxies = &[]string{"0.0.0.0/0"}
  133. }
  134. if c.API.Server.TrustedProxies != nil {
  135. c.API.Server.UseForwardedForHeaders = true
  136. }
  137. if err := c.API.Server.LoadProfiles(); err != nil {
  138. return errors.Wrap(err, "while loading profiles for LAPI")
  139. }
  140. if c.API.Server.ConsoleConfigPath == "" {
  141. c.API.Server.ConsoleConfigPath = DefaultConsoleConfigFilePath
  142. }
  143. if err := c.API.Server.LoadConsoleConfig(); err != nil {
  144. return errors.Wrap(err, "while loading console options")
  145. }
  146. if c.API.Server.OnlineClient != nil && c.API.Server.OnlineClient.CredentialsFilePath != "" {
  147. if err := c.API.Server.OnlineClient.Load(); err != nil {
  148. return errors.Wrap(err, "loading online client credentials")
  149. }
  150. }
  151. if c.API.Server.OnlineClient == nil || c.API.Server.OnlineClient.Credentials == nil {
  152. log.Printf("push and pull to Central API disabled")
  153. }
  154. if err := c.LoadDBConfig(); err != nil {
  155. return err
  156. }
  157. } else {
  158. log.Warningf("crowdsec local API is disabled")
  159. c.DisableAPI = true
  160. }
  161. return nil
  162. }
  163. func (c *Config) LoadAPIClient() error {
  164. if c.API == nil || c.API.Client == nil || c.API.Client.CredentialsFilePath == "" || c.DisableAgent {
  165. return fmt.Errorf("no API client section in configuration")
  166. }
  167. if err := c.API.Client.Load(); err != nil {
  168. return err
  169. }
  170. return nil
  171. }