Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the prompting/confirmation down into the password implementations. #552

Merged
merged 1 commit into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions cmd/cosign/cli/generate_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,30 +121,11 @@ func GenerateKeyPairCmd(ctx context.Context, kmsVal string, args []string) error
}

func GetPass(confirm bool) ([]byte, error) {
read := Read()
fmt.Fprint(os.Stderr, "Enter password for private key: ")
pw1, err := read()
fmt.Fprintln(os.Stderr)
if err != nil {
return nil, err
}
if !confirm {
return pw1, nil
}
fmt.Fprint(os.Stderr, "Enter again: ")
pw2, err := read()
fmt.Fprintln(os.Stderr)
if err != nil {
return nil, err
}

if string(pw1) != string(pw2) {
return nil, errors.New("passwords do not match")
}
return pw1, nil
read := Read(confirm)
return read()
}

func readPasswordFn() func() ([]byte, error) {
func readPasswordFn(confirm bool) func() ([]byte, error) {
pw, ok := os.LookupEnv("COSIGN_PASSWORD")
switch {
case ok:
Expand All @@ -153,7 +134,7 @@ func readPasswordFn() func() ([]byte, error) {
}
case term.IsTerminal(0):
return func() ([]byte, error) {
return term.ReadPassword(0)
return getPassFromTerm(confirm)
}
// Handle piped in passwords.
default:
Expand All @@ -162,3 +143,25 @@ func readPasswordFn() func() ([]byte, error) {
}
}
}

func getPassFromTerm(confirm bool) ([]byte, error) {
fmt.Fprint(os.Stderr, "Enter password for private key: ")
pw1, err := term.ReadPassword(0)
if err != nil {
return nil, err
}
if !confirm {
return pw1, nil
}
fmt.Fprint(os.Stderr, "Enter again: ")
pw2, err := term.ReadPassword(0)
fmt.Fprintln(os.Stderr)
if err != nil {
return nil, err
}

if string(pw1) != string(pw2) {
return nil, errors.New("passwords do not match")
}
return pw1, nil
}
4 changes: 2 additions & 2 deletions cmd/cosign/cli/generate_key_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
func TestReadPasswordFn_env(t *testing.T) {
os.Setenv("COSIGN_PASSWORD", "foo")
defer os.Unsetenv("COSIGN_PASSWORD")
b, err := readPasswordFn()()
b, err := readPasswordFn(true)()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -37,7 +37,7 @@ func TestReadPasswordFn_env(t *testing.T) {
func TestReadPasswordFn_envEmptyVal(t *testing.T) {
os.Setenv("COSIGN_PASSWORD", "")
defer os.Unsetenv("COSIGN_PASSWORD")
b, err := readPasswordFn()()
b, err := readPasswordFn(true)()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down