Skip to content

Commit 8fa174e

Browse files
committed
Add libgit2 checkout test with ED25519 key
This adds a test to detect any regression in libgit2's ED25519 key support. go-git supports ED25519 but not the current version of libgit2 used in flux. The updates to libgit2 in v1.2.0 adds support for ED25519. This test would help ensure the right version of libgit2 is used. Signed-off-by: Sunny <[email protected]>
1 parent 7c95db8 commit 8fa174e

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/Masterminds/semver/v3 v3.1.1
99
github.com/cyphar/filepath-securejoin v0.2.2
1010
github.com/fluxcd/pkg/apis/meta v0.11.0-rc.1
11-
github.com/fluxcd/pkg/gittestserver v0.3.2
11+
github.com/fluxcd/pkg/gittestserver v0.4.0
1212
github.com/fluxcd/pkg/gitutil v0.1.0
1313
github.com/fluxcd/pkg/helmtestserver v0.2.0
1414
github.com/fluxcd/pkg/lockedfile v0.1.0

go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ github.com/fluxcd/pkg/apis/acl v0.0.1 h1:biCgZMjpDSv3Q4mZPikUJILx3t2MuNXR4Oa5jRQ
230230
github.com/fluxcd/pkg/apis/acl v0.0.1/go.mod h1:y3qOXUFObVWk7jzOjubMnr/u18j1kCeSi6olycnxr/E=
231231
github.com/fluxcd/pkg/apis/meta v0.11.0-rc.1 h1:RHHrztAFv9wmjM+Pk7Svt1UdD+1SdnQSp76MWFiM7Hg=
232232
github.com/fluxcd/pkg/apis/meta v0.11.0-rc.1/go.mod h1:yUblM2vg+X8TE3A2VvJfdhkGmg+uqBlSPkLk7dxi0UM=
233-
github.com/fluxcd/pkg/gittestserver v0.3.2 h1:oc1OoZ4b+kAu0vu/RT9wUwuQZxSqEjBOlQWYYA+YeLM=
234-
github.com/fluxcd/pkg/gittestserver v0.3.2/go.mod h1:8j36Z6B0BuKNZZ6exAWoyDEpyQoFcjz1IX3WBT7PZNg=
233+
github.com/fluxcd/pkg/gittestserver v0.4.0 h1:VQzQ5TcHzohxbYGWpnQ/79w7/rnS2SQGC7FSDtbIsCA=
234+
github.com/fluxcd/pkg/gittestserver v0.4.0/go.mod h1:hUPx21fe/6oox336Wih/XF1fnmzLmptNMOvATbTZXNY=
235235
github.com/fluxcd/pkg/gitutil v0.1.0 h1:VO3kJY/CKOCO4ysDNqfdpTg04icAKBOSb3lbR5uE/IE=
236236
github.com/fluxcd/pkg/gitutil v0.1.0/go.mod h1:Ybz50Ck5gkcnvF0TagaMwtlRy3X3wXuiri1HVsK5id4=
237237
github.com/fluxcd/pkg/helmtestserver v0.2.0 h1:cE7YHDmrWI0hr9QpaaeQ0vQ16Z0IiqZKiINDpqdY610=
@@ -899,7 +899,6 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
899899
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
900900
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
901901
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
902-
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
903902
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
904903
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
905904
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=

pkg/git/libgit2/checkout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323
"time"
2424

2525
"github.com/Masterminds/semver/v3"
26-
"github.com/fluxcd/pkg/version"
2726
git2go "github.com/libgit2/git2go/v31"
2827

2928
"github.com/fluxcd/pkg/gitutil"
29+
"github.com/fluxcd/pkg/version"
3030

3131
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
3232
"github.com/fluxcd/source-controller/pkg/git"

pkg/git/libgit2/checkout_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ import (
2121
"crypto/sha256"
2222
"encoding/hex"
2323
"io"
24+
"net/url"
2425
"os"
2526
"path"
27+
"path/filepath"
2628
"testing"
29+
"time"
2730

31+
"github.com/fluxcd/pkg/gittestserver"
32+
"github.com/fluxcd/pkg/ssh"
2833
git2go "github.com/libgit2/git2go/v31"
34+
. "github.com/onsi/gomega"
35+
corev1 "k8s.io/api/core/v1"
2936

3037
"github.com/fluxcd/source-controller/pkg/git"
3138
)
@@ -77,3 +84,73 @@ func TestCheckoutTagSemVer_Checkout(t *testing.T) {
7784
t.Errorf("expected semver hash %s, got %s", cTag.Hash(), cSemVer.Hash())
7885
}
7986
}
87+
88+
// This test is specifically to detect regression in libgit2's ED25519 key
89+
// support.
90+
// Refer: https://github.com/fluxcd/source-controller/issues/399
91+
func TestCheckout_ED25519(t *testing.T) {
92+
g := NewWithT(t)
93+
timeout := 5 * time.Second
94+
95+
// Create a git test server.
96+
server, err := gittestserver.NewTempGitServer()
97+
g.Expect(err).ToNot(HaveOccurred())
98+
defer os.RemoveAll(server.Root())
99+
server.Auth("test-user", "test-pswd")
100+
server.AutoCreate()
101+
102+
server.KeyDir(filepath.Join(server.Root(), "keys"))
103+
g.Expect(server.ListenSSH()).To(Succeed())
104+
105+
go func() {
106+
server.StartSSH()
107+
}()
108+
defer server.StopSSH()
109+
110+
repoPath := "test.git"
111+
112+
err = server.InitRepo("testdata/git/repo", git.DefaultBranch, repoPath)
113+
g.Expect(err).NotTo(HaveOccurred())
114+
115+
sshURL := server.SSHAddress()
116+
repoURL := sshURL + "/" + repoPath
117+
118+
// Fetch host key.
119+
u, err := url.Parse(sshURL)
120+
g.Expect(err).NotTo(HaveOccurred())
121+
g.Expect(u.Host).ToNot(BeEmpty())
122+
knownHosts, err := ssh.ScanHostKey(u.Host, timeout)
123+
g.Expect(err).ToNot(HaveOccurred())
124+
125+
kp, err := ssh.NewEd25519Generator().Generate()
126+
g.Expect(err).ToNot(HaveOccurred())
127+
128+
secret := corev1.Secret{
129+
Data: map[string][]byte{
130+
"identity": kp.PrivateKey,
131+
"known_hosts": knownHosts,
132+
},
133+
}
134+
135+
authStrategy, err := AuthSecretStrategyForURL(repoURL)
136+
g.Expect(err).ToNot(HaveOccurred())
137+
gitAuth, err := authStrategy.Method(secret)
138+
g.Expect(err).ToNot(HaveOccurred())
139+
140+
// Prepare for checkout.
141+
branchCheckoutStrat := &CheckoutBranch{branch: git.DefaultBranch}
142+
tmpDir, _ := os.MkdirTemp("", "test")
143+
defer os.RemoveAll(tmpDir)
144+
145+
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
146+
defer cancel()
147+
148+
// Checkout the repo.
149+
// This should always fail because the generated key above isn't present in
150+
// the git server.
151+
_, _, err = branchCheckoutStrat.Checkout(ctx, tmpDir, repoURL, gitAuth)
152+
g.Expect(err).To(HaveOccurred())
153+
// NOTE: libgit2 v1.2+ supports ED25519. Flip this condition after updating
154+
// to libgit2 v1.2+.
155+
g.Expect(err.Error()).To(ContainSubstring("Unable to extract public key from private key"))
156+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test file

0 commit comments

Comments
 (0)