Skip to content

Commit

Permalink
Merge pull request #394 from aryan9600/gogit-callback
Browse files Browse the repository at this point in the history
git/gogit: configure hostkey callback only when known_hosts is provided
  • Loading branch information
stefanprodan authored Nov 8, 2022
2 parents 2ee90dd + 003506d commit a0c91ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 11 additions & 4 deletions git/gogit/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ func transportAuth(opts *git.AuthOptions) (transport.AuthMethod, error) {
if err != nil {
return nil, err
}
callback, err := knownhosts.New(opts.KnownHosts)
if err != nil {
return nil, err

var callback gossh.HostKeyCallback
if len(opts.KnownHosts) > 0 {
callback, err = knownhosts.New(opts.KnownHosts)
if err != nil {
return nil, err
}
}

customPK := &CustomPublicKeys{
pk: pk,
callback: callback,
Expand Down Expand Up @@ -95,7 +100,9 @@ func (a *CustomPublicKeys) ClientConfig() (*gossh.ClientConfig, error) {
return nil, err
}

config.HostKeyCallback = a.callback
if a.callback != nil {
config.HostKeyCallback = a.callback
}
if len(git.KexAlgos) > 0 {
config.Config.KeyExchanges = git.KexAlgos
}
Expand Down
14 changes: 14 additions & 0 deletions git/gogit/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ func Test_transportAuth(t *testing.T) {
},
wantErr: errors.New("knownhosts: knownhosts: missing host pattern"),
},
{
name: "SSH private key without known_hosts",
opts: &git.AuthOptions{
Transport: git.SSH,
Username: "example",
Identity: []byte(privateKeyFixture),
},
wantFunc: func(g *WithT, t transport.AuthMethod, opts *git.AuthOptions) {
tt, ok := t.(*CustomPublicKeys)
g.Expect(ok).To(BeTrue())
g.Expect(tt.pk.User).To(Equal(opts.Username))
g.Expect(tt.callback).To(BeNil())
},
},
{
name: "Empty",
opts: &git.AuthOptions{},
Expand Down

0 comments on commit a0c91ac

Please sign in to comment.