Skip to content

Commit 5c54c3f

Browse files
committed
api: change ManagerKmip.IsValidKey to use QueryCryptoKeyStatus
BREAKING: IsValidKey now requires a key providerID param.
1 parent ca05e10 commit 5c54c3f

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

crypto/manager_kmip.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,17 +353,33 @@ func (m ManagerKmip) ListKeys(
353353
return res.Returnval, nil
354354
}
355355

356+
const keyStateNotActiveOrEnabled = string(types.CryptoManagerKmipCryptoKeyStatusKeyUnavailableReasonKeyStateNotActiveOrEnabled)
357+
358+
// IsValidKey returns true if QueryCryptoKeyStatus results indicate the key is available or unavailable reason is `KeyStateNotActiveOrEnabled`.
359+
// This method is only valid for standard providers and will always return false for native providers.
356360
func (m ManagerKmip) IsValidKey(
357361
ctx context.Context,
362+
providerID,
358363
keyID string) (bool, error) {
359364

360-
keys, err := m.ListKeys(ctx, nil)
365+
id := []types.CryptoKeyId{{
366+
KeyId: keyID,
367+
ProviderId: &types.KeyProviderId{
368+
Id: providerID,
369+
}},
370+
}
371+
372+
res, err := m.QueryCryptoKeyStatus(ctx, id, CheckKeyAvailable)
361373
if err != nil {
362374
return false, err
363375
}
364376

365-
for i := range keys {
366-
if keys[i].KeyId == keyID {
377+
for _, status := range res {
378+
if status.KeyAvailable != nil && *status.KeyAvailable {
379+
return true, nil
380+
}
381+
382+
if status.Reason == keyStateNotActiveOrEnabled {
367383
return true, nil
368384
}
369385
}

crypto/manager_kmip_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ func TestCryptoManagerKmip(t *testing.T) {
987987
assert.NoError(t, err)
988988
assert.NotEmpty(t, keyID)
989989

990-
ok, err := m.IsValidKey(ctx, keyID)
990+
ok, err := m.IsValidKey(ctx, providerID, keyID)
991991
assert.NoError(t, err)
992992
assert.True(t, ok)
993993
})

0 commit comments

Comments
 (0)