Commit ef3beb1f by Erik Sundell Committed by Daniel Lee

Adds poc code for retrieving google auth accesstoken

parent 834d06c3
......@@ -2,7 +2,7 @@ package pluginproxy
import (
"bytes"
"encoding/json"
"context"
"errors"
"fmt"
"io/ioutil"
......@@ -22,6 +22,7 @@ import (
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
"golang.org/x/oauth2/jwt"
)
var (
......@@ -353,49 +354,64 @@ func (proxy *DataSourceProxy) applyRoute(req *http.Request) {
}
func (proxy *DataSourceProxy) getAccessToken(data templateData) (string, error) {
if cachedToken, found := tokenCache[proxy.getAccessTokenCacheKey()]; found {
if cachedToken.ExpiresOn.After(time.Now().Add(time.Second * 10)) {
logger.Info("Using token from cache")
return cachedToken.AccessToken, nil
}
conf := jwt.Config{
Email: "raintank-production-stackdrive@raintank-production.iam.gserviceaccount.com",
PrivateKey: []byte("-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCb1u1Srw8ICWfX\nb+hh0qyRoWJzHkf4jxdpOjjqiaYqlipf3fCWoyNgHvQSmX2trYbb1Kg+8Nv9/iaz\n6el48MvOlRN6WbcMfhFoT8AKkdjYD3DM+vK3C3uXmWeVMHzQimjECFWsX4WRU4fj\nLJ44B3svFvShe3bRJpt2e5LdfjcEQER7Bvte/zQi8v1jloHmcptyz8wb9NddVHs9\nINwFbrSaUiQnoJTDSIYEMiKDTvZHRenmLz/RexjG5RXbdA+I9ZB5EASoEbG+7Ssc\nsn+Bhu/J29s/+NVM5sYgcjOm54NxCYFwYlApbroa0+KcDDCs4DzgzA41sOcvBWIX\nOZuQRD6BAgMBAAECggEAGXUo28MBP5zZu9XqLmDOFBQ7Evc1ZqNpfaUnOxk1beuO\nDI8jCFiqJL+pu4gbgc3BJBQ/T9jk9z8Xb3iczUb45ExyHCCfyIinq1Sr2I4u0Ezl\nbnboQ4K6s+85fqOnICIcLz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f4RKwwAjIwJy2lhbi\nyA+63fAHMAwG+k22IkBqcu4=\n-----END PRIVATE KEY-----\n"),
Scopes: []string{"https://www.googleapis.com/auth/monitoring.read"},
TokenURL: "https://oauth2.googleapis.com/token",
}
urlInterpolated, err := interpolateString(proxy.route.TokenAuth.Url, data)
ctx := context.Background()
tokenSrc := conf.TokenSource(ctx)
// logger.Info("Accesstoken", tokenSrc.Token.AccessToken)
token, err := tokenSrc.Token()
if err != nil {
return "", err
}
params := make(url.Values)
for key, value := range proxy.route.TokenAuth.Params {
interpolatedParam, err := interpolateString(value, data)
if err != nil {
return "", err
}
params.Add(key, interpolatedParam)
logger.Info("GetToken", "Error", err)
}
getTokenReq, _ := http.NewRequest("POST", urlInterpolated, bytes.NewBufferString(params.Encode()))
getTokenReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
getTokenReq.Header.Add("Content-Length", strconv.Itoa(len(params.Encode())))
resp, err := client.Do(getTokenReq)
if err != nil {
return "", err
}
defer resp.Body.Close()
var token jwtToken
if err := json.NewDecoder(resp.Body).Decode(&token); err != nil {
return "", err
}
expiresOnEpoch, _ := strconv.ParseInt(token.ExpiresOnString, 10, 64)
token.ExpiresOn = time.Unix(expiresOnEpoch, 0)
tokenCache[proxy.getAccessTokenCacheKey()] = &token
logger.Info("Got new access token", "ExpiresOn", token.ExpiresOn)
logger.Info("GetToken", "Token", token.AccessToken)
return token.AccessToken, nil
// if cachedToken, found := tokenCache[proxy.getAccessTokenCacheKey()]; found {
// if cachedToken.ExpiresOn.After(time.Now().Add(time.Second * 10)) {
// logger.Info("Using token from cache")
// return cachedToken.AccessToken, nil
// }
// }
// urlInterpolated, err := interpolateString(proxy.route.TokenAuth.Url, data)
// if err != nil {
// return "", err
// }
// params := make(url.Values)
// for key, value := range proxy.route.TokenAuth.Params {
// interpolatedParam, err := interpolateString(value, data)
// if err != nil {
// return "", err
// }
// params.Add(key, interpolatedParam)
// }
// getTokenReq, _ := http.NewRequest("POST", urlInterpolated, bytes.NewBufferString(params.Encode()))
// getTokenReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// getTokenReq.Header.Add("Content-Length", strconv.Itoa(len(params.Encode())))
// resp, err := client.Do(getTokenReq)
// if err != nil {
// return "", err
// }
// defer resp.Body.Close()
// var token jwtToken
// if err := json.NewDecoder(resp.Body).Decode(&token); err != nil {
// return "", err
// }
// expiresOnEpoch, _ := strconv.ParseInt(token.ExpiresOnString, 10, 64)
// token.ExpiresOn = time.Unix(expiresOnEpoch, 0)
// tokenCache[proxy.getAccessTokenCacheKey()] = &token
// logger.Info("Got new access token", "ExpiresOn", token.ExpiresOn)
// return token.AccessToken, nil
}
func (proxy *DataSourceProxy) getAccessTokenCacheKey() string {
......
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