Skip to content

Commit

Permalink
Merge pull request #571 from fluxcd/hostkey-callback
Browse files Browse the repository at this point in the history
git/gogit: set `HostKeyCallback` for parent `PublicKeys` object
  • Loading branch information
aryan9600 authored May 25, 2023
2 parents 01a38c6 + 1e5b91f commit c33a483
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions git/gogit/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,15 @@ func (a *CustomPublicKeys) String() string {
}

func (a *CustomPublicKeys) ClientConfig() (*gossh.ClientConfig, error) {
if a.callback != nil {
a.pk.HostKeyCallback = a.callback
}

config, err := a.pk.ClientConfig()
if err != nil {
return nil, err
}

if a.callback != nil {
config.HostKeyCallback = a.callback
}
if len(git.KexAlgos) > 0 {
config.Config.KeyExchanges = git.KexAlgos
}
Expand Down Expand Up @@ -149,7 +150,6 @@ func (a *DefaultAuth) ClientConfig() (*gossh.ClientConfig, error) {
if err != nil {
return nil, err
}
config.HostKeyCallback, err = ssh.NewKnownHostsCallback()
if err != nil {
return nil, err
}
Expand Down
25 changes: 25 additions & 0 deletions git/gogit/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (

"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
. "github.com/onsi/gomega"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"

"github.com/fluxcd/pkg/git"
Expand Down Expand Up @@ -284,6 +286,29 @@ func Test_transportAuth(t *testing.T) {
}
}

func TestCustomPublicKeys_ClientConfig(t *testing.T) {
g := NewWithT(t)
pk, err := ssh.NewPublicKeys("user", []byte(privateKeyFixture), "password")
g.Expect(err).ToNot(HaveOccurred())

var count int
customCallback := func(hostname string, remote net.Addr, key gossh.PublicKey) error {
count += 1
return nil
}
customPK := CustomPublicKeys{
pk: pk,
callback: customCallback,
}
cfg, err := customPK.ClientConfig()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(cfg.HostKeyCallback).ToNot(BeNil())

err = cfg.HostKeyCallback("", nil, nil)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(count).To(Equal(1))
}

func Test_defaultKnownHosts(t *testing.T) {
g := NewWithT(t)
tmp, err := os.MkdirTemp("", "ssh_agent")
Expand Down

0 comments on commit c33a483

Please sign in to comment.