forked from wallix/awless
-
Notifications
You must be signed in to change notification settings - Fork 2
/
auths.go
47 lines (41 loc) · 1.08 KB
/
auths.go
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
40
41
42
43
44
45
46
47
package ssh
import (
"fmt"
"net"
"os"
"strings"
"syscall"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/terminal"
)
func agentAuth() (ssh.AuthMethod, error) {
sock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
return nil, err
}
return ssh.PublicKeysCallback(agent.NewClient(sock).Signers), nil
}
func privateKeyAuth(priv privateKey) (ssh.AuthMethod, error) {
signer, err := ssh.ParsePrivateKey(priv.body)
if err != nil {
if strings.Contains(err.Error(), "cannot decode encrypted private keys") {
return encryptedPrivKeyAuth(priv)
}
return nil, err
}
return ssh.PublicKeys(signer), nil
}
func encryptedPrivKeyAuth(priv privateKey) (ssh.AuthMethod, error) {
fmt.Fprintf(os.Stderr, "This SSH key is encrypted. Please enter passphrase for key '%s':", priv.path)
passphrase, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, err
}
fmt.Fprintln(os.Stderr)
signer, err := DecryptSSHKey(priv.body, passphrase)
if err != nil {
return nil, err
}
return ssh.PublicKeys(signer), nil
}