Skip to content

Commit e42561f

Browse files
committed
git: refactor transport into strategies
1 parent d048730 commit e42561f

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

controllers/gitrepository_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ func (r *GitRepositoryReconciler) sync(ctx context.Context, repository sourcev1.
149149
}
150150

151151
// determine auth method
152+
strategy := intgit.AuthSecretStrategyForURL(repository.Spec.URL)
152153
var auth transport.AuthMethod
153154
if repository.Spec.SecretRef != nil {
154155
name := types.NamespacedName{
@@ -163,12 +164,11 @@ func (r *GitRepositoryReconciler) sync(ctx context.Context, repository sourcev1.
163164
return sourcev1.GitRepositoryNotReady(repository, sourcev1.AuthenticationFailedReason, err.Error()), err
164165
}
165166

166-
method, err := intgit.AuthMethodFromSecret(repository.Spec.URL, secret)
167+
auth, err = strategy.Method(secret)
167168
if err != nil {
168169
err = fmt.Errorf("auth error: %w", err)
169170
return sourcev1.GitRepositoryNotReady(repository, sourcev1.AuthenticationFailedReason, err.Error()), err
170171
}
171-
auth = method
172172
}
173173

174174
// create tmp dir for the Git clone

internal/git/transport.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,23 @@ import (
2828
"github.com/fluxcd/source-controller/internal/crypto/ssh/knownhosts"
2929
)
3030

31-
func AuthMethodFromSecret(url string, secret corev1.Secret) (transport.AuthMethod, error) {
31+
func AuthSecretStrategyForURL(url string) AuthSecretStrategy {
3232
switch {
3333
case strings.HasPrefix(url, "http"):
34-
return BasicAuthFromSecret(secret)
34+
return &BasicAuth{}
3535
case strings.HasPrefix(url, "ssh"):
36-
return PublicKeysFromSecret(secret)
36+
return &PublicKeyAuth{}
3737
}
38-
return nil, nil
38+
return nil
3939
}
4040

41-
func BasicAuthFromSecret(secret corev1.Secret) (*http.BasicAuth, error) {
41+
type AuthSecretStrategy interface {
42+
Method(secret corev1.Secret) (transport.AuthMethod, error)
43+
}
44+
45+
type BasicAuth struct{}
46+
47+
func (s *BasicAuth) Method(secret corev1.Secret) (transport.AuthMethod, error) {
4248
auth := &http.BasicAuth{}
4349
if username, ok := secret.Data["username"]; ok {
4450
auth.Username = string(username)
@@ -52,7 +58,9 @@ func BasicAuthFromSecret(secret corev1.Secret) (*http.BasicAuth, error) {
5258
return auth, nil
5359
}
5460

55-
func PublicKeysFromSecret(secret corev1.Secret) (*ssh.PublicKeys, error) {
61+
type PublicKeyAuth struct{}
62+
63+
func (s *PublicKeyAuth) Method(secret corev1.Secret) (transport.AuthMethod, error) {
5664
identity := secret.Data["identity"]
5765
knownHosts := secret.Data["known_hosts"]
5866
if len(identity) == 0 || len(knownHosts) == 0 {

internal/git/transport_test.go

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/go-git/go-git/v5/plumbing/transport"
2424
"github.com/go-git/go-git/v5/plumbing/transport/http"
25-
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
2625
corev1 "k8s.io/api/core/v1"
2726
)
2827

@@ -65,39 +64,33 @@ var (
6564
}
6665
)
6766

68-
func TestAuthMethodFromSecret(t *testing.T) {
67+
func TestAuthSecretStrategyForURL(t *testing.T) {
6968
tests := []struct {
70-
name string
71-
url string
72-
secret corev1.Secret
73-
want transport.AuthMethod
74-
wantErr bool
69+
name string
70+
url string
71+
want AuthSecretStrategy
7572
}{
76-
{"HTTP", "http://git.example.com/org/repo.git", basicAuthSecretFixture, &http.BasicAuth{}, false},
77-
{"HTTPS", "https://git.example.com/org/repo.git", basicAuthSecretFixture, &http.BasicAuth{}, false},
78-
{"SSH", "ssh://git.example.com:2222/org/repo.git", privateKeySecretFixture, &ssh.PublicKeys{}, false},
79-
{"unsupported", "protocol://git.example.com/org/repo.git", corev1.Secret{}, nil, false},
73+
{"HTTP", "http://git.example.com/org/repo.git", &BasicAuth{}},
74+
{"HTTPS", "https://git.example.com/org/repo.git", &BasicAuth{}},
75+
{"SSH", "ssh://git.example.com:2222/org/repo.git", &PublicKeyAuth{}},
76+
{"unsupported", "protocol://example.com", nil},
8077
}
8178
for _, tt := range tests {
8279
t.Run(tt.name, func(t *testing.T) {
83-
got, err := AuthMethodFromSecret(tt.url, tt.secret)
84-
if (err != nil) != tt.wantErr {
85-
t.Errorf("AuthMethodFromSecret() error = %v, wantErr %v", err, tt.wantErr)
86-
return
87-
}
80+
got := AuthSecretStrategyForURL(tt.url)
8881
if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
89-
t.Errorf("AuthMethodFromSecret() got = %v, want %v", got, tt.want)
82+
t.Errorf("AuthSecretStrategyForURL() got = %v, want %v", got, tt.want)
9083
}
9184
})
9285
}
9386
}
9487

95-
func TestBasicAuthFromSecret(t *testing.T) {
88+
func TestBasicAuthStrategy_Method(t *testing.T) {
9689
tests := []struct {
9790
name string
9891
secret corev1.Secret
9992
modify func(secret *corev1.Secret)
100-
want *http.BasicAuth
93+
want transport.AuthMethod
10194
wantErr bool
10295
}{
10396
{"username and password", basicAuthSecretFixture, nil, &http.BasicAuth{Username: "git", Password: "password"}, false},
@@ -111,19 +104,20 @@ func TestBasicAuthFromSecret(t *testing.T) {
111104
if tt.modify != nil {
112105
tt.modify(secret)
113106
}
114-
got, err := BasicAuthFromSecret(*secret)
107+
s := &BasicAuth{}
108+
got, err := s.Method(*secret)
115109
if (err != nil) != tt.wantErr {
116-
t.Errorf("BasicAuthFromSecret() error = %v, wantErr %v", err, tt.wantErr)
110+
t.Errorf("Method() error = %v, wantErr %v", err, tt.wantErr)
117111
return
118112
}
119113
if !reflect.DeepEqual(got, tt.want) {
120-
t.Errorf("BasicAuthFromSecret() got = %v, want %v", got, tt.want)
114+
t.Errorf("Method() got = %v, want %v", got, tt.want)
121115
}
122116
})
123117
}
124118
}
125119

126-
func TestPublicKeysFromSecret(t *testing.T) {
120+
func TestPublicKeyStrategy_Method(t *testing.T) {
127121
tests := []struct {
128122
name string
129123
secret corev1.Secret
@@ -143,9 +137,10 @@ func TestPublicKeysFromSecret(t *testing.T) {
143137
if tt.modify != nil {
144138
tt.modify(secret)
145139
}
146-
_, err := PublicKeysFromSecret(*secret)
140+
s := &PublicKeyAuth{}
141+
_, err := s.Method(*secret)
147142
if (err != nil) != tt.wantErr {
148-
t.Errorf("PublicKeysFromSecret() error = %v, wantErr %v", err, tt.wantErr)
143+
t.Errorf("Method() error = %v, wantErr %v", err, tt.wantErr)
149144
return
150145
}
151146
})

0 commit comments

Comments
 (0)