fetch token from GCE metadata server

This commit is contained in:
Mitsuhiro Tanda 2018-10-02 18:40:39 +09:00 committed by Erik Sundell
parent 022a45becd
commit 43aa6603a3

View File

@ -12,6 +12,7 @@ import (
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/util"
"golang.org/x/oauth2/google"
)
//ApplyRoute should use the plugin route data to set auth headers and custom headers
@ -61,6 +62,21 @@ func ApplyRoute(ctx context.Context, req *http.Request, proxyPath string, route
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
}
}
if req.Header.Get("Authorization") == "" && ds.Type == "stackdriver" {
tokenSrc, err := google.DefaultTokenSource(ctx, "https://www.googleapis.com/auth/monitoring.read")
if err != nil {
logger.Error("Failed to get access token", "error", err)
} else {
token, err := tokenSrc.Token()
if err != nil {
logger.Error("Failed to get access token", "error", err)
} else {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken))
}
}
}
logger.Info("Requesting", "url", req.URL.String())
}