api.go 7.7 KB

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