Skip to content

Commit 8849078

Browse files
committed
fix panics on unmanaged http and proxy on managed http
Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent f35c144 commit 8849078

19 files changed

+845
-896
lines changed

controllers/gitrepository_controller.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -455,21 +455,6 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
455455
return sreconcile.ResultEmpty, e
456456
}
457457

458-
repositoryURL := obj.Spec.URL
459-
// managed GIT transport only affects the libgit2 implementation
460-
if managed.Enabled() && obj.Spec.GitImplementation == sourcev1.LibGit2Implementation {
461-
// We set the TransportAuthID of this set of authentication options here by constructing
462-
// a unique ID that won't clash in a multi tenant environment. This unique ID is used by
463-
// libgit2 managed transports. This enables us to bypass the inbuilt credentials callback in
464-
// libgit2, which is inflexible and unstable.
465-
if strings.HasPrefix(repositoryURL, "http") {
466-
authOpts.TransportAuthID = fmt.Sprintf("http://%s/%s/%d", obj.Name, obj.UID, obj.Generation)
467-
}
468-
if strings.HasPrefix(repositoryURL, "ssh") {
469-
authOpts.TransportAuthID = fmt.Sprintf("ssh://%s/%s/%d", obj.Name, obj.UID, obj.Generation)
470-
}
471-
}
472-
473458
// Fetch the included artifact metadata.
474459
artifacts, err := r.fetchIncludes(ctx, obj)
475460
if err != nil {
@@ -491,7 +476,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
491476
optimizedClone = true
492477
}
493478

494-
c, err := r.gitCheckout(ctx, obj, repositoryURL, authOpts, dir, optimizedClone)
479+
c, err := r.gitCheckout(ctx, obj, authOpts, dir, optimizedClone)
495480
if err != nil {
496481
return sreconcile.ResultEmpty, err
497482
}
@@ -525,7 +510,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
525510

526511
// If we can't skip the reconciliation, checkout again without any
527512
// optimization.
528-
c, err := r.gitCheckout(ctx, obj, repositoryURL, authOpts, dir, false)
513+
c, err := r.gitCheckout(ctx, obj, authOpts, dir, false)
529514
if err != nil {
530515
return sreconcile.ResultEmpty, err
531516
}
@@ -717,7 +702,7 @@ func (r *GitRepositoryReconciler) reconcileInclude(ctx context.Context,
717702
// gitCheckout builds checkout options with the given configurations and
718703
// performs a git checkout.
719704
func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context,
720-
obj *sourcev1.GitRepository, repoURL string, authOpts *git.AuthOptions, dir string, optimized bool) (*git.Commit, error) {
705+
obj *sourcev1.GitRepository, authOpts *git.AuthOptions, dir string, optimized bool) (*git.Commit, error) {
721706
// Configure checkout strategy.
722707
checkoutOpts := git.CheckoutOptions{RecurseSubmodules: obj.Spec.RecurseSubmodules}
723708
if ref := obj.Spec.Reference; ref != nil {
@@ -743,15 +728,34 @@ func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context,
743728
Err: fmt.Errorf("failed to configure checkout strategy for Git implementation '%s': %w", obj.Spec.GitImplementation, err),
744729
Reason: sourcev1.GitOperationFailedReason,
745730
}
746-
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error())
747731
// Do not return err as recovery without changes is impossible.
748732
return nil, e
749733
}
750734

735+
// managed GIT transport only affects the libgit2 implementation
736+
if managed.Enabled() && obj.Spec.GitImplementation == sourcev1.LibGit2Implementation {
737+
// We set the TransportOptionsURL of this set of authentication options here by constructing
738+
// a unique ID that won't clash in a multi tenant environment. This unique ID is used by
739+
// libgit2 managed transports. This enables us to bypass the inbuilt credentials callback in
740+
// libgit2, which is inflexible and unstable.
741+
if strings.HasPrefix(obj.Spec.URL, "http") {
742+
authOpts.TransportOptionsURL = fmt.Sprintf("http://%s/%s/%d", obj.Name, obj.UID, obj.Generation)
743+
} else if strings.HasPrefix(obj.Spec.URL, "ssh") {
744+
authOpts.TransportOptionsURL = fmt.Sprintf("ssh://%s/%s/%d", obj.Name, obj.UID, obj.Generation)
745+
} else {
746+
e := &serror.Stalling{
747+
Err: fmt.Errorf("git repository URL has invalid transport type: '%s'", obj.Spec.URL),
748+
Reason: sourcev1.GitOperationFailedReason,
749+
}
750+
return nil, e
751+
}
752+
}
753+
751754
// Checkout HEAD of reference in object
752755
gitCtx, cancel := context.WithTimeout(ctx, obj.Spec.Timeout.Duration)
753756
defer cancel()
754-
commit, err := checkoutStrategy.Checkout(gitCtx, dir, repoURL, authOpts)
757+
758+
commit, err := checkoutStrategy.Checkout(gitCtx, dir, obj.Spec.URL, authOpts)
755759
if err != nil {
756760
e := serror.NewGeneric(
757761
fmt.Errorf("failed to checkout and determine revision: %w", err),

controllers/gitrepository_controller_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func TestGitRepositoryReconciler_reconcileSource_authStrategy(t *testing.T) {
362362
},
363363
wantErr: true,
364364
assertConditions: []metav1.Condition{
365-
*conditions.TrueCondition(sourcev1.FetchFailedCondition, sourcev1.GitOperationFailedReason, "failed to checkout and determine revision: unable to fetch-connect to remote '<url>': PEM CA bundle could not be appended to x509 certificate pool"),
365+
*conditions.TrueCondition(sourcev1.FetchFailedCondition, sourcev1.GitOperationFailedReason, "failed to checkout and determine revision: unable to clone '<url>': PEM CA bundle could not be appended to x509 certificate pool"),
366366
},
367367
},
368368
{
@@ -645,10 +645,11 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
645645
}
646646
conditions.MarkTrue(obj, sourcev1.ArtifactInStorageCondition, meta.SucceededReason, "foo")
647647
},
648-
want: sreconcile.ResultEmpty,
649-
wantErr: true,
650-
wantRevision: "staging/<commit>",
651-
wantArtifactOutdated: false,
648+
want: sreconcile.ResultEmpty,
649+
wantErr: true,
650+
wantRevision: "staging/<commit>",
651+
wantArtifactOutdated: false,
652+
skipForImplementation: "libgit2",
652653
},
653654
{
654655
name: "Optimized clone different ignore",
@@ -669,9 +670,10 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
669670
}
670671
conditions.MarkTrue(obj, sourcev1.ArtifactInStorageCondition, meta.SucceededReason, "foo")
671672
},
672-
want: sreconcile.ResultSuccess,
673-
wantRevision: "staging/<commit>",
674-
wantArtifactOutdated: false,
673+
want: sreconcile.ResultSuccess,
674+
wantRevision: "staging/<commit>",
675+
wantArtifactOutdated: false,
676+
skipForImplementation: "libgit2",
675677
},
676678
}
677679

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require (
2828
github.com/fluxcd/pkg/helmtestserver v0.5.0
2929
github.com/fluxcd/pkg/lockedfile v0.1.0
3030
github.com/fluxcd/pkg/runtime v0.16.0
31-
github.com/fluxcd/pkg/ssh v0.3.4
31+
github.com/fluxcd/pkg/ssh v0.4.0
3232
github.com/fluxcd/pkg/testserver v0.2.0
3333
github.com/fluxcd/pkg/untar v0.1.0
3434
github.com/fluxcd/pkg/version v0.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ github.com/fluxcd/pkg/lockedfile v0.1.0 h1:YsYFAkd6wawMCcD74ikadAKXA4s2sukdxrn7w
350350
github.com/fluxcd/pkg/lockedfile v0.1.0/go.mod h1:EJLan8t9MiOcgTs8+puDjbE6I/KAfHbdvIy9VUgIjm8=
351351
github.com/fluxcd/pkg/runtime v0.16.0 h1:ynzvkOedFFZHlsa47EE7XtxZe8qs8edhtmjVZBEWi1Y=
352352
github.com/fluxcd/pkg/runtime v0.16.0/go.mod h1:Iklg+r/Jnqc9cNf2NK+iaosvw49CxX07Pyn0r3zSg/o=
353-
github.com/fluxcd/pkg/ssh v0.3.4 h1:Ko+MUNiiQG3evyoMO19iRk7d4X0VJ6w6+GEeVQ1jLC0=
354-
github.com/fluxcd/pkg/ssh v0.3.4/go.mod h1:KGgOUOy1uI6RC6+qxIBLvP1AeOOs/nLB25Ca6TZMIXE=
353+
github.com/fluxcd/pkg/ssh v0.4.0 h1:2HY88irZ5BCSMlzZExR6cnhRkjxCDsK/lTHHQqCJDJQ=
354+
github.com/fluxcd/pkg/ssh v0.4.0/go.mod h1:KGgOUOy1uI6RC6+qxIBLvP1AeOOs/nLB25Ca6TZMIXE=
355355
github.com/fluxcd/pkg/testserver v0.2.0 h1:Mj0TapmKaywI6Fi5wvt1LAZpakUHmtzWQpJNKQ0Krt4=
356356
github.com/fluxcd/pkg/testserver v0.2.0/go.mod h1:bgjjydkXsZTeFzjz9Cr4heGANr41uTB1Aj1Q5qzuYVk=
357357
github.com/fluxcd/pkg/untar v0.1.0 h1:k97V/xV5hFrAkIkVPuv5AVhyxh1ZzzAKba/lbDfGo6o=

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ func main() {
312312

313313
if enabled, _ := features.Enabled(features.GitManagedTransport); enabled {
314314
managed.InitManagedTransport(ctrl.Log.WithName("managed-transport"))
315+
} else {
316+
if optimize, _ := feathelper.Enabled(features.OptimizedGitClones); optimize {
317+
setupLog.Error(
318+
fmt.Errorf("OptimizedGitClones=true but GitManagedTransport=false"),
319+
"git clones can only be optimized when using managed transort",
320+
)
321+
os.Exit(1)
322+
}
315323
}
316324

317325
setupLog.Info("starting manager")

0 commit comments

Comments
 (0)