2016-02-10 09:43:35 -06:00
|
|
|
package pluginproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-02-10 09:43:35 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/encryption"
|
2020-03-03 04:45:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-02-10 09:43:35 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2020-03-03 04:45:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util/proxyutil"
|
2016-02-10 09:43:35 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type templateData struct {
|
|
|
|
JsonData map[string]interface{}
|
|
|
|
SecureJsonData map[string]string
|
|
|
|
}
|
|
|
|
|
2019-05-07 11:55:39 -05:00
|
|
|
// NewApiPluginProxy create a plugin proxy
|
2021-03-08 00:02:49 -06:00
|
|
|
func NewApiPluginProxy(ctx *models.ReqContext, proxyPath string, route *plugins.AppPluginRoute,
|
2021-10-07 09:33:50 -05:00
|
|
|
appID string, cfg *setting.Cfg, encryptionService encryption.Service) *httputil.ReverseProxy {
|
2016-02-10 09:43:35 -06:00
|
|
|
director := func(req *http.Request) {
|
2020-11-17 03:56:42 -06:00
|
|
|
query := models.GetPluginSettingByIdQuery{OrgId: ctx.OrgId, PluginId: appID}
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
ctx.JsonApiErr(500, "Failed to fetch plugin settings", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
secureJsonData, err := encryptionService.DecryptJsonData(ctx.Req.Context(), query.Result.SecureJsonData, setting.SecretKey)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JsonApiErr(500, "Failed to decrypt plugin settings", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-17 03:56:42 -06:00
|
|
|
data := templateData{
|
|
|
|
JsonData: query.Result.JsonData,
|
2021-10-07 09:33:50 -05:00
|
|
|
SecureJsonData: secureJsonData,
|
2020-11-17 03:56:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
interpolatedURL, err := interpolateString(route.URL, data)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JsonApiErr(500, "Could not interpolate plugin route url", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
targetURL, err := url.Parse(interpolatedURL)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JsonApiErr(500, "Could not parse url", err)
|
|
|
|
return
|
|
|
|
}
|
2018-03-22 16:13:46 -05:00
|
|
|
req.URL.Scheme = targetURL.Scheme
|
|
|
|
req.URL.Host = targetURL.Host
|
|
|
|
req.Host = targetURL.Host
|
2019-01-28 15:18:48 -06:00
|
|
|
req.URL.Path = util.JoinURLFragments(targetURL.Path, proxyPath)
|
2020-11-17 03:56:42 -06:00
|
|
|
|
2016-02-10 09:43:35 -06:00
|
|
|
// clear cookie headers
|
|
|
|
req.Header.Del("Cookie")
|
|
|
|
req.Header.Del("Set-Cookie")
|
|
|
|
|
2020-03-03 04:45:16 -06:00
|
|
|
proxyutil.PrepareProxyRequest(req)
|
2017-05-24 06:39:40 -05:00
|
|
|
|
|
|
|
// Create a HTTP header with the context in it.
|
2019-05-07 11:55:39 -05:00
|
|
|
ctxJSON, err := json.Marshal(ctx.SignedInUser)
|
2016-02-10 09:43:35 -06:00
|
|
|
if err != nil {
|
|
|
|
ctx.JsonApiErr(500, "failed to marshal context to json.", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:15:17 -05:00
|
|
|
req.Header.Set("X-Grafana-Context", string(ctxJSON))
|
2016-02-10 09:43:35 -06:00
|
|
|
|
2020-06-11 14:15:17 -05:00
|
|
|
applyUserHeader(cfg.SendUserHeader, req, ctx.SignedInUser)
|
2019-03-14 07:04:47 -05:00
|
|
|
|
2020-11-17 03:56:42 -06:00
|
|
|
if err := addHeaders(&req.Header, route, data); err != nil {
|
|
|
|
ctx.JsonApiErr(500, "Failed to render plugin headers", err)
|
|
|
|
return
|
2019-05-07 11:55:39 -05:00
|
|
|
}
|
2021-03-31 09:38:35 -05:00
|
|
|
|
|
|
|
if err := setBodyContent(req, route, data); err != nil {
|
|
|
|
logger.Error("Failed to set plugin route body content", "error", err)
|
|
|
|
}
|
2016-02-10 09:43:35 -06:00
|
|
|
}
|
|
|
|
|
2020-11-17 03:56:42 -06:00
|
|
|
return &httputil.ReverseProxy{Director: director}
|
2016-02-10 09:43:35 -06:00
|
|
|
}
|