Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 0bb17cd

Browse files
committed
ssh client
1 parent 49f40d9 commit 0bb17cd

File tree

4 files changed

+670
-0
lines changed

4 files changed

+670
-0
lines changed

clients/ssh/auth_method.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package ssh
2+
3+
import (
4+
"fmt"
5+
6+
"golang.org/x/crypto/ssh"
7+
"gopkg.in/src-d/go-git.v2/clients/common"
8+
)
9+
10+
// AuthMethod is the interface all auth methods for the ssh client
11+
// must implement. The clientConfig method returns the ssh client
12+
// configuration needed to establish an ssh connection.
13+
type AuthMethod interface {
14+
common.AuthMethod
15+
clientConfig() *ssh.ClientConfig
16+
}
17+
18+
// The names of the AuthMethod implementations. To be returned by the
19+
// Name() method. Most git servers only allow PublicKeysName and
20+
// PublicKeysCallbackName.
21+
const (
22+
KeyboardInteractiveName = "ssh-keyboard-interactive"
23+
PasswordName = "ssh-password"
24+
PasswordCallbackName = "ssh-password-callback"
25+
PublicKeysName = "ssh-public-keys"
26+
PublicKeysCallbackName = "ssh-public-key-callback"
27+
)
28+
29+
// KeyboardInteractive implements AuthMethod by using a
30+
// prompt/response sequence controlled by the server.
31+
type KeyboardInteractive struct {
32+
User string
33+
Challenge ssh.KeyboardInteractiveChallenge
34+
}
35+
36+
func (a *KeyboardInteractive) Name() string {
37+
return KeyboardInteractiveName
38+
}
39+
40+
func (a *KeyboardInteractive) String() string {
41+
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
42+
}
43+
44+
func (a *KeyboardInteractive) clientConfig() *ssh.ClientConfig {
45+
return &ssh.ClientConfig{
46+
User: a.User,
47+
Auth: []ssh.AuthMethod{ssh.KeyboardInteractiveChallenge(a.Challenge)},
48+
}
49+
}
50+
51+
// Password implements AuthMethod by using the given password.
52+
type Password struct {
53+
User string
54+
Pass string
55+
}
56+
57+
func (a *Password) Name() string {
58+
return PasswordName
59+
}
60+
61+
func (a *Password) String() string {
62+
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
63+
}
64+
65+
func (a *Password) clientConfig() *ssh.ClientConfig {
66+
return &ssh.ClientConfig{
67+
User: a.User,
68+
Auth: []ssh.AuthMethod{ssh.Password(a.Pass)},
69+
}
70+
}
71+
72+
// PasswordCallback implements AuthMethod by using a callback
73+
// to fetch the password.
74+
type PasswordCallback struct {
75+
User string
76+
Callback func() (pass string, err error)
77+
}
78+
79+
func (a *PasswordCallback) Name() string {
80+
return PasswordCallbackName
81+
}
82+
83+
func (a *PasswordCallback) String() string {
84+
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
85+
}
86+
87+
func (a *PasswordCallback) clientConfig() *ssh.ClientConfig {
88+
return &ssh.ClientConfig{
89+
User: a.User,
90+
Auth: []ssh.AuthMethod{ssh.PasswordCallback(a.Callback)},
91+
}
92+
}
93+
94+
// PublicKeys implements AuthMethod by using the given
95+
// key pairs.
96+
type PublicKeys struct {
97+
User string
98+
Signer ssh.Signer
99+
}
100+
101+
func (a *PublicKeys) Name() string {
102+
return PublicKeysName
103+
}
104+
105+
func (a *PublicKeys) String() string {
106+
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
107+
}
108+
109+
func (a *PublicKeys) clientConfig() *ssh.ClientConfig {
110+
return &ssh.ClientConfig{
111+
User: a.User,
112+
Auth: []ssh.AuthMethod{ssh.PublicKeys(a.Signer)},
113+
}
114+
}
115+
116+
// PublicKeysCallback implements AuthMethod by asking a
117+
// ssh.agent.Agent to act as a signer.
118+
type PublicKeysCallback struct {
119+
User string
120+
Callback func() (signers []ssh.Signer, err error)
121+
}
122+
123+
func (a *PublicKeysCallback) Name() string {
124+
return PublicKeysCallbackName
125+
}
126+
127+
func (a *PublicKeysCallback) String() string {
128+
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
129+
}
130+
131+
func (a *PublicKeysCallback) clientConfig() *ssh.ClientConfig {
132+
return &ssh.ClientConfig{
133+
User: a.User,
134+
Auth: []ssh.AuthMethod{ssh.PublicKeysCallback(a.Callback)},
135+
}
136+
}

clients/ssh/auth_method_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package ssh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
. "gopkg.in/check.v1"
8+
)
9+
10+
func Test(t *testing.T) { TestingT(t) }
11+
12+
type SuiteCommon struct{}
13+
14+
var _ = Suite(&SuiteCommon{})
15+
16+
func (s *SuiteRemote) TestKeyboardInteractiveName(c *C) {
17+
a := &KeyboardInteractive{
18+
User: "test",
19+
Challenge: nil,
20+
}
21+
c.Assert(a.Name(), Equals, KeyboardInteractiveName)
22+
}
23+
24+
func (s *SuiteRemote) TestKeyboardInteractiveString(c *C) {
25+
a := &KeyboardInteractive{
26+
User: "test",
27+
Challenge: nil,
28+
}
29+
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", KeyboardInteractiveName))
30+
}
31+
32+
func (s *SuiteRemote) TestPasswordName(c *C) {
33+
a := &Password{
34+
User: "test",
35+
Pass: "",
36+
}
37+
c.Assert(a.Name(), Equals, PasswordName)
38+
}
39+
40+
func (s *SuiteRemote) TestPasswordString(c *C) {
41+
a := &Password{
42+
User: "test",
43+
Pass: "",
44+
}
45+
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PasswordName))
46+
}
47+
48+
func (s *SuiteRemote) TestPasswordCallbackName(c *C) {
49+
a := &PasswordCallback{
50+
User: "test",
51+
Callback: nil,
52+
}
53+
c.Assert(a.Name(), Equals, PasswordCallbackName)
54+
}
55+
56+
func (s *SuiteRemote) TestPasswordCallbackString(c *C) {
57+
a := &PasswordCallback{
58+
User: "test",
59+
Callback: nil,
60+
}
61+
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PasswordCallbackName))
62+
}
63+
64+
func (s *SuiteRemote) TestPublicKeysName(c *C) {
65+
a := &PublicKeys{
66+
User: "test",
67+
Signer: nil,
68+
}
69+
c.Assert(a.Name(), Equals, PublicKeysName)
70+
}
71+
72+
func (s *SuiteRemote) TestPublicKeysString(c *C) {
73+
a := &PublicKeys{
74+
User: "test",
75+
Signer: nil,
76+
}
77+
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PublicKeysName))
78+
}
79+
80+
func (s *SuiteRemote) TestPublicKeysCallbackName(c *C) {
81+
a := &PublicKeysCallback{
82+
User: "test",
83+
Callback: nil,
84+
}
85+
c.Assert(a.Name(), Equals, PublicKeysCallbackName)
86+
}
87+
88+
func (s *SuiteRemote) TestPublicKeysCallbackString(c *C) {
89+
a := &PublicKeysCallback{
90+
User: "test",
91+
Callback: nil,
92+
}
93+
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PublicKeysCallbackName))
94+
}

0 commit comments

Comments
 (0)