2022-05-06 03:58:02 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2022-06-27 11:23:15 -05:00
|
|
|
|
2023-09-11 05:13:13 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware/requestmeta"
|
2023-09-25 05:10:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2023-04-28 07:02:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/httpresponsesender"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-06-27 11:23:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2022-05-06 03:58:02 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util/proxyutil"
|
|
|
|
"github.com/grafana/grafana/pkg/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CallResource passes a resource call from a plugin to the backend plugin.
|
|
|
|
//
|
|
|
|
// /api/plugins/:pluginId/resources/*
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) CallResource(c *contextmodel.ReqContext) {
|
2022-05-06 03:58:02 -05:00
|
|
|
hs.callPluginResource(c, web.Params(c.Req)[":pluginId"])
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) callPluginResource(c *contextmodel.ReqContext, pluginID string) {
|
2023-10-06 04:34:36 -05:00
|
|
|
pCtx, err := hs.pluginContextProvider.Get(c.Req.Context(), pluginID, c.SignedInUser, c.SignedInUser.GetOrgID())
|
2022-05-06 03:58:02 -05:00
|
|
|
if err != nil {
|
2023-09-25 05:10:47 -05:00
|
|
|
if errors.Is(err, plugins.ErrPluginNotRegistered) {
|
2023-06-08 06:59:51 -05:00
|
|
|
c.JsonApiErr(404, "Plugin not found", nil)
|
|
|
|
return
|
|
|
|
}
|
2022-05-06 03:58:02 -05:00
|
|
|
c.JsonApiErr(500, "Failed to get plugin settings", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := hs.pluginResourceRequest(c)
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(http.StatusBadRequest, "Failed for create plugin resource request", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = hs.makePluginResourceRequest(c.Resp, req, pCtx); err != nil {
|
|
|
|
handleCallResourceError(err, c)
|
2023-09-11 05:13:13 -05:00
|
|
|
return
|
2022-05-06 03:58:02 -05:00
|
|
|
}
|
2023-09-11 05:13:13 -05:00
|
|
|
|
|
|
|
requestmeta.WithStatusSource(c.Req.Context(), c.Resp.Status())
|
2022-05-06 03:58:02 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) callPluginResourceWithDataSource(c *contextmodel.ReqContext, pluginID string, ds *datasources.DataSource) {
|
2023-06-08 06:59:51 -05:00
|
|
|
pCtx, err := hs.pluginContextProvider.GetWithDataSource(c.Req.Context(), pluginID, c.SignedInUser, ds)
|
2022-05-06 03:58:02 -05:00
|
|
|
if err != nil {
|
2023-09-25 05:10:47 -05:00
|
|
|
if errors.Is(err, plugins.ErrPluginNotRegistered) {
|
2023-06-08 06:59:51 -05:00
|
|
|
c.JsonApiErr(404, "Plugin not found", nil)
|
|
|
|
return
|
|
|
|
}
|
2022-05-06 03:58:02 -05:00
|
|
|
c.JsonApiErr(500, "Failed to get plugin settings", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var dsURL string
|
|
|
|
if pCtx.DataSourceInstanceSettings != nil {
|
|
|
|
dsURL = pCtx.DataSourceInstanceSettings.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
err = hs.PluginRequestValidator.Validate(dsURL, c.Req)
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(http.StatusForbidden, "Access denied", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := hs.pluginResourceRequest(c)
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(http.StatusBadRequest, "Failed for create plugin resource request", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = hs.makePluginResourceRequest(c.Resp, req, pCtx); err != nil {
|
|
|
|
handleCallResourceError(err, c)
|
2023-09-11 05:13:13 -05:00
|
|
|
return
|
2022-05-06 03:58:02 -05:00
|
|
|
}
|
2023-09-11 05:13:13 -05:00
|
|
|
|
|
|
|
requestmeta.WithStatusSource(c.Req.Context(), c.Resp.Status())
|
2022-05-06 03:58:02 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) pluginResourceRequest(c *contextmodel.ReqContext) (*http.Request, error) {
|
2022-05-06 03:58:02 -05:00
|
|
|
clonedReq := c.Req.Clone(c.Req.Context())
|
|
|
|
rawURL := web.Params(c.Req)["*"]
|
|
|
|
if clonedReq.URL.RawQuery != "" {
|
|
|
|
rawURL += "?" + clonedReq.URL.RawQuery
|
|
|
|
}
|
|
|
|
urlPath, err := url.Parse(rawURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clonedReq.URL = urlPath
|
|
|
|
|
|
|
|
return clonedReq, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HTTPServer) makePluginResourceRequest(w http.ResponseWriter, req *http.Request, pCtx backend.PluginContext) error {
|
|
|
|
proxyutil.PrepareProxyRequest(req)
|
|
|
|
|
2022-08-10 08:37:51 -05:00
|
|
|
body, err := io.ReadAll(req.Body)
|
2022-05-06 03:58:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read request body: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
crReq := &backend.CallResourceRequest{
|
|
|
|
PluginContext: pCtx,
|
|
|
|
Path: req.URL.Path,
|
|
|
|
Method: req.Method,
|
|
|
|
URL: req.URL.String(),
|
|
|
|
Headers: req.Header,
|
|
|
|
Body: body,
|
|
|
|
}
|
|
|
|
|
2023-04-28 07:02:27 -05:00
|
|
|
httpSender := httpresponsesender.New(w)
|
|
|
|
return hs.pluginClient.CallResource(req.Context(), crReq, httpSender)
|
2022-05-06 03:58:02 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func handleCallResourceError(err error, reqCtx *contextmodel.ReqContext) {
|
2023-09-11 05:13:13 -05:00
|
|
|
resp := response.ErrOrFallback(http.StatusInternalServerError, "Failed to call resource", err)
|
|
|
|
resp.WriteTo(reqCtx)
|
2022-05-06 03:58:02 -05:00
|
|
|
}
|