-
Notifications
You must be signed in to change notification settings - Fork 4
/
credentials.go
217 lines (187 loc) · 5.24 KB
/
credentials.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
Package credentials describes credentials.
*/
package credentials
import (
"errors"
"fmt"
"io/fs"
"os"
"os/user"
"github.com/kevinburke/ssh_config"
"github.com/mitchellh/go-homedir"
"go.uber.org/zap"
)
type Credentials interface {
GetUsername() (string, error)
GetPasswords() []Secret
GetPrivateKeys() [][]byte
GetPassphrase() Secret
GetAgentSocket() string
}
type SimpleCredentials struct {
username string
passwords []Secret
privKeys [][]byte
passphrase Secret
agentSocket string
logger *zap.Logger
}
type CredentialsOption func(*SimpleCredentials)
func NewSimpleCredentials(opts ...CredentialsOption) *SimpleCredentials {
cred := &SimpleCredentials{
username: "",
passwords: []Secret{},
passphrase: "",
agentSocket: "",
logger: zap.NewNop(),
}
for _, opt := range opts {
opt(cred)
}
return cred
}
func WithUsername(username string) CredentialsOption {
return func(h *SimpleCredentials) {
h.username = username
}
}
func WithPassword(password Secret) CredentialsOption {
return func(h *SimpleCredentials) {
h.passwords = []Secret{password}
}
}
func WithPasswords(password []Secret) CredentialsOption {
return func(h *SimpleCredentials) {
h.passwords = password
}
}
func WithLogger(logger *zap.Logger) CredentialsOption {
return func(h *SimpleCredentials) {
h.logger = logger
}
}
func WithPrivateKey(key []byte) CredentialsOption {
return func(h *SimpleCredentials) {
h.privKeys = [][]byte{key}
}
}
func WithPrivateKeys(key [][]byte) CredentialsOption {
return func(h *SimpleCredentials) {
h.privKeys = key
}
}
func WithPassphrase(passphrase Secret) CredentialsOption {
return func(h *SimpleCredentials) {
h.passphrase = passphrase
}
}
func WithSSHAgentSocket(agentSocket string) CredentialsOption {
return func(h *SimpleCredentials) {
h.agentSocket = agentSocket
}
}
func (m SimpleCredentials) GetUsername() (string, error) {
if len(m.username) != 0 {
return m.username, nil
}
currentUser, err := user.Current()
if err == nil {
return currentUser.Username, nil
}
return "", fmt.Errorf("unable to get username")
}
func (m SimpleCredentials) GetPasswords() []Secret {
return m.passwords
}
func (m SimpleCredentials) GetPassphrase() Secret {
return m.passphrase
}
func (m SimpleCredentials) GetPrivateKeys() [][]byte {
return m.privKeys
}
func (m SimpleCredentials) GetAgentSocket() string {
return m.agentSocket
}
// GetDefaultAgentSocket returns default ssh authentication agent socket (read from SSH_AUTH_SOCK env)
func GetDefaultAgentSocket() string {
return os.Getenv("SSH_AUTH_SOCK")
}
// GetLogin tries to get sudo user from env, falling back to current user
func GetLogin() string {
login := os.Getenv("SUDO_USER")
if login == "" {
u, err := user.Current()
if err != nil {
return ""
}
return u.Username
}
return login
}
type Secret string
func (Secret) String() string {
return "..."
}
func (Secret) MarshalText() ([]byte, error) {
return []byte("..."), nil
}
func (m Secret) Value() string {
return string(m)
}
// GetUsernameFromConfig extracts User keyword value for given host from default ssh config.
func GetUsernameFromConfig(host string) string {
return ssh_config.Get(host, "User")
}
// GetAgentSocketFromConfig computes SSH authentication agent socket path using default ssh config's IdentityAgent and ForwardAgent keywords.
// IdentityAgent value supports tilde syntax, but it doesn't support %d, %h, %l and %r.
func GetAgentSocketFromConfig(host string) (string, error) {
// todo:
// IdentityAgent and IdentityFile accept the tokens %%, %d, %h, %l, %r, and %u.
ia := ssh_config.Get(host, "IdentityAgent")
expandedIa, err := homedir.Expand(ia)
if err != nil {
return "", err
}
if expandedIa == "none" {
return "", nil
}
if expandedIa == "SSH_AUTH_SOCK" {
return GetDefaultAgentSocket(), nil
}
if expandedIa == "" && ssh_config.Get(host, "ForwardAgent") == "yes" {
return GetDefaultAgentSocket(), nil
}
return expandedIa, nil
}
// GetPrivateKeysFromConfig tries to extract PrivateKeys from default config's IdentityFiles specifed for provided host.
// IdentityFile value supports tilde syntax, but it doesn't support %d, %u, %l, %h and %r.
func GetPrivateKeysFromConfig(host string) ([][]byte, error) {
identityFiles := ssh_config.GetAll(host, "IdentityFile")
privKeys := make([][]byte, 0, len(identityFiles))
for _, v := range identityFiles {
// todo:
// IdentityAgent and IdentityFile accept the tokens %%, %d, %h, %l, %r, and %u.
expandedPath, err := homedir.Expand(v)
if err != nil {
return nil, fmt.Errorf("failed to expand path of identity file %s: %w", v, err)
}
content, err := os.ReadFile(expandedPath)
if err != nil && isSSHConfigMadeUpDefaultFileError(identityFiles, err) {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to read identity file %s: %w", v, err)
}
privKeys = append(privKeys, content)
}
return privKeys, nil
}
// isSSHConfigMadeUpDefaultFileError checks if given error occured because
// ssh_config lib made up a default value for IdentityFile due to it's absence in config
func isSSHConfigMadeUpDefaultFileError(identityFiles []string, err error) bool {
if len(identityFiles) == 1 && identityFiles[0] == "~/.ssh/identity" && errors.Is(err, fs.ErrNotExist) {
return true
}
return false
}