@@ -61,8 +61,9 @@ func TestMasterKey_Encrypt(t *testing.T) {
6161 })
6262
6363 key := MasterKey {
64- grpcConn : newGRPCServer ("0" ),
65- ResourceID : testResourceID ,
64+ grpcConn : newGRPCServer ("0" ),
65+ ResourceID : testResourceID ,
66+ credentialJSON : []byte ("arbitrary credentials" ),
6667 }
6768 err := key .Encrypt ([]byte ("encrypt" ))
6869 assert .NoError (t , err )
@@ -88,9 +89,10 @@ func TestMasterKey_Decrypt(t *testing.T) {
8889 Plaintext : []byte (decryptedData ),
8990 })
9091 key := MasterKey {
91- grpcConn : newGRPCServer ("0" ),
92- ResourceID : testResourceID ,
93- EncryptedKey : "encryptedKey" ,
92+ grpcConn : newGRPCServer ("0" ),
93+ ResourceID : testResourceID ,
94+ EncryptedKey : "encryptedKey" ,
95+ credentialJSON : []byte ("arbitrary credentials" ),
9496 }
9597 data , err := key .Decrypt ()
9698 assert .NoError (t , err )
@@ -124,7 +126,7 @@ func TestMasterKey_ToMap(t *testing.T) {
124126 }, key .ToMap ())
125127}
126128
127- func TestMasterKey_createCloudKMSService (t * testing.T ) {
129+ func TestMasterKey_createCloudKMSService_withCredentialsFile (t * testing.T ) {
128130 tests := []struct {
129131 key MasterKey
130132 errString string
@@ -144,6 +146,12 @@ func TestMasterKey_createCloudKMSService(t *testing.T) {
144146 "type": "authorized_user"}` ),
145147 },
146148 },
149+ {
150+ key : MasterKey {
151+ ResourceID : testResourceID ,
152+ },
153+ errString : `credentials: failed to obtain credentials from "SOPS_GOOGLE_CREDENTIALS"` ,
154+ },
147155 }
148156
149157 for _ , tt := range tests {
@@ -157,6 +165,29 @@ func TestMasterKey_createCloudKMSService(t *testing.T) {
157165 }
158166}
159167
168+ func TestMasterKey_createCloudKMSService_withOauthToken (t * testing.T ) {
169+ t .Setenv (SopsGoogleCredentialsOAuthTokenEnv , "token" )
170+
171+ masterKey := MasterKey {
172+ ResourceID : testResourceID ,
173+ }
174+
175+ _ , err := masterKey .newKMSClient ()
176+
177+ assert .NoError (t , err )
178+ }
179+
180+ func TestMasterKey_createCloudKMSService_withoutCredentials (t * testing.T ) {
181+ masterKey := MasterKey {
182+ ResourceID : testResourceID ,
183+ }
184+
185+ _ , err := masterKey .newKMSClient ()
186+
187+ assert .Error (t , err )
188+ assert .ErrorContains (t , err , "credentials: could not find default credentials" )
189+ }
190+
160191func newGRPCServer (port string ) * grpc.ClientConn {
161192 serv := grpc .NewServer ()
162193 kmspb .RegisterKeyManagementServiceServer (serv , & mockKeyManagement )
0 commit comments