-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
196 additions
and
12 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/gnolang/gno/tm2/pkg/crypto/keys" | ||
) | ||
|
||
type RotateCfg struct { | ||
RootCfg *BaseCfg | ||
|
||
Force bool | ||
} | ||
|
||
func NewRotateCmd(rootCfg *BaseCfg, io commands.IO) *commands.Command { | ||
cfg := &RotateCfg{ | ||
RootCfg: rootCfg, | ||
} | ||
|
||
return commands.NewCommand( | ||
commands.Metadata{ | ||
Name: "rotate", | ||
ShortUsage: "rotate [flags] <key-name>", | ||
ShortHelp: "rotate the password of a key in the keybase to a new password", | ||
}, | ||
cfg, | ||
func(_ context.Context, args []string) error { | ||
return execRotate(cfg, args, io) | ||
}, | ||
) | ||
} | ||
|
||
func (c *RotateCfg) RegisterFlags(fs *flag.FlagSet) { | ||
} | ||
|
||
func execRotate(cfg *RotateCfg, args []string, io commands.IO) error { | ||
if len(args) != 1 { | ||
return flag.ErrHelp | ||
} | ||
|
||
nameOrBech32 := args[0] | ||
|
||
kb, err := keys.NewKeyBaseFromDir(cfg.RootCfg.Home) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
oldpass, err := io.GetPassword("Enter the current password:", cfg.RootCfg.InsecurePasswordStdin) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
newpass, err := io.GetCheckPassword( | ||
[2]string{ | ||
"Enter the new password to encrypt your key to disk:", | ||
"Repeat the password:", | ||
}, | ||
cfg.RootCfg.InsecurePasswordStdin, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("unable to parse provided password, %w", err) | ||
} | ||
|
||
getNewpass := func() (string, error) { return newpass, nil } | ||
err = kb.Rotate(nameOrBech32, oldpass, getNewpass) | ||
if err != nil { | ||
return err | ||
} | ||
io.ErrPrintln("Password rotated") | ||
|
||
return nil | ||
} |
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,95 @@ | ||
package client | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/gnolang/gno/tm2/pkg/crypto/keys" | ||
"github.com/gnolang/gno/tm2/pkg/testutils" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_execRotate(t *testing.T) { | ||
t.Parallel() | ||
|
||
// make new test dir | ||
kbHome, kbCleanUp := testutils.NewTestCaseDir(t) | ||
defer kbCleanUp() | ||
|
||
// initialize test options | ||
cfg := &RotateCfg{ | ||
RootCfg: &BaseCfg{ | ||
BaseOptions: BaseOptions{ | ||
Home: kbHome, | ||
InsecurePasswordStdin: true, | ||
}, | ||
}, | ||
} | ||
|
||
io := commands.NewTestIO() | ||
|
||
// Add test accounts to keybase. | ||
kb, err := keys.NewKeyBaseFromDir(kbHome) | ||
assert.NoError(t, err) | ||
|
||
keyName := "rotateApp_Key1" | ||
p1, p2 := "1234", "foobar" | ||
mnemonic := "equip will roof matter pink blind book anxiety banner elbow sun young" | ||
|
||
_, err = kb.CreateAccount(keyName, mnemonic, "", p1, 0, 0) | ||
assert.NoError(t, err) | ||
|
||
{ | ||
// test: Key not found | ||
args := []string{"blah"} | ||
io.SetIn(strings.NewReader(p1 + "\n" + p2 + "\n" + p2 + "\n")) | ||
err = execRotate(cfg, args, io) | ||
require.Error(t, err) | ||
require.Equal(t, "Key blah not found", err.Error()) | ||
} | ||
|
||
{ | ||
// test: Wrong password | ||
args := []string{keyName} | ||
io.SetIn(strings.NewReader("blah" + "\n" + p2 + "\n" + p2 + "\n")) | ||
err = execRotate(cfg, args, io) | ||
require.Error(t, err) | ||
require.Equal(t, "invalid account password", err.Error()) | ||
} | ||
|
||
{ | ||
// test: New passwords don't match | ||
args := []string{keyName} | ||
io.SetIn(strings.NewReader(p1 + "\n" + p2 + "\n" + "blah" + "\n")) | ||
err = execRotate(cfg, args, io) | ||
require.Error(t, err) | ||
require.Equal(t, "unable to parse provided password, passphrases don't match", err.Error()) | ||
} | ||
|
||
{ | ||
// Rotate the password | ||
args := []string{keyName} | ||
io.SetIn(strings.NewReader(p1 + "\n" + p2 + "\n" + p2 + "\n")) | ||
err = execRotate(cfg, args, io) | ||
require.NoError(t, err) | ||
} | ||
|
||
{ | ||
// test: The old password shouldn't work | ||
args := []string{keyName} | ||
io.SetIn(strings.NewReader(p1 + "\n" + p1 + "\n" + p1 + "\n")) | ||
err = execRotate(cfg, args, io) | ||
require.Error(t, err) | ||
require.Equal(t, "invalid account password", err.Error()) | ||
} | ||
|
||
{ | ||
// Updating the new password to itself should work | ||
args := []string{keyName} | ||
io.SetIn(strings.NewReader(p2 + "\n" + p2 + "\n" + p2 + "\n")) | ||
err = execRotate(cfg, args, io) | ||
require.NoError(t, 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
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