Say when --key - is waiting for the key (#75) #90
No files matched your search
+38
-17
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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 }
|
||||
|
||||
@@ -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 }
|
||||
|
||||
|
||||
Reference in new issue
Block a user