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