-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Add keys migrate command #5097
Merged
Merged
Add keys migrate command #5097
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
94b8bc0
Export NewTestKeyringKeybase
3942d28
Add keys migrate command
a055140
Update CHANGELOG
e4c3483
Goimports
dcf9754
remove test boolean flag from NewKeybaseKeyring
5ab4d2a
Fix keys command tests
35b5380
Remove sdkConfig singleton
b5d4517
Revert "Remove sdkConfig singleton"
d4b58d7
Rework sdkConfig singleton
acbabb2
Rename KeyringKeybase -> Keyring to avoid ambiguity
9b0a431
Merge branch 'master' into alessio/keys-migrate
72aed35
Rework comments
40745e8
Merge branch 'master' into alessio/keys-migrate
alexanderbez 413daec
Clarify passphrase usage
alexanderbez 98aff61
Update godoc
alexanderbez e03d7ff
Merge branch 'master' into alessio/keys-migrate
alexanderbez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package keys | ||
|
||
import ( | ||
"bufio" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/client/input" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys" | ||
"github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func migrateCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "migrate", | ||
Short: "Migrate key information from the lagacy key database to the OS secret store, or encrypted file store as a fall-back and save it", | ||
Long: `Migrate keys from the legacy on-disk secret store to the OS keyring. | ||
The command asks for every passphrase. If the passphrase is incorrect, it skips the respective key. | ||
`, | ||
Args: cobra.ExactArgs(0), | ||
RunE: runMigrateCmd, | ||
} | ||
|
||
cmd.Flags().Bool(flags.FlagDryRun, false, "Do everything which is supposed to be done, but don't write any changes to the keyring.") | ||
return cmd | ||
} | ||
|
||
func runMigrateCmd(cmd *cobra.Command, args []string) error { | ||
// instantiate legacy keybase | ||
rootDir := viper.GetString(flags.FlagHome) | ||
legacykb, err := NewKeyBaseFromDir(rootDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// fetch list of keys from legacy keybase | ||
oldKeys, err := legacykb.List() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// instantiate keyring | ||
var keyring keys.Keybase | ||
buf := bufio.NewReader(cmd.InOrStdin()) | ||
if viper.GetBool(flags.FlagDryRun) { | ||
keyring = keys.NewTestKeybaseKeyring(types.GetConfig().GetKeyringServiceName(), rootDir) | ||
} else { | ||
keyring = keys.NewKeybaseKeyring(types.GetConfig().GetKeyringServiceName(), rootDir, buf) | ||
} | ||
|
||
for _, key := range oldKeys { | ||
legKeyInfo, err := legacykb.Export(key.GetName()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
keyName := key.GetName() | ||
keyType := key.GetType() | ||
cmd.PrintErrf("Migrating %s (%s) ...\n", key.GetName(), keyType) | ||
if keyType != keys.TypeLocal { | ||
if err := keyring.Import(keyName, legKeyInfo); err != nil { | ||
return err | ||
} | ||
continue | ||
} | ||
|
||
passwd, err := input.GetPassword("Enter passphrase to decrypt your key:", buf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
armoredPriv, err := legacykb.ExportPrivKey(keyName, passwd, "abc") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := keyring.ImportPrivKey(keyName, armoredPriv, "abc"); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package keys | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/tests" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/tendermint/tendermint/libs/cli" | ||
) | ||
|
||
func Test_runMigrateCmd(t *testing.T) { | ||
cmd := addKeyCommand() | ||
assert.NotNil(t, cmd) | ||
mockIn, _, _ := tests.ApplyMockIO(cmd) | ||
|
||
kbHome, kbCleanUp := tests.NewTestCaseDir(t) | ||
assert.NotNil(t, kbHome) | ||
defer kbCleanUp() | ||
viper.Set(flags.FlagHome, kbHome) | ||
|
||
viper.Set(cli.OutputFlag, OutputFormatText) | ||
|
||
mockIn.Reset("test1234\ntest1234\n") | ||
err := runAddCmd(cmd, []string{"keyname1"}) | ||
assert.NoError(t, err) | ||
|
||
viper.Set(flags.FlagDryRun, true) | ||
cmd = migrateCommand() | ||
mockIn, _, _ = tests.ApplyMockIO(cmd) | ||
mockIn.Reset("test1234\n") | ||
assert.NoError(t, runMigrateCmd(cmd, []string{})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
huh? Shouldn't we export with a user's encryption passphrase instead of hard-codec "abc"?