ds_proxy_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. plugin := &plugins.DataSourcePlugin{}
  187. ds := &m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE}
  188. ctx := &m.ReqContext{}
  189. proxy := NewDataSourceProxy(ds, plugin, ctx, "/render")
  190. requestURL, _ := url.Parse("http://grafana.com/sub")
  191. req := http.Request{URL: requestURL, Header: http.Header{}}
  192. proxy.getDirector()(&req)
  193. Convey("Can translate request url and path", func() {
  194. So(req.URL.Host, ShouldEqual, "graphite:8080")
  195. So(req.URL.Path, ShouldEqual, "/render")
  196. })
  197. })
  198. Convey("When proxying InfluxDB", func() {
  199. plugin := &plugins.DataSourcePlugin{}
  200. ds := &m.DataSource{
  201. Type: m.DS_INFLUXDB_08,
  202. Url: "http://influxdb:8083",
  203. Database: "site",
  204. User: "user",
  205. Password: "password",
  206. }
  207. ctx := &m.ReqContext{}
  208. proxy := NewDataSourceProxy(ds, plugin, ctx, "")
  209. requestURL, _ := url.Parse("http://grafana.com/sub")
  210. req := http.Request{URL: requestURL, Header: http.Header{}}
  211. proxy.getDirector()(&req)
  212. Convey("Should add db to url", func() {
  213. So(req.URL.Path, ShouldEqual, "/db/site/")
  214. })
  215. Convey("Should add username and password", func() {
  216. queryVals := req.URL.Query()
  217. So(queryVals["u"][0], ShouldEqual, "user")
  218. So(queryVals["p"][0], ShouldEqual, "password")
  219. })
  220. })
  221. Convey("When proxying a data source with no keepCookies specified", func() {
  222. plugin := &plugins.DataSourcePlugin{}
  223. json, _ := simplejson.NewJson([]byte(`{"keepCookies": []}`))
  224. ds := &m.DataSource{
  225. Type: m.DS_GRAPHITE,
  226. Url: "http://graphite:8086",
  227. JsonData: json,
  228. }
  229. ctx := &m.ReqContext{}
  230. proxy := NewDataSourceProxy(ds, plugin, ctx, "")
  231. requestURL, _ := url.Parse("http://grafana.com/sub")
  232. req := http.Request{URL: requestURL, Header: make(http.Header)}
  233. cookies := "grafana_user=admin; grafana_remember=99; grafana_sess=11; JSESSION_ID=test"
  234. req.Header.Set("Cookie", cookies)
  235. proxy.getDirector()(&req)
  236. Convey("Should clear all cookies", func() {
  237. So(req.Header.Get("Cookie"), ShouldEqual, "")
  238. })
  239. })
  240. Convey("When proxying a data source with keep cookies specified", func() {
  241. plugin := &plugins.DataSourcePlugin{}
  242. json, _ := simplejson.NewJson([]byte(`{"keepCookies": ["JSESSION_ID"]}`))
  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 keep named cookies", func() {
  256. So(req.Header.Get("Cookie"), ShouldEqual, "JSESSION_ID=test")
  257. })
  258. })
  259. Convey("When interpolating string", func() {
  260. data := templateData{
  261. SecureJsonData: map[string]string{
  262. "Test": "0asd+asd",
  263. },
  264. }
  265. interpolated, err := interpolateString("{{.SecureJsonData.Test}}", data)
  266. So(err, ShouldBeNil)
  267. So(interpolated, ShouldEqual, "0asd+asd")
  268. })
  269. Convey("When proxying a data source with custom headers specified", func() {
  270. plugin := &plugins.DataSourcePlugin{}
  271. encryptedData, err := util.Encrypt([]byte(`Bearer xf5yhfkpsnmgo`), setting.SecretKey)
  272. ds := &m.DataSource{
  273. Type: m.DS_PROMETHEUS,
  274. Url: "http://prometheus:9090",
  275. JsonData: simplejson.NewFromAny(map[string]interface{}{
  276. "httpHeaderName1": "Authorization",
  277. }),
  278. SecureJsonData: map[string][]byte{
  279. "httpHeaderValue1": encryptedData,
  280. },
  281. }
  282. ctx := &m.ReqContext{}
  283. proxy := NewDataSourceProxy(ds, plugin, ctx, "")
  284. requestURL, _ := url.Parse("http://grafana.com/sub")
  285. req := http.Request{URL: requestURL, Header: make(http.Header)}
  286. proxy.getDirector()(&req)
  287. if err != nil {
  288. log.Fatal(4, err.Error())
  289. }
  290. Convey("Match header value after decryption", func() {
  291. So(req.Header.Get("Authorization"), ShouldEqual, "Bearer xf5yhfkpsnmgo")
  292. })
  293. })
  294. })
  295. }
  296. type httpClientStub struct {
  297. fakeBody []byte
  298. }
  299. func (c *httpClientStub) Do(req *http.Request) (*http.Response, error) {
  300. bodyJSON, _ := simplejson.NewJson(c.fakeBody)
  301. _, passedTokenCacheTest := bodyJSON.CheckGet("expires_on")
  302. So(passedTokenCacheTest, ShouldBeTrue)
  303. bodyJSON.Set("expires_on", fmt.Sprint(time.Now().Add(time.Second*60).Unix()))
  304. body, _ := bodyJSON.MarshalJSON()
  305. resp := &http.Response{
  306. Body: ioutil.NopCloser(bytes.NewReader(body)),
  307. }
  308. return resp, nil
  309. }
  310. func newFakeHTTPClient(fakeBody []byte) httpClient {
  311. return &httpClientStub{
  312. fakeBody: fakeBody,
  313. }
  314. }