/
auth.go
141 lines (126 loc) · 3.41 KB
/
auth.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package scp
import (
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/viant/afs/option"
"golang.org/x/crypto/ssh"
"io/ioutil"
"os"
"path"
"strings"
)
//AuthProvider represents ssh client config authProvider
type AuthProvider interface {
ClientConfig() (*ssh.ClientConfig, error)
}
type authProvider struct {
pemAuth KeyAuth
credAuth option.BasicAuth
}
//ClientConfig returns client config
func (p *authProvider) ClientConfig() (*ssh.ClientConfig, error) {
config := &ssh.ClientConfig{
User: os.Getenv("USER"),
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: make([]ssh.AuthMethod, 0),
}
if p.pemAuth != nil {
config.User = p.pemAuth.Username()
key, err := p.pemAuth.Singer()
if err != nil {
return nil, err
}
config.Auth = append(config.Auth, ssh.PublicKeys(key))
}
if p.credAuth != nil {
user, password := p.credAuth.Credentials()
config.User = user
config.Auth = append(config.Auth, ssh.Password(password))
}
return config, nil
}
//NewAuthProvider returns new auth provider
func NewAuthProvider(pemAuth KeyAuth, credAuth option.BasicAuth) AuthProvider {
return &authProvider{pemAuth: pemAuth, credAuth: credAuth}
}
//KeyAuth represents a key based auth
type KeyAuth interface {
//Singer returns signer key
Singer() (ssh.Signer, error)
Username() string
}
type keyAuthnticator struct {
keyLocation string
username string
_keyPassword string
}
//Username returns a username
func (a *keyAuthnticator) Username() string {
return a.username
}
func (a *keyAuthnticator) isKeyEncrypted(data []byte) bool {
block, _ := pem.Decode(data)
if block == nil {
return false
}
return strings.Contains(block.Headers["Proc-Type"], "ENCRYPTED")
}
func (a *keyAuthnticator) Singer() (ssh.Signer, error) {
rawPEM, err := a.pem()
if err != nil {
return nil, err
}
return ssh.ParsePrivateKey(rawPEM)
}
//PEM returns secure key container
func (a *keyAuthnticator) pem() ([]byte, error) {
pemBytes, err := ioutil.ReadFile(a.keyLocation)
if err != nil {
return nil, err
}
if a.isKeyEncrypted(pemBytes) {
block, _ := pem.Decode(pemBytes)
if block == nil {
return nil, fmt.Errorf("unable decode %v", a.keyLocation)
}
if x509.IsEncryptedPEMBlock(block) {
key, err := x509.DecryptPEMBlock(block, []byte(a._keyPassword))
if err != nil {
return nil, err
}
block = &pem.Block{Type: block.Type, Bytes: key}
pemBytes = pem.EncodeToMemory(block)
return pemBytes, nil
}
}
return pemBytes, nil
}
//NewKeyAuth returns a new private key authenticator
func NewKeyAuth(keyLocation, username, keyPassword string) KeyAuth {
return &keyAuthnticator{
keyLocation: keyLocation,
username: username,
_keyPassword: keyPassword,
}
}
//LocalhostKeyAuth returns a localhost key authenticator with ~/.ssh/authorized_keys
func LocalhostKeyAuth(keyPassword string, locations ...string) (KeyAuth, error) {
username := os.Getenv("USER")
if username == "" {
return nil, fmt.Errorf("username was empty")
}
locations = append(locations, path.Join(os.Getenv("HOME"), ".secret", "id_rsa"))
locations = append(locations, path.Join(os.Getenv("HOME"), ".ssh", "id_rsa"))
keyLocation := ""
for _, candidate := range locations {
if _, err := os.Stat(candidate); err == nil {
keyLocation = candidate
break
}
}
if keyLocation == "" {
return nil, fmt.Errorf("failed to lookup key location: %v", locations)
}
return NewKeyAuth(keyLocation, username, keyPassword), nil
}