ds_proxy_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. m "github.com/grafana/grafana/pkg/models"
  13. "github.com/grafana/grafana/pkg/plugins"
  14. "github.com/grafana/grafana/pkg/setting"
  15. "github.com/grafana/grafana/pkg/util"
  16. . "github.com/smartystreets/goconvey/convey"
  17. )
  18. func TestDSRouteRule(t *testing.T) {
  19. Convey("DataSourceProxy", t, func() {
  20. Convey("Plugin with routes", func() {
  21. plugin := &plugins.DataSourcePlugin{
  22. Routes: []*plugins.AppPluginRoute{
  23. {
  24. Path: "api/v4/",
  25. Url: "https://www.google.com",
  26. ReqRole: m.ROLE_EDITOR,
  27. Headers: []plugins.AppPluginRouteHeader{
  28. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  29. },
  30. },
  31. {
  32. Path: "api/admin",
  33. Url: "https://www.google.com",
  34. ReqRole: m.ROLE_ADMIN,
  35. Headers: []plugins.AppPluginRouteHeader{
  36. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  37. },
  38. },
  39. {
  40. Path: "api/anon",
  41. Url: "https://www.google.com",
  42. Headers: []plugins.AppPluginRouteHeader{
  43. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  44. },
  45. },
  46. },
  47. }
  48. setting.SecretKey = "password"
  49. key, _ := util.Encrypt([]byte("123"), "password")
  50. ds := &m.DataSource{
  51. JsonData: simplejson.NewFromAny(map[string]interface{}{
  52. "clientId": "asd",
  53. }),
  54. SecureJsonData: map[string][]byte{
  55. "key": key,
  56. },
  57. }
  58. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  59. ctx := &m.ReqContext{
  60. Context: &macaron.Context{
  61. Req: macaron.Request{Request: req},
  62. },
  63. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR},
  64. }
  65. Convey("When matching route path", func() {
  66. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method")
  67. proxy.route = plugin.Routes[0]
  68. proxy.applyRoute(req)
  69. Convey("should add headers and update url", func() {
  70. So(req.URL.String(), ShouldEqual, "https://www.google.com/some/method")
  71. So(req.Header.Get("x-header"), ShouldEqual, "my secret 123")
  72. })
  73. })
  74. Convey("Validating request", func() {
  75. Convey("plugin route with valid role", func() {
  76. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method")
  77. err := proxy.validateRequest()
  78. So(err, ShouldBeNil)
  79. })
  80. Convey("plugin route with admin role and user is editor", func() {
  81. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin")
  82. err := proxy.validateRequest()
  83. So(err, ShouldNotBeNil)
  84. })
  85. Convey("plugin route with admin role and user is admin", func() {
  86. ctx.SignedInUser.OrgRole = m.ROLE_ADMIN
  87. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin")
  88. err := proxy.validateRequest()
  89. So(err, ShouldBeNil)
  90. })
  91. })
  92. })
  93. Convey("Plugin with multiple routes for token auth", func() {
  94. plugin := &plugins.DataSourcePlugin{
  95. Routes: []*plugins.AppPluginRoute{
  96. {
  97. Path: "pathwithtoken1",
  98. Url: "https://api.nr1.io/some/path",
  99. TokenAuth: &plugins.JwtTokenAuth{
  100. Url: "https://login.server.com/{{.JsonData.tenantId}}/oauth2/token",
  101. Params: map[string]string{
  102. "grant_type": "client_credentials",
  103. "client_id": "{{.JsonData.clientId}}",
  104. "client_secret": "{{.SecureJsonData.clientSecret}}",
  105. "resource": "https://api.nr1.io",
  106. },
  107. },
  108. },
  109. {
  110. Path: "pathwithtoken2",
  111. Url: "https://api.nr2.io/some/path",
  112. TokenAuth: &plugins.JwtTokenAuth{
  113. Url: "https://login.server.com/{{.JsonData.tenantId}}/oauth2/token",
  114. Params: map[string]string{
  115. "grant_type": "client_credentials",
  116. "client_id": "{{.JsonData.clientId}}",
  117. "client_secret": "{{.SecureJsonData.clientSecret}}",
  118. "resource": "https://api.nr2.io",
  119. },
  120. },
  121. },
  122. },
  123. }
  124. setting.SecretKey = "password"
  125. key, _ := util.Encrypt([]byte("123"), "password")
  126. ds := &m.DataSource{
  127. JsonData: simplejson.NewFromAny(map[string]interface{}{
  128. "clientId": "asd",
  129. "tenantId": "mytenantId",
  130. }),
  131. SecureJsonData: map[string][]byte{
  132. "clientSecret": key,
  133. },
  134. }
  135. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  136. ctx := &m.ReqContext{
  137. Context: &macaron.Context{
  138. Req: macaron.Request{Request: req},
  139. },
  140. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR},
  141. }
  142. Convey("When creating and caching access tokens", func() {
  143. var authorizationHeaderCall1 string
  144. var authorizationHeaderCall2 string
  145. Convey("first call should add authorization header with access token", func() {
  146. json, err := ioutil.ReadFile("./test-data/access-token-1.json")
  147. So(err, ShouldBeNil)
  148. client = newFakeHTTPClient(json)
  149. proxy1 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1")
  150. proxy1.route = plugin.Routes[0]
  151. proxy1.applyRoute(req)
  152. authorizationHeaderCall1 = req.Header.Get("Authorization")
  153. So(req.URL.String(), ShouldEqual, "https://api.nr1.io/some/path")
  154. So(authorizationHeaderCall1, ShouldStartWith, "Bearer eyJ0e")
  155. Convey("second call to another route should add a different access token", func() {
  156. json2, err := ioutil.ReadFile("./test-data/access-token-2.json")
  157. So(err, ShouldBeNil)
  158. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  159. client = newFakeHTTPClient(json2)
  160. proxy2 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken2")
  161. proxy2.route = plugin.Routes[1]
  162. proxy2.applyRoute(req)
  163. authorizationHeaderCall2 = req.Header.Get("Authorization")
  164. So(req.URL.String(), ShouldEqual, "https://api.nr2.io/some/path")
  165. So(authorizationHeaderCall1, ShouldStartWith, "Bearer eyJ0e")
  166. So(authorizationHeaderCall2, ShouldStartWith, "Bearer eyJ0e")
  167. So(authorizationHeaderCall2, ShouldNotEqual, authorizationHeaderCall1)
  168. Convey("third call to first route should add cached access token", func() {
  169. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  170. client = newFakeHTTPClient([]byte{})
  171. proxy3 := NewDataSourceProxy(ds, plugin, ctx, "pathwithtoken1")
  172. proxy3.route = plugin.Routes[0]
  173. proxy3.applyRoute(req)
  174. authorizationHeaderCall3 := req.Header.Get("Authorization")
  175. So(req.URL.String(), ShouldEqual, "https://api.nr1.io/some/path")
  176. So(authorizationHeaderCall1, ShouldStartWith, "Bearer eyJ0e")
  177. So(authorizationHeaderCall3, ShouldStartWith, "Bearer eyJ0e")
  178. So(authorizationHeaderCall3, ShouldEqual, authorizationHeaderCall1)
  179. })
  180. })
  181. })
  182. })
  183. })
  184. Convey("When proxying graphite", func() {
  185. plugin := &plugins.DataSourcePlugin{}
  186. ds := &m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE}
  187. ctx := &m.ReqContext{}
  188. proxy := NewDataSourceProxy(ds, plugin, ctx, "/render")
  189. requestURL, _ := url.Parse("http://grafana.com/sub")
  190. req := http.Request{URL: requestURL}
  191. proxy.getDirector()(&req)
  192. Convey("Can translate request url and path", func() {
  193. So(req.URL.Host, ShouldEqual, "graphite:8080")
  194. So(req.URL.Path, ShouldEqual, "/render")
  195. })
  196. })
  197. Convey("When proxying InfluxDB", func() {
  198. plugin := &plugins.DataSourcePlugin{}
  199. ds := &m.DataSource{
  200. Type: m.DS_INFLUXDB_08,
  201. Url: "http://influxdb:8083",
  202. Database: "site",
  203. User: "user",
  204. Password: "password",
  205. }
  206. ctx := &m.ReqContext{}
  207. proxy := NewDataSourceProxy(ds, plugin, ctx, "")
  208. requestURL, _ := url.Parse("http://grafana.com/sub")
  209. req := http.Request{URL: requestURL}
  210. proxy.getDirector()(&req)
  211. Convey("Should add db to url", func() {
  212. So(req.URL.Path, ShouldEqual, "/db/site/")
  213. })
  214. Convey("Should add username and password", func() {
  215. queryVals := req.URL.Query()
  216. So(queryVals["u"][0], ShouldEqual, "user")
  217. So(queryVals["p"][0], ShouldEqual, "password")
  218. })
  219. })
  220. Convey("When proxying a data source with no keepCookies specified", func() {
  221. plugin := &plugins.DataSourcePlugin{}
  222. json, _ := simplejson.NewJson([]byte(`{"keepCookies": []}`))
  223. ds := &m.DataSource{
  224. Type: m.DS_GRAPHITE,
  225. Url: "http://graphite:8086",
  226. JsonData: json,
  227. }
  228. ctx := &m.ReqContext{}
  229. proxy := NewDataSourceProxy(ds, plugin, ctx, "")
  230. requestURL, _ := url.Parse("http://grafana.com/sub")
  231. req := http.Request{URL: requestURL, Header: make(http.Header)}
  232. cookies := "grafana_user=admin; grafana_remember=99; grafana_sess=11; JSESSION_ID=test"
  233. req.Header.Set("Cookie", cookies)
  234. proxy.getDirector()(&req)
  235. Convey("Should clear all cookies", func() {
  236. So(req.Header.Get("Cookie"), ShouldEqual, "")
  237. })
  238. })
  239. Convey("When proxying a data source with keep cookies specified", func() {
  240. plugin := &plugins.DataSourcePlugin{}
  241. json, _ := simplejson.NewJson([]byte(`{"keepCookies": ["JSESSION_ID"]}`))
  242. ds := &m.DataSource{
  243. Type: m.DS_GRAPHITE,
  244. Url: "http://graphite:8086",
  245. JsonData: json,
  246. }
  247. ctx := &m.ReqContext{}
  248. proxy := NewDataSourceProxy(ds, plugin, ctx, "")
  249. requestURL, _ := url.Parse("http://grafana.com/sub")
  250. req := http.Request{URL: requestURL, Header: make(http.Header)}
  251. cookies := "grafana_user=admin; grafana_remember=99; grafana_sess=11; JSESSION_ID=test"
  252. req.Header.Set("Cookie", cookies)
  253. proxy.getDirector()(&req)
  254. Convey("Should keep named cookies", func() {
  255. So(req.Header.Get("Cookie"), ShouldEqual, "JSESSION_ID=test")
  256. })
  257. })
  258. Convey("When interpolating string", func() {
  259. data := templateData{
  260. SecureJsonData: map[string]string{
  261. "Test": "0asd+asd",
  262. },
  263. }
  264. interpolated, err := interpolateString("{{.SecureJsonData.Test}}", data)
  265. So(err, ShouldBeNil)
  266. So(interpolated, ShouldEqual, "0asd+asd")
  267. })
  268. })
  269. }
  270. type httpClientStub struct {
  271. fakeBody []byte
  272. }
  273. func (c *httpClientStub) Do(req *http.Request) (*http.Response, error) {
  274. bodyJSON, _ := simplejson.NewJson(c.fakeBody)
  275. _, passedTokenCacheTest := bodyJSON.CheckGet("expires_on")
  276. So(passedTokenCacheTest, ShouldBeTrue)
  277. bodyJSON.Set("expires_on", fmt.Sprint(time.Now().Add(time.Second*60).Unix()))
  278. body, _ := bodyJSON.MarshalJSON()
  279. resp := &http.Response{
  280. Body: ioutil.NopCloser(bytes.NewReader(body)),
  281. }
  282. return resp, nil
  283. }
  284. func newFakeHTTPClient(fakeBody []byte) httpClient {
  285. return &httpClientStub{
  286. fakeBody: fakeBody,
  287. }
  288. }