api_key.go 6.6 KB

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