vendor: update all dependencies

This commit is contained in:
Nick Craig-Wood
2018-03-19 15:51:38 +00:00
parent 940df88eb2
commit d64789528d
4309 changed files with 1327278 additions and 1001118 deletions

View File

@@ -15,90 +15,28 @@
package internal
import (
"encoding/json"
"fmt"
"io/ioutil"
"time"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
// Creds returns credential information obtained from DialSettings, or if none, then
// it returns default credential information.
func Creds(ctx context.Context, ds *DialSettings) (*google.DefaultCredentials, error) {
if ds.Credentials != nil {
return ds.Credentials, nil
}
if ds.CredentialsFile != "" {
return credFileTokenSource(ctx, ds.CredentialsFile, ds.Scopes...)
data, err := ioutil.ReadFile(ds.CredentialsFile)
if err != nil {
return nil, fmt.Errorf("cannot read credentials file: %v", err)
}
return google.CredentialsFromJSON(ctx, data, ds.Scopes...)
}
if ds.TokenSource != nil {
return &google.DefaultCredentials{TokenSource: ds.TokenSource}, nil
}
return google.FindDefaultCredentials(ctx, ds.Scopes...)
}
// credFileTokenSource reads a refresh token file or a service account and returns
// a TokenSource constructed from the config.
func credFileTokenSource(ctx context.Context, filename string, scope ...string) (*google.DefaultCredentials, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("cannot read credentials file: %v", err)
}
// See if it is a refresh token credentials file first.
ts, ok, err := refreshTokenTokenSource(ctx, data, scope...)
if err != nil {
return nil, err
}
if ok {
return &google.DefaultCredentials{
TokenSource: ts,
JSON: data,
}, nil
}
// If not, it should be a service account.
cfg, err := google.JWTConfigFromJSON(data, scope...)
if err != nil {
return nil, fmt.Errorf("google.JWTConfigFromJSON: %v", err)
}
// jwt.Config does not expose the project ID, so re-unmarshal to get it.
var pid struct {
ProjectID string `json:"project_id"`
}
if err := json.Unmarshal(data, &pid); err != nil {
return nil, err
}
return &google.DefaultCredentials{
ProjectID: pid.ProjectID,
TokenSource: cfg.TokenSource(ctx),
JSON: data,
}, nil
}
func refreshTokenTokenSource(ctx context.Context, data []byte, scope ...string) (oauth2.TokenSource, bool, error) {
var c cred
if err := json.Unmarshal(data, &c); err != nil {
return nil, false, fmt.Errorf("cannot unmarshal credentials file: %v", err)
}
if c.ClientID == "" || c.ClientSecret == "" || c.RefreshToken == "" || c.Type != "authorized_user" {
return nil, false, nil
}
cfg := &oauth2.Config{
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
Endpoint: google.Endpoint,
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
Scopes: scope,
}
return cfg.TokenSource(ctx, &oauth2.Token{
RefreshToken: c.RefreshToken,
Expiry: time.Now(),
}), true, nil
}
type cred struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RefreshToken string `json:"refresh_token"`
Type string `json:"type"`
}

View File

@@ -85,46 +85,3 @@ const validServiceAccountJSON = `{
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dumba-504%40appspot.gserviceaccount.com"
}`
func TestRefreshTokenTokenSource(t *testing.T) {
tests := []struct {
name string
data []byte
wantOK bool
wantErr bool
}{
{
name: "empty",
data: []byte{},
wantOK: false,
wantErr: true, // not valid JSON
},
{
name: "non refresh token JSON",
data: []byte("{}"),
wantOK: false,
wantErr: false,
},
{
name: "service account JSON",
data: []byte(validServiceAccountJSON),
wantOK: false,
wantErr: false,
},
{
name: "valid refresh token JSON",
data: []byte(validRefeshTokenJSON),
wantOK: true,
wantErr: false,
},
}
for _, tt := range tests {
_, ok, err := refreshTokenTokenSource(context.Background(), tt.data)
if (err != nil) != tt.wantErr {
t.Errorf("%v: refreshTokenTokenSource() err = %v, wantErr %v", tt.name, err, tt.wantErr)
}
if ok != tt.wantOK {
t.Errorf("%v: refreshTokenTokenSource() ok = %v, want %v", tt.name, ok, tt.wantOK)
}
}
}

View File

@@ -20,6 +20,7 @@ import (
"net/http"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/grpc"
)
@@ -29,6 +30,7 @@ type DialSettings struct {
Endpoint string
Scopes []string
TokenSource oauth2.TokenSource
Credentials *google.DefaultCredentials
CredentialsFile string // if set, Token Source is ignored.
UserAgent string
APIKey string
@@ -40,10 +42,16 @@ type DialSettings struct {
// Validate reports an error if ds is invalid.
func (ds *DialSettings) Validate() error {
hasCreds := ds.APIKey != "" || ds.TokenSource != nil || ds.CredentialsFile != ""
hasCreds := ds.APIKey != "" || ds.TokenSource != nil || ds.CredentialsFile != "" || ds.Credentials != nil
if ds.NoAuth && hasCreds {
return errors.New("options.WithoutAuthentication is incompatible with any option that provides credentials")
}
// Credentials should not appear with other options.
// We currently allow TokenSource and CredentialsFile to coexist.
// TODO(jba): make TokenSource & CredentialsFile an error (breaking change).
if ds.Credentials != nil && (ds.APIKey != "" || ds.TokenSource != nil || ds.CredentialsFile != "") {
return errors.New("multiple credential options provided")
}
if ds.HTTPClient != nil && ds.GRPCConn != nil {
return errors.New("WithHTTPClient is incompatible with WithGRPCConn")
}

View File

@@ -22,6 +22,7 @@ import (
"google.golang.org/grpc"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
func TestSettingsValidate(t *testing.T) {
@@ -32,6 +33,7 @@ func TestSettingsValidate(t *testing.T) {
{Scopes: []string{"s"}},
{CredentialsFile: "f"},
{TokenSource: dummyTS{}},
{CredentialsFile: "f", TokenSource: dummyTS{}}, // keep for backwards compatibility
{HTTPClient: &http.Client{}},
{GRPCConn: &grpc.ClientConn{}},
// Although NoAuth and Scopes are technically incompatible, too many
@@ -50,6 +52,9 @@ func TestSettingsValidate(t *testing.T) {
{NoAuth: true, APIKey: "x"},
{NoAuth: true, CredentialsFile: "f"},
{NoAuth: true, TokenSource: dummyTS{}},
{NoAuth: true, Credentials: &google.DefaultCredentials{}},
{Credentials: &google.DefaultCredentials{}, CredentialsFile: "f"},
{Credentials: &google.DefaultCredentials{}, TokenSource: dummyTS{}},
{HTTPClient: &http.Client{}, GRPCConn: &grpc.ClientConn{}},
{HTTPClient: &http.Client{}, GRPCDialOpts: []grpc.DialOption{grpc.WithInsecure()}},
} {