ds_proxy_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package pluginproxy
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. "time"
  10. macaron "gopkg.in/macaron.v1"
  11. "github.com/grafana/grafana/pkg/components/simplejson"
  12. "github.com/grafana/grafana/pkg/log"
  13. m "github.com/grafana/grafana/pkg/models"
  14. "github.com/grafana/grafana/pkg/plugins"
  15. "github.com/grafana/grafana/pkg/setting"
  16. "github.com/grafana/grafana/pkg/util"
  17. . "github.com/smartystreets/goconvey/convey"
  18. )
  19. func TestDSRouteRule(t *testing.T) {
  20. Convey("DataSourceProxy", t, func() {
  21. Convey("Plugin with routes", func() {
  22. plugin := &plugins.DataSourcePlugin{
  23. Routes: []*plugins.AppPluginRoute{
  24. {
  25. Path: "api/v4/",
  26. Url: "https://www.google.com",
  27. ReqRole: m.ROLE_EDITOR,
  28. Headers: []plugins.AppPluginRouteHeader{
  29. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  30. },
  31. },
  32. {
  33. Path: "api/admin",
  34. Url: "https://www.google.com",
  35. ReqRole: m.ROLE_ADMIN,
  36. Headers: []plugins.AppPluginRouteHeader{
  37. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  38. },
  39. },
  40. {
  41. Path: "api/anon",
  42. Url: "https://www.google.com",
  43. Headers: []plugins.AppPluginRouteHeader{
  44. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  45. },
  46. },
  47. {
  48. Path: "api/common",
  49. Url: "{{.JsonData.dynamicUrl}}",
  50. Headers: []plugins.AppPluginRouteHeader{
  51. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  52. },
  53. },
  54. },
  55. }
  56. setting.SecretKey = "password"
  57. key, _ := util.Encrypt([]byte("123"), "password")
  58. ds := &m.DataSource{
  59. JsonData: simplejson.NewFromAny(map[string]interface{}{
  60. "clientId": "asd",
  61. "dynamicUrl": "https://dynamic.grafana.com",
  62. }),
  63. SecureJsonData: map[string][]byte{
  64. "key": key,
  65. },
  66. }
  67. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  68. ctx := &m.ReqContext{
  69. Context: &macaron.Context{
  70. Req: macaron.Request{Request: req},
  71. },
  72. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR},
  73. }
  74. Convey("When matching route path", func() {
  75. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method")
  76. proxy.route = plugin.Routes[0]
  77. ApplyRoute(proxy.ctx.Req.Context(), req, proxy.proxyPath, proxy.route, proxy.ds)
  78. Convey("should add headers and update url", func() {
  79. So(req.URL.String(), ShouldEqual, "https://www.google.com/some/method")
  80. So(req.Header.Get("x-header"), ShouldEqual, "my secret 123")
  81. })
  82. })
  83. Convey("When matching route path and has dynamic url", func() {
  84. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/common/some/method")
  85. proxy.route = plugin.Routes[3]
  86. ApplyRoute(proxy.ctx.Req.Context(), req, proxy.proxyPath, proxy.route, proxy.ds)
  87. Convey("should add headers and interpolate the url", func() {
  88. So(req.URL.String(), ShouldEqual, "https://dynamic.grafana.com/some/method")
  89. So(req.Header.Get("x-header"), ShouldEqual, "my secret 123")
  90. })
  91. })
  92. Convey("Validating request", func() {
  93. Convey("plugin route with valid role", func() {
  94. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method")
  95. err := proxy.validateRequest()
  96. So(err, ShouldBeNil)
  97. })
  98. Convey("plugin route with admin role and user is editor", func() {
  99. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin")
  100. err := proxy.validateRequest()
  101. So(err, ShouldNotBeNil)
  102. })
  103. Convey("plugin route with admin role and user is admin", func() {
  104. ctx.SignedInUser.OrgRole = m.ROLE_ADMIN
  105. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin")
  106. err := proxy.validateRequest()
  107. So(err, ShouldBeNil)
  108. })
  109. })
  110. })
  111. Convey("Plugin with multiple routes for token auth", func() {
  112. plugin := &plugins.DataSourcePlugin{
  113. Routes: []*plugins.AppPluginRoute{
  114. {
  115. Path: "pathwithtoken1",
  116. Url: "https://api.nr1.io/some/path",
  117. TokenAuth: &plugins.JwtTokenAuth{
  118. Url: "https://login.server.com/{{.JsonData.tenantId}}/oauth2/token",
  119. Params: map[string]string{
  120. "grant_type": "client_credentials",
  121. "client_id": "{{.JsonData.clientId}}",
  122. "client_secret": "{{.SecureJsonData.clientSecret}}",
  123. "resource": "https://api.nr1.io",
  124. },
  125. },
  126. },
  127. {
  128. Path: "pathwithtoken2",
  129. Url: "https://api.nr2.io/some/path",
  130. TokenAuth: &plugins.JwtTokenAuth{
  131. Url: "https://login.server.com/{{.JsonData.tenantId}}/oauth2/token",
  132. Params: map[string]string{
  133. "grant_type": "client_credentials",
  134. "client_id": "{{.JsonData.clientId}}",
  135. "client_secret": "{{.SecureJsonData.clientSecret}}",
  136. "resource": "https://api.nr2.io",
  137. },
  138. },
  139. },
  140. },
  141. }
  142. setting.SecretKey = "password"
  143. key, _ := util.Encrypt([]byte("123"), "password")
  144. ds := &m.DataSource{
  145. JsonData: simplejson.NewFromAny(map[string]interface{}{
  146. "clientId": "asd",
  147. "tenantId": "mytenantId",
  148. }),
  149. SecureJsonData: map[string][]byte{
  150. "clientSecret": key,
  151. },
  152. }
  153. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  154. ctx := &m.ReqContext{
  155. Context: &macaron.Context{
  156. Req: macaron.Request{Request: req},
  157. },
  158. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR},
  159. }
  160. Convey("When creating and caching access tokens", func() {
  161. var authorizationHeaderCall1 string
  162. var authorizationHeaderCall2 string
  163. Convey("first call should add authorization header with access token", func() {
  164. json, err := ioutil.ReadFile("./test-data/access-token-1.json")
  165. So(err, ShouldBeNil)
  166. client = newFakeHTTPClient(json)
  167. proxy1 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1")
  168. proxy1.route = plugin.Routes[0]
  169. ApplyRoute(proxy1.ctx.Req.Context(), req, proxy1.proxyPath, proxy1.route, proxy1.ds)
  170. authorizationHeaderCall1 = req.Header.Get("Authorization")
  171. So(req.URL.String(), ShouldEqual, "https://api.nr1.io/some/path")
  172. So(authorizationHeaderCall1, ShouldStartWith, "Bearer eyJ0e")
  173. Convey("second call to another route should add a different access token", func() {
  174. json2, err := ioutil.ReadFile("./test-data/access-token-2.json")
  175. So(err, ShouldBeNil)
  176. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  177. client = newFakeHTTPClient(json2)
  178. proxy2 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken2")
  179. proxy2.route = plugin.Routes[1]
  180. ApplyRoute(proxy2.ctx.Req.Context(), req, proxy2.proxyPath, proxy2.route, proxy2.ds)
  181. authorizationHeaderCall2 = req.Header.Get("Authorization")
  182. So(req.URL.String(), ShouldEqual, "https://api.nr2.io/some/path")
  183. So(authorizationHeaderCall1, ShouldStartWith, "Bearer eyJ0e")
  184. So(authorizationHeaderCall2, ShouldStartWith, "Bearer eyJ0e")
  185. So(authorizationHeaderCall2, ShouldNotEqual, authorizationHeaderCall1)
  186. Convey("third call to first route should add cached access token", func() {
  187. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  188. client = newFakeHTTPClient([]byte{})
  189. proxy3 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1")
  190. proxy3.route = plugin.Routes[0]
  191. ApplyRoute(proxy3.ctx.Req.Context(), req, proxy3.proxyPath, proxy3.route, proxy3.ds)
  192. authorizationHeaderCall3 := req.Header.Get("Authorization")
  193. So(req.URL.String(), ShouldEqual, "https://api.nr1.io/some/path")
  194. So(authorizationHeaderCall1, ShouldStartWith, "Bearer eyJ0e")
  195. So(authorizationHeaderCall3, ShouldStartWith, "Bearer eyJ0e")
  196. So(authorizationHeaderCall3, ShouldEqual, authorizationHeaderCall1)
  197. })
  198. })
  199. })
  200. })
  201. })
  202. Convey("When proxying graphite", func() {
  203. setting.BuildVersion = "5.3.0"
  204. plugin := &plugins.DataSourcePlugin{}
  205. ds := &m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE}
  206. ctx := &m.ReqContext{}
  207. proxy := NewDataSourceProxy(ds, plugin, ctx, "/render")
  208. req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil)
  209. So(err, ShouldBeNil)
  210. proxy.getDirector()(req)
  211. Convey("Can translate request url and path", func() {
  212. So(req.URL.Host, ShouldEqual, "graphite:8080")
  213. So(req.URL.Path, ShouldEqual, "/render")
  214. So(req.Header.Get("User-Agent"), ShouldEqual, "Grafana/5.3.0")
  215. })
  216. })
  217. Convey("When proxying InfluxDB", func() {
  218. plugin := &plugins.DataSourcePlugin{}
  219. ds := &m.DataSource{
  220. Type: m.DS_INFLUXDB_08,
  221. Url: "http://influxdb:8083",
  222. Database: "site",
  223. User: "user",
  224. Password: "password",
  225. }
  226. ctx := &m.ReqContext{}
  227. proxy := NewDataSourceProxy(ds, plugin, ctx, "")
  228. req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil)
  229. So(err, ShouldBeNil)
  230. proxy.getDirector()(req)
  231. Convey("Should add db to url", func() {
  232. So(req.URL.Path, ShouldEqual, "/db/site/")
  233. })
  234. Convey("Should add username and password", func() {
  235. queryVals := req.URL.Query()
  236. So(queryVals["u"][0], ShouldEqual, "user")
  237. So(queryVals["p"][0], ShouldEqual, "password")
  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, "")
  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, "")
  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, "")
  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/")
  311. req, err := http.NewRequest(http.MethodGet, "http://grafana.com/sub", nil)
  312. So(err, ShouldBeNil)
  313. proxy.getDirector()(req)
  314. Convey("Shoudl keep user request (including trailing slash)", func() {
  315. So(req.URL.String(), ShouldEqual, "http://host/root/path/to/folder/")
  316. })
  317. })
  318. })
  319. }
  320. type httpClientStub struct {
  321. fakeBody []byte
  322. }
  323. func (c *httpClientStub) Do(req *http.Request) (*http.Response, error) {
  324. bodyJSON, _ := simplejson.NewJson(c.fakeBody)
  325. _, passedTokenCacheTest := bodyJSON.CheckGet("expires_on")
  326. So(passedTokenCacheTest, ShouldBeTrue)
  327. bodyJSON.Set("expires_on", fmt.Sprint(time.Now().Add(time.Second*60).Unix()))
  328. body, _ := bodyJSON.MarshalJSON()
  329. resp := &http.Response{
  330. Body: ioutil.NopCloser(bytes.NewReader(body)),
  331. }
  332. return resp, nil
  333. }
  334. func newFakeHTTPClient(fakeBody []byte) httpClient {
  335. return &httpClientStub{
  336. fakeBody: fakeBody,
  337. }
  338. }