ds_proxy_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package pluginproxy
  2. import (
  3. "net/http"
  4. "net/url"
  5. "testing"
  6. macaron "gopkg.in/macaron.v1"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/plugins"
  10. "github.com/grafana/grafana/pkg/setting"
  11. "github.com/grafana/grafana/pkg/util"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. func TestDSRouteRule(t *testing.T) {
  15. Convey("DataSourceProxy", t, func() {
  16. Convey("Plugin with routes", func() {
  17. plugin := &plugins.DataSourcePlugin{
  18. Routes: []*plugins.AppPluginRoute{
  19. {
  20. Path: "api/v4/",
  21. Url: "https://www.google.com",
  22. ReqRole: m.ROLE_EDITOR,
  23. Headers: []plugins.AppPluginRouteHeader{
  24. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  25. },
  26. },
  27. {
  28. Path: "api/admin",
  29. Url: "https://www.google.com",
  30. ReqRole: m.ROLE_ADMIN,
  31. Headers: []plugins.AppPluginRouteHeader{
  32. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  33. },
  34. },
  35. {
  36. Path: "api/anon",
  37. Url: "https://www.google.com",
  38. Headers: []plugins.AppPluginRouteHeader{
  39. {Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
  40. },
  41. },
  42. },
  43. }
  44. setting.SecretKey = "password"
  45. key, _ := util.Encrypt([]byte("123"), "password")
  46. ds := &m.DataSource{
  47. JsonData: simplejson.NewFromAny(map[string]interface{}{
  48. "clientId": "asd",
  49. }),
  50. SecureJsonData: map[string][]byte{
  51. "key": key,
  52. },
  53. }
  54. req, _ := http.NewRequest("GET", "http://localhost/asd", nil)
  55. ctx := &m.ReqContext{
  56. Context: &macaron.Context{
  57. Req: macaron.Request{Request: req},
  58. },
  59. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR},
  60. }
  61. Convey("When matching route path", func() {
  62. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method")
  63. proxy.route = plugin.Routes[0]
  64. proxy.applyRoute(req)
  65. Convey("should add headers and update url", func() {
  66. So(req.URL.String(), ShouldEqual, "https://www.google.com/some/method")
  67. So(req.Header.Get("x-header"), ShouldEqual, "my secret 123")
  68. })
  69. })
  70. Convey("Validating request", func() {
  71. Convey("plugin route with valid role", func() {
  72. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/v4/some/method")
  73. err := proxy.validateRequest()
  74. So(err, ShouldBeNil)
  75. })
  76. Convey("plugin route with admin role and user is editor", func() {
  77. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin")
  78. err := proxy.validateRequest()
  79. So(err, ShouldNotBeNil)
  80. })
  81. Convey("plugin route with admin role and user is admin", func() {
  82. ctx.SignedInUser.OrgRole = m.ROLE_ADMIN
  83. proxy := NewDataSourceProxy(ds, plugin, ctx, "api/admin")
  84. err := proxy.validateRequest()
  85. So(err, ShouldBeNil)
  86. })
  87. })
  88. })
  89. Convey("When proxying graphite", func() {
  90. plugin := &plugins.DataSourcePlugin{}
  91. ds := &m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE}
  92. ctx := &m.ReqContext{}
  93. proxy := NewDataSourceProxy(ds, plugin, ctx, "/render")
  94. requestUrl, _ := url.Parse("http://grafana.com/sub")
  95. req := http.Request{URL: requestUrl}
  96. proxy.getDirector()(&req)
  97. Convey("Can translate request url and path", func() {
  98. So(req.URL.Host, ShouldEqual, "graphite:8080")
  99. So(req.URL.Path, ShouldEqual, "/render")
  100. })
  101. })
  102. Convey("When proxying InfluxDB", func() {
  103. plugin := &plugins.DataSourcePlugin{}
  104. ds := &m.DataSource{
  105. Type: m.DS_INFLUXDB_08,
  106. Url: "http://influxdb:8083",
  107. Database: "site",
  108. User: "user",
  109. Password: "password",
  110. }
  111. ctx := &m.ReqContext{}
  112. proxy := NewDataSourceProxy(ds, plugin, ctx, "")
  113. requestUrl, _ := url.Parse("http://grafana.com/sub")
  114. req := http.Request{URL: requestUrl}
  115. proxy.getDirector()(&req)
  116. Convey("Should add db to url", func() {
  117. So(req.URL.Path, ShouldEqual, "/db/site/")
  118. })
  119. Convey("Should add username and password", func() {
  120. queryVals := req.URL.Query()
  121. So(queryVals["u"][0], ShouldEqual, "user")
  122. So(queryVals["p"][0], ShouldEqual, "password")
  123. })
  124. })
  125. Convey("When proxying a data source with no keepCookies specified", func() {
  126. plugin := &plugins.DataSourcePlugin{}
  127. json, _ := simplejson.NewJson([]byte(`{"keepCookies": []}`))
  128. ds := &m.DataSource{
  129. Type: m.DS_GRAPHITE,
  130. Url: "http://graphite:8086",
  131. JsonData: json,
  132. }
  133. ctx := &m.ReqContext{}
  134. proxy := NewDataSourceProxy(ds, plugin, ctx, "")
  135. requestUrl, _ := url.Parse("http://grafana.com/sub")
  136. req := http.Request{URL: requestUrl, Header: make(http.Header)}
  137. cookies := "grafana_user=admin; grafana_remember=99; grafana_sess=11; JSESSION_ID=test"
  138. req.Header.Set("Cookie", cookies)
  139. proxy.getDirector()(&req)
  140. Convey("Should clear all cookies", func() {
  141. So(req.Header.Get("Cookie"), ShouldEqual, "")
  142. })
  143. })
  144. Convey("When proxying a data source with keep cookies specified", func() {
  145. plugin := &plugins.DataSourcePlugin{}
  146. json, _ := simplejson.NewJson([]byte(`{"keepCookies": ["JSESSION_ID"]}`))
  147. ds := &m.DataSource{
  148. Type: m.DS_GRAPHITE,
  149. Url: "http://graphite:8086",
  150. JsonData: json,
  151. }
  152. ctx := &m.ReqContext{}
  153. proxy := NewDataSourceProxy(ds, plugin, ctx, "")
  154. requestUrl, _ := url.Parse("http://grafana.com/sub")
  155. req := http.Request{URL: requestUrl, Header: make(http.Header)}
  156. cookies := "grafana_user=admin; grafana_remember=99; grafana_sess=11; JSESSION_ID=test"
  157. req.Header.Set("Cookie", cookies)
  158. proxy.getDirector()(&req)
  159. Convey("Should keep named cookies", func() {
  160. So(req.Header.Get("Cookie"), ShouldEqual, "JSESSION_ID=test")
  161. })
  162. })
  163. Convey("When interpolating string", func() {
  164. data := templateData{
  165. SecureJsonData: map[string]string{
  166. "Test": "0asd+asd",
  167. },
  168. }
  169. interpolated, err := interpolateString("{{.SecureJsonData.Test}}", data)
  170. So(err, ShouldBeNil)
  171. So(interpolated, ShouldEqual, "0asd+asd")
  172. })
  173. })
  174. }