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:
		
							parent
							
								
									f9a34045e1
								
							
						
					
					
						commit
						a7f2e870bf
					
				|  | @ -230,7 +230,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour | |||
| 	defer os.RemoveAll(tmpGit) | ||||
| 
 | ||||
| 	// Configure auth options using secret
 | ||||
| 	authOpts := &git.AuthOptions{} | ||||
| 	var authOpts *git.AuthOptions | ||||
| 	if repository.Spec.SecretRef != nil { | ||||
| 		name := types.NamespacedName{ | ||||
| 			Namespace: repository.GetNamespace(), | ||||
|  |  | |||
|  | @ -17,6 +17,8 @@ limitations under the License. | |||
| package gogit | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 
 | ||||
| 	"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/ssh" | ||||
|  | @ -53,6 +55,10 @@ func transportAuth(opts *git.AuthOptions) (transport.AuthMethod, error) { | |||
| 			} | ||||
| 			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 | ||||
| } | ||||
|  |  | |||
|  | @ -170,10 +170,14 @@ func Test_transportAuth(t *testing.T) { | |||
| 		{ | ||||
| 			name:    "Empty", | ||||
| 			opts:    &git.AuthOptions{}, | ||||
| 			wantFunc: func(g *WithT, t transport.AuthMethod, opts *git.AuthOptions) { | ||||
| 				g.Expect(t).To(BeNil()) | ||||
| 			wantErr: errors.New("no transport type set"), | ||||
| 		}, | ||||
| 			wantErr: nil, | ||||
| 		{ | ||||
| 			name: "Unknown transport", | ||||
| 			opts: &git.AuthOptions{ | ||||
| 				Transport: "foo", | ||||
| 			}, | ||||
| 			wantErr: errors.New("unknown transport 'foo'"), | ||||
| 		}, | ||||
| 	} | ||||
| 	for _, tt := range tests { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue