traefik/vendor/golang.org/x/oauth2/google/default.go

116 lines
3.5 KiB
Go
Raw Normal View History

2017-04-07 10:49:53 +00:00
// Copyright 2015 The Go Authors. All rights reserved.
2017-02-07 21:33:23 +00:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package google
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
2017-04-07 10:49:53 +00:00
"cloud.google.com/go/compute/metadata"
2017-02-07 21:33:23 +00:00
"golang.org/x/net/context"
"golang.org/x/oauth2"
)
// DefaultClient returns an HTTP Client that uses the
// DefaultTokenSource to obtain authentication credentials.
func DefaultClient(ctx context.Context, scope ...string) (*http.Client, error) {
ts, err := DefaultTokenSource(ctx, scope...)
if err != nil {
return nil, err
}
return oauth2.NewClient(ctx, ts), nil
}
2017-04-07 10:49:53 +00:00
// DefaultTokenSource returns the token source for
2017-02-07 21:33:23 +00:00
// "Application Default Credentials".
2017-04-07 10:49:53 +00:00
// It is a shortcut for FindDefaultCredentials(ctx, scope).TokenSource.
func DefaultTokenSource(ctx context.Context, scope ...string) (oauth2.TokenSource, error) {
creds, err := FindDefaultCredentials(ctx, scope...)
if err != nil {
return nil, err
}
return creds.TokenSource, nil
}
2018-05-31 07:30:04 +00:00
// Common implementation for FindDefaultCredentials.
func findDefaultCredentials(ctx context.Context, scopes []string) (*DefaultCredentials, error) {
2017-02-07 21:33:23 +00:00
// First, try the environment variable.
const envVar = "GOOGLE_APPLICATION_CREDENTIALS"
if filename := os.Getenv(envVar); filename != "" {
2018-05-31 07:30:04 +00:00
creds, err := readCredentialsFile(ctx, filename, scopes)
2017-02-07 21:33:23 +00:00
if err != nil {
return nil, fmt.Errorf("google: error getting credentials using %v environment variable: %v", envVar, err)
}
2017-04-07 10:49:53 +00:00
return creds, nil
2017-02-07 21:33:23 +00:00
}
// Second, try a well-known file.
filename := wellKnownFile()
2018-05-31 07:30:04 +00:00
if creds, err := readCredentialsFile(ctx, filename, scopes); err == nil {
2017-04-07 10:49:53 +00:00
return creds, nil
} else if !os.IsNotExist(err) {
2017-02-07 21:33:23 +00:00
return nil, fmt.Errorf("google: error getting credentials using well-known file (%v): %v", filename, err)
}
// Third, if we're on Google App Engine use those credentials.
2017-04-07 10:49:53 +00:00
if appengineTokenFunc != nil && !appengineFlex {
return &DefaultCredentials{
ProjectID: appengineAppIDFunc(ctx),
2018-05-31 07:30:04 +00:00
TokenSource: AppEngineTokenSource(ctx, scopes...),
2017-04-07 10:49:53 +00:00
}, nil
2017-02-07 21:33:23 +00:00
}
// Fourth, if we're on Google Compute Engine use the metadata server.
if metadata.OnGCE() {
2017-04-07 10:49:53 +00:00
id, _ := metadata.ProjectID()
return &DefaultCredentials{
ProjectID: id,
TokenSource: ComputeTokenSource(""),
}, nil
2017-02-07 21:33:23 +00:00
}
// None are found; return helpful error.
2017-04-07 10:49:53 +00:00
const url = "https://developers.google.com/accounts/docs/application-default-credentials"
2017-02-07 21:33:23 +00:00
return nil, fmt.Errorf("google: could not find default credentials. See %v for more information.", url)
}
2018-05-31 07:30:04 +00:00
// Common implementation for CredentialsFromJSON.
func credentialsFromJSON(ctx context.Context, jsonData []byte, scopes []string) (*DefaultCredentials, error) {
var f credentialsFile
if err := json.Unmarshal(jsonData, &f); err != nil {
return nil, err
}
ts, err := f.tokenSource(ctx, append([]string(nil), scopes...))
if err != nil {
return nil, err
}
return &DefaultCredentials{
ProjectID: f.ProjectID,
TokenSource: ts,
JSON: jsonData,
}, nil
}
2017-02-07 21:33:23 +00:00
func wellKnownFile() string {
const f = "application_default_credentials.json"
if runtime.GOOS == "windows" {
return filepath.Join(os.Getenv("APPDATA"), "gcloud", f)
}
return filepath.Join(guessUnixHomeDir(), ".config", "gcloud", f)
}
2017-04-07 10:49:53 +00:00
func readCredentialsFile(ctx context.Context, filename string, scopes []string) (*DefaultCredentials, error) {
2017-02-07 21:33:23 +00:00
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
2018-05-31 07:30:04 +00:00
return CredentialsFromJSON(ctx, b, scopes...)
2017-02-07 21:33:23 +00:00
}