Skip to content

Commit

Permalink
add --force to keys delete for unattended execution
Browse files Browse the repository at this point in the history
Skip password check when --force is on and delete
keys unconditionally.

Closes: #2961
  • Loading branch information
Alessio Treglia committed Nov 30, 2018
1 parent 0473621 commit 225bd25
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ FEATURES
* Gaia REST API (`gaiacli advanced rest-server`)

* Gaia CLI (`gaiacli`)
- [\#2961](https://github.com/cosmos/cosmos-sdk/issues/2961) Add --force flag to gaiacli keys delete command to skip passphrase check and force key deletion unconditionally.

* Gaia

Expand Down
23 changes: 15 additions & 8 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
)

const (
flagYes = "yes"
flagYes = "yes"
flagForce = "force"
)

func deleteKeyCommand() *cobra.Command {
Expand All @@ -39,6 +40,8 @@ gaiacli.

cmd.Flags().BoolP(flagYes, "y", false,
"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")
return cmd
}

Expand All @@ -62,20 +65,24 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error {
return err
}
}
if err := kb.Delete(name, ""); err != nil {
if err := kb.Delete(name, "", true); err != nil {
return err
}
fmt.Fprintln(os.Stderr, "Public key reference deleted")
return nil
}

oldpass, err := client.GetPassword(
"DANGER - enter password to permanently delete key:", buf)
if err != nil {
return err
// skip passphrase check if run with --force
skipPass := viper.GetBool(flagForce)
var oldpass string
if !skipPass {
if oldpass, err = client.GetPassword(
"DANGER - enter password to permanently delete key:", buf); err != nil {
return err
}
}

err = kb.Delete(name, oldpass)
err = kb.Delete(name, oldpass, skipPass)
if err != nil {
return err
}
Expand Down Expand Up @@ -113,7 +120,7 @@ func DeleteKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
return
}

err = kb.Delete(name, m.Password)
err = kb.Delete(name, m.Password, false)
if keyerror.IsErrKeyNotFound(err) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(err.Error()))
Expand Down
4 changes: 2 additions & 2 deletions crypto/keys/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,13 @@ func (kb dbKeybase) ImportPubKey(name string, armor string) (err error) {
// passphrases don't match.
// Passphrase is ignored when deleting references to
// offline and Ledger / HW wallet keys.
func (kb dbKeybase) Delete(name, passphrase string) error {
func (kb dbKeybase) Delete(name, passphrase string, skipPass bool) error {
// verify we have the proper password before deleting
info, err := kb.Get(name)
if err != nil {
return err
}
if linfo, ok := info.(localInfo); ok {
if linfo, ok := info.(localInfo); ok && !skipPass {
if _, err = mintkey.UnarmorDecryptPrivKey(linfo.PrivKeyArmor, passphrase); err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions crypto/keys/keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func TestKeyManagement(t *testing.T) {
require.Equal(t, i2.GetPubKey(), keyS[0].GetPubKey())

// deleting a key removes it
err = cstore.Delete("bad name", "foo")
err = cstore.Delete("bad name", "foo", false)
require.NotNil(t, err)
err = cstore.Delete(n1, p1)
err = cstore.Delete(n1, p1, false)
require.NoError(t, err)
keyS, err = cstore.List()
require.NoError(t, err)
Expand All @@ -96,14 +96,14 @@ func TestKeyManagement(t *testing.T) {
require.Equal(t, 2, len(keyS))

// delete the offline key
err = cstore.Delete(o1, "")
err = cstore.Delete(o1, "", false)
require.NoError(t, err)
keyS, err = cstore.List()
require.NoError(t, err)
require.Equal(t, 1, len(keyS))

// addr cache gets nuked
err = cstore.Delete(n2, p2)
// addr cache gets nuked - and test skip flag
err = cstore.Delete(n2, "", true)
require.NoError(t, err)
require.False(t, db.Has(addrKey(i2.GetAddress())))
}
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestSeedPhrase(t *testing.T) {
assert.NotEmpty(t, mnemonic)

// now, let us delete this key
err = cstore.Delete(n1, p1)
err = cstore.Delete(n1, p1, false)
require.Nil(t, err, "%+v", err)
_, err = cstore.Get(n1)
require.NotNil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion crypto/keys/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Keybase interface {
List() ([]Info, error)
Get(name string) (Info, error)
GetByAddress(address types.AccAddress) (Info, error)
Delete(name, passphrase string) error
Delete(name, passphrase string, skipPass bool) error

// Sign some bytes, looking up the private key to use
Sign(name, passphrase string, msg []byte) ([]byte, crypto.PubKey, error)
Expand Down

0 comments on commit 225bd25

Please sign in to comment.