aboutsummaryrefslogtreecommitdiff
path: root/internal/crypt/ssh_key_io.go
blob: 59ea44819813a67a1778c8c99d2eefbfaecc567e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package crypt

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"github.com/charmbracelet/x/term"
)

func promptSSHKeyPassphrase(privKeyPath string) []byte {
	fd := os.Stdin.Fd()
	oldState, err := term.GetState(fd)
	if err != nil {
		fmt.Fprintf(os.Stderr, "eno: failed to get terminal state: %v\n", err)
		return nil
	}

	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
	defer signal.Stop(sigCh)

	go func() {
		<-sigCh
		term.Restore(fd, oldState)
		os.Exit(130)
	}()

	fmt.Fprintf(os.Stderr, "Enter passphrase for SSH key %s: ", privKeyPath)
	passphrase, err := term.ReadPassword(fd)
	fmt.Fprintln(os.Stderr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "eno: failed to read passphrase: %v\n", err)
		return nil
	}

	return passphrase
}