Skip to content

Commit

Permalink
Fixes regression accessing GitLab public repositories
Browse files Browse the repository at this point in the history
Some git servers are more accommodating than others. Gitlab will try to
validate credentials when they are provided, even if they are empty and
the target repository is public, leading to a failed authentication error.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
  • Loading branch information
Paulo Gomes authored and hiddeco committed Mar 30, 2022
1 parent 9b1140c commit c841790
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/git/gogit/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ func transportAuth(opts *git.AuthOptions) (transport.AuthMethod, error) {
}
switch opts.Transport {
case git.HTTPS, git.HTTP:
return &http.BasicAuth{
Username: opts.Username,
Password: opts.Password,
}, nil
// Some providers (i.e. GitLab) will reject empty credentials for
// public repositories.
if opts.Username != "" || opts.Password != "" {
return &http.BasicAuth{
Username: opts.Username,
Password: opts.Password,
}, nil
}
return nil, nil
case git.SSH:
if len(opts.Identity) > 0 {
pk, err := ssh.NewPublicKeys(opts.Username, opts.Identity, opts.Password)
Expand Down
18 changes: 18 additions & 0 deletions pkg/git/gogit/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ func Test_transportAuth(t *testing.T) {
wantFunc func(g *WithT, t transport.AuthMethod, opts *git.AuthOptions)
wantErr error
}{
{
name: "Public HTTP Repositories",
opts: &git.AuthOptions{
Transport: git.HTTP,
},
wantFunc: func(g *WithT, t transport.AuthMethod, opts *git.AuthOptions) {
g.Expect(t).To(BeNil())
},
},
{
name: "Public HTTPS Repositories",
opts: &git.AuthOptions{
Transport: git.HTTP,
},
wantFunc: func(g *WithT, t transport.AuthMethod, opts *git.AuthOptions) {
g.Expect(t).To(BeNil())
},
},
{
name: "HTTP basic auth",
opts: &git.AuthOptions{
Expand Down

0 comments on commit c841790

Please sign in to comment.