ds_proxy_test.go 11 KB

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