@@ -44,6 +44,8 @@ import (
44
44
"testing"
45
45
"time"
46
46
47
+ "cloud.google.com/go/auth"
48
+ "cloud.google.com/go/auth/credentials"
47
49
"cloud.google.com/go/compute/metadata"
48
50
"cloud.google.com/go/httpreplay"
49
51
"cloud.google.com/go/iam"
@@ -107,6 +109,13 @@ var (
107
109
controlClient * control.StorageControlClient
108
110
)
109
111
112
+ var (
113
+ testScopes = []string {
114
+ ScopeFullControl ,
115
+ "https://www.googleapis.com/auth/cloud-platform" ,
116
+ }
117
+ )
118
+
110
119
func TestMain (m * testing.M ) {
111
120
cleanup := initIntegrationTest ()
112
121
cleanupEmulatorClients := initEmulatorClients ()
@@ -6320,6 +6329,24 @@ func TestIntegration_NewReaderWithContentEncodingGzip(t *testing.T) {
6320
6329
})
6321
6330
}
6322
6331
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
+
6323
6350
func TestIntegration_HMACKey (t * testing.T ) {
6324
6351
ctx := skipExtraReadAPIs (skipGRPC ("hmac not implemented" ), "no reads in test" )
6325
6352
multiTransportTest (ctx , t , func (t * testing.T , ctx context.Context , _ , _ string , client * Client ) {
@@ -6339,13 +6366,12 @@ func TestIntegration_HMACKey(t *testing.T) {
6339
6366
if credentials .JSON == nil {
6340
6367
t .Fatal ("could not read the JSON key file, is GCLOUD_TESTS_GOLANG_KEY set correctly?" )
6341
6368
}
6342
- conf , err := google . JWTConfigFromJSON (credentials .JSON )
6369
+ conf , err := jwtConfigFromJSON (credentials .JSON )
6343
6370
if err != nil {
6344
6371
t .Fatal (err )
6345
6372
}
6346
- serviceAccountEmail := conf .Email
6347
6373
6348
- hmacKey , err := client .CreateHMACKey (ctx , projectID , serviceAccountEmail )
6374
+ hmacKey , err := client .CreateHMACKey (ctx , projectID , conf . ClientEmail )
6349
6375
if err != nil {
6350
6376
t .Fatalf ("Failed to create HMACKey: %v" , err )
6351
6377
}
@@ -6571,14 +6597,8 @@ func TestIntegration_SignedURL_WithCreds(t *testing.T) {
6571
6597
t .Skip ("Integration tests skipped in short mode" )
6572
6598
}
6573
6599
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 ) {
6582
6602
// We can use any client to create the object
6583
6603
obj := "testBucketSignedURL"
6584
6604
contents := []byte ("test" )
@@ -6598,7 +6618,17 @@ func TestIntegration_SignedURL_WithCreds(t *testing.T) {
6598
6618
if err := verifySignedURL (url , nil , contents ); err != nil {
6599
6619
t .Fatalf ("problem with the signed URL: %v" , err )
6600
6620
}
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 ))
6602
6632
}
6603
6633
6604
6634
func TestIntegration_SignedURL_DefaultSignBytes (t * testing.T ) {
@@ -6652,16 +6682,8 @@ func TestIntegration_PostPolicyV4_WithCreds(t *testing.T) {
6652
6682
t .Skip ("Integration tests skipped in short mode" )
6653
6683
}
6654
6684
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
-
6663
6685
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 ) {
6665
6687
h := testHelper {t }
6666
6688
6667
6689
statusCodeToRespond := 200
@@ -6700,7 +6722,17 @@ func TestIntegration_PostPolicyV4_WithCreds(t *testing.T) {
6700
6722
}
6701
6723
})
6702
6724
}
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 ))
6704
6736
6705
6737
}
6706
6738
@@ -7014,7 +7046,7 @@ func verifyPostPolicy(pv4 *PostPolicyV4, obj *ObjectHandle, bytesToWrite []byte,
7014
7046
})
7015
7047
}
7016
7048
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 ) {
7018
7050
key := os .Getenv (envVar )
7019
7051
var opts []option.ClientOption
7020
7052
if len (scopes ) > 0 {
@@ -7026,6 +7058,13 @@ func findTestCredentials(ctx context.Context, envVar string, scopes ...string) (
7026
7058
return transport .Creds (ctx , opts ... )
7027
7059
}
7028
7060
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
+
7029
7068
type testHelper struct {
7030
7069
t * testing.T
7031
7070
}
0 commit comments