Skip to content

Commit

Permalink
api: change ManagerKmip.IsValidKey to use QueryCryptoKeyStatus
Browse files Browse the repository at this point in the history
BREAKING: IsValidKey now requires a key providerID param.
  • Loading branch information
dougm committed Oct 29, 2024
1 parent ca05e10 commit 5c54c3f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 19 additions & 3 deletions crypto/manager_kmip.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,33 @@ func (m ManagerKmip) ListKeys(
return res.Returnval, nil
}

const keyStateNotActiveOrEnabled = string(types.CryptoManagerKmipCryptoKeyStatusKeyUnavailableReasonKeyStateNotActiveOrEnabled)

// IsValidKey returns true if QueryCryptoKeyStatus results indicate the key is available or unavailable reason is `KeyStateNotActiveOrEnabled`.
// This method is only valid for standard providers and will always return false for native providers.
func (m ManagerKmip) IsValidKey(
ctx context.Context,
providerID,
keyID string) (bool, error) {

keys, err := m.ListKeys(ctx, nil)
id := []types.CryptoKeyId{{
KeyId: keyID,
ProviderId: &types.KeyProviderId{
Id: providerID,
}},
}

res, err := m.QueryCryptoKeyStatus(ctx, id, CheckKeyAvailable)
if err != nil {
return false, err
}

for i := range keys {
if keys[i].KeyId == keyID {
for _, status := range res {
if status.KeyAvailable != nil && *status.KeyAvailable {
return true, nil
}

if status.Reason == keyStateNotActiveOrEnabled {
return true, nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion crypto/manager_kmip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ func TestCryptoManagerKmip(t *testing.T) {
assert.NoError(t, err)
assert.NotEmpty(t, keyID)

ok, err := m.IsValidKey(ctx, keyID)
ok, err := m.IsValidKey(ctx, providerID, keyID)
assert.NoError(t, err)
assert.True(t, ok)
})
Expand Down

0 comments on commit 5c54c3f

Please sign in to comment.