@@ -20,6 +20,8 @@ import (
2020 gogit "github.com/go-git/go-git/v5"
2121 "github.com/stretchr/testify/assert"
2222 "github.com/stretchr/testify/require"
23+
24+ fleetgit "github.com/rancher/fleet/pkg/git"
2325)
2426
2527// newSelfSignedTLSServer returns an HTTPS test server with a freshly generated
@@ -120,6 +122,73 @@ func TestGitDownloadCABundle(t *testing.T) {
120122 })
121123}
122124
125+ // TestGitDownloadProxyCABundle verifies that PROXY_CA_BUNDLE is merged into
126+ // the effective CA bundle used for TLS verification in gitDownload.
127+ //
128+ // - PROXY_CA_BUNDLE alone (no auth.CABundle): TLS succeeds when the env var
129+ // contains the server's cert, confirming the merge happens even without an
130+ // explicit CA bundle in the Auth struct.
131+ // - PROXY_CA_BUNDLE merged with auth.CABundle: both certs are trusted.
132+ // - Empty PROXY_CA_BUNDLE: falls back to auth.CABundle only.
133+ //
134+ // Not parallel: the test mutates the process-global PROXY_CA_BUNDLE env var.
135+ func TestGitDownloadProxyCABundle (t * testing.T ) {
136+ srv , srvCertPEM := newSelfSignedTLSServer (t )
137+ otherSrv , otherCertPEM := newSelfSignedTLSServer (t )
138+
139+ t .Run ("PROXY_CA_BUNDLE alone trusts the server" , func (t * testing.T ) {
140+ t .Setenv (fleetgit .ProxyCABundleEnvVar , string (srvCertPEM ))
141+ dst := t .TempDir ()
142+ err := gitDownload (context .Background (), dst , srv .URL , Auth {})
143+ require .Error (t , err )
144+ // TLS succeeded; expect a git-protocol error, not a certificate error.
145+ assert .NotContains (t , err .Error (), "certificate" )
146+ })
147+
148+ t .Run ("PROXY_CA_BUNDLE is merged with auth.CABundle" , func (t * testing.T ) {
149+ // auth.CABundle covers srv; PROXY_CA_BUNDLE covers otherSrv.
150+ t .Setenv (fleetgit .ProxyCABundleEnvVar , string (otherCertPEM ))
151+
152+ // auth.CABundle server: trusted via auth.CABundle (PROXY_CA_BUNDLE not needed).
153+ dst := t .TempDir ()
154+ err := gitDownload (context .Background (), dst , srv .URL , Auth {CABundle : srvCertPEM })
155+ require .Error (t , err )
156+ assert .NotContains (t , err .Error (), "certificate" , "auth.CABundle server should get past TLS" )
157+
158+ // PROXY_CA_BUNDLE server: trusted via the merged env var cert.
159+ dst = t .TempDir ()
160+ err = gitDownload (context .Background (), dst , otherSrv .URL , Auth {CABundle : srvCertPEM })
161+ require .Error (t , err )
162+ assert .NotContains (t , err .Error (), "certificate" , "PROXY_CA_BUNDLE server should get past TLS via merge" )
163+ })
164+
165+ t .Run ("empty PROXY_CA_BUNDLE uses auth.CABundle only" , func (t * testing.T ) {
166+ t .Setenv (fleetgit .ProxyCABundleEnvVar , "" )
167+ dst := t .TempDir ()
168+ err := gitDownload (context .Background (), dst , srv .URL , Auth {CABundle : srvCertPEM })
169+ require .Error (t , err )
170+ assert .NotContains (t , err .Error (), "certificate" )
171+ })
172+
173+ t .Run ("wrong PROXY_CA_BUNDLE without auth.CABundle fails with TLS error" , func (t * testing.T ) {
174+ // otherCertPEM does not cover srv, and there is no auth.CABundle fallback,
175+ // so TLS must fail with a certificate error.
176+ t .Setenv (fleetgit .ProxyCABundleEnvVar , string (otherCertPEM ))
177+ dst := t .TempDir ()
178+ err := gitDownload (context .Background (), dst , srv .URL , Auth {})
179+ require .Error (t , err )
180+ assert .Contains (t , err .Error (), "certificate" )
181+ })
182+
183+ t .Run ("no PROXY_CA_BUNDLE and no auth.CABundle fails with TLS error" , func (t * testing.T ) {
184+ t .Setenv (fleetgit .ProxyCABundleEnvVar , "" )
185+ dst := t .TempDir ()
186+ err := gitDownload (context .Background (), dst , srv .URL , Auth {})
187+ require .Error (t , err )
188+ assert .Contains (t , err .Error (), "certificate" )
189+ })
190+ }
191+
123192// generateEd25519PEM returns a PEM-encoded Ed25519 private key for use in tests.
124193func generateEd25519PEM (t * testing.T ) []byte {
125194 t .Helper ()
0 commit comments