2015-10-06 04:20:50 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-08-24 22:43:25 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2016-02-10 09:43:35 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/pluginproxy"
|
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"
|
2017-09-28 05:10:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-10-06 04:20:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2017-09-28 05:14:12 -05:00
|
|
|
macaron "gopkg.in/macaron.v1"
|
2015-10-06 04:20:50 -05:00
|
|
|
)
|
|
|
|
|
2017-09-28 05:10:59 -05:00
|
|
|
var pluginProxyTransport *http.Transport
|
2016-08-24 22:43:25 -05:00
|
|
|
|
2018-07-02 01:35:50 -05:00
|
|
|
func (hs *HTTPServer) initAppPluginRoutes(r *macaron.Macaron) {
|
2017-09-28 05:10:59 -05:00
|
|
|
pluginProxyTransport = &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: setting.PluginAppsSkipVerifyTLS,
|
|
|
|
Renegotiation: tls.RenegotiateFreelyAsClient,
|
|
|
|
},
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
2016-01-21 11:15:04 -06:00
|
|
|
for _, plugin := range plugins.Apps {
|
2015-11-19 11:44:07 -06:00
|
|
|
for _, route := range plugin.Routes {
|
2019-01-28 15:18:48 -06:00
|
|
|
url := util.JoinURLFragments("/api/plugin-proxy/"+plugin.Id, route.Path)
|
2015-11-27 02:26:30 -06:00
|
|
|
handlers := make([]macaron.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 != "" {
|
2015-11-27 02:26:30 -06:00
|
|
|
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-21 11:15:04 -06:00
|
|
|
handlers = append(handlers, AppPluginRoute(route, plugin.Id))
|
2015-11-27 02:26:30 -06:00
|
|
|
r.Route(url, route.Method, handlers...)
|
2016-06-03 09:15:36 -05:00
|
|
|
log.Debug("Plugins: Adding proxy route %s", url)
|
2015-10-06 04:20:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
func AppPluginRoute(route *plugins.AppPluginRoute, appID string) macaron.Handler {
|
2018-03-07 10:54:50 -06:00
|
|
|
return func(c *m.ReqContext) {
|
2015-10-06 04:20:50 -05:00
|
|
|
path := c.Params("*")
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
proxy := pluginproxy.NewApiPluginProxy(c, path, route, appID)
|
2016-08-24 22:43:25 -05:00
|
|
|
proxy.Transport = pluginProxyTransport
|
2016-01-13 08:51:47 -06:00
|
|
|
proxy.ServeHTTP(c.Resp, c.Req.Request)
|
2015-10-06 04:20:50 -05:00
|
|
|
}
|
|
|
|
}
|