commit f7bc536d5692b2a965a699139d920e420c1f6e7e
parent 3e905a0842688c09529b776eeb654abaad7698d3
Author: Sean Enck <sean@ttypty.com>
Date: Sun, 11 Aug 2024 07:37:26 -0400
rekey should use the same handling as insert inputs
Diffstat:
4 files changed, 16 insertions(+), 75 deletions(-)
diff --git a/internal/app/core.go b/internal/app/core.go
@@ -96,6 +96,7 @@ type (
UserInputOptions interface {
CommandOptions
IsPipe() bool
+ Input(bool, bool) ([]byte, error)
}
// DefaultCommand is the default CLI app type for actual execution
diff --git a/internal/app/insert.go b/internal/app/insert.go
@@ -12,11 +12,6 @@ import (
type (
// InsertMode changes how inserts are handled
InsertMode uint
- // InsertOptions are functions required for insert
- InsertOptions interface {
- UserInputOptions
- Input(bool, bool) ([]byte, error)
- }
)
const (
@@ -29,7 +24,7 @@ const (
)
// Insert will execute an insert
-func Insert(cmd InsertOptions, mode InsertMode) error {
+func Insert(cmd UserInputOptions, mode InsertMode) error {
t := cmd.Transaction()
args := cmd.Args()
if len(args) != 1 {
diff --git a/internal/app/rekey.go b/internal/app/rekey.go
@@ -1,44 +1,11 @@
package app
import (
- "errors"
- "fmt"
-
"github.com/seanenck/lockbox/internal/config"
)
-type (
- // KeyerOptions defines how rekeying happens
- KeyerOptions interface {
- UserInputOptions
- Password() (string, error)
- ReadLine() (string, error)
- }
-)
-
-func getNewPassword(pipe bool, text, against string, r KeyerOptions) (string, error) {
- if pipe {
- val, err := r.ReadLine()
- if err != nil {
- return "", err
- }
- return val, nil
- }
- fmt.Printf("%s ", text)
- p, err := r.Password()
- if err != nil {
- return "", err
- }
- if against != "" {
- if p != against {
- return "", errors.New("rekey passwords do not match")
- }
- }
- return p, nil
-}
-
// ReKey handles entry rekeying
-func ReKey(cmd KeyerOptions) error {
+func ReKey(cmd UserInputOptions) error {
args := cmd.Args()
vars, err := config.GetReKey(args)
if err != nil {
@@ -52,21 +19,11 @@ func ReKey(cmd KeyerOptions) error {
}
var pass string
if !vars.NoKey {
- first, err := getNewPassword(piping, "new", "", cmd)
+ p, err := cmd.Input(piping, false)
if err != nil {
return err
}
- if !piping {
- fmt.Println()
- if _, err := getNewPassword(piping, "verify", first, cmd); err != nil {
- return err
- }
- fmt.Println()
- }
- pass = first
- if pass == "" {
- return errors.New("password required but not given")
- }
+ pass = string(p)
}
return cmd.Transaction().ReKey(pass, vars.KeyFile)
}
diff --git a/internal/app/rekey_test.go b/internal/app/rekey_test.go
@@ -11,13 +11,12 @@ import (
type (
mockKeyer struct {
- pass string
- secondPass string
- confirm bool
- args []string
- buf bytes.Buffer
- t *testing.T
- pipe bool
+ pass string
+ confirm bool
+ args []string
+ buf bytes.Buffer
+ t *testing.T
+ pipe bool
}
)
@@ -33,15 +32,8 @@ func (m *mockKeyer) Args() []string {
return m.args
}
-func (m *mockKeyer) ReadLine() (string, error) {
- return m.Password()
-}
-
-func (m *mockKeyer) Password() (string, error) {
- p := m.pass
- m.pass = m.secondPass
- m.secondPass = ""
- return p, nil
+func (m *mockKeyer) Input(pipe, multi bool) ([]byte, error) {
+ return []byte(m.pass), nil
}
func (m *mockKeyer) IsPipe() bool {
@@ -59,15 +51,11 @@ func TestReKey(t *testing.T) {
t.Errorf("invalid error: %v", err)
}
mock.confirm = true
- if err := app.ReKey(mock); err == nil || err.Error() != "password required but not given" {
+ mock.pipe = false
+ if err := app.ReKey(mock); err == nil || err.Error() != "key and/or keyfile must be set" {
t.Errorf("invalid error: %v", err)
}
mock.pass = "abc"
- if err := app.ReKey(mock); err == nil || err.Error() != "rekey passwords do not match" {
- t.Errorf("invalid error: %v", err)
- }
- mock.pass = "xyz"
- mock.secondPass = "xyz"
if err := app.ReKey(mock); err != nil {
t.Errorf("invalid error: %v", err)
}
@@ -77,7 +65,7 @@ func TestReKeyPipe(t *testing.T) {
newMockCommand(t)
mock := &mockKeyer{}
mock.pipe = true
- if err := app.ReKey(mock); err == nil || err.Error() != "password required but not given" {
+ if err := app.ReKey(mock); err == nil || err.Error() != "key and/or keyfile must be set" {
t.Errorf("invalid error: %v", err)
}
mock.pass = "abc"