Commit df215d9d by Erik Sundell

stackdriver: WIP - test retrieving project id from gce metadata

parent fbe0ffd1
...@@ -64,11 +64,33 @@ func ApplyRoute(ctx context.Context, req *http.Request, proxyPath string, route ...@@ -64,11 +64,33 @@ func ApplyRoute(ctx context.Context, req *http.Request, proxyPath string, route
} }
if req.Header.Get("Authorization") == "" && ds.Type == "stackdriver" { if req.Header.Get("Authorization") == "" && ds.Type == "stackdriver" {
tokenSrc, err := google.DefaultTokenSource(ctx, "https://www.googleapis.com/auth/monitoring.read") defaultCredentials, err := google.FindDefaultCredentials(ctx)
projectName := ds.JsonData.Get("defaultProject").MustString()
if projectName == "" {
ds.JsonData.Set("defaultProject", defaultCredentials.ProjectID)
}
if err != nil {
logger.Error("Failed to get default credentials", "error", err)
} else {
token, err := defaultCredentials.TokenSource.Token()
if err != nil {
logger.Error("Failed to get default access token", "error", err)
} else {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken))
}
}
}
if req.Header.Get("Authorization") == "" && ds.Type == "stackdriver" {
credentials, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/monitoring.read")
projectName := ds.JsonData.Get("defaultProject").MustString()
if projectName == "" {
ds.JsonData.Set("defaultProject", credentials.ProjectID)
}
if err != nil { if err != nil {
logger.Error("Failed to get access token", "error", err) logger.Error("Failed to get access token", "error", err)
} else { } else {
token, err := tokenSrc.Token() token, err := credentials.TokenSource.Token()
if err != nil { if err != nil {
logger.Error("Failed to get access token", "error", err) logger.Error("Failed to get access token", "error", err)
} else { } else {
......
...@@ -15,6 +15,8 @@ import ( ...@@ -15,6 +15,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
"golang.org/x/net/context/ctxhttp" "golang.org/x/net/context/ctxhttp"
"github.com/grafana/grafana/pkg/api/pluginproxy" "github.com/grafana/grafana/pkg/api/pluginproxy"
...@@ -551,6 +553,7 @@ func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models. ...@@ -551,6 +553,7 @@ func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models.
return nil, errors.New("Unable to find datasource plugin Stackdriver") return nil, errors.New("Unable to find datasource plugin Stackdriver")
} }
projectName := dsInfo.JsonData.Get("defaultProject").MustString() projectName := dsInfo.JsonData.Get("defaultProject").MustString()
logger.Info("projectName", "projectName", projectName)
proxyPass := fmt.Sprintf("stackdriver%s", "v3/projects/"+projectName+"/timeSeries") proxyPass := fmt.Sprintf("stackdriver%s", "v3/projects/"+projectName+"/timeSeries")
var stackdriverRoute *plugins.AppPluginRoute var stackdriverRoute *plugins.AppPluginRoute
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment