Skip to content

Commit

Permalink
fix: load keystore even if --insecure flag is set (#1276)
Browse files Browse the repository at this point in the history
This commit fixes a problem when `--insecure` flag is used
with the CLI and some sub-command wants to load a key or
obtain a default eth address from the key store.

Therefore, now we'll load keystore prematurely for any command.
Now if insecure mode is activated, only TLS credentials creation
will be skipped for gRPC's client connection.
  • Loading branch information
ALex Nikonov authored and 3Hren committed Aug 7, 2018
1 parent dd5bb87 commit 0ab78d0
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/cli/commands/blacklist.go
Expand Up @@ -19,7 +19,7 @@ func init() {
var blacklistRootCmd = &cobra.Command{
Use: "blacklist",
Short: "Manage blacklisted addresses",
PersistentPreRunE: loadKeyStoreIfRequired,
PersistentPreRunE: loadKeyStoreWrapper,
}

var blacklistListCmd = &cobra.Command{
Expand Down
19 changes: 4 additions & 15 deletions cmd/cli/commands/common.go
Expand Up @@ -215,10 +215,10 @@ func initKeystore() (*accounts.MultiKeystore, error) {
}, accounts.NewInteractivePassPhraser())
}

// loadKeyStoreWrapper implemented to match cobra.Command.PreRun signature.
//
// Function loads and opens keystore. Also, storing opened key in "sessionKey" var
// to be able to reuse it into cli during one session.
// loadKeyStoreWrapper is matching cobra.Command.PreRunE signature.
// It loads default key from the given keystore and keeps the keystore instance
// in the global variable `keystore` that available for any CLI's sub-commands.
// Note that the keystore must be loaded before any command's logic it started to execute.
func loadKeyStoreWrapper(_ *cobra.Command, _ []string) error {
var err error
keystore, err = initKeystore()
Expand Down Expand Up @@ -246,17 +246,6 @@ func loadKeyStoreWrapper(_ *cobra.Command, _ []string) error {
return nil
}

// loadKeyStoreIfRequired loads eth keystore if `insecure` flag is not set.
// this wrapper is required for any command that not require eth keys implicitly
// but may use TLS to connect to the Node.
func loadKeyStoreIfRequired(cmd *cobra.Command, _ []string) error {
if !insecureFlag {
return loadKeyStoreWrapper(cmd, nil)
}

return nil
}

func showJSON(cmd Printer, s interface{}) {
b, _ := json.Marshal(s)
cmd.Printf("%s\r\n", b)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/commands/deals.go
Expand Up @@ -52,7 +52,7 @@ func init() {
var dealRootCmd = &cobra.Command{
Use: "deal",
Short: "Manage deals",
PersistentPreRunE: loadKeyStoreIfRequired,
PersistentPreRunE: loadKeyStoreWrapper,
}

var dealListCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/commands/master.go
Expand Up @@ -22,7 +22,7 @@ func init() {
var masterRootCmd = &cobra.Command{
Use: "master",
Short: "Manage master and workers addresses",
PersistentPreRunE: loadKeyStoreIfRequired,
PersistentPreRunE: loadKeyStoreWrapper,
}

var masterListCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/commands/orders.go
Expand Up @@ -27,7 +27,7 @@ func init() {
var orderRootCmd = &cobra.Command{
Use: "order",
Short: "Manage orders",
PersistentPreRunE: loadKeyStoreIfRequired,
PersistentPreRunE: loadKeyStoreWrapper,
}

var orderListCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/commands/profiles.go
Expand Up @@ -19,7 +19,7 @@ func init() {
var profileRootCmd = &cobra.Command{
Use: "profile",
Short: "Manage profiles",
PersistentPreRunE: loadKeyStoreIfRequired,
PersistentPreRunE: loadKeyStoreWrapper,
}

var profileStatusCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/commands/tasks.go
Expand Up @@ -51,7 +51,7 @@ var taskPullOutput string
var taskRootCmd = &cobra.Command{
Use: "task",
Short: "Tasks management",
PersistentPreRunE: loadKeyStoreIfRequired,
PersistentPreRunE: loadKeyStoreWrapper,
}

func getActiveDealIDs(ctx context.Context) ([]*big.Int, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/commands/tokens.go
Expand Up @@ -31,7 +31,7 @@ func init() {
var tokenRootCmd = &cobra.Command{
Use: "token",
Short: "Manage tokens",
PersistentPreRunE: loadKeyStoreIfRequired,
PersistentPreRunE: loadKeyStoreWrapper,
}

var tokenGetCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/commands/worker.go
Expand Up @@ -22,7 +22,7 @@ var (
)

func workerPreRunE(cmd *cobra.Command, args []string) error {
if err := loadKeyStoreIfRequired(cmd, args); err != nil {
if err := loadKeyStoreWrapper(cmd, args); err != nil {
return err
}

Expand Down

0 comments on commit 0ab78d0

Please sign in to comment.