2015-10-06 04:20:50 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-01-20 21:41:59 -06:00
|
|
|
"bytes"
|
2015-10-06 04:20:50 -05:00
|
|
|
"encoding/json"
|
2016-01-20 21:41:59 -06:00
|
|
|
"fmt"
|
2015-11-19 11:44:07 -06:00
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2016-01-20 21:41:59 -06:00
|
|
|
"text/template"
|
2015-11-19 11:44:07 -06:00
|
|
|
|
2016-01-13 08:51:47 -06:00
|
|
|
"gopkg.in/macaron.v1"
|
|
|
|
|
2016-01-20 21:41:59 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
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))
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 21:41:59 -06:00
|
|
|
handlers = append(handlers, ApiPlugin(route, plugin.IncludedInAppId))
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 21:41:59 -06:00
|
|
|
func ApiPlugin(route *plugins.ApiPluginRoute, includedInAppId string) macaron.Handler {
|
2015-10-06 04:20:50 -05:00
|
|
|
return func(c *middleware.Context) {
|
|
|
|
path := c.Params("*")
|
|
|
|
|
2016-01-20 21:41:59 -06:00
|
|
|
proxy := NewApiPluginProxy(c, path, route, includedInAppId)
|
2015-10-06 04:20:50 -05:00
|
|
|
proxy.Transport = dataProxyTransport
|
2016-01-13 08:51:47 -06:00
|
|
|
proxy.ServeHTTP(c.Resp, c.Req.Request)
|
2015-10-06 04:20:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 21:41:59 -06:00
|
|
|
func NewApiPluginProxy(ctx *middleware.Context, proxyPath string, route *plugins.ApiPluginRoute, includedInAppId string) *httputil.ReverseProxy {
|
|
|
|
targetUrl, _ := url.Parse(route.Url)
|
|
|
|
|
2015-10-06 04:20:50 -05:00
|
|
|
director := func(req *http.Request) {
|
2016-01-20 21:41:59 -06:00
|
|
|
|
2015-10-06 04:20:50 -05:00
|
|
|
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")
|
2016-01-20 21:41:59 -06:00
|
|
|
|
|
|
|
//Create a HTTP header with the context in it.
|
|
|
|
ctxJson, err := json.Marshal(ctx.SignedInUser)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JsonApiErr(500, "failed to marshal context to json.", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Grafana-Context", string(ctxJson))
|
|
|
|
// add custom headers defined in the plugin config.
|
|
|
|
for _, header := range route.Headers {
|
|
|
|
var contentBuf bytes.Buffer
|
|
|
|
t, err := template.New("content").Parse(header.Content)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JsonApiErr(500, fmt.Sprintf("could not parse header content template for header %s.", header.Name), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonData := make(map[string]interface{})
|
|
|
|
|
|
|
|
if includedInAppId != "" {
|
|
|
|
//lookup appSettings
|
|
|
|
query := m.GetAppSettingByAppIdQuery{OrgId: ctx.OrgId, AppId: includedInAppId}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
ctx.JsonApiErr(500, "failed to get AppSettings of includedAppId.", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonData = query.Result.JsonData
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.Execute(&contentBuf, jsonData)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JsonApiErr(500, fmt.Sprintf("failed to execute header content template for header %s.", header.Name), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("Adding header to proxy request. %s: %s", header.Name, contentBuf.String())
|
|
|
|
req.Header.Add(header.Name, contentBuf.String())
|
|
|
|
}
|
2015-10-06 04:20:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &httputil.ReverseProxy{Director: director}
|
|
|
|
}
|