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

Commit b08cc8d

Browse files
authored
Merge pull request #649 from mcuadros/auth
transport: made public all the fields and standardized AuthMethod
2 parents 97fb5e9 + 2f51048 commit b08cc8d

File tree

5 files changed

+59
-55
lines changed

5 files changed

+59
-55
lines changed

plumbing/transport/http/common.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,20 @@ func basicAuthFromEndpoint(ep *transport.Endpoint) *BasicAuth {
151151
return nil
152152
}
153153

154-
return NewBasicAuth(u, ep.Password)
154+
return &BasicAuth{u, ep.Password}
155155
}
156156

157157
// BasicAuth represent a HTTP basic auth
158158
type BasicAuth struct {
159-
username, password string
160-
}
161-
162-
// NewBasicAuth returns a basicAuth base on the given user and password
163-
func NewBasicAuth(username, password string) *BasicAuth {
164-
return &BasicAuth{username, password}
159+
Username, Password string
165160
}
166161

167162
func (a *BasicAuth) setAuth(r *http.Request) {
168163
if a == nil {
169164
return
170165
}
171166

172-
r.SetBasicAuth(a.username, a.password)
167+
r.SetBasicAuth(a.Username, a.Password)
173168
}
174169

175170
// Name is name of the auth
@@ -179,11 +174,11 @@ func (a *BasicAuth) Name() string {
179174

180175
func (a *BasicAuth) String() string {
181176
masked := "*******"
182-
if a.password == "" {
177+
if a.Password == "" {
183178
masked = "<empty>"
184179
}
185180

186-
return fmt.Sprintf("%s - %s:%s", a.Name(), a.username, masked)
181+
return fmt.Sprintf("%s - %s:%s", a.Name(), a.Username, masked)
187182
}
188183

189184
// Err is a dedicated error to return errors based on status code

plumbing/transport/http/common_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (s *UploadPackSuite) TestNewClient(c *C) {
4949
}
5050

5151
func (s *ClientSuite) TestNewBasicAuth(c *C) {
52-
a := NewBasicAuth("foo", "qux")
52+
a := &BasicAuth{"foo", "qux"}
5353

5454
c.Assert(a.Name(), Equals, "http-basic-auth")
5555
c.Assert(a.String(), Equals, "http-basic-auth - foo:*******")

plumbing/transport/ssh/auth_method.go

+48-34
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ const DefaultUsername = "git"
2525
// configuration needed to establish an ssh connection.
2626
type AuthMethod interface {
2727
transport.AuthMethod
28-
clientConfig() *ssh.ClientConfig
29-
hostKeyCallback() (ssh.HostKeyCallback, error)
28+
// ClientConfig should return a valid ssh.ClientConfig to be used to create
29+
// a connection to the SSH server.
30+
ClientConfig() (*ssh.ClientConfig, error)
3031
}
3132

3233
// The names of the AuthMethod implementations. To be returned by the
@@ -45,7 +46,7 @@ const (
4546
type KeyboardInteractive struct {
4647
User string
4748
Challenge ssh.KeyboardInteractiveChallenge
48-
baseAuthMethod
49+
HostKeyCallbackHelper
4950
}
5051

5152
func (a *KeyboardInteractive) Name() string {
@@ -56,18 +57,20 @@ func (a *KeyboardInteractive) String() string {
5657
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
5758
}
5859

59-
func (a *KeyboardInteractive) clientConfig() *ssh.ClientConfig {
60-
return &ssh.ClientConfig{
60+
func (a *KeyboardInteractive) ClientConfig() (*ssh.ClientConfig, error) {
61+
return a.SetHostKeyCallback(&ssh.ClientConfig{
6162
User: a.User,
62-
Auth: []ssh.AuthMethod{ssh.KeyboardInteractiveChallenge(a.Challenge)},
63-
}
63+
Auth: []ssh.AuthMethod{
64+
ssh.KeyboardInteractiveChallenge(a.Challenge),
65+
},
66+
})
6467
}
6568

6669
// Password implements AuthMethod by using the given password.
6770
type Password struct {
68-
User string
69-
Pass string
70-
baseAuthMethod
71+
User string
72+
Password string
73+
HostKeyCallbackHelper
7174
}
7275

7376
func (a *Password) Name() string {
@@ -78,19 +81,19 @@ func (a *Password) String() string {
7881
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
7982
}
8083

81-
func (a *Password) clientConfig() *ssh.ClientConfig {
82-
return &ssh.ClientConfig{
84+
func (a *Password) ClientConfig() (*ssh.ClientConfig, error) {
85+
return a.SetHostKeyCallback(&ssh.ClientConfig{
8386
User: a.User,
84-
Auth: []ssh.AuthMethod{ssh.Password(a.Pass)},
85-
}
87+
Auth: []ssh.AuthMethod{ssh.Password(a.Password)},
88+
})
8689
}
8790

8891
// PasswordCallback implements AuthMethod by using a callback
8992
// to fetch the password.
9093
type PasswordCallback struct {
9194
User string
9295
Callback func() (pass string, err error)
93-
baseAuthMethod
96+
HostKeyCallbackHelper
9497
}
9598

9699
func (a *PasswordCallback) Name() string {
@@ -101,25 +104,25 @@ func (a *PasswordCallback) String() string {
101104
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
102105
}
103106

104-
func (a *PasswordCallback) clientConfig() *ssh.ClientConfig {
105-
return &ssh.ClientConfig{
107+
func (a *PasswordCallback) ClientConfig() (*ssh.ClientConfig, error) {
108+
return a.SetHostKeyCallback(&ssh.ClientConfig{
106109
User: a.User,
107110
Auth: []ssh.AuthMethod{ssh.PasswordCallback(a.Callback)},
108-
}
111+
})
109112
}
110113

111114
// PublicKeys implements AuthMethod by using the given key pairs.
112115
type PublicKeys struct {
113116
User string
114117
Signer ssh.Signer
115-
baseAuthMethod
118+
HostKeyCallbackHelper
116119
}
117120

118121
// NewPublicKeys returns a PublicKeys from a PEM encoded private key. An
119122
// encryption password should be given if the pemBytes contains a password
120123
// encrypted PEM block otherwise password should be empty. It supports RSA
121124
// (PKCS#1), DSA (OpenSSL), and ECDSA private keys.
122-
func NewPublicKeys(user string, pemBytes []byte, password string) (AuthMethod, error) {
125+
func NewPublicKeys(user string, pemBytes []byte, password string) (*PublicKeys, error) {
123126
block, _ := pem.Decode(pemBytes)
124127
if x509.IsEncryptedPEMBlock(block) {
125128
key, err := x509.DecryptPEMBlock(block, []byte(password))
@@ -142,7 +145,7 @@ func NewPublicKeys(user string, pemBytes []byte, password string) (AuthMethod, e
142145
// NewPublicKeysFromFile returns a PublicKeys from a file containing a PEM
143146
// encoded private key. An encryption password should be given if the pemBytes
144147
// contains a password encrypted PEM block otherwise password should be empty.
145-
func NewPublicKeysFromFile(user, pemFile, password string) (AuthMethod, error) {
148+
func NewPublicKeysFromFile(user, pemFile, password string) (*PublicKeys, error) {
146149
bytes, err := ioutil.ReadFile(pemFile)
147150
if err != nil {
148151
return nil, err
@@ -159,11 +162,11 @@ func (a *PublicKeys) String() string {
159162
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
160163
}
161164

162-
func (a *PublicKeys) clientConfig() *ssh.ClientConfig {
163-
return &ssh.ClientConfig{
165+
func (a *PublicKeys) ClientConfig() (*ssh.ClientConfig, error) {
166+
return a.SetHostKeyCallback(&ssh.ClientConfig{
164167
User: a.User,
165168
Auth: []ssh.AuthMethod{ssh.PublicKeys(a.Signer)},
166-
}
169+
})
167170
}
168171

169172
func username() (string, error) {
@@ -173,9 +176,11 @@ func username() (string, error) {
173176
} else {
174177
username = os.Getenv("USER")
175178
}
179+
176180
if username == "" {
177181
return "", errors.New("failed to get username")
178182
}
183+
179184
return username, nil
180185
}
181186

@@ -184,13 +189,13 @@ func username() (string, error) {
184189
type PublicKeysCallback struct {
185190
User string
186191
Callback func() (signers []ssh.Signer, err error)
187-
baseAuthMethod
192+
HostKeyCallbackHelper
188193
}
189194

190195
// NewSSHAgentAuth returns a PublicKeysCallback based on a SSH agent, it opens
191196
// a pipe with the SSH agent and uses the pipe as the implementer of the public
192197
// key callback function.
193-
func NewSSHAgentAuth(u string) (AuthMethod, error) {
198+
func NewSSHAgentAuth(u string) (*PublicKeysCallback, error) {
194199
var err error
195200
if u == "" {
196201
u, err = username()
@@ -218,11 +223,11 @@ func (a *PublicKeysCallback) String() string {
218223
return fmt.Sprintf("user: %s, name: %s", a.User, a.Name())
219224
}
220225

221-
func (a *PublicKeysCallback) clientConfig() *ssh.ClientConfig {
222-
return &ssh.ClientConfig{
226+
func (a *PublicKeysCallback) ClientConfig() (*ssh.ClientConfig, error) {
227+
return a.SetHostKeyCallback(&ssh.ClientConfig{
223228
User: a.User,
224229
Auth: []ssh.AuthMethod{ssh.PublicKeysCallback(a.Callback)},
225-
}
230+
})
226231
}
227232

228233
// NewKnownHostsCallback returns ssh.HostKeyCallback based on a file based on a
@@ -287,17 +292,26 @@ func filterKnownHostsFiles(files ...string) ([]string, error) {
287292
return out, nil
288293
}
289294

290-
type baseAuthMethod struct {
295+
// HostKeyCallbackHelper is a helper that provides common functionality to
296+
// configure HostKeyCallback into a ssh.ClientConfig.
297+
type HostKeyCallbackHelper struct {
291298
// HostKeyCallback is the function type used for verifying server keys.
292-
// If nil default callback will be create using NewKnownHostsHostKeyCallback
299+
// If nil default callback will be create using NewKnownHostsCallback
293300
// without argument.
294301
HostKeyCallback ssh.HostKeyCallback
295302
}
296303

297-
func (m *baseAuthMethod) hostKeyCallback() (ssh.HostKeyCallback, error) {
304+
// SetHostKeyCallback sets the field HostKeyCallback in the given cfg. If
305+
// HostKeyCallback is empty a default callback is created using
306+
// NewKnownHostsCallback.
307+
func (m *HostKeyCallbackHelper) SetHostKeyCallback(cfg *ssh.ClientConfig) (*ssh.ClientConfig, error) {
308+
var err error
298309
if m.HostKeyCallback == nil {
299-
return NewKnownHostsCallback()
310+
if m.HostKeyCallback, err = NewKnownHostsCallback(); err != nil {
311+
return cfg, err
312+
}
300313
}
301314

302-
return m.HostKeyCallback, nil
315+
cfg.HostKeyCallback = m.HostKeyCallback
316+
return cfg, nil
303317
}

plumbing/transport/ssh/auth_method_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ func (s *SuiteCommon) TestKeyboardInteractiveString(c *C) {
3232

3333
func (s *SuiteCommon) TestPasswordName(c *C) {
3434
a := &Password{
35-
User: "test",
36-
Pass: "",
35+
User: "test",
36+
Password: "",
3737
}
3838
c.Assert(a.Name(), Equals, PasswordName)
3939
}
4040

4141
func (s *SuiteCommon) TestPasswordString(c *C) {
4242
a := &Password{
43-
User: "test",
44-
Pass: "",
43+
User: "test",
44+
Password: "",
4545
}
4646
c.Assert(a.String(), Equals, fmt.Sprintf("user: test, name: %s", PasswordName))
4747
}

plumbing/transport/ssh/common.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ func (c *command) connect() error {
9898
}
9999

100100
var err error
101-
config := c.auth.clientConfig()
102-
config.HostKeyCallback, err = c.auth.hostKeyCallback()
101+
config, err := c.auth.ClientConfig()
103102
if err != nil {
104103
return err
105104
}
@@ -159,7 +158,3 @@ func overrideConfig(overrides *ssh.ClientConfig, c *ssh.ClientConfig) {
159158

160159
*c = vc.Interface().(ssh.ClientConfig)
161160
}
162-
163-
func isZeroValue(v reflect.Value) bool {
164-
return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
165-
}

0 commit comments

Comments
 (0)