app_routes.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package api
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "net/http/httputil"
  8. "net/url"
  9. "text/template"
  10. "gopkg.in/macaron.v1"
  11. "github.com/grafana/grafana/pkg/bus"
  12. "github.com/grafana/grafana/pkg/log"
  13. "github.com/grafana/grafana/pkg/middleware"
  14. m "github.com/grafana/grafana/pkg/models"
  15. "github.com/grafana/grafana/pkg/plugins"
  16. "github.com/grafana/grafana/pkg/util"
  17. )
  18. func InitAppPluginRoutes(r *macaron.Macaron) {
  19. for _, plugin := range plugins.Apps {
  20. for _, route := range plugin.Routes {
  21. log.Info("Plugin: Adding proxy route for app plugin")
  22. url := util.JoinUrlFragments("/api/plugin-proxy/", route.Path)
  23. handlers := make([]macaron.Handler, 0)
  24. if route.ReqSignedIn {
  25. handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true}))
  26. }
  27. if route.ReqGrafanaAdmin {
  28. handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true}))
  29. }
  30. if route.ReqSignedIn && route.ReqRole != "" {
  31. if route.ReqRole == m.ROLE_ADMIN {
  32. handlers = append(handlers, middleware.RoleAuth(m.ROLE_ADMIN))
  33. } else if route.ReqRole == m.ROLE_EDITOR {
  34. handlers = append(handlers, middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN))
  35. }
  36. }
  37. handlers = append(handlers, AppPluginRoute(route, plugin.Id))
  38. r.Route(url, route.Method, handlers...)
  39. log.Info("Plugin: Adding route %s", url)
  40. }
  41. }
  42. }
  43. func AppPluginRoute(route *plugins.AppPluginRoute, appId string) macaron.Handler {
  44. return func(c *middleware.Context) {
  45. path := c.Params("*")
  46. proxy := NewApiPluginProxy(c, path, route, appId)
  47. proxy.Transport = dataProxyTransport
  48. proxy.ServeHTTP(c.Resp, c.Req.Request)
  49. }
  50. }
  51. func NewApiPluginProxy(ctx *middleware.Context, proxyPath string, route *plugins.AppPluginRoute, appId string) *httputil.ReverseProxy {
  52. targetUrl, _ := url.Parse(route.Url)
  53. director := func(req *http.Request) {
  54. req.URL.Scheme = targetUrl.Scheme
  55. req.URL.Host = targetUrl.Host
  56. req.Host = targetUrl.Host
  57. req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
  58. // clear cookie headers
  59. req.Header.Del("Cookie")
  60. req.Header.Del("Set-Cookie")
  61. //Create a HTTP header with the context in it.
  62. ctxJson, err := json.Marshal(ctx.SignedInUser)
  63. if err != nil {
  64. ctx.JsonApiErr(500, "failed to marshal context to json.", err)
  65. return
  66. }
  67. req.Header.Add("Grafana-Context", string(ctxJson))
  68. // add custom headers defined in the plugin config.
  69. for _, header := range route.Headers {
  70. var contentBuf bytes.Buffer
  71. t, err := template.New("content").Parse(header.Content)
  72. if err != nil {
  73. ctx.JsonApiErr(500, fmt.Sprintf("could not parse header content template for header %s.", header.Name), err)
  74. return
  75. }
  76. //lookup appSettings
  77. query := m.GetAppSettingByAppIdQuery{OrgId: ctx.OrgId, AppId: appId}
  78. if err := bus.Dispatch(&query); err != nil {
  79. ctx.JsonApiErr(500, "failed to get AppSettings.", err)
  80. return
  81. }
  82. type templateData struct {
  83. JsonData map[string]interface{}
  84. SecureJsonData map[string]string
  85. }
  86. data := templateData{
  87. JsonData: query.Result.JsonData,
  88. SecureJsonData: query.Result.SecureJsonData.Decrypt(),
  89. }
  90. err = t.Execute(&contentBuf, data)
  91. if err != nil {
  92. ctx.JsonApiErr(500, fmt.Sprintf("failed to execute header content template for header %s.", header.Name), err)
  93. return
  94. }
  95. log.Debug("Adding header to proxy request. %s: %s", header.Name, contentBuf.String())
  96. req.Header.Add(header.Name, contentBuf.String())
  97. }
  98. }
  99. return &httputil.ReverseProxy{Director: director}
  100. }