@@ -13,6 +13,7 @@ import (
1313 "cloud.google.com/go/kms/apiv1/kmspb"
1414 gax "github.com/googleapis/gax-go/v2"
1515 "github.com/stretchr/testify/assert"
16+ "github.com/stretchr/testify/require"
1617 "go.step.sm/crypto/kms/apiv1"
1718 "go.step.sm/crypto/kms/uri"
1819 "go.step.sm/crypto/pemutil"
@@ -174,13 +175,9 @@ func TestCloudKMS_CreateSigner(t *testing.T) {
174175 keyURI := uri .NewOpaque (Scheme , keyName ).String ()
175176
176177 pemBytes , err := os .ReadFile ("testdata/pub.pem" )
177- if err != nil {
178- t .Fatal (err )
179- }
178+ require .NoError (t , err )
180179 pk , err := pemutil .ParseKey (pemBytes )
181- if err != nil {
182- t .Fatal (err )
183- }
180+ require .NoError (t , err )
184181
185182 type fields struct {
186183 client KeyManagementClient
@@ -196,17 +193,20 @@ func TestCloudKMS_CreateSigner(t *testing.T) {
196193 wantErr bool
197194 }{
198195 {"ok" , fields {& MockClient {
199- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
196+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
197+ assert .NotContains (t , r .Name , "cloudkms:" )
200198 return & kmspb.PublicKey {Pem : string (pemBytes )}, nil
201199 },
202200 }}, args {& apiv1.CreateSignerRequest {SigningKey : keyName }}, & Signer {client : & MockClient {}, signingKey : keyName , publicKey : pk }, false },
203201 {"ok with uri" , fields {& MockClient {
204- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
202+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
203+ assert .NotContains (t , r .Name , "cloudkms:" )
205204 return & kmspb.PublicKey {Pem : string (pemBytes )}, nil
206205 },
207206 }}, args {& apiv1.CreateSignerRequest {SigningKey : keyURI }}, & Signer {client : & MockClient {}, signingKey : keyName , publicKey : pk }, false },
208207 {"fail" , fields {& MockClient {
209- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
208+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
209+ assert .NotContains (t , r .Name , "cloudkms:" )
210210 return nil , fmt .Errorf ("test error" )
211211 },
212212 }}, args {& apiv1.CreateSignerRequest {SigningKey : "" }}, nil , true },
@@ -238,13 +238,9 @@ func TestCloudKMS_CreateKey(t *testing.T) {
238238 alreadyExists := status .Error (codes .AlreadyExists , "already exists" )
239239
240240 pemBytes , err := os .ReadFile ("testdata/pub.pem" )
241- if err != nil {
242- t .Fatal (err )
243- }
241+ require .NoError (t , err )
244242 pk , err := pemutil .ParseKey (pemBytes )
245- if err != nil {
246- t .Fatal (err )
247- }
243+ require .NoError (t , err )
248244
249245 var retries int
250246 type fields struct {
@@ -269,7 +265,8 @@ func TestCloudKMS_CreateKey(t *testing.T) {
269265 assert .Nil (t , req .CryptoKey .DestroyScheduledDuration )
270266 return & kmspb.CryptoKey {Name : keyName }, nil
271267 },
272- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
268+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
269+ assert .NotContains (t , r .Name , "cloudkms:" )
273270 return & kmspb.PublicKey {Pem : string (pemBytes )}, nil
274271 },
275272 }},
@@ -284,7 +281,8 @@ func TestCloudKMS_CreateKey(t *testing.T) {
284281 assert .Equal (t , req .CryptoKey .DestroyScheduledDuration , durationpb .New (24 * time .Hour ))
285282 return & kmspb.CryptoKey {Name : keyName }, nil
286283 },
287- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
284+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
285+ assert .NotContains (t , r .Name , "cloudkms:" )
288286 return & kmspb.PublicKey {Pem : string (pemBytes )}, nil
289287 },
290288 }},
@@ -301,7 +299,8 @@ func TestCloudKMS_CreateKey(t *testing.T) {
301299 createCryptoKey : func (_ context.Context , _ * kmspb.CreateCryptoKeyRequest , _ ... gax.CallOption ) (* kmspb.CryptoKey , error ) {
302300 return & kmspb.CryptoKey {Name : keyName }, nil
303301 },
304- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
302+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
303+ assert .NotContains (t , r .Name , "cloudkms:" )
305304 return & kmspb.PublicKey {Pem : string (pemBytes )}, nil
306305 },
307306 }},
@@ -318,7 +317,8 @@ func TestCloudKMS_CreateKey(t *testing.T) {
318317 createCryptoKeyVersion : func (_ context.Context , _ * kmspb.CreateCryptoKeyVersionRequest , _ ... gax.CallOption ) (* kmspb.CryptoKeyVersion , error ) {
319318 return & kmspb.CryptoKeyVersion {Name : keyName + "/cryptoKeyVersions/2" }, nil
320319 },
321- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
320+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
321+ assert .NotContains (t , r .Name , "cloudkms:" )
322322 return & kmspb.PublicKey {Pem : string (pemBytes )}, nil
323323 },
324324 }},
@@ -332,7 +332,8 @@ func TestCloudKMS_CreateKey(t *testing.T) {
332332 createCryptoKey : func (_ context.Context , _ * kmspb.CreateCryptoKeyRequest , _ ... gax.CallOption ) (* kmspb.CryptoKey , error ) {
333333 return & kmspb.CryptoKey {Name : keyName }, nil
334334 },
335- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
335+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
336+ assert .NotContains (t , r .Name , "cloudkms:" )
336337 if retries != 2 {
337338 retries ++
338339 return nil , status .Error (codes .FailedPrecondition , "key is not enabled, current state is: PENDING_GENERATION" )
@@ -391,7 +392,8 @@ func TestCloudKMS_CreateKey(t *testing.T) {
391392 createCryptoKey : func (_ context.Context , _ * kmspb.CreateCryptoKeyRequest , _ ... gax.CallOption ) (* kmspb.CryptoKey , error ) {
392393 return & kmspb.CryptoKey {Name : keyName }, nil
393394 },
394- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
395+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
396+ assert .NotContains (t , r .Name , "cloudkms:" )
395397 return nil , testError
396398 },
397399 }},
@@ -424,13 +426,9 @@ func TestCloudKMS_GetPublicKey(t *testing.T) {
424426 testError := fmt .Errorf ("an error" )
425427
426428 pemBytes , err := os .ReadFile ("testdata/pub.pem" )
427- if err != nil {
428- t .Fatal (err )
429- }
429+ require .NoError (t , err )
430430 pk , err := pemutil .ParseKey (pemBytes )
431- if err != nil {
432- t .Fatal (err )
433- }
431+ require .NoError (t , err )
434432
435433 var retries int
436434 type fields struct {
@@ -448,28 +446,32 @@ func TestCloudKMS_GetPublicKey(t *testing.T) {
448446 }{
449447 {"ok" , fields {
450448 & MockClient {
451- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
449+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
450+ assert .NotContains (t , r .Name , "cloudkms:" )
452451 return & kmspb.PublicKey {Pem : string (pemBytes )}, nil
453452 },
454453 }},
455454 args {& apiv1.GetPublicKeyRequest {Name : keyName }}, pk , false },
456455 {"ok with uri" , fields {
457456 & MockClient {
458- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
457+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
458+ assert .NotContains (t , r .Name , "cloudkms:" )
459459 return & kmspb.PublicKey {Pem : string (pemBytes )}, nil
460460 },
461461 }},
462462 args {& apiv1.GetPublicKeyRequest {Name : keyURI }}, pk , false },
463463 {"ok with resource uri" , fields {
464464 & MockClient {
465- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
465+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
466+ assert .NotContains (t , r .Name , "cloudkms:" )
466467 return & kmspb.PublicKey {Pem : string (pemBytes )}, nil
467468 },
468469 }},
469470 args {& apiv1.GetPublicKeyRequest {Name : keyResource }}, pk , false },
470471 {"ok with retries" , fields {
471472 & MockClient {
472- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
473+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
474+ assert .NotContains (t , r .Name , "cloudkms:" )
473475 if retries != 2 {
474476 retries ++
475477 return nil , status .Error (codes .FailedPrecondition , "key is not enabled, current state is: PENDING_GENERATION" )
@@ -481,14 +483,16 @@ func TestCloudKMS_GetPublicKey(t *testing.T) {
481483 {"fail name" , fields {& MockClient {}}, args {& apiv1.GetPublicKeyRequest {}}, nil , true },
482484 {"fail get public key" , fields {
483485 & MockClient {
484- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
486+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
487+ assert .NotContains (t , r .Name , "cloudkms:" )
485488 return nil , testError
486489 },
487490 }},
488491 args {& apiv1.GetPublicKeyRequest {Name : keyName }}, nil , true },
489492 {"fail parse pem" , fields {
490493 & MockClient {
491- getPublicKey : func (_ context.Context , _ * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
494+ getPublicKey : func (_ context.Context , r * kmspb.GetPublicKeyRequest , _ ... gax.CallOption ) (* kmspb.PublicKey , error ) {
495+ assert .NotContains (t , r .Name , "cloudkms:" )
492496 return & kmspb.PublicKey {Pem : string ("bad pem" )}, nil
493497 },
494498 }},
0 commit comments