From 238620e16b6863907a790a1325f3cd6b985cda35 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 18 Nov 2026 11:31:40 -0700 Subject: [PATCH] Say when --key - is waiting for the key (#75) `--key -` and `--token -` read a line of standard input with no prompt, so `inbuxa upgrade --key -` looked stuck. On a terminal it now prints "API key (paste it and press Enter): " to stderr and doesn't echo what's pasted. Piped input is read as before. One helper, readSecret, now reads it for install/upgrade, join, certs, dns --refresh and relay --token, instead of three copies of the read. --- cmd/inbuxa/main.go | 55 ++++++++++++++++++++++++++------------ cmd/inbuxa/main_test.go | 20 ++++++++++++++ internal/tui/term_linux.go | 19 +++++++++++++ internal/tui/term_other.go | 3 +++ 4 files changed, 80 insertions(+), 17 deletions(-) diff --git a/cmd/inbuxa/main.go b/cmd/inbuxa/main.go index 13c473a..1fd2a88 100644 --- a/cmd/inbuxa/main.go +++ b/cmd/inbuxa/main.go @@ -499,12 +499,12 @@ func join(args []string) int { return 2 } if *key == "-" { - line, err := bufio.NewReader(os.Stdin).ReadString('\n') - if err != nil && line == "" { - fmt.Fprintln(os.Stderr, "reading the key from standard input: "+err.Error()) + k, err := readKey(*key) + if err != nil { + fmt.Fprintln(os.Stderr, err.Error()) return 2 } - *key = strings.TrimSpace(line) + *key = k } if *key == "" { fmt.Fprintln(os.Stderr, "an API key is needed: make one in the console (your account, API keys) and pass it with --key or INBUXA_JOIN_KEY") @@ -1178,12 +1178,12 @@ func certsCmd(args []string) int { return 0 } if *key == "-" { - line, err := bufio.NewReader(os.Stdin).ReadString('\n') - if err != nil && line == "" { - fmt.Fprintln(os.Stderr, "reading the key from standard input: "+err.Error()) + k, err := readKey(*key) + if err != nil { + fmt.Fprintln(os.Stderr, err.Error()) return 2 } - *key = strings.TrimSpace(line) + *key = k } if *key == "" { fmt.Println("\nA new order needs an API key: make one in the console (your account, API keys), then") @@ -1277,12 +1277,12 @@ func dnsCmd(args []string) int { } if *key == "-" { - line, err := bufio.NewReader(os.Stdin).ReadString('\n') - if err != nil && line == "" { - fmt.Fprintln(os.Stderr, "reading the key from standard input: "+err.Error()) + k, err := readKey(*key) + if err != nil { + fmt.Fprintln(os.Stderr, err.Error()) return 2 } - *key = strings.TrimSpace(line) + *key = k } if *key == "" { fmt.Println("Reading the records live needs an API key: make one in the console (your account, API") @@ -1536,7 +1536,7 @@ func relayInstall(ctx context.Context, args []string) int { } // Read before the plan, which needs to know whether there is one; a // plan that then refuses has still read it, and forgets it. - t, err := readKey(*token) + t, err := readSecret(*token, "Relay token") if err != nil { fmt.Fprintln(os.Stderr, err.Error()) return 2 @@ -1918,12 +1918,33 @@ func render(f host.Facts) string { return plan.SurveyText(f) } // readKey is an API key given as --key: "-" reads it from standard input, // so it stays out of the shell's history. func readKey(key string) (string, error) { - if key != "-" { - return key, nil + return readSecret(key, "API key") +} + +// readSecret is a --key or --token value: as given, or, for "-", one line of +// standard input. On a terminal it says it's waiting and doesn't echo what's +// pasted (#75); piped input is read as it comes. +func readSecret(value, what string) (string, error) { + if value != "-" { + return value, nil } - line, err := bufio.NewReader(os.Stdin).ReadString('\n') + if tui.IsTerminal(os.Stdin) { + fmt.Fprintf(os.Stderr, "%s (paste it and press Enter): ", what) + // The newline still echoes, so the next output starts on a line of + // its own. + if restore, err := tui.NoEcho(os.Stdin); err == nil { + defer restore() + } + } + return readSecretLine(os.Stdin, what) +} + +// readSecretLine reads the first line of r, without its line ending. +func readSecretLine(r io.Reader, what string) (string, error) { + line, err := bufio.NewReader(r).ReadString('\n') if err != nil && line == "" { - return "", fmt.Errorf("reading the key from standard input: %w", err) + f := strings.Fields(what) + return "", fmt.Errorf("reading the %s from standard input: %w", strings.ToLower(f[len(f)-1]), err) } return strings.TrimSpace(line), nil } diff --git a/cmd/inbuxa/main_test.go b/cmd/inbuxa/main_test.go index 77ac5b9..a04eb03 100644 --- a/cmd/inbuxa/main_test.go +++ b/cmd/inbuxa/main_test.go @@ -6,6 +6,7 @@ package main import ( "os" "path/filepath" + "strings" "testing" ) @@ -56,3 +57,22 @@ func TestDNSTakesItsFlags(t *testing.T) { t.Errorf("an unknown option was accepted: %d", code) } } + +// "--key -" with piped input reads the first line as it comes, prompts for +// nothing, and says which secret it couldn't read (#75). +func TestReadSecretLine(t *testing.T) { + got, err := readSecretLine(strings.NewReader(" abc123 \nsecond\n"), "API key") + if err != nil || got != "abc123" { + t.Fatalf("got %q, %v", got, err) + } + if got, err := readSecretLine(strings.NewReader("nonewline"), "API key"); err != nil || got != "nonewline" { + t.Fatalf("no newline: got %q, %v", got, err) + } + _, err = readSecretLine(strings.NewReader(""), "Relay token") + if err == nil || !strings.Contains(err.Error(), "reading the token from standard input") { + t.Fatalf("empty input: %v", err) + } + if got, err := readSecret("given", "API key"); err != nil || got != "given" { + t.Fatalf("a value other than - is used as given: %q, %v", got, err) + } +} diff --git a/internal/tui/term_linux.go b/internal/tui/term_linux.go index 2dff589..9ea4d14 100644 --- a/internal/tui/term_linux.go +++ b/internal/tui/term_linux.go @@ -37,6 +37,25 @@ func rawMode(f *os.File) (restore func(), err error) { }, nil } +// NoEcho turns off echo on the terminal f, keeping line editing and Ctrl-C, +// so a pasted secret isn't shown, and returns how to put it back. +func NoEcho(f *os.File) (restore func(), err error) { + fd := f.Fd() + var old syscall.Termios + if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, fd, syscall.TCGETS, uintptr(unsafe.Pointer(&old))); e != 0 { + return nil, e + } + quiet := old + quiet.Lflag &^= syscall.ECHO + quiet.Lflag |= syscall.ECHONL + if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, fd, syscall.TCSETS, uintptr(unsafe.Pointer(&quiet))); e != 0 { + return nil, e + } + return func() { + syscall.Syscall(syscall.SYS_IOCTL, fd, syscall.TCSETS, uintptr(unsafe.Pointer(&old))) + }, nil +} + // size is the terminal's width and height in cells. func size(f *os.File) (w, h int) { var ws struct{ Row, Col, X, Y uint16 } diff --git a/internal/tui/term_other.go b/internal/tui/term_other.go index 0c8d5f2..e36f2e9 100644 --- a/internal/tui/term_other.go +++ b/internal/tui/term_other.go @@ -17,6 +17,9 @@ import ( func rawMode(*os.File) (func(), error) { return nil, errors.New("not a Linux terminal") } func size(*os.File) (int, int) { return 80, 24 } +// NoEcho is never reached off Linux, where nothing is a terminal. +func NoEcho(*os.File) (func(), error) { return nil, errors.New("not a Linux terminal") } + // IsTerminal is false off Linux: the interface is never started there. func IsTerminal(*os.File) bool { return false } -- 2.54.0