Skip to content

Commit d3d1917

Browse files
committed
Add tests for libgit2
Signed-off-by: Somtochi Onyekwere <[email protected]>
1 parent e82c8e8 commit d3d1917

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

pkg/git/gogit/transport.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,12 @@ func (s *PublicKeyAuth) Method(secret corev1.Secret) (*git.Auth, error) {
8383
return nil, fmt.Errorf("invalid '%s' secret data: required fields 'identity' and 'known_hosts'", secret.Name)
8484
}
8585

86-
password := secret.Data["password"]
87-
8886
user := s.user
8987
if user == "" {
9088
user = git.DefaultPublicKeyAuthUser
9189
}
9290

91+
password := secret.Data["password"]
9392
pk, err := ssh.NewPublicKeys(user, identity, string(password))
9493
if err != nil {
9594
return nil, err

pkg/git/libgit2/transport.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ func (s *PublicKeyAuth) Method(secret corev1.Secret) (*git.Auth, error) {
119119
return nil, err
120120
}
121121

122-
password := secret.Data["password"]
123122
// Need to validate private key as it is not
124123
// done by git2go when loading the key
125-
if len(password) == 0 {
126-
_, err = ssh.ParsePrivateKey(identity)
127-
} else {
124+
password, ok := secret.Data["password"]
125+
if ok {
128126
_, err = ssh.ParsePrivateKeyWithPassphrase(identity, password)
127+
} else {
128+
_, err = ssh.ParsePrivateKey(identity)
129129
}
130130

131131
if err != nil {

pkg/git/libgit2/transport_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ v2MYnxRjc9INpi/Dyzz2MMvOnOW+aDuOh/If2AtVCmeJUx1pf4CFk3viQwJBAKyC
4444
t824+evjv+NQBlme3AOF6PgxtV4D4wWoJ5Uk/dTejER0j/Hbl6sqPxuiILRRV9qJ
4545
Ngkgu4mLjc3RfenEhJECQAx8zjWUE6kHHPGAd9DfiAIQ4bChqnyS0Nwb9+Gd4hSE
4646
P0Ah10mHiK/M0o3T8Eanwum0gbQHPnOwqZgsPkwXRqQ=
47+
-----END RSA PRIVATE KEY-----`
48+
49+
// secretKeyFixture is a randomly generated
50+
// 512bit RSA private key with password foobar.
51+
secretPassphraseFixture = `-----BEGIN RSA PRIVATE KEY-----
52+
Proc-Type: 4,ENCRYPTED
53+
DEK-Info: AES-256-CBC,0B016973B2A761D31E6B388D0F327C35
54+
55+
X9GET/qAyZkAJBl/RK+1XX75NxONgdUfZDw7PIYi/g+Efh3Z5zH5kh/dx9lxH5ZG
56+
HGCqPAeMO/ofGDGtDULWW6iqDUFRu5gPgEVSCnnbqoHNU325WHhXdhejVAItwObC
57+
IpL/zYfs2+gDHXct/n9FJ/9D/EGXZihwPqYaK8GQSfZAxz0QjLuh0wU1qpbm3y3N
58+
q+o9FLv3b2Ys/tCJOUsYVQOYLSrZEI77y1ii3nWgQ8lXiTJbBUKzuq4f1YWeO8Ah
59+
RZbdhTa57AF5lUaRtL7Nrm3HJUrK1alBbU7HHyjeW4Q4n/D3fiRDC1Mh2Bi4EOOn
60+
wGctSx4kHsZGhJv5qwKqqPEFPhUzph8D2tm2TABk8HJa5KJFDbGrcfvk2uODAoZr
61+
MbcpIxCfl8oB09bWfY6tDQjyvwSYYo2Phdwm7kT92xc=
4762
-----END RSA PRIVATE KEY-----`
4863

4964
// knownHostsFixture is known_hosts fixture in the expected
@@ -64,6 +79,13 @@ var (
6479
"known_hosts": []byte(knownHostsFixture),
6580
},
6681
}
82+
privateKeySecretWithPassphraseFixture = corev1.Secret{
83+
Data: map[string][]byte{
84+
"identity": []byte(secretPassphraseFixture),
85+
"known_hosts": []byte(knownHostsFixture),
86+
"password": []byte("foobar"),
87+
},
88+
}
6789
)
6890

6991
func TestAuthSecretStrategyForURL(t *testing.T) {
@@ -126,10 +148,13 @@ func TestPublicKeyStrategy_Method(t *testing.T) {
126148
wantErr bool
127149
}{
128150
{"private key and known_hosts", privateKeySecretFixture, nil, false},
151+
{"private key with passphrase and known_hosts", privateKeySecretWithPassphraseFixture, nil, false},
129152
{"missing private key", privateKeySecretFixture, func(s *corev1.Secret) { delete(s.Data, "identity") }, true},
130153
{"invalid private key", privateKeySecretFixture, func(s *corev1.Secret) { s.Data["identity"] = []byte(`-----BEGIN RSA PRIVATE KEY-----`) }, true},
131154
{"missing known_hosts", privateKeySecretFixture, func(s *corev1.Secret) { delete(s.Data, "known_hosts") }, true},
132155
{"invalid known_hosts", privateKeySecretFixture, func(s *corev1.Secret) { s.Data["known_hosts"] = []byte(`invalid`) }, true},
156+
{"missing password", privateKeySecretWithPassphraseFixture, func(s *corev1.Secret) { delete(s.Data, "password") }, true},
157+
{"invalid password", privateKeySecretWithPassphraseFixture, func(s *corev1.Secret) { s.Data["password"] = []byte("foo") }, true},
133158
{"empty", corev1.Secret{}, nil, true},
134159
}
135160
for _, tt := range tests {

0 commit comments

Comments
 (0)