@@ -3,6 +3,7 @@ package ssh
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "io/ioutil"
6
7
"net"
7
8
"os"
8
9
"os/user"
@@ -13,6 +14,8 @@ import (
13
14
"golang.org/x/crypto/ssh/agent"
14
15
)
15
16
17
+ const DefaultUsername = "git"
18
+
16
19
var ErrEmptySSHAgentAddr = errors .New ("SSH_AUTH_SOCK env variable is required" )
17
20
18
21
// AuthMethod is the interface all auth methods for the ssh client
@@ -102,14 +105,35 @@ func (a *PasswordCallback) clientConfig() *ssh.ClientConfig {
102
105
}
103
106
}
104
107
105
- // PublicKeys implements AuthMethod by using the given
106
- // key pairs.
108
+ // PublicKeys implements AuthMethod by using the given key pairs.
107
109
type PublicKeys struct {
108
110
User string
109
111
Signer ssh.Signer
110
112
baseAuthMethod
111
113
}
112
114
115
+ // NewPublicKeys returns a PublicKeys from a PEM encoded private key. It
116
+ // supports RSA (PKCS#1), DSA (OpenSSL), and ECDSA private keys.
117
+ func NewPublicKeys (user string , pemBytes []byte ) (AuthMethod , error ) {
118
+ signer , err := ssh .ParsePrivateKey (pemBytes )
119
+ if err != nil {
120
+ return nil , err
121
+ }
122
+
123
+ return & PublicKeys {User : user , Signer : signer }, nil
124
+ }
125
+
126
+ // NewPublicKeysFromFile returns a PublicKeys from a file containing a PEM
127
+ // encoded private key.
128
+ func NewPublicKeysFromFile (user string , pemFile string ) (AuthMethod , error ) {
129
+ bytes , err := ioutil .ReadFile (pemFile )
130
+ if err != nil {
131
+ return nil , err
132
+ }
133
+
134
+ return NewPublicKeys (user , bytes )
135
+ }
136
+
113
137
func (a * PublicKeys ) Name () string {
114
138
return PublicKeysName
115
139
}
@@ -133,28 +157,12 @@ type PublicKeysCallback struct {
133
157
baseAuthMethod
134
158
}
135
159
136
- func (a * PublicKeysCallback ) Name () string {
137
- return PublicKeysCallbackName
138
- }
139
-
140
- func (a * PublicKeysCallback ) String () string {
141
- return fmt .Sprintf ("user: %s, name: %s" , a .User , a .Name ())
142
- }
143
-
144
- func (a * PublicKeysCallback ) clientConfig () * ssh.ClientConfig {
145
- return & ssh.ClientConfig {
146
- User : a .User ,
147
- Auth : []ssh.AuthMethod {ssh .PublicKeysCallback (a .Callback )},
148
- }
149
- }
150
-
151
- const DefaultSSHUsername = "git"
152
-
153
- // NewSSHAgentAuth opens a pipe with the SSH agent and uses the pipe
154
- // as the implementer of the public key callback function.
155
- func NewSSHAgentAuth (user string ) (* PublicKeysCallback , error ) {
160
+ // NewSSHAgentAuth returns a PublicKeysCallback based on a SSH agent, it opens
161
+ // a pipe with the SSH agent and uses the pipe as the implementer of the public
162
+ // key callback function.
163
+ func NewSSHAgentAuth (user string ) (AuthMethod , error ) {
156
164
if user == "" {
157
- user = DefaultSSHUsername
165
+ user = DefaultUsername
158
166
}
159
167
160
168
sshAgentAddr := os .Getenv ("SSH_AUTH_SOCK" )
@@ -173,6 +181,21 @@ func NewSSHAgentAuth(user string) (*PublicKeysCallback, error) {
173
181
}, nil
174
182
}
175
183
184
+ func (a * PublicKeysCallback ) Name () string {
185
+ return PublicKeysCallbackName
186
+ }
187
+
188
+ func (a * PublicKeysCallback ) String () string {
189
+ return fmt .Sprintf ("user: %s, name: %s" , a .User , a .Name ())
190
+ }
191
+
192
+ func (a * PublicKeysCallback ) clientConfig () * ssh.ClientConfig {
193
+ return & ssh.ClientConfig {
194
+ User : a .User ,
195
+ Auth : []ssh.AuthMethod {ssh .PublicKeysCallback (a .Callback )},
196
+ }
197
+ }
198
+
176
199
// NewKnownHostsCallback returns ssh.HostKeyCallback based on a file based on a
177
200
// know_hosts file. http://man.openbsd.org/sshd#SSH_KNOWN_HOSTS_FILE_FORMAT
178
201
//
0 commit comments