api.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 errors.Wrapf(err, "failed to read api client credential configuration file '%s'", l.CredentialsFilePath)
  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. if !strings.HasSuffix(l.Credentials.URL, "/") {
  61. l.Credentials.URL = l.Credentials.URL + "/"
  62. }
  63. } else {
  64. log.Warningf("no credentials or URL found in api client configuration '%s'", l.CredentialsFilePath)
  65. }
  66. if l.InsecureSkipVerify == nil {
  67. apiclient.InsecureSkipVerify = false
  68. } else {
  69. apiclient.InsecureSkipVerify = *l.InsecureSkipVerify
  70. }
  71. return nil
  72. }
  73. func (lapiCfg *LocalApiServerCfg) GetTrustedIPs() ([]net.IPNet, error) {
  74. trustedIPs := make([]net.IPNet, 0)
  75. for _, ip := range lapiCfg.TrustedIPs {
  76. cidr := toValidCIDR(ip)
  77. _, ipNet, err := net.ParseCIDR(cidr)
  78. if err != nil {
  79. return nil, err
  80. }
  81. trustedIPs = append(trustedIPs, *ipNet)
  82. }
  83. return trustedIPs, nil
  84. }
  85. func toValidCIDR(ip string) string {
  86. if strings.Contains(ip, "/") {
  87. return ip
  88. }
  89. if strings.Contains(ip, ":") {
  90. return ip + "/128"
  91. }
  92. return ip + "/32"
  93. }
  94. /*local api service configuration*/
  95. type LocalApiServerCfg struct {
  96. ListenURI string `yaml:"listen_uri,omitempty"` //127.0.0.1:8080
  97. TLS *TLSCfg `yaml:"tls"`
  98. DbConfig *DatabaseCfg `yaml:"-"`
  99. LogDir string `yaml:"-"`
  100. LogMedia string `yaml:"-"`
  101. OnlineClient *OnlineApiClientCfg `yaml:"online_client"`
  102. ProfilesPath string `yaml:"profiles_path,omitempty"`
  103. ConsoleConfigPath string `yaml:"console_path,omitempty"`
  104. ConsoleConfig *ConsoleConfig `yaml:"-"`
  105. Profiles []*ProfileCfg `yaml:"-"`
  106. LogLevel *log.Level `yaml:"log_level"`
  107. UseForwardedForHeaders bool `yaml:"use_forwarded_for_headers,omitempty"`
  108. TrustedProxies *[]string `yaml:"trusted_proxies,omitempty"`
  109. CompressLogs *bool `yaml:"-"`
  110. LogMaxSize int `yaml:"-"`
  111. LogMaxAge int `yaml:"-"`
  112. LogMaxFiles int `yaml:"-"`
  113. TrustedIPs []string `yaml:"trusted_ips,omitempty"`
  114. }
  115. type TLSCfg struct {
  116. CertFilePath string `yaml:"cert_file"`
  117. KeyFilePath string `yaml:"key_file"`
  118. }
  119. func (c *Config) LoadAPIServer() error {
  120. if c.API.Server != nil && !c.DisableAPI {
  121. if err := c.LoadCommon(); err != nil {
  122. return fmt.Errorf("loading common configuration: %s", err.Error())
  123. }
  124. c.API.Server.LogDir = c.Common.LogDir
  125. c.API.Server.LogMedia = c.Common.LogMedia
  126. c.API.Server.CompressLogs = c.Common.CompressLogs
  127. c.API.Server.LogMaxSize = c.Common.LogMaxSize
  128. c.API.Server.LogMaxAge = c.Common.LogMaxAge
  129. c.API.Server.LogMaxFiles = c.Common.LogMaxFiles
  130. if c.API.Server.UseForwardedForHeaders && c.API.Server.TrustedProxies == nil {
  131. c.API.Server.TrustedProxies = &[]string{"0.0.0.0/0"}
  132. }
  133. if c.API.Server.TrustedProxies != nil {
  134. c.API.Server.UseForwardedForHeaders = true
  135. }
  136. if err := c.API.Server.LoadProfiles(); err != nil {
  137. return errors.Wrap(err, "while loading profiles for LAPI")
  138. }
  139. if c.API.Server.ConsoleConfigPath == "" {
  140. c.API.Server.ConsoleConfigPath = DefaultConsoleConfigFilePath
  141. }
  142. if err := c.API.Server.LoadConsoleConfig(); err != nil {
  143. return errors.Wrap(err, "while loading console options")
  144. }
  145. if c.API.Server.OnlineClient != nil && c.API.Server.OnlineClient.CredentialsFilePath != "" {
  146. if err := c.API.Server.OnlineClient.Load(); err != nil {
  147. return errors.Wrap(err, "loading online client credentials")
  148. }
  149. }
  150. if c.API.Server.OnlineClient == nil || c.API.Server.OnlineClient.Credentials == nil {
  151. log.Printf("push and pull to Central API disabled")
  152. }
  153. if err := c.LoadDBConfig(); err != nil {
  154. return err
  155. }
  156. } else {
  157. log.Warningf("crowdsec local API is disabled")
  158. c.DisableAPI = true
  159. }
  160. return nil
  161. }
  162. func (c *Config) LoadAPIClient() error {
  163. if c.API != nil && c.API.Client != nil && c.API.Client.CredentialsFilePath != "" && !c.DisableAgent {
  164. if err := c.API.Client.Load(); err != nil {
  165. return err
  166. }
  167. } else {
  168. return fmt.Errorf("no API client section in configuration")
  169. }
  170. return nil
  171. }