From d8e2884d4e07a4b64eda4a397f5c90604845dd50 Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Wed, 25 Jan 2023 11:18:28 +0100 Subject: [PATCH] feat(keycard): `DeleteKeypair` endpoint added --- multiaccounts/accounts/database_test.go | 24 +++++++++++++++++++----- multiaccounts/keypairs/database.go | 18 ++++++++++++++++++ services/accounts/accounts.go | 4 ++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/multiaccounts/accounts/database_test.go b/multiaccounts/accounts/database_test.go index 28c3e48a6..7b8ece865 100644 --- a/multiaccounts/accounts/database_test.go +++ b/multiaccounts/accounts/database_test.go @@ -324,7 +324,7 @@ func TestKeypairs(t *testing.T) { require.NoError(t, err) locked = true for _, kp := range rows { - if kp.KeyUID == keyPair1.KeyUID { + if kp.KeycardUID == keycardUID { locked = kp.KeycardLocked } } @@ -337,11 +337,25 @@ func TestKeypairs(t *testing.T) { require.NoError(t, err) require.Equal(t, 1, len(rows)) // Test if correct keycard is deleted - deletedKeyPair1 := true + deletedKeyCard := true for _, kp := range rows { - if kp.KeyUID == keyPair1.KeyUID { - deletedKeyPair1 = false + if kp.KeycardUID == keycardUID { + deletedKeyCard = false } } - require.Equal(t, true, deletedKeyPair1) + require.Equal(t, true, deletedKeyCard) + + // Test detleting a keypair + err = db.DeleteKeypair(keyPair2.KeyUID) + require.NoError(t, err) + rows, err = db.GetAllMigratedKeyPairs() + require.NoError(t, err) + // Test if correct keycard is deleted + deletedKeyPair2And3 := true + for _, kp := range rows { + if kp.KeyUID == keyPair2.KeyUID { + deletedKeyPair2And3 = false + } + } + require.Equal(t, true, deletedKeyPair2And3) } diff --git a/multiaccounts/keypairs/database.go b/multiaccounts/keypairs/database.go index ebcca6642..f0c893c50 100644 --- a/multiaccounts/keypairs/database.go +++ b/multiaccounts/keypairs/database.go @@ -263,3 +263,21 @@ func (kp *KeyPairs) DeleteKeycard(kcUID string) (err error) { return err } + +func (kp *KeyPairs) DeleteKeypair(keyUID string) (err error) { + delete, err := kp.db.Prepare(` + DELETE + FROM + keypairs + WHERE + key_uid = ? + `) + if err != nil { + return err + } + defer delete.Close() + + _, err = delete.Exec(keyUID) + + return err +} diff --git a/services/accounts/accounts.go b/services/accounts/accounts.go index 7194c972a..e100c137e 100644 --- a/services/accounts/accounts.go +++ b/services/accounts/accounts.go @@ -486,6 +486,10 @@ func (api *API) DeleteKeycard(ctx context.Context, kcUID string) error { return api.db.DeleteKeycard(kcUID) } +func (api *API) DeleteKeypair(ctx context.Context, keyUID string) error { + return api.db.DeleteKeypair(keyUID) +} + func (api *API) UpdateKeycardUID(ctx context.Context, oldKcUID string, newKcUID string) error { return api.db.UpdateKeycardUID(oldKcUID, newKcUID) }