Skip to content

Commit 0f9899a

Browse files
committed
Consolidate passphrase reading functionality.
Basically readPassphrase was replaced by readSecret in FiloSottile/age@c0e80ef so we can just use that and stick to the latest version of cmd/age/tui.go. Signed-off-by: Felix Fontein <[email protected]>
1 parent 1b99a29 commit 0f9899a

File tree

2 files changed

+8
-39
lines changed

2 files changed

+8
-39
lines changed

age/encrypted_keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func unwrapIdentities(key string, reader io.Reader) (ParsedIdentities, error) {
145145
Passphrase: func() (string, error) {
146146
conn, err := gpgagent.NewConn()
147147
if err != nil {
148-
passphrase, err := readPassphrase("Enter passphrase for identity " + key + ":")
148+
passphrase, err := readSecret("Enter passphrase for identity " + key + ":")
149149
if err != nil {
150150
return "", err
151151
}

age/tui.go

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// These functions have been copied from the age project
2-
// https://github.com/FiloSottile/age/blob/v1.0.0/cmd/age/encrypted_keys.go
32
// https://github.com/FiloSottile/age/blob/3d91014ea095e8d70f7c6c4833f89b53a96e0832/cmd/age/tui.go
43
//
54
// Copyright 2021 The age Authors. All rights reserved.
@@ -27,43 +26,6 @@ const (
2726
SopsAgePasswordEnv = "SOPS_AGE_PASSWORD"
2827
)
2928

30-
// readPassphrase reads a passphrase from the terminal. It does not read from a
31-
// non-terminal stdin, so it does not check stdinInUse.
32-
func readPassphrase(prompt string) ([]byte, error) {
33-
if testing.Testing() {
34-
password := os.Getenv(SopsAgePasswordEnv)
35-
if password != "" {
36-
return []byte(password), nil
37-
}
38-
}
39-
40-
var (
41-
err error
42-
passphrase []byte
43-
)
44-
45-
err = withTerminal(func(in, out *os.File) error {
46-
_, err := fmt.Fprintf(out, "%s ", prompt)
47-
if err != nil {
48-
return fmt.Errorf("could not write prompt: %v", err)
49-
}
50-
51-
// Use CRLF to work around an apparent bug in WSL2's handling of CONOUT$.
52-
// Only when running a Windows binary from WSL2, the cursor would not go
53-
// back to the start of the line with a simple LF. Honestly, it's impressive
54-
// CONIN$ and CONOUT$ even work at all inside WSL2.
55-
defer fmt.Fprintf(out, "\r\n")
56-
57-
if passphrase, err = term.ReadPassword(int(in.Fd())); err != nil {
58-
return fmt.Errorf("could not read passphrase: %v", err)
59-
}
60-
61-
return nil
62-
})
63-
64-
return passphrase, err
65-
}
66-
6729
func printf(format string, v ...interface{}) {
6830
log.Printf("age: "+format, v...)
6931
}
@@ -133,6 +95,13 @@ func withTerminal(f func(in, out *os.File) error) error {
13395

13496
// readSecret reads a value from the terminal with no echo. The prompt is ephemeral.
13597
func readSecret(prompt string) (s []byte, err error) {
98+
if testing.Testing() {
99+
password := os.Getenv(SopsAgePasswordEnv)
100+
if password != "" {
101+
return []byte(password), nil
102+
}
103+
}
104+
136105
err = withTerminal(func(in, out *os.File) error {
137106
fmt.Fprintf(out, "%s ", prompt)
138107
defer clearLine(out)

0 commit comments

Comments
 (0)