2015-10-06 04:20:50 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-11-17 05:04:22 -06:00
|
|
|
"context"
|
2016-08-24 22:43:25 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-09-13 06:32:11 -05:00
|
|
|
"strings"
|
2016-08-24 22:43:25 -05:00
|
|
|
"time"
|
|
|
|
|
2016-02-10 09:43:35 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/pluginproxy"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2015-10-06 04:20:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2020-03-04 05:57:20 -06:00
|
|
|
"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"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-10-06 04:20:50 -05:00
|
|
|
)
|
|
|
|
|
2017-09-28 05:10:59 -05:00
|
|
|
var pluginProxyTransport *http.Transport
|
2021-11-08 10:56:56 -06:00
|
|
|
var applog = log.New("app.routes")
|
2016-08-24 22:43:25 -05:00
|
|
|
|
2021-10-11 07:30:59 -05:00
|
|
|
func (hs *HTTPServer) initAppPluginRoutes(r *web.Mux) {
|
2017-09-28 05:10:59 -05:00
|
|
|
pluginProxyTransport = &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
2019-04-12 06:46:42 -05:00
|
|
|
InsecureSkipVerify: hs.Cfg.PluginsAppsSkipVerifyTLS,
|
2017-09-28 05:10:59 -05:00
|
|
|
Renegotiation: tls.RenegotiateFreelyAsClient,
|
|
|
|
},
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
2021-12-28 03:26:18 -06:00
|
|
|
for _, plugin := range hs.pluginStore.Plugins(context.Background(), plugins.App) {
|
2015-11-19 11:44:07 -06:00
|
|
|
for _, route := range plugin.Routes {
|
2021-11-01 04:53:33 -05:00
|
|
|
url := util.JoinURLFragments("/api/plugin-proxy/"+plugin.ID, route.Path)
|
2021-10-11 07:30:59 -05:00
|
|
|
handlers := make([]web.Handler, 0)
|
2016-02-10 09:43:35 -06:00
|
|
|
handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{
|
2017-08-23 03:52:31 -05:00
|
|
|
ReqSignedIn: true,
|
2016-02-10 09:43:35 -06:00
|
|
|
}))
|
|
|
|
|
|
|
|
if route.ReqRole != "" {
|
2020-03-04 05:57:20 -06:00
|
|
|
if route.ReqRole == models.ROLE_ADMIN {
|
|
|
|
handlers = append(handlers, middleware.RoleAuth(models.ROLE_ADMIN))
|
|
|
|
} else if route.ReqRole == models.ROLE_EDITOR {
|
|
|
|
handlers = append(handlers, middleware.RoleAuth(models.ROLE_EDITOR, models.ROLE_ADMIN))
|
2015-11-27 02:26:30 -06:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 04:53:33 -05:00
|
|
|
handlers = append(handlers, AppPluginRoute(route, plugin.ID, hs))
|
2021-09-13 06:32:11 -05:00
|
|
|
for _, method := range strings.Split(route.Method, ",") {
|
|
|
|
r.Handle(strings.TrimSpace(method), url, handlers)
|
|
|
|
}
|
2021-11-08 10:56:56 -06:00
|
|
|
|
|
|
|
applog.Debug("Plugins: Adding proxy route", "url", url)
|
2015-10-06 04:20:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
func AppPluginRoute(route *plugins.Route, appID string, hs *HTTPServer) web.Handler {
|
2020-03-04 05:57:20 -06:00
|
|
|
return func(c *models.ReqContext) {
|
2021-10-11 07:30:59 -05:00
|
|
|
path := web.Params(c.Req)["*"]
|
2015-10-06 04:20:50 -05:00
|
|
|
|
2021-11-04 11:47:21 -05:00
|
|
|
proxy := pluginproxy.NewApiPluginProxy(c, path, route, appID, hs.Cfg, hs.SecretsService)
|
2016-08-24 22:43:25 -05:00
|
|
|
proxy.Transport = pluginProxyTransport
|
2021-09-01 04:18:30 -05:00
|
|
|
proxy.ServeHTTP(c.Resp, c.Req)
|
2015-10-06 04:20:50 -05:00
|
|
|
}
|
|
|
|
}
|