api_key.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package v1
  2. import (
  3. "crypto/rand"
  4. "crypto/sha512"
  5. "encoding/base64"
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. "github.com/gin-gonic/gin"
  10. log "github.com/sirupsen/logrus"
  11. "github.com/crowdsecurity/crowdsec/pkg/database"
  12. "github.com/crowdsecurity/crowdsec/pkg/database/ent"
  13. "github.com/crowdsecurity/crowdsec/pkg/types"
  14. )
  15. const (
  16. APIKeyHeader = "X-Api-Key"
  17. bouncerContextKey = "bouncer_info"
  18. // max allowed by bcrypt 72 = 54 bytes in base64
  19. dummyAPIKeySize = 54
  20. )
  21. type APIKey struct {
  22. HeaderName string
  23. DbClient *database.Client
  24. TlsAuth *TLSAuth
  25. }
  26. func GenerateAPIKey(n int) (string, error) {
  27. bytes := make([]byte, n)
  28. if _, err := rand.Read(bytes); err != nil {
  29. return "", err
  30. }
  31. encoded := base64.StdEncoding.EncodeToString(bytes)
  32. // the '=' can cause issues on some bouncers
  33. return strings.TrimRight(encoded, "="), nil
  34. }
  35. func NewAPIKey(dbClient *database.Client) *APIKey {
  36. return &APIKey{
  37. HeaderName: APIKeyHeader,
  38. DbClient: dbClient,
  39. TlsAuth: &TLSAuth{},
  40. }
  41. }
  42. func HashSHA512(str string) string {
  43. hashedKey := sha512.New()
  44. hashedKey.Write([]byte(str))
  45. hashStr := fmt.Sprintf("%x", hashedKey.Sum(nil))
  46. return hashStr
  47. }
  48. func (a *APIKey) authTLS(c *gin.Context, logger *log.Entry) *ent.Bouncer {
  49. if a.TlsAuth == nil {
  50. logger.Error("TLS Auth is not configured but client presented a certificate")
  51. return nil
  52. }
  53. validCert, extractedCN, err := a.TlsAuth.ValidateCert(c)
  54. if !validCert {
  55. logger.Error(err)
  56. return nil
  57. }
  58. if err != nil {
  59. logger.Error(err)
  60. return nil
  61. }
  62. logger = logger.WithFields(log.Fields{
  63. "cn": extractedCN,
  64. })
  65. bouncerName := fmt.Sprintf("%s@%s", extractedCN, c.ClientIP())
  66. bouncer, err := a.DbClient.SelectBouncerByName(bouncerName)
  67. //This is likely not the proper way, but isNotFound does not seem to work
  68. if err != nil && strings.Contains(err.Error(), "bouncer not found") {
  69. //Because we have a valid cert, automatically create the bouncer in the database if it does not exist
  70. //Set a random API key, but it will never be used
  71. apiKey, err := GenerateAPIKey(dummyAPIKeySize)
  72. if err != nil {
  73. logger.Errorf("error generating mock api key: %s", err)
  74. return nil
  75. }
  76. logger.Infof("Creating bouncer %s", bouncerName)
  77. bouncer, err = a.DbClient.CreateBouncer(bouncerName, c.ClientIP(), HashSHA512(apiKey), types.TlsAuthType)
  78. if err != nil {
  79. logger.Errorf("while creating bouncer db entry: %s", err)
  80. return nil
  81. }
  82. } else if err != nil {
  83. //error while selecting bouncer
  84. logger.Errorf("while selecting bouncers: %s", err)
  85. return nil
  86. } else if bouncer.AuthType != types.TlsAuthType {
  87. //bouncer was found in DB
  88. logger.Errorf("bouncer isn't allowed to auth by TLS")
  89. return nil
  90. }
  91. return bouncer
  92. }
  93. func (a *APIKey) authPlain(c *gin.Context, logger *log.Entry) *ent.Bouncer {
  94. val, ok := c.Request.Header[APIKeyHeader]
  95. if !ok {
  96. logger.Errorf("API key not found")
  97. return nil
  98. }
  99. hashStr := HashSHA512(val[0])
  100. bouncer, err := a.DbClient.SelectBouncer(hashStr)
  101. if err != nil {
  102. logger.Errorf("while fetching bouncer info: %s", err)
  103. return nil
  104. }
  105. if bouncer.AuthType != types.ApiKeyAuthType {
  106. logger.Errorf("bouncer %s attempted to login using an API key but it is configured to auth with %s", bouncer.Name, bouncer.AuthType)
  107. return nil
  108. }
  109. return bouncer
  110. }
  111. func (a *APIKey) MiddlewareFunc() gin.HandlerFunc {
  112. return func(c *gin.Context) {
  113. var bouncer *ent.Bouncer
  114. logger := log.WithFields(log.Fields{
  115. "ip": c.ClientIP(),
  116. })
  117. if c.Request.TLS != nil && len(c.Request.TLS.PeerCertificates) > 0 {
  118. bouncer = a.authTLS(c, logger)
  119. } else {
  120. bouncer = a.authPlain(c, logger)
  121. }
  122. if bouncer == nil {
  123. c.JSON(http.StatusForbidden, gin.H{"message": "access forbidden"})
  124. c.Abort()
  125. return
  126. }
  127. logger = logger.WithFields(log.Fields{
  128. "name": bouncer.Name,
  129. })
  130. // maybe we want to store the whole bouncer object in the context instead, this would avoid another db query
  131. // in StreamDecision
  132. c.Set("BOUNCER_NAME", bouncer.Name)
  133. c.Set("BOUNCER_HASHED_KEY", bouncer.APIKey)
  134. if bouncer.IPAddress == "" {
  135. if err := a.DbClient.UpdateBouncerIP(c.ClientIP(), bouncer.ID); err != nil {
  136. logger.Errorf("Failed to update ip address for '%s': %s\n", bouncer.Name, err)
  137. c.JSON(http.StatusForbidden, gin.H{"message": "access forbidden"})
  138. c.Abort()
  139. return
  140. }
  141. }
  142. //Don't update IP on HEAD request, as it's used by the appsec to check the validity of the API key provided
  143. if bouncer.IPAddress != c.ClientIP() && bouncer.IPAddress != "" && c.Request.Method != http.MethodHead {
  144. log.Warningf("new IP address detected for bouncer '%s': %s (old: %s)", bouncer.Name, c.ClientIP(), bouncer.IPAddress)
  145. if err := a.DbClient.UpdateBouncerIP(c.ClientIP(), bouncer.ID); err != nil {
  146. logger.Errorf("Failed to update ip address for '%s': %s\n", bouncer.Name, err)
  147. c.JSON(http.StatusForbidden, gin.H{"message": "access forbidden"})
  148. c.Abort()
  149. return
  150. }
  151. }
  152. useragent := strings.Split(c.Request.UserAgent(), "/")
  153. if len(useragent) != 2 {
  154. logger.Warningf("bad user agent '%s'", c.Request.UserAgent())
  155. useragent = []string{c.Request.UserAgent(), "N/A"}
  156. }
  157. if bouncer.Version != useragent[1] || bouncer.Type != useragent[0] {
  158. if err := a.DbClient.UpdateBouncerTypeAndVersion(useragent[0], useragent[1], bouncer.ID); err != nil {
  159. logger.Errorf("failed to update bouncer version and type: %s", err)
  160. c.JSON(http.StatusForbidden, gin.H{"message": "bad user agent"})
  161. c.Abort()
  162. return
  163. }
  164. }
  165. c.Set(bouncerContextKey, bouncer)
  166. c.Next()
  167. }
  168. }