middleware.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package middleware
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/Unknwon/macaron"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/components/apikeygen"
  9. "github.com/grafana/grafana/pkg/log"
  10. "github.com/grafana/grafana/pkg/metrics"
  11. m "github.com/grafana/grafana/pkg/models"
  12. "github.com/grafana/grafana/pkg/setting"
  13. )
  14. type Context struct {
  15. *macaron.Context
  16. *m.SignedInUser
  17. Session SessionStore
  18. IsSignedIn bool
  19. AllowAnonymous bool
  20. }
  21. func GetContextHandler() macaron.Handler {
  22. return func(c *macaron.Context) {
  23. ctx := &Context{
  24. Context: c,
  25. SignedInUser: &m.SignedInUser{},
  26. Session: GetSession(),
  27. IsSignedIn: false,
  28. AllowAnonymous: false,
  29. }
  30. // the order in which these are tested are important
  31. // look for api key in Authorization header first
  32. // then init session and look for userId in session
  33. // then look for api key in session (special case for render calls via api)
  34. // then test if anonymous access is enabled
  35. if initContextWithApiKey(ctx) ||
  36. initContextWithUserSessionCookie(ctx) ||
  37. initContextWithApiKeyFromSession(ctx) ||
  38. initContextWithAnonymousUser(ctx) {
  39. }
  40. c.Map(ctx)
  41. }
  42. }
  43. func initContextWithAnonymousUser(ctx *Context) bool {
  44. if !setting.AnonymousEnabled {
  45. return false
  46. }
  47. orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
  48. if err := bus.Dispatch(&orgQuery); err != nil {
  49. log.Error(3, "Anonymous access organization error: '%s': %s", setting.AnonymousOrgName, err)
  50. return false
  51. } else {
  52. ctx.IsSignedIn = false
  53. ctx.AllowAnonymous = true
  54. ctx.SignedInUser = &m.SignedInUser{}
  55. ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
  56. ctx.OrgId = orgQuery.Result.Id
  57. ctx.OrgName = orgQuery.Result.Name
  58. return true
  59. }
  60. }
  61. func initContextWithUserSessionCookie(ctx *Context) bool {
  62. // initialize session
  63. if err := ctx.Session.Start(ctx); err != nil {
  64. log.Error(3, "Failed to start session", err)
  65. return false
  66. }
  67. var userId int64
  68. if userId = getRequestUserId(ctx); userId == 0 {
  69. fmt.Printf("Not userId")
  70. return false
  71. }
  72. query := m.GetSignedInUserQuery{UserId: userId}
  73. if err := bus.Dispatch(&query); err != nil {
  74. log.Error(3, "Failed to get user with id %v", userId)
  75. return false
  76. } else {
  77. ctx.SignedInUser = query.Result
  78. ctx.IsSignedIn = true
  79. return true
  80. }
  81. }
  82. func initContextWithApiKey(ctx *Context) bool {
  83. var keyString string
  84. if keyString = getApiKey(ctx); keyString == "" {
  85. return false
  86. }
  87. // base64 decode key
  88. decoded, err := apikeygen.Decode(keyString)
  89. if err != nil {
  90. ctx.JsonApiErr(401, "Invalid API key", err)
  91. return true
  92. }
  93. // fetch key
  94. keyQuery := m.GetApiKeyByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
  95. if err := bus.Dispatch(&keyQuery); err != nil {
  96. ctx.JsonApiErr(401, "Invalid API key", err)
  97. return true
  98. } else {
  99. apikey := keyQuery.Result
  100. // validate api key
  101. if !apikeygen.IsValid(decoded, apikey.Key) {
  102. ctx.JsonApiErr(401, "Invalid API key", err)
  103. return true
  104. }
  105. ctx.IsSignedIn = true
  106. ctx.SignedInUser = &m.SignedInUser{}
  107. ctx.OrgRole = apikey.Role
  108. ctx.ApiKeyId = apikey.Id
  109. ctx.OrgId = apikey.OrgId
  110. return true
  111. }
  112. }
  113. // special case for panel render calls with api key
  114. func initContextWithApiKeyFromSession(ctx *Context) bool {
  115. keyId := ctx.Session.Get(SESS_KEY_APIKEY)
  116. if keyId == nil {
  117. return false
  118. }
  119. keyQuery := m.GetApiKeyByIdQuery{ApiKeyId: keyId.(int64)}
  120. if err := bus.Dispatch(&keyQuery); err != nil {
  121. log.Error(3, "Failed to get api key by id", err)
  122. return false
  123. } else {
  124. apikey := keyQuery.Result
  125. ctx.IsSignedIn = true
  126. ctx.SignedInUser = &m.SignedInUser{}
  127. ctx.OrgRole = apikey.Role
  128. ctx.ApiKeyId = apikey.Id
  129. ctx.OrgId = apikey.OrgId
  130. return true
  131. }
  132. }
  133. // Handle handles and logs error by given status.
  134. func (ctx *Context) Handle(status int, title string, err error) {
  135. if err != nil {
  136. log.Error(4, "%s: %v", title, err)
  137. if setting.Env != setting.PROD {
  138. ctx.Data["ErrorMsg"] = err
  139. }
  140. }
  141. switch status {
  142. case 200:
  143. metrics.M_Page_Status_200.Inc(1)
  144. case 404:
  145. metrics.M_Page_Status_404.Inc(1)
  146. case 500:
  147. metrics.M_Page_Status_500.Inc(1)
  148. }
  149. ctx.Data["Title"] = title
  150. ctx.HTML(status, strconv.Itoa(status))
  151. }
  152. func (ctx *Context) JsonOK(message string) {
  153. resp := make(map[string]interface{})
  154. resp["message"] = message
  155. ctx.JSON(200, resp)
  156. }
  157. func (ctx *Context) IsApiRequest() bool {
  158. return strings.HasPrefix(ctx.Req.URL.Path, "/api")
  159. }
  160. func (ctx *Context) JsonApiErr(status int, message string, err error) {
  161. resp := make(map[string]interface{})
  162. if err != nil {
  163. log.Error(4, "%s: %v", message, err)
  164. if setting.Env != setting.PROD {
  165. resp["error"] = err.Error()
  166. }
  167. }
  168. switch status {
  169. case 404:
  170. resp["message"] = "Not Found"
  171. metrics.M_Api_Status_500.Inc(1)
  172. case 500:
  173. metrics.M_Api_Status_404.Inc(1)
  174. resp["message"] = "Internal Server Error"
  175. }
  176. if message != "" {
  177. resp["message"] = message
  178. }
  179. ctx.JSON(status, resp)
  180. }