transportAuth(): Add checks for invalid transports

Update GitRepositoryReconciler to use a nil authOpts unless it's
configured.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
This commit is contained in:
Sunny 2021-10-27 00:40:17 +05:30
parent f9a34045e1
commit a7f2e870bf
3 changed files with 16 additions and 6 deletions

View File

@ -230,7 +230,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
defer os.RemoveAll(tmpGit) defer os.RemoveAll(tmpGit)
// Configure auth options using secret // Configure auth options using secret
authOpts := &git.AuthOptions{} var authOpts *git.AuthOptions
if repository.Spec.SecretRef != nil { if repository.Spec.SecretRef != nil {
name := types.NamespacedName{ name := types.NamespacedName{
Namespace: repository.GetNamespace(), Namespace: repository.GetNamespace(),

View File

@ -17,6 +17,8 @@ limitations under the License.
package gogit package gogit
import ( import (
"fmt"
"github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh" "github.com/go-git/go-git/v5/plumbing/transport/ssh"
@ -53,6 +55,10 @@ func transportAuth(opts *git.AuthOptions) (transport.AuthMethod, error) {
} }
return pk, nil return pk, nil
} }
case "":
return nil, fmt.Errorf("no transport type set")
default:
return nil, fmt.Errorf("unknown transport '%s'", opts.Transport)
} }
return nil, nil return nil, nil
} }

View File

@ -168,12 +168,16 @@ func Test_transportAuth(t *testing.T) {
wantErr: errors.New("knownhosts: knownhosts: missing host pattern"), wantErr: errors.New("knownhosts: knownhosts: missing host pattern"),
}, },
{ {
name: "Empty", name: "Empty",
opts: &git.AuthOptions{}, opts: &git.AuthOptions{},
wantFunc: func(g *WithT, t transport.AuthMethod, opts *git.AuthOptions) { wantErr: errors.New("no transport type set"),
g.Expect(t).To(BeNil()) },
{
name: "Unknown transport",
opts: &git.AuthOptions{
Transport: "foo",
}, },
wantErr: nil, wantErr: errors.New("unknown transport 'foo'"),
}, },
} }
for _, tt := range tests { for _, tt := range tests {