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

keyring's encrypted file backend integration #5355

Merged
merged 34 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
376fe1f
Start working on keyring's encrypted file backend integration
Dec 3, 2019
a97002e
Store keyring and test keyring files under ~/.appcli/{,test-}keyring …
Dec 4, 2019
32c374c
Keybase -> keyring migration is in fact an API and ABI breakage break…
Dec 4, 2019
79f691d
Merge branch 'master' into alessio/keyring-file-backend
Dec 4, 2019
7a11d4e
Merge branch 'master' into alessio/keyring-file-backend
fedekunze Dec 5, 2019
14c1252
Improve keyring docs
Dec 5, 2019
69bb4af
Merge branch 'master' into alessio/keyring-file-backend
Dec 5, 2019
411cb42
Replace COSMOS_SDK_TEST_KEYRING env var with a global --keybase-backe…
Dec 6, 2019
9f0c12e
Merge branch 'master' into alessio/keyring-file-backend
Dec 6, 2019
5e963a9
Merge branch 'master' into alessio/keyring-file-backend
Dec 6, 2019
ed89b9c
Add tests
Dec 6, 2019
aca7026
Cosmetic adjustments
Dec 6, 2019
fd2355e
Fix gentx
Dec 6, 2019
abc25e0
Bind flag
Dec 6, 2019
8eaa41b
Merge branch 'master' into alessio/keyring-file-backend
Dec 6, 2019
563e2f3
Simplify flag handling
Dec 7, 2019
6b925f3
Add test case
Dec 8, 2019
e72641c
Add test case
Dec 8, 2019
ed05225
Update CHANGELOG
Dec 8, 2019
fc190ab
Update
Dec 8, 2019
c4b3fa6
Merge branch 'master' into alessio/keyring-file-backend
Dec 9, 2019
dcd967e
Merge branch 'master' into alessio/keyring-file-backend
alexanderbez Dec 9, 2019
5f777bf
Create README.md
Dec 9, 2019
810083f
Typo
Dec 9, 2019
7fc84fb
Fixes
Dec 9, 2019
34ced6d
Update crypto/keys/README.md
Dec 9, 2019
c2b248f
Update crypto/keys/README.md
Dec 9, 2019
6b3b8f3
Fix NewInMemory description
Dec 9, 2019
afbb163
Add info on where keyring files are stored
Dec 9, 2019
d1cfa57
Merge branch 'master' into alessio/keyring-file-backend
alexanderbez Dec 10, 2019
1701751
Merge branch 'alessio/keyring-file-backend' of github.com:cosmos/cosm…
Dec 10, 2019
d79acf2
Merge branch 'master' into alessio/keyring-file-backend
Dec 10, 2019
81bf4fb
Merge branch 'master' into alessio/keyring-file-backend
alexanderbez Dec 11, 2019
24ba280
Merge branch 'master' into alessio/keyring-file-backend
Dec 11, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ if the provided arguments are invalid.
* (x/auth) [\#5006](https://github.com/cosmos/cosmos-sdk/pull/5006) The gas required to pass the `AnteHandler` has
increased significantly due to modular `AnteHandler` support. Increase GasLimit accordingly.
* (rest) [\#5336](https://github.com/cosmos/cosmos-sdk/issues/5336) `MsgEditValidator` uses `description` instead of `Description` as a JSON key.
* (keys) [\#5097](https://github.com/cosmos/cosmos-sdk/pull/5097) Due to the keybase -> keyring transition, keys need to be migrated. See `keys migrate` command for more info.
alessio marked this conversation as resolved.
Show resolved Hide resolved

### Features

Expand Down
4 changes: 2 additions & 2 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
// get config value for a given key
if getAction {
switch key {
case "trace", "trust-node", "indent":
case "trace", "trust-node", "indent", "keyring-file":
fmt.Println(tree.GetDefault(key, false).(bool))

default:
Expand All @@ -101,7 +101,7 @@ func runConfigCmd(cmd *cobra.Command, args []string) error {
case "chain-id", "output", "node", "broadcast-mode":
tree.Set(key, value)

case "trace", "trust-node", "indent":
case "trace", "trust-node", "indent", "keyring-file":
boolVal, err := strconv.ParseBool(value)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
FlagRPCWriteTimeout = "write-timeout"
FlagOutputDocument = "output-document" // inspired by wget -O
FlagSkipConfirmation = "yes"
FlagKeyringFile = "keyring-file"
)

// LineBreak can be included in a command list to provide a blank line
Expand Down Expand Up @@ -99,6 +100,7 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
c.Flags().Bool(FlagDryRun, false, "ignore the --gas flag and perform a simulation of a transaction, but don't broadcast it")
c.Flags().Bool(FlagGenerateOnly, false, "Build an unsigned transaction and write it to STDOUT (when enabled, the local Keybase is not accessible and the node operates offline)")
c.Flags().BoolP(FlagSkipConfirmation, "y", false, "Skip tx broadcasting prompt confirmation")
c.Flags().Bool(FlagKeyringFile, false, "Use the keyring's encrypted file backend")
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

// --gas can accept integers and "simulate"
c.Flags().Var(&GasFlagVar, "gas", fmt.Sprintf(
Expand All @@ -109,6 +111,7 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode))
viper.BindPFlag(FlagUseLedger, c.Flags().Lookup(FlagUseLedger))
viper.BindPFlag(FlagNode, c.Flags().Lookup(FlagNode))
viper.BindPFlag(FlagKeyringFile, c.Flags().Lookup(FlagKeyringFile))

c.MarkFlagRequired(FlagChainID)
}
Expand Down
1 change: 1 addition & 0 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ the flag --nosort is set.
cmd.Flags().Uint32(flagAccount, 0, "Account number for HD derivation")
cmd.Flags().Uint32(flagIndex, 0, "Address index number for HD derivation")
cmd.Flags().Bool(flags.FlagIndentResponse, false, "Add indent to JSON response")
cmd.Flags().Bool(flags.FlagKeyringFile, false, "Use the keyring's encrypted file backend")
return cmd
}

Expand Down
3 changes: 3 additions & 0 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"errors"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keys"

Expand Down Expand Up @@ -34,6 +35,8 @@ private keys stored in a ledger device cannot be deleted with the CLI.
"Skip confirmation prompt when deleting offline or ledger key references")
cmd.Flags().BoolP(flagForce, "f", false,
"Remove the key unconditionally without asking for the passphrase")
cmd.Flags().Bool(flags.FlagKeyringFile, false,
"Use the keyring's encrypted file backend")
return cmd
}

Expand Down
2 changes: 2 additions & 0 deletions client/keys/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
)

Expand All @@ -16,6 +17,7 @@ func exportKeyCommand() *cobra.Command {
Args: cobra.ExactArgs(1),
RunE: runExportCmd,
}
cmd.Flags().Bool(flags.FlagKeyringFile, false, "Use the keyring's encrypted file backend")
return cmd
}

Expand Down
2 changes: 2 additions & 0 deletions client/keys/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
)

Expand All @@ -17,6 +18,7 @@ func importKeyCommand() *cobra.Command {
Args: cobra.ExactArgs(2),
RunE: runImportCmd,
}
cmd.Flags().Bool(flags.FlagKeyringFile, false, "Use the keyring's encrypted file backend")
return cmd
}

Expand Down
1 change: 1 addition & 0 deletions client/keys/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ along with their associated name and address.`,
RunE: runListCmd,
}
cmd.Flags().Bool(flags.FlagIndentResponse, false, "Add indent to JSON response")
cmd.Flags().Bool(flags.FlagKeyringFile, false, "Use the keyring's encrypted file backend")
return cmd
}

Expand Down
1 change: 1 addition & 0 deletions client/keys/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The command asks for every passphrase. If the passphrase is incorrect, it skips
RunE: runMigrateCmd,
}

cmd.Flags().Bool(flags.FlagKeyringFile, false, "Use the keyring's encrypted file backend")
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
}
Expand Down
1 change: 1 addition & 0 deletions client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ consisting of all the keys provided by name and multisig threshold.`,
cmd.Flags().BoolP(FlagDevice, "d", false, "Output the address in a ledger device")
cmd.Flags().Uint(flagMultiSigThreshold, 1, "K out of N required signatures")
cmd.Flags().Bool(flags.FlagIndentResponse, false, "Add indent to JSON response")
cmd.Flags().Bool(flags.FlagKeyringFile, false, "Use the keyring's encrypted file backend")

return cmd
}
Expand Down
5 changes: 5 additions & 0 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func NewKeyringFromDir(rootDir string, input io.Reader) (keys.Keybase, error) {
if os.Getenv("COSMOS_SDK_TEST_KEYRING") != "" {
return keys.NewTestKeyring(sdk.GetConfig().GetKeyringServiceName(), rootDir)
}

if viper.GetBool(flags.FlagKeyringFile) {
return keys.NewKeyringFile(sdk.GetConfig().GetKeyringServiceName(), rootDir, input)
}

return keys.NewKeyring(sdk.GetConfig().GetKeyringServiceName(), rootDir, input)
}

Expand Down
43 changes: 35 additions & 8 deletions crypto/keys/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import (
"github.com/cosmos/cosmos-sdk/types"
)

const (
keyringDirName = "keyring"
testKeyringDirName = "keyring-test"
)

var _ Keybase = keyringKeybase{}

// keyringKeybase implements the Keybase interface by using the Keyring library
Expand All @@ -47,6 +52,16 @@ func NewKeyring(name string, dir string, userInput io.Reader) (Keybase, error) {
return newKeyringKeybase(db), nil
}

// NewKeyringFile creates a new instance of an encrypted file-backed keyring.
func NewKeyringFile(name string, dir string, userInput io.Reader) (Keybase, error) {
db, err := keyring.Open(newFileBackendKeyringConfig(name, dir, userInput))
if err != nil {
return nil, err
}

return newKeyringKeybase(db), nil
}

// NewTestKeyring creates a new instance of an on-disk keyring for
// testing purposes that does not prompt users for password.
func NewTestKeyring(name string, dir string) (Keybase, error) {
Expand Down Expand Up @@ -458,12 +473,30 @@ func lkbToKeyringConfig(name, dir string, buf io.Reader, test bool) keyring.Conf
return keyring.Config{
AllowedBackends: []keyring.BackendType{"file"},
ServiceName: name,
FileDir: dir,
FileDir: filepath.Join(dir, testKeyringDirName),
FilePasswordFunc: fakePrompt,
}
}

realPrompt := func(prompt string) (string, error) {
return keyring.Config{
ServiceName: name,
FileDir: dir,
FilePasswordFunc: newRealPrompt(dir, buf),
}
}

func newFileBackendKeyringConfig(name, dir string, buf io.Reader) keyring.Config {
fileDir := filepath.Join(dir, keyringDirName)
return keyring.Config{
AllowedBackends: []keyring.BackendType{"file"},
ServiceName: name,
FileDir: fileDir,
FilePasswordFunc: newRealPrompt(fileDir, buf),
}
}

func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {
return func(prompt string) (string, error) {
keyhashStored := false
keyhashFilePath := filepath.Join(dir, "keyhash")

Expand Down Expand Up @@ -532,12 +565,6 @@ func lkbToKeyringConfig(name, dir string, buf io.Reader, test bool) keyring.Conf
return pass, nil
}
}

return keyring.Config{
ServiceName: name,
FileDir: dir,
FilePasswordFunc: realPrompt,
}
}

func fakePrompt(prompt string) (string, error) {
Expand Down