Browse Source

fetch token from GCE metadata server

Mitsuhiro Tanda 7 years ago
parent
commit
43aa6603a3
1 changed files with 16 additions and 0 deletions
  1. 16 0
      pkg/api/pluginproxy/ds_auth_provider.go

+ 16 - 0
pkg/api/pluginproxy/ds_auth_provider.go

@@ -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())
 
 }