api.go 5.7 KB

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