Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto/keyring: fix offline keys migration #8639

Merged
merged 43 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
5610039
add fix for multisig keys
sahith-narahari Feb 19, 2021
f599220
update tests
sahith-narahari Feb 19, 2021
48286a3
Merge branch 'master' into sahith/fix-key-multi
Feb 19, 2021
ca120e8
fix lint
sahith-narahari Feb 19, 2021
266c5f5
Merge branch 'master' into sahith/fix-key-multi
Feb 22, 2021
03e84ec
Merge branch 'master' into sahith/fix-key-multi
jgimeno Feb 23, 2021
88e1eed
rename function legacy
jgimeno Feb 23, 2021
48068ae
Merge branch 'master' into sahith/fix-key-multi
sahith-narahari Feb 23, 2021
3288631
fix linter
jgimeno Feb 23, 2021
da71b04
update keys
sahith-narahari Feb 23, 2021
4b9f9d9
test offline
jgimeno Feb 23, 2021
42682ce
temp commit
jgimeno Feb 23, 2021
9208860
Merge branch 'sahith/fix-key-multi' of github.com:cosmos/cosmos-sdk i…
sahith-narahari Feb 23, 2021
1afcbe3
update ledger keys
sahith-narahari Feb 23, 2021
8944c17
Merge branch 'master' into sahith/fix-key-multi
jgimeno Feb 23, 2021
a34d140
Apply suggestions from code review
Feb 23, 2021
5a65cf6
Merge branch 'master' into sahith/fix-key-multi
Feb 23, 2021
a95da25
Fix `keys migrate` command (#8703)
amaury1093 Feb 25, 2021
ea0807a
Merge branch 'master' into sahith/fix-key-multi
Feb 25, 2021
f212a49
Merge branch 'master' into sahith/fix-key-multi
Feb 26, 2021
eb03b8f
Jonathan/remove unused import pub (#8704)
jgimeno Feb 26, 2021
9e8e55d
update godoc
sahith-narahari Feb 26, 2021
3ce470c
Merge branch 'master' into sahith/fix-key-multi
Feb 26, 2021
5168b53
revert api breaking changes
sahith-narahari Feb 26, 2021
85700ae
Merge branch 'sahith/fix-key-multi' of github.com:cosmos/cosmos-sdk i…
sahith-narahari Feb 26, 2021
b53bc9e
update docs
sahith-narahari Feb 26, 2021
5a048ab
add changelog
sahith-narahari Feb 26, 2021
98416d1
Merge branch 'master' into sahith/fix-key-multi
sahith-narahari Feb 26, 2021
4e328c3
fix end message
Feb 26, 2021
32aeb7b
Merge branch 'master' into sahith/fix-key-multi
Feb 26, 2021
17b606e
try display race detector while running
Feb 26, 2021
e10efd0
turn --old-home into a positional argument
Feb 26, 2021
28070f2
Update CHANGELOG.md
Feb 26, 2021
f7d907d
remove redundant comment
Feb 26, 2021
5478cb6
try fix test
Feb 26, 2021
cbee3fd
Merge branch 'master' into sahith/fix-key-multi
Feb 26, 2021
a7babac
fixing the test case
Feb 26, 2021
455ccce
new testdata
Feb 27, 2021
a49440e
Merge branch 'master' into sahith/fix-key-multi
Feb 27, 2021
d6fca2b
crypto/keyring: reinstate the InfoImporter interface
Feb 27, 2021
8b85b5c
Update error message
sahith-narahari Feb 27, 2021
72229d2
Merge branch 'master' into sahith/fix-key-multi
sahith-narahari Mar 1, 2021
f5df8f1
Update client/keys/migrate.go
Mar 1, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ jobs:
if: env.GIT_DIFF
- name: test & coverage report creation
run: |
cat pkgs.txt.part.${{ matrix.part }} | xargs go test -mod=readonly -json -timeout 30m -race -tags='cgo ledger test_ledger_mock' > ${{ matrix.part }}-race-output.txt
xargs --arg-file=pkgs.txt.part.${{ matrix.part }} go test -mod=readonly -json -timeout 30m -race -tags='cgo ledger test_ledger_mock' | tee ${{ matrix.part }}-race-output.txt
alessio marked this conversation as resolved.
Show resolved Hide resolved
if: env.GIT_DIFF
- uses: actions/upload-artifact@v2
with:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/slashing) [\#8427](https://github.com/cosmos/cosmos-sdk/pull/8427) Fix query signing infos command
* (server) [\#8399](https://github.com/cosmos/cosmos-sdk/pull/8399) fix gRPC-web flag default value
* (server) [\#8641](https://github.com/cosmos/cosmos-sdk/pull/8641) Fix Tendermint and application configuration reading from file
* (client/keys) [\#8639] (https://github.com/cosmos/cosmos-sdk/pull/8639) Fix keys migrate for mulitisig, offline, and ledger keys. The migrate command now takes a positional old_home_dir argument.

## [v0.41.3](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.41.3) - 2021-02-18

Expand Down
36 changes: 21 additions & 15 deletions client/keys/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ const migratePassphrase = "NOOP_PASSPHRASE"
// MigrateCommand migrates key information from legacy keybase to OS secret store.
func MigrateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "migrate",
Use: "migrate <old_home_dir>",
Short: "Migrate keys from the legacy (db-based) Keybase",
Long: `Migrate key information from the legacy (db-based) Keybase to the new keyring-based Keybase.
Long: `Migrate key information from the legacy (db-based) Keybase to the new keyring-based Keyring.
The legacy Keybase used to persist keys in a LevelDB database stored in a 'keys' sub-directory of
the old client application's home directory, e.g. $HOME/.gaiacli/keys/.
For each key material entry, the command will prompt if the key should be skipped or not. If the key
is not to be skipped, the passphrase must be entered. The key will only be migrated if the passphrase
is correct. Otherwise, the command will exit and migration must be repeated.

It is recommended to run in 'dry-run' mode first to verify all key migration material.
`,
Args: cobra.ExactArgs(0),
Args: cobra.ExactArgs(1),
RunE: runMigrateCmd,
}

Expand All @@ -44,12 +46,12 @@ func runMigrateCmd(cmd *cobra.Command, args []string) error {

// instantiate legacy keybase
var legacyKb keyring.LegacyKeybase
legacyKb, err := NewLegacyKeyBaseFromDir(rootDir)
legacyKb, err := NewLegacyKeyBaseFromDir(args[0])
if err != nil {
return err
}

defer legacyKb.Close()
defer func() { _ = legacyKb.Close() }()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we need to do this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to ignore that error, as it does not really matter whether the keybase can be closed or not (we are basically doing only read-only ops)


// fetch list of keys from legacy keybase
oldKeys, err := legacyKb.List()
Expand All @@ -71,7 +73,7 @@ func runMigrateCmd(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "failed to create temporary directory for dryrun migration")
}

defer os.RemoveAll(tmpDir)
defer func() { _ = os.RemoveAll(tmpDir) }()

migrator, err = keyring.New(keyringServiceName, keyring.BackendTest, tmpDir, buf)
} else {
Expand All @@ -91,11 +93,11 @@ func runMigrateCmd(cmd *cobra.Command, args []string) error {
return nil
}

for _, key := range oldKeys {
keyName := key.GetName()
keyType := key.GetType()
for _, oldInfo := range oldKeys {
keyName := oldInfo.GetName()
keyType := oldInfo.GetType()

cmd.PrintErrf("Migrating key: '%s (%s)' ...\n", key.GetName(), keyType)
cmd.PrintErrf("Migrating key: '%s (%s)' ...\n", keyName, keyType)

// allow user to skip migrating specific keys
ok, err := input.GetConfirmation("Skip key migration?", buf, cmd.ErrOrStderr())
Expand All @@ -106,13 +108,15 @@ func runMigrateCmd(cmd *cobra.Command, args []string) error {
continue
}

// TypeLocal needs an additional step to ask password.
// The other keyring types are handled by ImportInfo.
if keyType != keyring.TypeLocal {
pubkeyArmor, err := legacyKb.ExportPubKey(keyName)
if err != nil {
return err
infoImporter, ok := migrator.(keyring.InfoImporter)
if !ok {
return fmt.Errorf("the Keyring implementation does not support import operations of Info types.")
sahith-narahari marked this conversation as resolved.
Show resolved Hide resolved
}

if err := migrator.ImportPubKey(keyName, pubkeyArmor); err != nil {
if err = infoImporter.ImportInfo(oldInfo); err != nil {
return err
}

Expand All @@ -135,8 +139,10 @@ func runMigrateCmd(cmd *cobra.Command, args []string) error {
if err := migrator.ImportPrivKey(keyName, armoredPriv, migratePassphrase); err != nil {
return err
}

}
cmd.Print("Migration Complete")

alessio marked this conversation as resolved.
Show resolved Hide resolved
cmd.PrintErrln("Migration complete.")

return err
}
30 changes: 12 additions & 18 deletions client/keys/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,38 @@ import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/client"

"github.com/stretchr/testify/require"

"github.com/otiai10/copy"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
)

func Test_runMigrateCmd(t *testing.T) {
cmd := AddKeyCommand()
_ = testutil.ApplyMockIODiscardOutErr(cmd)
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())

kbHome := t.TempDir()

clientCtx := client.Context{}.WithKeyringDir(kbHome)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

copy.Copy("testdata", kbHome)
cmd.SetArgs([]string{
"keyname1",
fmt.Sprintf("--%s=%s", cli.OutputFlag, OutputFormatText),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})
assert.NoError(t, cmd.ExecuteContext(ctx))
require.NoError(t, copy.Copy("testdata", kbHome))

cmd = MigrateCommand()
cmd := MigrateCommand()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
//mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
mockIn, mockOut := testutil.ApplyMockIO(cmd)

cmd.SetArgs([]string{
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
kbHome,
//fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
fmt.Sprintf("--%s=true", flags.FlagDryRun),
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
})

mockIn.Reset("test1234\ntest1234\n")
mockIn.Reset("\n12345678\n\n\n\n\n")
t.Log(mockOut.String())
alessio marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, cmd.ExecuteContext(ctx))
}
Binary file added client/keys/testdata/keys/keys.db/000136.ldb
Binary file not shown.
Binary file added client/keys/testdata/keys/keys.db/000137.ldb
Binary file not shown.
2 changes: 1 addition & 1 deletion client/keys/testdata/keys/keys.db/CURRENT
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MANIFEST-000005
MANIFEST-000167
2 changes: 1 addition & 1 deletion client/keys/testdata/keys/keys.db/CURRENT.bak
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MANIFEST-000003
MANIFEST-000165
Loading