Skip to content

Commit e3b2458

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 <darkowlzz@protonmail.com>
1 parent 7c95db8 commit e3b2458

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

pkg/git/libgit2/checkout_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ import (
2424
"os"
2525
"path"
2626
"testing"
27+
"time"
2728

29+
"github.com/fluxcd/pkg/ssh"
2830
git2go "github.com/libgit2/git2go/v31"
31+
. "github.com/onsi/gomega"
32+
corev1 "k8s.io/api/core/v1"
2933

3034
"github.com/fluxcd/source-controller/pkg/git"
3135
)
@@ -77,3 +81,48 @@ func TestCheckoutTagSemVer_Checkout(t *testing.T) {
7781
t.Errorf("expected semver hash %s, got %s", cTag.Hash(), cSemVer.Hash())
7882
}
7983
}
84+
85+
// This test is specifically to detect regression in libgit2's ED25519 key
86+
// support.
87+
// Refer: https://github.com/fluxcd/source-controller/issues/399
88+
func TestCheckout_ED25519(t *testing.T) {
89+
g := NewWithT(t)
90+
91+
timeout := 5 * time.Second
92+
url := "ssh://git@github.com/projectcontour/contour"
93+
94+
// Fetch github host key.
95+
githubHostKey, err := ssh.ScanHostKey("github.com:22", timeout)
96+
g.Expect(err).NotTo(HaveOccurred())
97+
98+
kp, err := ssh.NewEd25519Generator().Generate()
99+
g.Expect(err).ToNot(HaveOccurred())
100+
101+
secret := corev1.Secret{
102+
Data: map[string][]byte{
103+
"identity": kp.PrivateKey,
104+
"known_hosts": []byte(githubHostKey),
105+
},
106+
}
107+
108+
authStrategy, err := AuthSecretStrategyForURL(url)
109+
g.Expect(err).ToNot(HaveOccurred())
110+
gitAuth, err := authStrategy.Method(secret)
111+
g.Expect(err).ToNot(HaveOccurred())
112+
113+
branchCheckoutStrat := &CheckoutBranch{branch: "main"}
114+
tmpDir, _ := os.MkdirTemp("", "test")
115+
defer os.RemoveAll(tmpDir)
116+
117+
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
118+
defer cancel()
119+
120+
// This should always fail because the generated key above isn't present in
121+
// the git server.
122+
_, _, err = branchCheckoutStrat.Checkout(ctx, tmpDir, url, gitAuth)
123+
g.Expect(err).To(HaveOccurred())
124+
125+
// NOTE: Flip this condition once libgit2 build update in
126+
// https://github.com/fluxcd/source-controller/pull/437 gets merged.
127+
g.Expect(err.Error()).To(ContainSubstring("Unable to extract public key from private key"))
128+
}

0 commit comments

Comments
 (0)