Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit da6c18e

Browse files
committed
Remove password reader and mock AskSecret wherever needed
1 parent 0f95f2b commit da6c18e

10 files changed

Lines changed: 64 additions & 73 deletions

File tree

internals/cli/ui/ask.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func AskWithDefault(io IO, question, defaultValue string) (string, error) {
5151

5252
// AskSecret prints out the question and reads back the input,
5353
// without echoing it back. Useful for passwords and other sensitive inputs.
54-
func AskSecret(io IO, passwordReader PasswordReader, question string) (string, error) {
54+
func AskSecret(io IO, question string) (string, error) {
5555
promptIn, promptOut, err := io.Prompts()
5656
if err != nil {
5757
return "", err
@@ -62,7 +62,7 @@ func AskSecret(io IO, passwordReader PasswordReader, question string) (string, e
6262
return "", err
6363
}
6464

65-
secret, err := passwordReader.read(promptIn)
65+
secret, err := readPassword(promptIn)
6666
if err != nil {
6767
return "", ErrReadInput(err)
6868
}
@@ -145,14 +145,14 @@ func ConfirmCaseInsensitive(io IO, question string, expected ...string) (bool, e
145145
// the answers still haven't matched after trying n times, the error
146146
// ErrPassphrasesDoNotMatch is returned. For the empty answer ("") no
147147
// confirmation is asked.
148-
func AskPassphrase(io IO, passwordReader PasswordReader, question string, repeatPhrase string, n int) (string, error) {
148+
func AskPassphrase(io IO, question string, repeatPhrase string, n int) (string, error) {
149149
_, promptOut, err := io.Prompts()
150150
if err != nil {
151151
return "", err
152152
}
153153

154154
for i := 0; i < n; i++ {
155-
answer, err := AskSecret(io, passwordReader, question)
155+
answer, err := AskSecret(io, question)
156156
if err != nil {
157157
return "", err
158158
}
@@ -161,7 +161,7 @@ func AskPassphrase(io IO, passwordReader PasswordReader, question string, repeat
161161
return answer, nil
162162
}
163163

164-
confirmed, err := AskSecret(io, passwordReader, repeatPhrase)
164+
confirmed, err := AskSecret(io, repeatPhrase)
165165
if err != nil {
166166
return "", err
167167
}

internals/cli/ui/io.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,8 @@ func (o UserIO) IsStdoutPiped() bool {
7575
return isPiped(o.Output)
7676
}
7777

78-
type PasswordReader interface {
79-
read(reader io.Reader) (string, error)
80-
}
81-
82-
type passwordReader struct{}
83-
84-
// NewPasswordReader returns a reader that reads a string from the terminal without echoing the user input.
85-
func NewPasswordReader() PasswordReader {
86-
return &passwordReader{}
87-
}
88-
89-
// Read reads one line of input from the terminal without echoing the user input.
90-
func (pr *passwordReader) read(r io.Reader) (string, error) {
78+
// readPassword reads one line of input from the terminal without echoing the user input.
79+
func readPassword(r io.Reader) (string, error) {
9180
file, ok := r.(*os.File)
9281
if !ok {
9382
return "", ErrCannotAsk

internals/secrethub/account_init.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ var (
3636
// AccountInitCommand creates, stores and outputs a credential.
3737
type AccountInitCommand struct {
3838
io ui.IO
39-
passwordReader ui.PasswordReader
4039
useClipboard bool
4140
noWait bool
4241
isContinue bool
@@ -51,7 +50,6 @@ type AccountInitCommand struct {
5150
func NewAccountInitCommand(io ui.IO, newClient newClientFunc, credentialStore CredentialConfig) *AccountInitCommand {
5251
return &AccountInitCommand{
5352
io: io,
54-
passwordReader: ui.NewPasswordReader(),
5553
credentialStore: credentialStore,
5654
clipper: clip.NewClipboard(),
5755
progressPrinter: progress.NewPrinter(io.Stdout(), 500*time.Millisecond),
@@ -162,7 +160,7 @@ func (cmd *AccountInitCommand) Run() error {
162160
var passphrase string
163161
if !cmd.credentialStore.IsPassphraseSet() && !cmd.force {
164162
var err error
165-
passphrase, err = ui.AskPassphrase(cmd.io, cmd.passwordReader, "Please enter a passphrase to protect your local credential (leave empty for no passphrase): ", "Enter the same passphrase again: ", 3)
163+
passphrase, err = ui.AskPassphrase(cmd.io, "Please enter a passphrase to protect your local credential (leave empty for no passphrase): ", "Enter the same passphrase again: ", 3)
166164
if err != nil {
167165
return err
168166
}

internals/secrethub/config_update_passphrase.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import (
1111

1212
type ConfigUpdatePassphraseCommand struct {
1313
io ui.IO
14-
passwordReader ui.PasswordReader
1514
credentialStore CredentialConfig
1615
}
1716

1817
// NewConfigUpdatePassphraseCommand creates a new ConfigUpdatePassphraseCommand.
1918
func NewConfigUpdatePassphraseCommand(io ui.IO, credentialStore CredentialConfig) *ConfigUpdatePassphraseCommand {
2019
return &ConfigUpdatePassphraseCommand{
2120
io: io,
22-
passwordReader: ui.NewPasswordReader(),
2321
credentialStore: credentialStore,
2422
}
2523
}
@@ -60,7 +58,7 @@ func (cmd *ConfigUpdatePassphraseCommand) Run() error {
6058
return err
6159
}
6260

63-
passphrase, err := ui.AskPassphrase(cmd.io, cmd.passwordReader, "Please enter a passphrase to (re)encrypt your local credential (leave empty for no passphrase): ", "Enter the same passphrase again: ", 3)
61+
passphrase, err := ui.AskPassphrase(cmd.io, "Please enter a passphrase to (re)encrypt your local credential (leave empty for no passphrase): ", "Enter the same passphrase again: ", 3)
6462
if err != nil {
6563
return err
6664
}

internals/secrethub/init.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ type InitCommand struct {
1919
backupCode string
2020
force bool
2121
io ui.IO
22-
passwordReader ui.PasswordReader
2322
newClient newClientFunc
2423
newClientWithoutCredentials func(credentials.Provider) (secrethub.ClientInterface, error)
2524
credentialStore CredentialConfig
@@ -30,7 +29,6 @@ type InitCommand struct {
3029
func NewInitCommand(io ui.IO, newClient newClientFunc, newClientWithoutCredentials func(credentials.Provider) (secrethub.ClientInterface, error), credentialStore CredentialConfig) *InitCommand {
3130
return &InitCommand{
3231
io: io,
33-
passwordReader: ui.NewPasswordReader(),
3432
newClient: newClient,
3533
newClientWithoutCredentials: newClientWithoutCredentials,
3634
credentialStore: credentialStore,
@@ -169,7 +167,7 @@ func (cmd *InitCommand) Run() error {
169167
var passphrase string
170168
if !cmd.credentialStore.IsPassphraseSet() && !cmd.force {
171169
var err error
172-
passphrase, err = ui.AskPassphrase(cmd.io, cmd.passwordReader, "Please enter a passphrase to protect your local credential (leave empty for no passphrase): ", "Enter the same passphrase again: ", 3)
170+
passphrase, err = ui.AskPassphrase(cmd.io, "Please enter a passphrase to protect your local credential (leave empty for no passphrase): ", "Enter the same passphrase again: ", 3)
173171
if err != nil {
174172
return err
175173
}

internals/secrethub/keyring.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ type PassphraseReader interface {
4444

4545
// passphraseReader provides passphrase reading capability to the CLI.
4646
type passphraseReader struct {
47-
tries int
48-
hasAsked bool
49-
io ui.IO
50-
passwordReader ui.PasswordReader
51-
FlagValue string
52-
Cache *PassphraseCache
47+
tries int
48+
hasAsked bool
49+
io ui.IO
50+
FlagValue string
51+
Cache *PassphraseCache
5352
}
5453

5554
func (pr *passphraseReader) Read() ([]byte, error) {
@@ -74,10 +73,9 @@ func NewPassphraseReader(io ui.IO, credentialPassphrase string, credentialPassph
7473
keyring := NewKeyring()
7574

7675
return &passphraseReader{
77-
io: io,
78-
passwordReader: ui.NewPasswordReader(),
79-
FlagValue: credentialPassphrase,
80-
Cache: NewPassphraseCache(ttl, cleaner, keyring),
76+
io: io,
77+
FlagValue: credentialPassphrase,
78+
Cache: NewPassphraseCache(ttl, cleaner, keyring),
8179
}
8280
}
8381

@@ -106,9 +104,9 @@ func (pr *passphraseReader) get() (string, error) {
106104
var err error
107105
var passphrase string
108106
if pr.hasAsked {
109-
passphrase, err = ui.AskSecret(pr.io, pr.passwordReader, "Incorrect passphrase, try again:")
107+
passphrase, err = ui.AskSecret(pr.io, "Incorrect passphrase, try again:")
110108
} else {
111-
passphrase, err = ui.AskSecret(pr.io, pr.passwordReader, "Please put in the passphrase to unlock your credential:")
109+
passphrase, err = ui.AskSecret(pr.io, "Please put in the passphrase to unlock your credential:")
112110
}
113111
if err == ui.ErrCannotAsk {
114112
return "", ErrPassphraseFlagNotSet // if we cannot ask, users should use the --passphrase flag

internals/secrethub/service_deploy_winrm.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,21 @@ var (
3838

3939
// ServiceDeployWinRmCommand creates a service and installs the configuration using WinRM.
4040
type ServiceDeployWinRmCommand struct {
41-
resourceURI *url.URL
42-
authType string
43-
username string
44-
password string
45-
clientCert string
46-
clientKey string
47-
caCert string
48-
noVerify bool
49-
io ui.IO
50-
passwordReader ui.PasswordReader
41+
resourceURI *url.URL
42+
authType string
43+
username string
44+
password string
45+
clientCert string
46+
clientKey string
47+
caCert string
48+
noVerify bool
49+
io ui.IO
5150
}
5251

5352
// NewServiceDeployWinRmCommand creates a new ServiceDeployWinRmCommand.
5453
func NewServiceDeployWinRmCommand(io ui.IO) *ServiceDeployWinRmCommand {
5554
return &ServiceDeployWinRmCommand{
56-
io: io,
57-
passwordReader: ui.NewPasswordReader(),
55+
io: io,
5856
}
5957
}
6058

@@ -133,7 +131,7 @@ func (cmd *ServiceDeployWinRmCommand) Run() error {
133131
}
134132

135133
if cmd.password == "" {
136-
cmd.password, err = ui.AskSecret(cmd.io, cmd.passwordReader, fmt.Sprintf("What is the password for user %s?\n", cmd.username))
134+
cmd.password, err = ui.AskSecret(cmd.io, fmt.Sprintf("What is the password for user %s?\n", cmd.username))
137135
if err != nil {
138136
return err
139137
}

internals/secrethub/signup.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ type SignUpCommand struct {
2727
orgDescription string
2828
force bool
2929
io ui.IO
30-
passwordReader ui.PasswordReader
3130
newClient newClientFunc
3231
credentialStore CredentialConfig
3332
progressPrinter progress.Printer
@@ -37,7 +36,6 @@ type SignUpCommand struct {
3736
func NewSignUpCommand(io ui.IO, newClient newClientFunc, credentialStore CredentialConfig) *SignUpCommand {
3837
return &SignUpCommand{
3938
io: io,
40-
passwordReader: ui.NewPasswordReader(),
4139
newClient: newClient,
4240
credentialStore: credentialStore,
4341
progressPrinter: progress.NewPrinter(io.Stdout(), 500*time.Millisecond),
@@ -132,7 +130,7 @@ func (cmd *SignUpCommand) Run() error {
132130
var passphrase string
133131
if !cmd.credentialStore.IsPassphraseSet() && !cmd.force {
134132
var err error
135-
passphrase, err = ui.AskPassphrase(cmd.io, cmd.passwordReader, "Please enter a passphrase to protect your local credential (leave empty for no passphrase): ", "Enter the same passphrase again: ", 3)
133+
passphrase, err = ui.AskPassphrase(cmd.io, "Please enter a passphrase to protect your local credential (leave empty for no passphrase): ", "Enter the same passphrase again: ", 3)
136134
if err != nil {
137135
return err
138136
}

internals/secrethub/write.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ var (
2121

2222
// WriteCommand is a command to write content to a secret.
2323
type WriteCommand struct {
24-
io ui.IO
25-
passwordReader ui.PasswordReader
26-
path api.SecretPath
27-
inFile string
28-
multiline bool
29-
useClipboard bool
30-
noTrim bool
31-
clipper clip.Clipper
32-
newClient newClientFunc
24+
io ui.IO
25+
askSecret func(io ui.IO, question string) (string, error)
26+
path api.SecretPath
27+
inFile string
28+
multiline bool
29+
useClipboard bool
30+
noTrim bool
31+
clipper clip.Clipper
32+
newClient newClientFunc
3333
}
3434

3535
// NewWriteCommand creates a new WriteCommand.
3636
func NewWriteCommand(io ui.IO, newClient newClientFunc) *WriteCommand {
3737
return &WriteCommand{
38-
clipper: clip.NewClipboard(),
39-
io: io,
40-
passwordReader: ui.NewPasswordReader(),
41-
newClient: newClient,
38+
clipper: clip.NewClipboard(),
39+
io: io,
40+
askSecret: ui.AskSecret,
41+
newClient: newClient,
4242
}
4343
}
4444

@@ -96,7 +96,7 @@ func (cmd *WriteCommand) Run() error {
9696
return err
9797
}
9898
} else {
99-
str, err := ui.AskSecret(cmd.io, cmd.passwordReader, "Please type in the value of the secret, followed by an [ENTER]:")
99+
str, err := cmd.askSecret(cmd.io, "Please type in the value of the secret, followed by an [ENTER]:")
100100
if err != nil {
101101
return err
102102
}

internals/secrethub/write_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package secrethub
22

33
import (
4+
"bufio"
45
"bytes"
56
"testing"
67

@@ -18,6 +19,19 @@ import (
1819
func TestWriteCommand_Run(t *testing.T) {
1920
testErr := errio.Namespace("test").Code("test").Error("test error")
2021

22+
fakeAskSecretFunc := func(io ui.IO, question string) (s string, err error) {
23+
reader, writer, err := io.Prompts()
24+
if err != nil {
25+
return "", err
26+
}
27+
_, err = writer.Write([]byte(question + "\n"))
28+
if err != nil {
29+
return "", err
30+
}
31+
line, _, err := bufio.NewReader(reader).ReadLine()
32+
return string(line), err
33+
}
34+
2135
cases := map[string]struct {
2236
cmd WriteCommand
2337
writeFunc func(path string, data []byte) (*api.SecretVersion, error)
@@ -121,8 +135,8 @@ func TestWriteCommand_Run(t *testing.T) {
121135
},
122136
"ask secret success": {
123137
cmd: WriteCommand{
124-
path: "namespace/repo/secret",
125-
passwordReader: ui.FakePasswordReader{},
138+
path: "namespace/repo/secret",
139+
askSecret: fakeAskSecretFunc,
126140
},
127141
promptIn: "asked secret value",
128142
promptOut: "Please type in the value of the secret, followed by an [ENTER]:\n",
@@ -138,8 +152,8 @@ func TestWriteCommand_Run(t *testing.T) {
138152
},
139153
"ask secret error": {
140154
cmd: WriteCommand{
141-
path: "namespace/repo/secret",
142-
passwordReader: ui.FakePasswordReader{},
155+
path: "namespace/repo/secret",
156+
askSecret: fakeAskSecretFunc,
143157
},
144158
promptErr: testErr,
145159
err: testErr,

0 commit comments

Comments
 (0)