Merge PR #2965: add --force to keys delete for unattended execution

* add --force to keys delete for unattended execution

Skip password check when --force is on and delete
keys unconditionally.

Closes: #2961

* Forcibly cleanup keys on fixtures initialization
This commit is contained in:
Alessio Treglia 2018-11-30 20:36:55 +00:00 committed by Christopher Goes
parent a55f4490f9
commit 956c2229bf
6 changed files with 27 additions and 19 deletions

View File

@ -19,6 +19,7 @@ FEATURES
* Gaia REST API (`gaiacli advanced rest-server`) * Gaia REST API (`gaiacli advanced rest-server`)
* Gaia CLI (`gaiacli`) * 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 * Gaia

View File

@ -20,6 +20,7 @@ import (
const ( const (
flagYes = "yes" flagYes = "yes"
flagForce = "force"
) )
func deleteKeyCommand() *cobra.Command { func deleteKeyCommand() *cobra.Command {
@ -39,6 +40,8 @@ gaiacli.
cmd.Flags().BoolP(flagYes, "y", false, cmd.Flags().BoolP(flagYes, "y", false,
"Skip confirmation prompt when deleting offline or ledger key references") "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 return cmd
} }
@ -62,20 +65,24 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error {
return err return err
} }
} }
if err := kb.Delete(name, ""); err != nil { if err := kb.Delete(name, "", true); err != nil {
return err return err
} }
fmt.Fprintln(os.Stderr, "Public key reference deleted") fmt.Fprintln(os.Stderr, "Public key reference deleted")
return nil return nil
} }
oldpass, err := client.GetPassword( // skip passphrase check if run with --force
"DANGER - enter password to permanently delete key:", buf) skipPass := viper.GetBool(flagForce)
if err != nil { var oldpass string
if !skipPass {
if oldpass, err = client.GetPassword(
"DANGER - enter password to permanently delete key:", buf); err != nil {
return err return err
} }
}
err = kb.Delete(name, oldpass) err = kb.Delete(name, oldpass, skipPass)
if err != nil { if err != nil {
return err return err
} }
@ -113,7 +120,7 @@ func DeleteKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
err = kb.Delete(name, m.Password) err = kb.Delete(name, m.Password, false)
if keyerror.IsErrKeyNotFound(err) { if keyerror.IsErrKeyNotFound(err) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))

View File

@ -616,8 +616,8 @@ func getTestingHomeDirs() (string, string) {
func initializeFixtures(t *testing.T) (chainID, servAddr, port string) { func initializeFixtures(t *testing.T) (chainID, servAddr, port string) {
tests.ExecuteT(t, fmt.Sprintf("gaiad --home=%s unsafe-reset-all", gaiadHome), "") tests.ExecuteT(t, fmt.Sprintf("gaiad --home=%s unsafe-reset-all", gaiadHome), "")
os.RemoveAll(filepath.Join(gaiadHome, "config", "gentx")) os.RemoveAll(filepath.Join(gaiadHome, "config", "gentx"))
executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s foo", gaiacliHome), app.DefaultKeyPass) executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s --force foo", gaiacliHome))
executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s bar", gaiacliHome), app.DefaultKeyPass) executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s --force bar", gaiacliHome))
executeWriteCheckErr(t, fmt.Sprintf("gaiacli keys add --home=%s foo", gaiacliHome), app.DefaultKeyPass, app.DefaultKeyPass) executeWriteCheckErr(t, fmt.Sprintf("gaiacli keys add --home=%s foo", gaiacliHome), app.DefaultKeyPass, app.DefaultKeyPass)
executeWriteCheckErr(t, fmt.Sprintf("gaiacli keys add --home=%s bar", gaiacliHome), app.DefaultKeyPass, app.DefaultKeyPass) executeWriteCheckErr(t, fmt.Sprintf("gaiacli keys add --home=%s bar", gaiacliHome), app.DefaultKeyPass, app.DefaultKeyPass)
fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf( fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf(

View File

@ -371,13 +371,13 @@ func (kb dbKeybase) ImportPubKey(name string, armor string) (err error) {
// passphrases don't match. // passphrases don't match.
// Passphrase is ignored when deleting references to // Passphrase is ignored when deleting references to
// offline and Ledger / HW wallet keys. // 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 // verify we have the proper password before deleting
info, err := kb.Get(name) info, err := kb.Get(name)
if err != nil { if err != nil {
return err return err
} }
if linfo, ok := info.(localInfo); ok { if linfo, ok := info.(localInfo); ok && !skipPass {
if _, err = mintkey.UnarmorDecryptPrivKey(linfo.PrivKeyArmor, passphrase); err != nil { if _, err = mintkey.UnarmorDecryptPrivKey(linfo.PrivKeyArmor, passphrase); err != nil {
return err return err
} }

View File

@ -73,9 +73,9 @@ func TestKeyManagement(t *testing.T) {
require.Equal(t, i2.GetPubKey(), keyS[0].GetPubKey()) require.Equal(t, i2.GetPubKey(), keyS[0].GetPubKey())
// deleting a key removes it // deleting a key removes it
err = cstore.Delete("bad name", "foo") err = cstore.Delete("bad name", "foo", false)
require.NotNil(t, err) require.NotNil(t, err)
err = cstore.Delete(n1, p1) err = cstore.Delete(n1, p1, false)
require.NoError(t, err) require.NoError(t, err)
keyS, err = cstore.List() keyS, err = cstore.List()
require.NoError(t, err) require.NoError(t, err)
@ -96,14 +96,14 @@ func TestKeyManagement(t *testing.T) {
require.Equal(t, 2, len(keyS)) require.Equal(t, 2, len(keyS))
// delete the offline key // delete the offline key
err = cstore.Delete(o1, "") err = cstore.Delete(o1, "", false)
require.NoError(t, err) require.NoError(t, err)
keyS, err = cstore.List() keyS, err = cstore.List()
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(keyS)) require.Equal(t, 1, len(keyS))
// addr cache gets nuked // addr cache gets nuked - and test skip flag
err = cstore.Delete(n2, p2) err = cstore.Delete(n2, "", true)
require.NoError(t, err) require.NoError(t, err)
require.False(t, db.Has(addrKey(i2.GetAddress()))) require.False(t, db.Has(addrKey(i2.GetAddress())))
} }
@ -336,7 +336,7 @@ func TestSeedPhrase(t *testing.T) {
assert.NotEmpty(t, mnemonic) assert.NotEmpty(t, mnemonic)
// now, let us delete this key // now, let us delete this key
err = cstore.Delete(n1, p1) err = cstore.Delete(n1, p1, false)
require.Nil(t, err, "%+v", err) require.Nil(t, err, "%+v", err)
_, err = cstore.Get(n1) _, err = cstore.Get(n1)
require.NotNil(t, err) require.NotNil(t, err)

View File

@ -14,7 +14,7 @@ type Keybase interface {
List() ([]Info, error) List() ([]Info, error)
Get(name string) (Info, error) Get(name string) (Info, error)
GetByAddress(address types.AccAddress) (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 some bytes, looking up the private key to use
Sign(name, passphrase string, msg []byte) ([]byte, crypto.PubKey, error) Sign(name, passphrase string, msg []byte) ([]byte, crypto.PubKey, error)