api.go 6.3 KB

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