middleware_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. package middleware
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "net/http/httptest"
  8. "path/filepath"
  9. "testing"
  10. "time"
  11. "github.com/grafana/grafana/pkg/api/dtos"
  12. "github.com/grafana/grafana/pkg/bus"
  13. "github.com/grafana/grafana/pkg/infra/remotecache"
  14. m "github.com/grafana/grafana/pkg/models"
  15. "github.com/grafana/grafana/pkg/services/auth"
  16. "github.com/grafana/grafana/pkg/setting"
  17. "github.com/grafana/grafana/pkg/util"
  18. . "github.com/smartystreets/goconvey/convey"
  19. "gopkg.in/macaron.v1"
  20. )
  21. func TestMiddlewareContext(t *testing.T) {
  22. setting.ERR_TEMPLATE_NAME = "error-template"
  23. Convey("Given the grafana middleware", t, func() {
  24. middlewareScenario(t, "middleware should add context to injector", func(sc *scenarioContext) {
  25. sc.fakeReq("GET", "/").exec()
  26. So(sc.context, ShouldNotBeNil)
  27. })
  28. middlewareScenario(t, "Default middleware should allow get request", func(sc *scenarioContext) {
  29. sc.fakeReq("GET", "/").exec()
  30. So(sc.resp.Code, ShouldEqual, 200)
  31. })
  32. middlewareScenario(t, "middleware should add Cache-Control header for requests to API", func(sc *scenarioContext) {
  33. sc.fakeReq("GET", "/api/search").exec()
  34. So(sc.resp.Header().Get("Cache-Control"), ShouldEqual, "no-cache")
  35. So(sc.resp.Header().Get("Pragma"), ShouldEqual, "no-cache")
  36. So(sc.resp.Header().Get("Expires"), ShouldEqual, "-1")
  37. })
  38. middlewareScenario(t, "middleware should not add Cache-Control header for requests to datasource proxy API", func(sc *scenarioContext) {
  39. sc.fakeReq("GET", "/api/datasources/proxy/1/test").exec()
  40. So(sc.resp.Header().Get("Cache-Control"), ShouldBeEmpty)
  41. So(sc.resp.Header().Get("Pragma"), ShouldBeEmpty)
  42. So(sc.resp.Header().Get("Expires"), ShouldBeEmpty)
  43. })
  44. middlewareScenario(t, "middleware should add Cache-Control header for GET requests with html response", func(sc *scenarioContext) {
  45. sc.handler(func(c *m.ReqContext) {
  46. data := &dtos.IndexViewData{
  47. User: &dtos.CurrentUser{},
  48. Settings: map[string]interface{}{},
  49. NavTree: []*dtos.NavLink{},
  50. }
  51. c.HTML(200, "index-template", data)
  52. })
  53. sc.fakeReq("GET", "/").exec()
  54. So(sc.resp.Code, ShouldEqual, 200)
  55. So(sc.resp.Header().Get("Cache-Control"), ShouldEqual, "no-cache")
  56. So(sc.resp.Header().Get("Pragma"), ShouldEqual, "no-cache")
  57. So(sc.resp.Header().Get("Expires"), ShouldEqual, "-1")
  58. })
  59. middlewareScenario(t, "Invalid api key", func(sc *scenarioContext) {
  60. sc.apiKey = "invalid_key_test"
  61. sc.fakeReq("GET", "/").exec()
  62. Convey("Should not init session", func() {
  63. So(sc.resp.Header().Get("Set-Cookie"), ShouldBeEmpty)
  64. })
  65. Convey("Should return 401", func() {
  66. So(sc.resp.Code, ShouldEqual, 401)
  67. So(sc.respJson["message"], ShouldEqual, "Invalid API key")
  68. })
  69. })
  70. middlewareScenario(t, "Using basic auth", func(sc *scenarioContext) {
  71. bus.AddHandler("test", func(query *m.GetUserByLoginQuery) error {
  72. query.Result = &m.User{
  73. Password: util.EncodePassword("myPass", "salt"),
  74. Salt: "salt",
  75. }
  76. return nil
  77. })
  78. bus.AddHandler("test", func(loginUserQuery *m.LoginUserQuery) error {
  79. return nil
  80. })
  81. bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
  82. query.Result = &m.SignedInUser{OrgId: 2, UserId: 12}
  83. return nil
  84. })
  85. setting.BasicAuthEnabled = true
  86. authHeader := util.GetBasicAuthHeader("myUser", "myPass")
  87. sc.fakeReq("GET", "/").withAuthorizationHeader(authHeader).exec()
  88. Convey("Should init middleware context with user", func() {
  89. So(sc.context.IsSignedIn, ShouldEqual, true)
  90. So(sc.context.OrgId, ShouldEqual, 2)
  91. So(sc.context.UserId, ShouldEqual, 12)
  92. })
  93. })
  94. middlewareScenario(t, "Valid api key", func(sc *scenarioContext) {
  95. keyhash := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd")
  96. bus.AddHandler("test", func(query *m.GetApiKeyByNameQuery) error {
  97. query.Result = &m.ApiKey{OrgId: 12, Role: m.ROLE_EDITOR, Key: keyhash}
  98. return nil
  99. })
  100. sc.fakeReq("GET", "/").withValidApiKey().exec()
  101. Convey("Should return 200", func() {
  102. So(sc.resp.Code, ShouldEqual, 200)
  103. })
  104. Convey("Should init middleware context", func() {
  105. So(sc.context.IsSignedIn, ShouldEqual, true)
  106. So(sc.context.OrgId, ShouldEqual, 12)
  107. So(sc.context.OrgRole, ShouldEqual, m.ROLE_EDITOR)
  108. })
  109. })
  110. middlewareScenario(t, "Valid api key, but does not match db hash", func(sc *scenarioContext) {
  111. keyhash := "something_not_matching"
  112. bus.AddHandler("test", func(query *m.GetApiKeyByNameQuery) error {
  113. query.Result = &m.ApiKey{OrgId: 12, Role: m.ROLE_EDITOR, Key: keyhash}
  114. return nil
  115. })
  116. sc.fakeReq("GET", "/").withValidApiKey().exec()
  117. Convey("Should return api key invalid", func() {
  118. So(sc.resp.Code, ShouldEqual, 401)
  119. So(sc.respJson["message"], ShouldEqual, "Invalid API key")
  120. })
  121. })
  122. middlewareScenario(t, "Valid api key via Basic auth", func(sc *scenarioContext) {
  123. keyhash := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd")
  124. bus.AddHandler("test", func(query *m.GetApiKeyByNameQuery) error {
  125. query.Result = &m.ApiKey{OrgId: 12, Role: m.ROLE_EDITOR, Key: keyhash}
  126. return nil
  127. })
  128. authHeader := util.GetBasicAuthHeader("api_key", "eyJrIjoidjVuQXdwTWFmRlA2em5hUzR1cmhkV0RMUzU1MTFNNDIiLCJuIjoiYXNkIiwiaWQiOjF9")
  129. sc.fakeReq("GET", "/").withAuthorizationHeader(authHeader).exec()
  130. Convey("Should return 200", func() {
  131. So(sc.resp.Code, ShouldEqual, 200)
  132. })
  133. Convey("Should init middleware context", func() {
  134. So(sc.context.IsSignedIn, ShouldEqual, true)
  135. So(sc.context.OrgId, ShouldEqual, 12)
  136. So(sc.context.OrgRole, ShouldEqual, m.ROLE_EDITOR)
  137. })
  138. })
  139. middlewareScenario(t, "Non-expired auth token in cookie which not are being rotated", func(sc *scenarioContext) {
  140. sc.withTokenSessionCookie("token")
  141. bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
  142. query.Result = &m.SignedInUser{OrgId: 2, UserId: 12}
  143. return nil
  144. })
  145. sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*m.UserToken, error) {
  146. return &m.UserToken{
  147. UserId: 12,
  148. UnhashedToken: unhashedToken,
  149. }, nil
  150. }
  151. sc.fakeReq("GET", "/").exec()
  152. Convey("should init context with user info", func() {
  153. So(sc.context.IsSignedIn, ShouldBeTrue)
  154. So(sc.context.UserId, ShouldEqual, 12)
  155. So(sc.context.UserToken.UserId, ShouldEqual, 12)
  156. So(sc.context.UserToken.UnhashedToken, ShouldEqual, "token")
  157. })
  158. Convey("should not set cookie", func() {
  159. So(sc.resp.Header().Get("Set-Cookie"), ShouldEqual, "")
  160. })
  161. })
  162. middlewareScenario(t, "Non-expired auth token in cookie which are being rotated", func(sc *scenarioContext) {
  163. sc.withTokenSessionCookie("token")
  164. bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
  165. query.Result = &m.SignedInUser{OrgId: 2, UserId: 12}
  166. return nil
  167. })
  168. sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*m.UserToken, error) {
  169. return &m.UserToken{
  170. UserId: 12,
  171. UnhashedToken: "",
  172. }, nil
  173. }
  174. sc.userAuthTokenService.TryRotateTokenProvider = func(ctx context.Context, userToken *m.UserToken, clientIP, userAgent string) (bool, error) {
  175. userToken.UnhashedToken = "rotated"
  176. return true, nil
  177. }
  178. maxAgeHours := (time.Duration(setting.LoginMaxLifetimeDays) * 24 * time.Hour)
  179. maxAge := (maxAgeHours + time.Hour).Seconds()
  180. expectedCookie := &http.Cookie{
  181. Name: setting.LoginCookieName,
  182. Value: "rotated",
  183. Path: setting.AppSubUrl + "/",
  184. HttpOnly: true,
  185. MaxAge: int(maxAge),
  186. Secure: setting.CookieSecure,
  187. SameSite: setting.CookieSameSite,
  188. }
  189. sc.fakeReq("GET", "/").exec()
  190. Convey("should init context with user info", func() {
  191. So(sc.context.IsSignedIn, ShouldBeTrue)
  192. So(sc.context.UserId, ShouldEqual, 12)
  193. So(sc.context.UserToken.UserId, ShouldEqual, 12)
  194. So(sc.context.UserToken.UnhashedToken, ShouldEqual, "rotated")
  195. })
  196. Convey("should set cookie", func() {
  197. So(sc.resp.Header().Get("Set-Cookie"), ShouldEqual, expectedCookie.String())
  198. })
  199. })
  200. middlewareScenario(t, "Invalid/expired auth token in cookie", func(sc *scenarioContext) {
  201. sc.withTokenSessionCookie("token")
  202. sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*m.UserToken, error) {
  203. return nil, m.ErrUserTokenNotFound
  204. }
  205. sc.fakeReq("GET", "/").exec()
  206. Convey("should not init context with user info", func() {
  207. So(sc.context.IsSignedIn, ShouldBeFalse)
  208. So(sc.context.UserId, ShouldEqual, 0)
  209. So(sc.context.UserToken, ShouldBeNil)
  210. })
  211. })
  212. middlewareScenario(t, "When anonymous access is enabled", func(sc *scenarioContext) {
  213. setting.AnonymousEnabled = true
  214. setting.AnonymousOrgName = "test"
  215. setting.AnonymousOrgRole = string(m.ROLE_EDITOR)
  216. bus.AddHandler("test", func(query *m.GetOrgByNameQuery) error {
  217. So(query.Name, ShouldEqual, "test")
  218. query.Result = &m.Org{Id: 2, Name: "test"}
  219. return nil
  220. })
  221. sc.fakeReq("GET", "/").exec()
  222. Convey("should init context with org info", func() {
  223. So(sc.context.UserId, ShouldEqual, 0)
  224. So(sc.context.OrgId, ShouldEqual, 2)
  225. So(sc.context.OrgRole, ShouldEqual, m.ROLE_EDITOR)
  226. })
  227. Convey("context signed in should be false", func() {
  228. So(sc.context.IsSignedIn, ShouldBeFalse)
  229. })
  230. })
  231. Convey("auth_proxy", func() {
  232. setting.AuthProxyEnabled = true
  233. setting.AuthProxyWhitelist = ""
  234. setting.AuthProxyAutoSignUp = true
  235. setting.LdapEnabled = true
  236. setting.AuthProxyHeaderName = "X-WEBAUTH-USER"
  237. setting.AuthProxyHeaderProperty = "username"
  238. name := "markelog"
  239. middlewareScenario(t, "should not sync the user if it's in the cache", func(sc *scenarioContext) {
  240. bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
  241. query.Result = &m.SignedInUser{OrgId: 4, UserId: query.UserId}
  242. return nil
  243. })
  244. key := fmt.Sprintf(cachePrefix, name)
  245. sc.remoteCacheService.Set(key, int64(33), 0)
  246. sc.fakeReq("GET", "/")
  247. sc.req.Header.Add(setting.AuthProxyHeaderName, name)
  248. sc.exec()
  249. Convey("Should init user via cache", func() {
  250. So(sc.context.IsSignedIn, ShouldBeTrue)
  251. So(sc.context.UserId, ShouldEqual, 33)
  252. So(sc.context.OrgId, ShouldEqual, 4)
  253. })
  254. })
  255. middlewareScenario(t, "should create an user from a header", func(sc *scenarioContext) {
  256. setting.LdapEnabled = false
  257. setting.AuthProxyAutoSignUp = true
  258. bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
  259. if query.UserId > 0 {
  260. query.Result = &m.SignedInUser{OrgId: 4, UserId: 33}
  261. return nil
  262. }
  263. return m.ErrUserNotFound
  264. })
  265. bus.AddHandler("test", func(cmd *m.UpsertUserCommand) error {
  266. cmd.Result = &m.User{Id: 33}
  267. return nil
  268. })
  269. sc.fakeReq("GET", "/")
  270. sc.req.Header.Add(setting.AuthProxyHeaderName, name)
  271. sc.exec()
  272. Convey("Should create user from header info", func() {
  273. So(sc.context.IsSignedIn, ShouldBeTrue)
  274. So(sc.context.UserId, ShouldEqual, 33)
  275. So(sc.context.OrgId, ShouldEqual, 4)
  276. })
  277. })
  278. middlewareScenario(t, "should get an existing user from header", func(sc *scenarioContext) {
  279. setting.LdapEnabled = false
  280. bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
  281. query.Result = &m.SignedInUser{OrgId: 2, UserId: 12}
  282. return nil
  283. })
  284. bus.AddHandler("test", func(cmd *m.UpsertUserCommand) error {
  285. cmd.Result = &m.User{Id: 12}
  286. return nil
  287. })
  288. sc.fakeReq("GET", "/")
  289. sc.req.Header.Add(setting.AuthProxyHeaderName, name)
  290. sc.exec()
  291. Convey("should init context with user info", func() {
  292. So(sc.context.IsSignedIn, ShouldBeTrue)
  293. So(sc.context.UserId, ShouldEqual, 12)
  294. So(sc.context.OrgId, ShouldEqual, 2)
  295. })
  296. })
  297. middlewareScenario(t, "should allow the request from whitelist IP", func(sc *scenarioContext) {
  298. setting.AuthProxyWhitelist = "192.168.1.0/24, 2001::0/120"
  299. setting.LdapEnabled = false
  300. bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
  301. query.Result = &m.SignedInUser{OrgId: 4, UserId: 33}
  302. return nil
  303. })
  304. bus.AddHandler("test", func(cmd *m.UpsertUserCommand) error {
  305. cmd.Result = &m.User{Id: 33}
  306. return nil
  307. })
  308. sc.fakeReq("GET", "/")
  309. sc.req.Header.Add(setting.AuthProxyHeaderName, name)
  310. sc.req.RemoteAddr = "[2001::23]:12345"
  311. sc.exec()
  312. Convey("Should init context with user info", func() {
  313. So(sc.context.IsSignedIn, ShouldBeTrue)
  314. So(sc.context.UserId, ShouldEqual, 33)
  315. So(sc.context.OrgId, ShouldEqual, 4)
  316. })
  317. })
  318. middlewareScenario(t, "should not allow the request from whitelist IP", func(sc *scenarioContext) {
  319. setting.AuthProxyWhitelist = "8.8.8.8"
  320. setting.LdapEnabled = false
  321. bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
  322. query.Result = &m.SignedInUser{OrgId: 4, UserId: 33}
  323. return nil
  324. })
  325. bus.AddHandler("test", func(cmd *m.UpsertUserCommand) error {
  326. cmd.Result = &m.User{Id: 33}
  327. return nil
  328. })
  329. sc.fakeReq("GET", "/")
  330. sc.req.Header.Add(setting.AuthProxyHeaderName, name)
  331. sc.req.RemoteAddr = "[2001::23]:12345"
  332. sc.exec()
  333. Convey("should return 407 status code", func() {
  334. So(sc.resp.Code, ShouldEqual, 407)
  335. So(sc.context, ShouldBeNil)
  336. })
  337. })
  338. })
  339. })
  340. }
  341. func middlewareScenario(t *testing.T, desc string, fn scenarioFunc) {
  342. Convey(desc, func() {
  343. defer bus.ClearBusHandlers()
  344. setting.LoginCookieName = "grafana_session"
  345. setting.LoginMaxLifetimeDays = 30
  346. sc := &scenarioContext{}
  347. viewsPath, _ := filepath.Abs("../../public/views")
  348. sc.m = macaron.New()
  349. sc.m.Use(AddDefaultResponseHeaders())
  350. sc.m.Use(macaron.Renderer(macaron.RenderOptions{
  351. Directory: viewsPath,
  352. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  353. }))
  354. sc.userAuthTokenService = auth.NewFakeUserAuthTokenService()
  355. sc.remoteCacheService = remotecache.NewFakeStore(t)
  356. sc.m.Use(GetContextHandler(sc.userAuthTokenService, sc.remoteCacheService))
  357. sc.m.Use(OrgRedirect())
  358. sc.defaultHandler = func(c *m.ReqContext) {
  359. sc.context = c
  360. if sc.handlerFunc != nil {
  361. sc.handlerFunc(sc.context)
  362. }
  363. }
  364. sc.m.Get("/", sc.defaultHandler)
  365. fn(sc)
  366. })
  367. }
  368. type scenarioContext struct {
  369. m *macaron.Macaron
  370. context *m.ReqContext
  371. resp *httptest.ResponseRecorder
  372. apiKey string
  373. authHeader string
  374. tokenSessionCookie string
  375. respJson map[string]interface{}
  376. handlerFunc handlerFunc
  377. defaultHandler macaron.Handler
  378. url string
  379. userAuthTokenService *auth.FakeUserAuthTokenService
  380. remoteCacheService *remotecache.RemoteCache
  381. req *http.Request
  382. }
  383. func (sc *scenarioContext) withValidApiKey() *scenarioContext {
  384. sc.apiKey = "eyJrIjoidjVuQXdwTWFmRlA2em5hUzR1cmhkV0RMUzU1MTFNNDIiLCJuIjoiYXNkIiwiaWQiOjF9"
  385. return sc
  386. }
  387. func (sc *scenarioContext) withTokenSessionCookie(unhashedToken string) *scenarioContext {
  388. sc.tokenSessionCookie = unhashedToken
  389. return sc
  390. }
  391. func (sc *scenarioContext) withAuthorizationHeader(authHeader string) *scenarioContext {
  392. sc.authHeader = authHeader
  393. return sc
  394. }
  395. func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext {
  396. sc.resp = httptest.NewRecorder()
  397. req, err := http.NewRequest(method, url, nil)
  398. So(err, ShouldBeNil)
  399. sc.req = req
  400. return sc
  401. }
  402. func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map[string]string) *scenarioContext {
  403. sc.resp = httptest.NewRecorder()
  404. req, err := http.NewRequest(method, url, nil)
  405. q := req.URL.Query()
  406. for k, v := range queryParams {
  407. q.Add(k, v)
  408. }
  409. req.URL.RawQuery = q.Encode()
  410. So(err, ShouldBeNil)
  411. sc.req = req
  412. return sc
  413. }
  414. func (sc *scenarioContext) handler(fn handlerFunc) *scenarioContext {
  415. sc.handlerFunc = fn
  416. return sc
  417. }
  418. func (sc *scenarioContext) exec() {
  419. if sc.apiKey != "" {
  420. sc.req.Header.Add("Authorization", "Bearer "+sc.apiKey)
  421. }
  422. if sc.authHeader != "" {
  423. sc.req.Header.Add("Authorization", sc.authHeader)
  424. }
  425. if sc.tokenSessionCookie != "" {
  426. sc.req.AddCookie(&http.Cookie{
  427. Name: setting.LoginCookieName,
  428. Value: sc.tokenSessionCookie,
  429. })
  430. }
  431. sc.m.ServeHTTP(sc.resp, sc.req)
  432. if sc.resp.Header().Get("Content-Type") == "application/json; charset=UTF-8" {
  433. err := json.NewDecoder(sc.resp.Body).Decode(&sc.respJson)
  434. So(err, ShouldBeNil)
  435. }
  436. }
  437. type scenarioFunc func(c *scenarioContext)
  438. type handlerFunc func(c *m.ReqContext)