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-01-13 08:51:47 -06:00
|
|
|
"gopkg.in/macaron.v1"
|
|
|
|
|
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"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
2016-08-24 22:43:25 -05:00
|
|
|
var pluginProxyTransport = &http.Transport{
|
2017-09-13 18:20:24 -05:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
2017-09-24 11:59:21 -05:00
|
|
|
Renegotiation: tls.RenegotiateFreelyAsClient,
|
2017-09-13 18:20:24 -05:00
|
|
|
},
|
2017-09-24 11:59:21 -05:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2016-08-24 22:43:25 -05:00
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
2017-09-28 00:25:00 -05:00
|
|
|
DualStack: true,
|
2016-08-24 22:43:25 -05:00
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
2016-01-21 11:15:04 -06:00
|
|
|
func InitAppPluginRoutes(r *macaron.Macaron) {
|
|
|
|
for _, plugin := range plugins.Apps {
|
2015-11-19 11:44:07 -06:00
|
|
|
for _, route := range plugin.Routes {
|
2016-02-10 09:43:35 -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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 11:15:04 -06:00
|
|
|
func AppPluginRoute(route *plugins.AppPluginRoute, appId string) macaron.Handler {
|
2015-10-06 04:20:50 -05:00
|
|
|
return func(c *middleware.Context) {
|
|
|
|
path := c.Params("*")
|
|
|
|
|
2016-02-10 09:43:35 -06: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
|
|
|
}
|
|
|
|
}
|