2015-10-06 04:20:50 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-11-19 11:44:07 -06:00
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
|
2015-10-06 04:20:50 -05:00
|
|
|
"github.com/Unknwon/macaron"
|
2015-11-11 00:30:07 -06:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2015-10-06 04:20:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2015-11-27 02:26:30 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2015-10-06 04:20:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
2015-12-17 09:53:58 -06:00
|
|
|
func InitApiPluginRoutes(r *macaron.Macaron) {
|
|
|
|
for _, plugin := range plugins.ApiPlugins {
|
|
|
|
log.Info("Plugin: Adding proxy routes for api plugin")
|
2015-11-19 11:44:07 -06:00
|
|
|
for _, route := range plugin.Routes {
|
2015-11-20 02:43:10 -06:00
|
|
|
url := util.JoinUrlFragments("/api/plugin-proxy/", route.Path)
|
2015-11-27 02:26:30 -06:00
|
|
|
handlers := make([]macaron.Handler, 0)
|
|
|
|
if route.ReqSignedIn {
|
|
|
|
handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true}))
|
|
|
|
}
|
|
|
|
if route.ReqGrafanaAdmin {
|
|
|
|
handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true}))
|
|
|
|
}
|
|
|
|
if route.ReqSignedIn && route.ReqRole != "" {
|
|
|
|
if route.ReqRole == m.ROLE_ADMIN {
|
|
|
|
handlers = append(handlers, middleware.RoleAuth(m.ROLE_ADMIN))
|
|
|
|
} else if route.ReqRole == m.ROLE_EDITOR {
|
|
|
|
handlers = append(handlers, middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN))
|
|
|
|
}
|
|
|
|
}
|
2015-12-17 09:53:58 -06:00
|
|
|
handlers = append(handlers, ApiPlugin(route.Url))
|
2015-11-27 02:26:30 -06:00
|
|
|
r.Route(url, route.Method, handlers...)
|
2015-11-20 02:43:10 -06:00
|
|
|
log.Info("Plugin: Adding route %s", url)
|
2015-10-06 04:20:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 09:53:58 -06:00
|
|
|
func ApiPlugin(routeUrl string) macaron.Handler {
|
2015-10-06 04:20:50 -05:00
|
|
|
return func(c *middleware.Context) {
|
|
|
|
path := c.Params("*")
|
|
|
|
|
|
|
|
//Create a HTTP header with the context in it.
|
|
|
|
ctx, err := json.Marshal(c.SignedInUser)
|
|
|
|
if err != nil {
|
2015-11-11 00:30:07 -06:00
|
|
|
c.JsonApiErr(500, "failed to marshal context to json.", err)
|
2015-10-06 04:20:50 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
targetUrl, _ := url.Parse(routeUrl)
|
2015-12-17 09:53:58 -06:00
|
|
|
proxy := NewApiPluginProxy(string(ctx), path, targetUrl)
|
2015-10-06 04:20:50 -05:00
|
|
|
proxy.Transport = dataProxyTransport
|
|
|
|
proxy.ServeHTTP(c.RW(), c.Req.Request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 09:53:58 -06:00
|
|
|
func NewApiPluginProxy(ctx string, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy {
|
2015-10-06 04:20:50 -05:00
|
|
|
director := func(req *http.Request) {
|
|
|
|
req.URL.Scheme = targetUrl.Scheme
|
|
|
|
req.URL.Host = targetUrl.Host
|
|
|
|
req.Host = targetUrl.Host
|
|
|
|
|
|
|
|
req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
|
|
|
|
|
|
|
|
// clear cookie headers
|
|
|
|
req.Header.Del("Cookie")
|
|
|
|
req.Header.Del("Set-Cookie")
|
|
|
|
req.Header.Add("Grafana-Context", ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &httputil.ReverseProxy{Director: director}
|
|
|
|
}
|