ds_proxy_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. package pluginproxy
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "net/url"
  9. "testing"
  10. "time"
  11. "github.com/grafana/grafana/pkg/components/securejsondata"
  12. "golang.org/x/oauth2"
  13. macaron "gopkg.in/macaron.v1"
  14. "github.com/grafana/grafana/pkg/bus"
  15. "github.com/grafana/grafana/pkg/components/simplejson"
  16. "github.com/grafana/grafana/pkg/infra/log"
  17. "github.com/grafana/grafana/pkg/login/social"
  18. m "github.com/grafana/grafana/pkg/models"
  19. "github.com/grafana/grafana/pkg/plugins"
  20. "github.com/grafana/grafana/pkg/setting"
  21. "github.com/grafana/grafana/pkg/util"
  22. . "github.com/smartystreets/goconvey/convey"
  23. )
  24. func TestDSRouteRule(t *testing.T) {
  25. Convey("DataSourceProxy", t, func() {
  26. Convey("Plugin with routes", func() {
  27. plugin := &plugins.DataSourcePlugin{
  28. Routes: []*plugins.AppPluginRoute{
  29. {
  30. Path: "api/v4/",
  31. Url: "https://www.google.com",
  32. ReqRole: m.ROLE_EDITOR,
  33. Headers: []plugins.AppPluginRouteHeader{
  34. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  35. },
  36. },
  37. {
  38. Path: "api/admin",
  39. Url: "https://www.google.com",
  40. ReqRole: m.ROLE_ADMIN,
  41. Headers: []plugins.AppPluginRouteHeader{
  42. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  43. },
  44. },
  45. {
  46. Path: "api/anon",
  47. Url: "https://www.google.com",
  48. Headers: []plugins.AppPluginRouteHeader{
  49. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  50. },
  51. },
  52. {
  53. Path: "api/common",
  54. Url: "{{.JsonData.dynamicUrl}}",
  55. Headers: []plugins.AppPluginRouteHeader{
  56. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  57. },
  58. },
  59. },
  60. }
  61. setting.SecretKey = "password"
  62. key, _ := util.Encrypt([]byte("123"), "password")
  63. ds := &m.DataSource{
  64. JsonData: simplejson.NewFromAny(map[string]interface{}{
  65. "clientId": "asd",
  66. "dynamicUrl": "https://dynamic.grafana.com",
  67. }),
  68. SecureJsonData: map[string][]byte{
  69. "key": key,
  70. },
  71. }
  72. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  73. ctx := &m.ReqContext{
  74. Context: &macaron.Context{
  75. Req: macaron.Request{Request: req},
  76. },
  77. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR},
  78. }
  79. Convey("When matching route path", func() {
  80. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method", &setting.Cfg{})
  81. proxy.route = plugin.Routes[0]
  82. ApplyRoute(proxy.ctx.Req.Context(), req, proxy.proxyPath, proxy.route, proxy.ds)
  83. Convey("should add headers and update url", func() {
  84. So(req.URL.String(), ShouldEqual, "https://www.google.com/some/method")
  85. So(req.Header.Get("x-header"), ShouldEqual, "my secret 123")
  86. })
  87. })
  88. Convey("When matching route path and has dynamic url", func() {
  89. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/common/some/method", &setting.Cfg{})
  90. proxy.route = plugin.Routes[3]
  91. ApplyRoute(proxy.ctx.Req.Context(), req, proxy.proxyPath, proxy.route, proxy.ds)
  92. Convey("should add headers and interpolate the url", func() {
  93. So(req.URL.String(), ShouldEqual, "https://dynamic.grafana.com/some/method")
  94. So(req.Header.Get("x-header"), ShouldEqual, "my secret 123")
  95. })
  96. })
  97. Convey("Validating request", func() {
  98. Convey("plugin route with valid role", func() {
  99. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method", &setting.Cfg{})
  100. err := proxy.validateRequest()
  101. So(err, ShouldBeNil)
  102. })
  103. Convey("plugin route with admin role and user is editor", func() {
  104. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin", &setting.Cfg{})
  105. err := proxy.validateRequest()
  106. So(err, ShouldNotBeNil)
  107. })
  108. Convey("plugin route with admin role and user is admin", func() {
  109. ctx.SignedInUser.OrgRole = m.ROLE_ADMIN
  110. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin", &setting.Cfg{})
  111. err := proxy.validateRequest()
  112. So(err, ShouldBeNil)
  113. })
  114. })
  115. })
  116. Convey("Plugin with multiple routes for token auth", func() {
  117. plugin := &plugins.DataSourcePlugin{
  118. Routes: []*plugins.AppPluginRoute{
  119. {
  120. Path: "pathwithtoken1",
  121. Url: "https://api.nr1.io/some/path",
  122. TokenAuth: &plugins.JwtTokenAuth{
  123. Url: "https://login.server.com/{{.JsonData.tenantId}}/oauth2/token",
  124. Params: map[string]string{
  125. "grant_type": "client_credentials",
  126. "client_id": "{{.JsonData.clientId}}",
  127. "client_secret": "{{.SecureJsonData.clientSecret}}",
  128. "resource": "https://api.nr1.io",
  129. },
  130. },
  131. },
  132. {
  133. Path: "pathwithtoken2",
  134. Url: "https://api.nr2.io/some/path",
  135. TokenAuth: &plugins.JwtTokenAuth{
  136. Url: "https://login.server.com/{{.JsonData.tenantId}}/oauth2/token",
  137. Params: map[string]string{
  138. "grant_type": "client_credentials",
  139. "client_id": "{{.JsonData.clientId}}",
  140. "client_secret": "{{.SecureJsonData.clientSecret}}",
  141. "resource": "https://api.nr2.io",
  142. },
  143. },
  144. },
  145. },
  146. }
  147. setting.SecretKey = "password"
  148. key, _ := util.Encrypt([]byte("123"), "password")
  149. ds := &m.DataSource{
  150. JsonData: simplejson.NewFromAny(map[string]interface{}{
  151. "clientId": "asd",
  152. "tenantId": "mytenantId",
  153. }),
  154. SecureJsonData: map[string][]byte{
  155. "clientSecret": key,
  156. },
  157. }
  158. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  159. ctx := &m.ReqContext{
  160. Context: &macaron.Context{
  161. Req: macaron.Request{Request: req},
  162. },
  163. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR},
  164. }
  165. Convey("When creating and caching access tokens", func() {
  166. var authorizationHeaderCall1 string
  167. var authorizationHeaderCall2 string
  168. Convey("first call should add authorization header with access token", func() {
  169. json, err := ioutil.ReadFile("./test-data/access-token-1.json")
  170. So(err, ShouldBeNil)
  171. client = newFakeHTTPClient(json)
  172. proxy1 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1", &setting.Cfg{})
  173. proxy1.route = plugin.Routes[0]
  174. ApplyRoute(proxy1.ctx.Req.Context(), req, proxy1.proxyPath, proxy1.route, proxy1.ds)
  175. authorizationHeaderCall1 = req.Header.Get("Authorization")
  176. So(req.URL.String(), ShouldEqual, "https://api.nr1.io/some/path")
  177. So(authorizationHeaderCall1, ShouldStartWith, "Bearer eyJ0e")
  178. Convey("second call to another route should add a different access token", func() {
  179. json2, err := ioutil.ReadFile("./test-data/access-token-2.json")
  180. So(err, ShouldBeNil)
  181. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  182. client = newFakeHTTPClient(json2)
  183. proxy2 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken2", &setting.Cfg{})
  184. proxy2.route = plugin.Routes[1]
  185. ApplyRoute(proxy2.ctx.Req.Context(), req, proxy2.proxyPath, proxy2.route, proxy2.ds)
  186. authorizationHeaderCall2 = req.Header.Get("Authorization")
  187. So(req.URL.String(), ShouldEqual, "https://api.nr2.io/some/path")
  188. So(authorizationHeaderCall1, ShouldStartWith, "Bearer eyJ0e")
  189. So(authorizationHeaderCall2, ShouldStartWith, "Bearer eyJ0e")
  190. So(authorizationHeaderCall2, ShouldNotEqual, authorizationHeaderCall1)
  191. Convey("third call to first route should add cached access token", func() {
  192. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  193. client = newFakeHTTPClient([]byte{})
  194. proxy3 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1", &setting.Cfg{})
  195. proxy3.route = plugin.Routes[0]
  196. ApplyRoute(proxy3.ctx.Req.Context(), req, proxy3.proxyPath, proxy3.route, proxy3.ds)
  197. authorizationHeaderCall3 := req.Header.Get("Authorization")
  198. So(req.URL.String(), ShouldEqual, "https://api.nr1.io/some/path")
  199. So(authorizationHeaderCall1, ShouldStartWith, "Bearer eyJ0e")
  200. So(authorizationHeaderCall3, ShouldStartWith, "Bearer eyJ0e")
  201. So(authorizationHeaderCall3, ShouldEqual, authorizationHeaderCall1)
  202. })
  203. })
  204. })
  205. })
  206. })
  207. Convey("When proxying graphite", func() {
  208. setting.BuildVersion = "5.3.0"
  209. plugin := &plugins.DataSourcePlugin{}
  210. ds := &m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE}
  211. ctx := &m.ReqContext{}
  212. proxy := NewDataSourceProxy(ds, plugin, ctx, "/render", &setting.Cfg{})
  213. req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil)
  214. So(err, ShouldBeNil)
  215. proxy.getDirector()(req)
  216. Convey("Can translate request url and path", func() {
  217. So(req.URL.Host, ShouldEqual, "graphite:8080")
  218. So(req.URL.Path, ShouldEqual, "/render")
  219. So(req.Header.Get("User-Agent"), ShouldEqual, "Grafana/5.3.0")
  220. })
  221. })
  222. Convey("When proxying InfluxDB", func() {
  223. plugin := &plugins.DataSourcePlugin{}
  224. ds := &m.DataSource{
  225. Type: m.DS_INFLUXDB_08,
  226. Url: "http://influxdb:8083",
  227. Database: "site",
  228. User: "user",
  229. Password: "password",
  230. }
  231. ctx := &m.ReqContext{}
  232. proxy := NewDataSourceProxy(ds, plugin, ctx, "", &setting.Cfg{})
  233. req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil)
  234. So(err, ShouldBeNil)
  235. proxy.getDirector()(req)
  236. Convey("Should add db to url", func() {
  237. So(req.URL.Path, ShouldEqual, "/db/site/")
  238. })
  239. })
  240. Convey("When proxying a data source with no keepCookies specified", func() {
  241. plugin := &plugins.DataSourcePlugin{}
  242. json, _ := simplejson.NewJson([]byte(`{"keepCookies": []}`))
  243. ds := &m.DataSource{
  244. Type: m.DS_GRAPHITE,
  245. Url: "http://graphite:8086",
  246. JsonData: json,
  247. }
  248. ctx := &m.ReqContext{}
  249. proxy := NewDataSourceProxy(ds, plugin, ctx, "", &setting.Cfg{})
  250. requestURL, _ := url.Parse("http://grafana.com/sub")
  251. req := http.Request{URL: requestURL, Header: make(http.Header)}
  252. cookies := "grafana_user=admin; grafana_remember=99; grafana_sess=11; JSESSION_ID=test"
  253. req.Header.Set("Cookie", cookies)
  254. proxy.getDirector()(&req)
  255. Convey("Should clear all cookies", func() {
  256. So(req.Header.Get("Cookie"), ShouldEqual, "")
  257. })
  258. })
  259. Convey("When proxying a data source with keep cookies specified", func() {
  260. plugin := &plugins.DataSourcePlugin{}
  261. json, _ := simplejson.NewJson([]byte(`{"keepCookies": ["JSESSION_ID"]}`))
  262. ds := &m.DataSource{
  263. Type: m.DS_GRAPHITE,
  264. Url: "http://graphite:8086",
  265. JsonData: json,
  266. }
  267. ctx := &m.ReqContext{}
  268. proxy := NewDataSourceProxy(ds, plugin, ctx, "", &setting.Cfg{})
  269. requestURL, _ := url.Parse("http://grafana.com/sub")
  270. req := http.Request{URL: requestURL, Header: make(http.Header)}
  271. cookies := "grafana_user=admin; grafana_remember=99; grafana_sess=11; JSESSION_ID=test"
  272. req.Header.Set("Cookie", cookies)
  273. proxy.getDirector()(&req)
  274. Convey("Should keep named cookies", func() {
  275. So(req.Header.Get("Cookie"), ShouldEqual, "JSESSION_ID=test")
  276. })
  277. })
  278. Convey("When proxying a data source with custom headers specified", func() {
  279. plugin := &plugins.DataSourcePlugin{}
  280. encryptedData, err := util.Encrypt([]byte(`Bearer xf5yhfkpsnmgo`), setting.SecretKey)
  281. ds := &m.DataSource{
  282. Type: m.DS_PROMETHEUS,
  283. Url: "http://prometheus:9090",
  284. JsonData: simplejson.NewFromAny(map[string]interface{}{
  285. "httpHeaderName1": "Authorization",
  286. }),
  287. SecureJsonData: map[string][]byte{
  288. "httpHeaderValue1": encryptedData,
  289. },
  290. }
  291. ctx := &m.ReqContext{}
  292. proxy := NewDataSourceProxy(ds, plugin, ctx, "", &setting.Cfg{})
  293. requestURL, _ := url.Parse("http://grafana.com/sub")
  294. req := http.Request{URL: requestURL, Header: make(http.Header)}
  295. proxy.getDirector()(&req)
  296. if err != nil {
  297. log.Fatal(4, err.Error())
  298. }
  299. Convey("Match header value after decryption", func() {
  300. So(req.Header.Get("Authorization"), ShouldEqual, "Bearer xf5yhfkpsnmgo")
  301. })
  302. })
  303. Convey("When proxying a custom datasource", func() {
  304. plugin := &plugins.DataSourcePlugin{}
  305. ds := &m.DataSource{
  306. Type: "custom-datasource",
  307. Url: "http://host/root/",
  308. }
  309. ctx := &m.ReqContext{}
  310. proxy := NewDataSourceProxy(ds, plugin, ctx, "/path/to/folder/", &setting.Cfg{})
  311. req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil)
  312. req.Header.Add("Origin", "grafana.com")
  313. req.Header.Add("Referer", "grafana.com")
  314. req.Header.Add("X-Canary", "stillthere")
  315. So(err, ShouldBeNil)
  316. proxy.getDirector()(req)
  317. Convey("Should keep user request (including trailing slash)", func() {
  318. So(req.URL.String(), ShouldEqual, "http://host/root/path/to/folder/")
  319. })
  320. Convey("Origin and Referer headers should be dropped", func() {
  321. So(req.Header.Get("Origin"), ShouldEqual, "")
  322. So(req.Header.Get("Referer"), ShouldEqual, "")
  323. So(req.Header.Get("X-Canary"), ShouldEqual, "stillthere")
  324. })
  325. })
  326. Convey("When proxying a datasource that has oauth token pass-thru enabled", func() {
  327. social.SocialMap["generic_oauth"] = &social.SocialGenericOAuth{
  328. SocialBase: &social.SocialBase{
  329. Config: &oauth2.Config{},
  330. },
  331. }
  332. bus.AddHandler("test", func(query *m.GetAuthInfoQuery) error {
  333. query.Result = &m.UserAuth{
  334. Id: 1,
  335. UserId: 1,
  336. AuthModule: "generic_oauth",
  337. OAuthAccessToken: "testtoken",
  338. OAuthRefreshToken: "testrefreshtoken",
  339. OAuthTokenType: "Bearer",
  340. OAuthExpiry: time.Now().AddDate(0, 0, 1),
  341. }
  342. return nil
  343. })
  344. plugin := &plugins.DataSourcePlugin{}
  345. ds := &m.DataSource{
  346. Type: "custom-datasource",
  347. Url: "http://host/root/",
  348. JsonData: simplejson.NewFromAny(map[string]interface{}{
  349. "oauthPassThru": true,
  350. }),
  351. }
  352. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  353. ctx := &m.ReqContext{
  354. SignedInUser: &m.SignedInUser{UserId: 1},
  355. Context: &macaron.Context{
  356. Req: macaron.Request{Request: req},
  357. },
  358. }
  359. proxy := NewDataSourceProxy(ds, plugin, ctx, "/path/to/folder/", &setting.Cfg{})
  360. req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil)
  361. So(err, ShouldBeNil)
  362. proxy.getDirector()(req)
  363. Convey("Should have access token in header", func() {
  364. So(req.Header.Get("Authorization"), ShouldEqual, fmt.Sprintf("%s %s", "Bearer", "testtoken"))
  365. })
  366. })
  367. Convey("When SendUserHeader config is enabled", func() {
  368. req := getDatasourceProxiedRequest(
  369. &m.ReqContext{
  370. SignedInUser: &m.SignedInUser{
  371. Login: "test_user",
  372. },
  373. },
  374. &setting.Cfg{SendUserHeader: true},
  375. )
  376. Convey("Should add header with username", func() {
  377. So(req.Header.Get("X-Grafana-User"), ShouldEqual, "test_user")
  378. })
  379. })
  380. Convey("When SendUserHeader config is disabled", func() {
  381. req := getDatasourceProxiedRequest(
  382. &m.ReqContext{
  383. SignedInUser: &m.SignedInUser{
  384. Login: "test_user",
  385. },
  386. },
  387. &setting.Cfg{SendUserHeader: false},
  388. )
  389. Convey("Should not add header with username", func() {
  390. // Get will return empty string even if header is not set
  391. So(req.Header.Get("X-Grafana-User"), ShouldEqual, "")
  392. })
  393. })
  394. Convey("When SendUserHeader config is enabled but user is anonymous", func() {
  395. req := getDatasourceProxiedRequest(
  396. &m.ReqContext{
  397. SignedInUser: &m.SignedInUser{IsAnonymous: true},
  398. },
  399. &setting.Cfg{SendUserHeader: true},
  400. )
  401. Convey("Should not add header with username", func() {
  402. // Get will return empty string even if header is not set
  403. So(req.Header.Get("X-Grafana-User"), ShouldEqual, "")
  404. })
  405. })
  406. Convey("When proxying data source proxy should handle authentication", func() {
  407. tests := []*Test{
  408. createAuthTest(m.DS_INFLUXDB_08, AUTHTYPE_PASSWORD, AUTHCHECK_QUERY, false),
  409. createAuthTest(m.DS_INFLUXDB_08, AUTHTYPE_PASSWORD, AUTHCHECK_QUERY, true),
  410. createAuthTest(m.DS_INFLUXDB, AUTHTYPE_PASSWORD, AUTHCHECK_HEADER, true),
  411. createAuthTest(m.DS_INFLUXDB, AUTHTYPE_PASSWORD, AUTHCHECK_HEADER, false),
  412. createAuthTest(m.DS_INFLUXDB, AUTHTYPE_BASIC, AUTHCHECK_HEADER, true),
  413. createAuthTest(m.DS_INFLUXDB, AUTHTYPE_BASIC, AUTHCHECK_HEADER, false),
  414. // These two should be enough for any other datasource at the moment. Proxy has special handling
  415. // only for Influx, others have the same path and only BasicAuth. Non BasicAuth datasources
  416. // do not go through proxy but through TSDB API which is not tested here.
  417. createAuthTest(m.DS_ES, AUTHTYPE_BASIC, AUTHCHECK_HEADER, false),
  418. createAuthTest(m.DS_ES, AUTHTYPE_BASIC, AUTHCHECK_HEADER, true),
  419. }
  420. for _, test := range tests {
  421. runDatasourceAuthTest(test)
  422. }
  423. })
  424. Convey("HandleRequest()", func() {
  425. backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  426. http.SetCookie(w, &http.Cookie{Name: "flavor", Value: "chocolateChip"})
  427. w.WriteHeader(200)
  428. w.Write([]byte("I am the backend"))
  429. }))
  430. defer backend.Close()
  431. plugin := &plugins.DataSourcePlugin{}
  432. ds := &m.DataSource{Url: backend.URL, Type: m.DS_GRAPHITE}
  433. responseRecorder := &CloseNotifierResponseRecorder{
  434. ResponseRecorder: httptest.NewRecorder(),
  435. }
  436. defer responseRecorder.Close()
  437. setupCtx := func(fn func(http.ResponseWriter)) *m.ReqContext {
  438. responseWriter := macaron.NewResponseWriter("GET", responseRecorder)
  439. if fn != nil {
  440. fn(responseWriter)
  441. }
  442. return &m.ReqContext{
  443. SignedInUser: &m.SignedInUser{},
  444. Context: &macaron.Context{
  445. Req: macaron.Request{
  446. Request: httptest.NewRequest("GET", "/render", nil),
  447. },
  448. Resp: responseWriter,
  449. },
  450. }
  451. }
  452. Convey("When response header Set-Cookie is not set should remove proxied Set-Cookie header", func() {
  453. ctx := setupCtx(nil)
  454. proxy := NewDataSourceProxy(ds, plugin, ctx, "/render", &setting.Cfg{})
  455. proxy.HandleRequest()
  456. So(proxy.ctx.Resp.Header().Get("Set-Cookie"), ShouldBeEmpty)
  457. })
  458. Convey("When response header Set-Cookie is set should remove proxied Set-Cookie header and restore the original Set-Cookie header", func() {
  459. ctx := setupCtx(func(w http.ResponseWriter) {
  460. w.Header().Set("Set-Cookie", "important_cookie=important_value")
  461. })
  462. proxy := NewDataSourceProxy(ds, plugin, ctx, "/render", &setting.Cfg{})
  463. proxy.HandleRequest()
  464. So(proxy.ctx.Resp.Header().Get("Set-Cookie"), ShouldEqual, "important_cookie=important_value")
  465. })
  466. })
  467. })
  468. }
  469. type CloseNotifierResponseRecorder struct {
  470. *httptest.ResponseRecorder
  471. closeChan chan bool
  472. }
  473. func (r *CloseNotifierResponseRecorder) CloseNotify() <-chan bool {
  474. r.closeChan = make(chan bool)
  475. return r.closeChan
  476. }
  477. func (r *CloseNotifierResponseRecorder) Close() {
  478. close(r.closeChan)
  479. }
  480. // getDatasourceProxiedRequest is a helper for easier setup of tests based on global config and ReqContext.
  481. func getDatasourceProxiedRequest(ctx *m.ReqContext, cfg *setting.Cfg) *http.Request {
  482. plugin := &plugins.DataSourcePlugin{}
  483. ds := &m.DataSource{
  484. Type: "custom",
  485. Url: "http://host/root/",
  486. }
  487. proxy := NewDataSourceProxy(ds, plugin, ctx, "", cfg)
  488. req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil)
  489. So(err, ShouldBeNil)
  490. proxy.getDirector()(req)
  491. return req
  492. }
  493. type httpClientStub struct {
  494. fakeBody []byte
  495. }
  496. func (c *httpClientStub) Do(req *http.Request) (*http.Response, error) {
  497. bodyJSON, _ := simplejson.NewJson(c.fakeBody)
  498. _, passedTokenCacheTest := bodyJSON.CheckGet("expires_on")
  499. So(passedTokenCacheTest, ShouldBeTrue)
  500. bodyJSON.Set("expires_on", fmt.Sprint(time.Now().Add(time.Second*60).Unix()))
  501. body, _ := bodyJSON.MarshalJSON()
  502. resp := &http.Response{
  503. Body: ioutil.NopCloser(bytes.NewReader(body)),
  504. }
  505. return resp, nil
  506. }
  507. func newFakeHTTPClient(fakeBody []byte) httpClient {
  508. return &httpClientStub{
  509. fakeBody: fakeBody,
  510. }
  511. }
  512. type Test struct {
  513. datasource *m.DataSource
  514. checkReq func(req *http.Request)
  515. }
  516. const (
  517. AUTHTYPE_PASSWORD = "password"
  518. AUTHTYPE_BASIC = "basic"
  519. )
  520. const (
  521. AUTHCHECK_QUERY = "query"
  522. AUTHCHECK_HEADER = "header"
  523. )
  524. func createAuthTest(dsType string, authType string, authCheck string, useSecureJsonData bool) *Test {
  525. // Basic user:password
  526. base64AthHeader := "Basic dXNlcjpwYXNzd29yZA=="
  527. test := &Test{
  528. datasource: &m.DataSource{
  529. Type: dsType,
  530. JsonData: simplejson.New(),
  531. },
  532. }
  533. var message string
  534. if authType == AUTHTYPE_PASSWORD {
  535. message = fmt.Sprintf("%v should add username and password", dsType)
  536. test.datasource.User = "user"
  537. if useSecureJsonData {
  538. test.datasource.SecureJsonData = securejsondata.GetEncryptedJsonData(map[string]string{
  539. "password": "password",
  540. })
  541. } else {
  542. test.datasource.Password = "password"
  543. }
  544. } else {
  545. message = fmt.Sprintf("%v should add basic auth username and password", dsType)
  546. test.datasource.BasicAuth = true
  547. test.datasource.BasicAuthUser = "user"
  548. if useSecureJsonData {
  549. test.datasource.SecureJsonData = securejsondata.GetEncryptedJsonData(map[string]string{
  550. "basicAuthPassword": "password",
  551. })
  552. } else {
  553. test.datasource.BasicAuthPassword = "password"
  554. }
  555. }
  556. if useSecureJsonData {
  557. message += " from securejsondata"
  558. }
  559. if authCheck == AUTHCHECK_QUERY {
  560. message += " to query params"
  561. test.checkReq = func(req *http.Request) {
  562. Convey(message, func() {
  563. queryVals := req.URL.Query()
  564. So(queryVals["u"][0], ShouldEqual, "user")
  565. So(queryVals["p"][0], ShouldEqual, "password")
  566. })
  567. }
  568. } else {
  569. message += " to auth header"
  570. test.checkReq = func(req *http.Request) {
  571. Convey(message, func() {
  572. So(req.Header.Get("Authorization"), ShouldEqual, base64AthHeader)
  573. })
  574. }
  575. }
  576. return test
  577. }
  578. func runDatasourceAuthTest(test *Test) {
  579. plugin := &plugins.DataSourcePlugin{}
  580. ctx := &m.ReqContext{}
  581. proxy := NewDataSourceProxy(test.datasource, plugin, ctx, "", &setting.Cfg{})
  582. req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil)
  583. So(err, ShouldBeNil)
  584. proxy.getDirector()(req)
  585. test.checkReq(req)
  586. }