From 0b5687bb54660e8ee14a4862cb11f96fe4202523 Mon Sep 17 00:00:00 2001 From: atheeshp <59333759+atheeshp@users.noreply.github.com> Date: Thu, 2 Jun 2022 19:37:50 +0530 Subject: [PATCH] fix: keys migration issue (#12122) ## Description Closes: #12093 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- CHANGELOG.md | 1 + crypto/keyring/keyring.go | 10 ++++++---- crypto/keyring/keyring_test.go | 14 +++++++++++++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 143a8381e3..e181fe9b0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -361,6 +361,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (store) [\#11117](https://github.com/cosmos/cosmos-sdk/pull/11117) Fix data race in store trace component * (x/authz) [\#11252](https://github.com/cosmos/cosmos-sdk/pull/11252) Allow insufficient funds error for authz simulation * (crypto) [\#11298](https://github.com/cosmos/cosmos-sdk/pull/11298) Fix cgo secp signature verification and update libscep256k1 library. +* (crypto) [\#12122](https://github.com/cosmos/cosmos-sdk/pull/12122) Fix keyring migration issue. ### Improvements diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index 303b0ed07f..5434a27eed 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -498,7 +498,10 @@ func (ks keystore) List() ([]*Record, error) { var res []*Record //nolint:prealloc sort.Strings(keys) for _, key := range keys { - if strings.HasSuffix(key, addressSuffix) { + // Recall that each key is twice in the keyring: + // - once with the `.info` suffix, which holds the key info + // - another time with the `.address` suffix, which only holds a reference to its associated `.info` key + if !strings.HasSuffix(key, infoSuffix) { continue } @@ -872,9 +875,8 @@ func (ks keystore) MigrateAll() error { } for _, key := range keys { - // The keyring items with `.address` suffix only holds as Data the - // key name uid, so there's nothing to migrate. - if strings.HasSuffix(key, addressSuffix) { + // The keyring items only with `.info` consists the key info. + if !strings.HasSuffix(key, infoSuffix) { continue } diff --git a/crypto/keyring/keyring_test.go b/crypto/keyring/keyring_test.go index a0a006b4f1..235a66d268 100644 --- a/crypto/keyring/keyring_test.go +++ b/crypto/keyring/keyring_test.go @@ -3,6 +3,8 @@ package keyring import ( "encoding/hex" "fmt" + "os" + "path/filepath" "strings" "testing" @@ -59,7 +61,8 @@ func TestNewKeyring(t *testing.T) { func TestKeyManagementKeyRing(t *testing.T) { cdc := getCodec() - kb, err := New("keybasename", "test", t.TempDir(), nil, cdc) + tempDir := t.TempDir() + kb, err := New("keybasename", "test", tempDir, nil, cdc) require.NoError(t, err) require.NotNil(t, cdc) @@ -151,6 +154,15 @@ func TestKeyManagementKeyRing(t *testing.T) { require.NoError(t, err) require.Equal(t, 1, len(keyS)) + // create some random directory inside the keyring directory to check migrate ignores + // all files other than *.info + newPath := filepath.Join(tempDir, "random") + require.NoError(t, os.Mkdir(newPath, 0755)) + items, err := os.ReadDir(tempDir) + require.GreaterOrEqual(t, len(items), 2) + keyS, err = kb.List() + require.NoError(t, err) + // addr cache gets nuked - and test skip flag require.NoError(t, kb.Delete(n2)) }