2015-08-26 20:42:02 -04:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
2015-02-26 00:39:41 -05: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"
|
|
|
|
|
2016-07-30 18:43:56 -04:00
|
|
|
"cloud.google.com/go/compute/metadata"
|
2015-02-26 00:39:41 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
2016-11-07 16:06:47 -05:00
|
|
|
// DefaultCredentials holds "Application Default Credentials".
|
2015-02-26 00:39:41 -05:00
|
|
|
// For more details, see:
|
2015-04-04 21:18:45 -04:00
|
|
|
// https://developers.google.com/accounts/docs/application-default-credentials
|
2016-11-07 16:06:47 -05:00
|
|
|
type DefaultCredentials struct {
|
|
|
|
ProjectID string // may be empty
|
|
|
|
TokenSource oauth2.TokenSource
|
2017-07-25 12:55:14 -04:00
|
|
|
|
|
|
|
// JSON contains the raw bytes from a JSON credentials file.
|
|
|
|
// This field may be nil if authentication is provided by the
|
|
|
|
// environment and not with a credentials file, e.g. when code is
|
|
|
|
// running on Google Cloud Platform.
|
|
|
|
JSON []byte
|
2016-11-07 16:06:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultClient returns an HTTP Client that uses the
|
|
|
|
// DefaultTokenSource to obtain authentication credentials.
|
2015-02-26 00:39:41 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-11-07 16:06:47 -05:00
|
|
|
// DefaultTokenSource returns the token source for
|
2015-02-26 00:39:41 -05:00
|
|
|
// "Application Default Credentials".
|
2016-11-07 16:06:47 -05: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
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindDefaultCredentials searches for "Application Default Credentials".
|
2015-02-26 00:39:41 -05:00
|
|
|
//
|
|
|
|
// It looks for credentials in the following places,
|
|
|
|
// preferring the first location found:
|
|
|
|
//
|
|
|
|
// 1. A JSON file whose path is specified by the
|
|
|
|
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
|
|
|
|
// 2. A JSON file in a location known to the gcloud command-line tool.
|
|
|
|
// On Windows, this is %APPDATA%/gcloud/application_default_credentials.json.
|
|
|
|
// On other systems, $HOME/.config/gcloud/application_default_credentials.json.
|
|
|
|
// 3. On Google App Engine it uses the appengine.AccessToken function.
|
2015-09-15 20:49:47 -04:00
|
|
|
// 4. On Google Compute Engine and Google App Engine Managed VMs, it fetches
|
|
|
|
// credentials from the metadata server.
|
2015-02-26 00:39:41 -05:00
|
|
|
// (In this final case any provided scopes are ignored.)
|
2016-11-07 16:06:47 -05:00
|
|
|
func FindDefaultCredentials(ctx context.Context, scope ...string) (*DefaultCredentials, error) {
|
2015-02-26 00:39:41 -05:00
|
|
|
// First, try the environment variable.
|
|
|
|
const envVar = "GOOGLE_APPLICATION_CREDENTIALS"
|
|
|
|
if filename := os.Getenv(envVar); filename != "" {
|
2016-11-07 16:06:47 -05:00
|
|
|
creds, err := readCredentialsFile(ctx, filename, scope)
|
2015-02-26 00:39:41 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("google: error getting credentials using %v environment variable: %v", envVar, err)
|
|
|
|
}
|
2016-11-07 16:06:47 -05:00
|
|
|
return creds, nil
|
2015-02-26 00:39:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Second, try a well-known file.
|
|
|
|
filename := wellKnownFile()
|
2016-11-07 16:06:47 -05:00
|
|
|
if creds, err := readCredentialsFile(ctx, filename, scope); err == nil {
|
|
|
|
return creds, nil
|
|
|
|
} else if !os.IsNotExist(err) {
|
2015-02-26 00:39:41 -05: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-02-22 01:40:33 -05:00
|
|
|
if appengineTokenFunc != nil && !appengineFlex {
|
2016-11-07 16:06:47 -05:00
|
|
|
return &DefaultCredentials{
|
|
|
|
ProjectID: appengineAppIDFunc(ctx),
|
|
|
|
TokenSource: AppEngineTokenSource(ctx, scope...),
|
|
|
|
}, nil
|
2015-02-26 00:39:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fourth, if we're on Google Compute Engine use the metadata server.
|
|
|
|
if metadata.OnGCE() {
|
2016-11-07 16:06:47 -05:00
|
|
|
id, _ := metadata.ProjectID()
|
|
|
|
return &DefaultCredentials{
|
|
|
|
ProjectID: id,
|
|
|
|
TokenSource: ComputeTokenSource(""),
|
|
|
|
}, nil
|
2015-02-26 00:39:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// None are found; return helpful error.
|
2015-04-04 21:18:45 -04:00
|
|
|
const url = "https://developers.google.com/accounts/docs/application-default-credentials"
|
2015-02-26 00:39:41 -05:00
|
|
|
return nil, fmt.Errorf("google: could not find default credentials. See %v for more information.", url)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-11-07 16:06:47 -05:00
|
|
|
func readCredentialsFile(ctx context.Context, filename string, scopes []string) (*DefaultCredentials, error) {
|
2015-02-26 00:39:41 -05:00
|
|
|
b, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-03 18:50:36 -04:00
|
|
|
var f credentialsFile
|
|
|
|
if err := json.Unmarshal(b, &f); err != nil {
|
2015-02-26 00:39:41 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-07 16:06:47 -05:00
|
|
|
ts, err := f.tokenSource(ctx, append([]string(nil), scopes...))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &DefaultCredentials{
|
|
|
|
ProjectID: f.ProjectID,
|
|
|
|
TokenSource: ts,
|
2017-07-25 12:55:14 -04:00
|
|
|
JSON: b,
|
2016-11-07 16:06:47 -05:00
|
|
|
}, nil
|
2015-02-26 00:39:41 -05:00
|
|
|
}
|