@@ -44,6 +44,8 @@ import (
4444 "testing"
4545 "time"
4646
47+ "cloud.google.com/go/auth"
48+ "cloud.google.com/go/auth/credentials"
4749 "cloud.google.com/go/compute/metadata"
4850 "cloud.google.com/go/httpreplay"
4951 "cloud.google.com/go/iam"
@@ -107,6 +109,13 @@ var (
107109 controlClient * control.StorageControlClient
108110)
109111
112+ var (
113+ testScopes = []string {
114+ ScopeFullControl ,
115+ "https://www.googleapis.com/auth/cloud-platform" ,
116+ }
117+ )
118+
110119func TestMain (m * testing.M ) {
111120 cleanup := initIntegrationTest ()
112121 cleanupEmulatorClients := initEmulatorClients ()
@@ -6320,6 +6329,24 @@ func TestIntegration_NewReaderWithContentEncodingGzip(t *testing.T) {
63206329 })
63216330}
63226331
6332+ type credentialsFile struct {
6333+ Type string `json:"type"`
6334+
6335+ // Service Account email
6336+ ClientEmail string `json:"client_email"`
6337+ }
6338+
6339+ func jwtConfigFromJSON (jsonKey []byte ) (* credentialsFile , error ) {
6340+ var f credentialsFile
6341+ if err := json .Unmarshal (jsonKey , & f ); err != nil {
6342+ return nil , err
6343+ }
6344+ if f .Type != "service_account" {
6345+ return nil , fmt .Errorf ("read JWT from JSON credentials: 'type' field is %q (expected service_account)" , f .Type )
6346+ }
6347+ return & f , nil
6348+ }
6349+
63236350func TestIntegration_HMACKey (t * testing.T ) {
63246351 ctx := skipExtraReadAPIs (skipGRPC ("hmac not implemented" ), "no reads in test" )
63256352 multiTransportTest (ctx , t , func (t * testing.T , ctx context.Context , _ , _ string , client * Client ) {
@@ -6339,13 +6366,12 @@ func TestIntegration_HMACKey(t *testing.T) {
63396366 if credentials .JSON == nil {
63406367 t .Fatal ("could not read the JSON key file, is GCLOUD_TESTS_GOLANG_KEY set correctly?" )
63416368 }
6342- conf , err := google . JWTConfigFromJSON (credentials .JSON )
6369+ conf , err := jwtConfigFromJSON (credentials .JSON )
63436370 if err != nil {
63446371 t .Fatal (err )
63456372 }
6346- serviceAccountEmail := conf .Email
63476373
6348- hmacKey , err := client .CreateHMACKey (ctx , projectID , serviceAccountEmail )
6374+ hmacKey , err := client .CreateHMACKey (ctx , projectID , conf . ClientEmail )
63496375 if err != nil {
63506376 t .Fatalf ("Failed to create HMACKey: %v" , err )
63516377 }
@@ -6571,14 +6597,8 @@ func TestIntegration_SignedURL_WithCreds(t *testing.T) {
65716597 t .Skip ("Integration tests skipped in short mode" )
65726598 }
65736599
6574- ctx := context .Background ()
6575-
6576- creds , err := findTestCredentials (ctx , "GCLOUD_TESTS_GOLANG_KEY" , ScopeFullControl , "https://www.googleapis.com/auth/cloud-platform" )
6577- if err != nil {
6578- t .Fatalf ("unable to find test credentials: %v" , err )
6579- }
6580-
6581- multiTransportTest (skipGRPC ("creds capture logic must be implemented for gRPC constructor" ), t , func (t * testing.T , ctx context.Context , bucket , _ string , client * Client ) {
6600+ ctx := skipGRPC ("creds capture logic must be implemented for gRPC constructor" )
6601+ tFunc := func (t * testing.T , ctx context.Context , bucket , _ string , client * Client ) {
65826602 // We can use any client to create the object
65836603 obj := "testBucketSignedURL"
65846604 contents := []byte ("test" )
@@ -6598,7 +6618,17 @@ func TestIntegration_SignedURL_WithCreds(t *testing.T) {
65986618 if err := verifySignedURL (url , nil , contents ); err != nil {
65996619 t .Fatalf ("problem with the signed URL: %v" , err )
66006620 }
6601- }, option .WithCredentials (creds ))
6621+ }
6622+ creds , err := findLegacyOAuth2TestCredentials (ctx , "GCLOUD_TESTS_GOLANG_KEY" , testScopes )
6623+ if err != nil {
6624+ t .Fatalf ("unable to find test credentials: %v" , err )
6625+ }
6626+ multiTransportTest (ctx , t , tFunc , option .WithCredentials (creds ))
6627+ newAuthCreds , err := findNewAuthTestCredentials (ctx , "GCLOUD_TESTS_GOLANG_KEY" , testScopes )
6628+ if err != nil {
6629+ t .Fatalf ("unable to find test credentials: %v" , err )
6630+ }
6631+ multiTransportTest (ctx , t , tFunc , option .WithAuthCredentials (newAuthCreds ))
66026632}
66036633
66046634func TestIntegration_SignedURL_DefaultSignBytes (t * testing.T ) {
@@ -6652,16 +6682,8 @@ func TestIntegration_PostPolicyV4_WithCreds(t *testing.T) {
66526682 t .Skip ("Integration tests skipped in short mode" )
66536683 }
66546684
6655- // By default we are authed with a token source, so don't have the context to
6656- // read some of the fields from the keyfile.
6657- // Here we explictly send the key to the client.
6658- creds , err := findTestCredentials (context .Background (), "GCLOUD_TESTS_GOLANG_KEY" , ScopeFullControl , "https://www.googleapis.com/auth/cloud-platform" )
6659- if err != nil {
6660- t .Fatalf ("unable to find test credentials: %v" , err )
6661- }
6662-
66636685 ctx := skipExtraReadAPIs (skipGRPC ("creds capture logic must be implemented for gRPC constructor" ), "test is not testing the read behaviour" )
6664- multiTransportTest ( ctx , t , func (t * testing.T , ctx context.Context , bucket , _ string , clientWithCredentials * Client ) {
6686+ tFunc := func (t * testing.T , ctx context.Context , bucket , _ string , clientWithCredentials * Client ) {
66656687 h := testHelper {t }
66666688
66676689 statusCodeToRespond := 200
@@ -6700,7 +6722,17 @@ func TestIntegration_PostPolicyV4_WithCreds(t *testing.T) {
67006722 }
67016723 })
67026724 }
6703- }, option .WithCredentials (creds ))
6725+ }
6726+ creds , err := findLegacyOAuth2TestCredentials (ctx , "GCLOUD_TESTS_GOLANG_KEY" , testScopes )
6727+ if err != nil {
6728+ t .Fatalf ("unable to find test credentials: %v" , err )
6729+ }
6730+ multiTransportTest (ctx , t , tFunc , option .WithCredentials (creds ))
6731+ newAuthCreds , err := findNewAuthTestCredentials (ctx , "GCLOUD_TESTS_GOLANG_KEY" , testScopes )
6732+ if err != nil {
6733+ t .Fatalf ("unable to find test credentials: %v" , err )
6734+ }
6735+ multiTransportTest (ctx , t , tFunc , option .WithAuthCredentials (newAuthCreds ))
67046736
67056737}
67066738
@@ -7014,7 +7046,7 @@ func verifyPostPolicy(pv4 *PostPolicyV4, obj *ObjectHandle, bytesToWrite []byte,
70147046 })
70157047}
70167048
7017- func findTestCredentials (ctx context.Context , envVar string , scopes ... string ) (* google.Credentials , error ) {
7049+ func findLegacyOAuth2TestCredentials (ctx context.Context , envVar string , scopes [] string ) (* google.Credentials , error ) {
70187050 key := os .Getenv (envVar )
70197051 var opts []option.ClientOption
70207052 if len (scopes ) > 0 {
@@ -7026,6 +7058,13 @@ func findTestCredentials(ctx context.Context, envVar string, scopes ...string) (
70267058 return transport .Creds (ctx , opts ... )
70277059}
70287060
7061+ func findNewAuthTestCredentials (ctx context.Context , envVar string , scopes []string ) (* auth.Credentials , error ) {
7062+ return credentials .DetectDefault (& credentials.DetectOptions {
7063+ CredentialsFile : os .Getenv (envVar ),
7064+ Scopes : scopes ,
7065+ })
7066+ }
7067+
70297068type testHelper struct {
70307069 t * testing.T
70317070}
0 commit comments