2021-05-03 20:46:32 +08:00
|
|
|
package pluginproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-08 20:46:35 +08:00
|
|
|
"time"
|
2021-05-03 20:46:32 +08:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
)
|
|
|
|
|
|
|
|
type gceAccessTokenProvider struct {
|
|
|
|
datasourceId int64
|
2021-10-08 20:46:35 +08:00
|
|
|
datasourceUpdated time.Time
|
2021-05-03 20:46:32 +08:00
|
|
|
ctx context.Context
|
|
|
|
route *plugins.AppPluginRoute
|
2021-05-07 04:05:23 +08:00
|
|
|
authParams *plugins.JwtTokenAuth
|
2021-05-03 20:46:32 +08:00
|
|
|
}
|
|
|
|
|
2021-10-08 20:46:35 +08:00
|
|
|
func newGceAccessTokenProvider(ctx context.Context, ds DSInfo, pluginRoute *plugins.AppPluginRoute,
|
2021-05-07 04:05:23 +08:00
|
|
|
authParams *plugins.JwtTokenAuth) *gceAccessTokenProvider {
|
2021-05-03 20:46:32 +08:00
|
|
|
return &gceAccessTokenProvider{
|
2021-10-08 20:46:35 +08:00
|
|
|
datasourceId: ds.ID,
|
|
|
|
datasourceUpdated: ds.Updated,
|
2021-05-03 20:46:32 +08:00
|
|
|
ctx: ctx,
|
|
|
|
route: pluginRoute,
|
2021-05-07 04:05:23 +08:00
|
|
|
authParams: authParams,
|
2021-05-03 20:46:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 23:02:24 +08:00
|
|
|
func (provider *gceAccessTokenProvider) GetAccessToken() (string, error) {
|
2021-05-07 04:05:23 +08:00
|
|
|
tokenSrc, err := google.DefaultTokenSource(provider.ctx, provider.authParams.Scopes...)
|
2021-05-03 20:46:32 +08:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to get default token from meta data server", "error", err)
|
|
|
|
return "", err
|
|
|
|
} else {
|
|
|
|
token, err := tokenSrc.Token()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to get default access token from meta data server", "error", err)
|
|
|
|
return "", err
|
|
|
|
} else {
|
|
|
|
return token.AccessToken, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|