2018-09-10 09:59:29 -05:00
|
|
|
package pluginproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2018-09-10 09:59:29 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
// ApplyRoute should use the plugin route data to set auth headers and custom headers.
|
2021-03-08 00:02:49 -06:00
|
|
|
func ApplyRoute(ctx context.Context, req *http.Request, proxyPath string, route *plugins.AppPluginRoute,
|
|
|
|
ds *models.DataSource) {
|
2018-09-10 09:59:29 -05:00
|
|
|
proxyPath = strings.TrimPrefix(proxyPath, route.Path)
|
|
|
|
|
|
|
|
data := templateData{
|
|
|
|
JsonData: ds.JsonData.Interface().(map[string]interface{}),
|
|
|
|
SecureJsonData: ds.SecureJsonData.Decrypt(),
|
|
|
|
}
|
|
|
|
|
2020-09-18 06:22:07 -05:00
|
|
|
if len(route.URL) > 0 {
|
2020-11-17 03:56:42 -06:00
|
|
|
interpolatedURL, err := interpolateString(route.URL, data)
|
2020-09-18 06:22:07 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error interpolating proxy url", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
routeURL, err := url.Parse(interpolatedURL)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error parsing plugin route url", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.URL.Scheme = routeURL.Scheme
|
|
|
|
req.URL.Host = routeURL.Host
|
|
|
|
req.Host = routeURL.Host
|
|
|
|
req.URL.Path = util.JoinURLFragments(routeURL.Path, proxyPath)
|
2018-09-10 09:59:29 -05:00
|
|
|
}
|
|
|
|
|
2020-04-24 03:32:13 -05:00
|
|
|
if err := addQueryString(req, route, data); err != nil {
|
|
|
|
logger.Error("Failed to render plugin URL query string", "error", err)
|
|
|
|
}
|
|
|
|
|
2018-09-10 09:59:29 -05:00
|
|
|
if err := addHeaders(&req.Header, route, data); err != nil {
|
|
|
|
logger.Error("Failed to render plugin headers", "error", err)
|
|
|
|
}
|
|
|
|
|
2021-03-31 09:38:35 -05:00
|
|
|
if err := setBodyContent(req, route, data); err != nil {
|
|
|
|
logger.Error("Failed to set plugin route body content", "error", err)
|
|
|
|
}
|
|
|
|
|
2021-05-03 07:46:32 -05:00
|
|
|
if tokenProvider := getTokenProvider(ctx, ds, route, data); tokenProvider != nil {
|
|
|
|
if token, err := tokenProvider.getAccessToken(); err != nil {
|
2018-09-10 09:59:29 -05:00
|
|
|
logger.Error("Failed to get access token", "error", err)
|
|
|
|
} else {
|
2018-11-30 13:12:55 -06:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
2018-09-10 09:59:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 07:46:32 -05:00
|
|
|
logger.Info("Requesting", "url", req.URL.String())
|
|
|
|
}
|
2018-10-08 06:49:27 -05:00
|
|
|
|
2021-05-03 07:46:32 -05:00
|
|
|
func getTokenProvider(ctx context.Context, ds *models.DataSource, pluginRoute *plugins.AppPluginRoute,
|
|
|
|
data templateData) accessTokenProvider {
|
|
|
|
authenticationType := ds.JsonData.Get("authenticationType").MustString()
|
|
|
|
|
|
|
|
switch authenticationType {
|
|
|
|
case "gce":
|
|
|
|
return newGceAccessTokenProvider(ctx, ds, pluginRoute)
|
|
|
|
case "jwt":
|
|
|
|
if pluginRoute.JwtTokenAuth != nil {
|
|
|
|
return newJwtAccessTokenProvider(ctx, ds, pluginRoute, data)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Fallback to authentication options when authentication type isn't explicitly configured
|
|
|
|
if pluginRoute.TokenAuth != nil {
|
|
|
|
return newGenericAccessTokenProvider(ds, pluginRoute, data)
|
|
|
|
}
|
|
|
|
if pluginRoute.JwtTokenAuth != nil {
|
|
|
|
return newJwtAccessTokenProvider(ctx, ds, pluginRoute, data)
|
2018-10-03 10:08:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 07:46:32 -05:00
|
|
|
return nil
|
2018-09-10 09:59:29 -05:00
|
|
|
}
|