Skip to content

Commit a8b2fc5

Browse files
committed
Augment system cert pool instead of replacing it
When a CA bundle is provided, use x509.SystemCertPool() as the base and append the custom certificates to it. Previously x509.NewCertPool() was used, which created an empty pool containing only the custom CA. This caused TLS verification to fail for public HTTPS endpoints (e.g. GitHub) when the gitjob was configured with a Rancher CA bundle, because the required public root CA was not in the pool. The fix matches go-git's own transportWithCABundle behavior.
1 parent 06074e8 commit a8b2fc5

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

internal/bundlereader/gitclone.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ func gitDownload(ctx context.Context, dst, rawURL string, auth Auth) error {
6666
httpsProtocolMu.Lock()
6767
defer httpsProtocolMu.Unlock()
6868

69-
certPool := x509.NewCertPool()
69+
certPool, certPoolErr := x509.SystemCertPool()
70+
if certPoolErr != nil {
71+
certPool = x509.NewCertPool()
72+
}
7073
if ok := certPool.AppendCertsFromPEM(auth.CABundle); !ok && !auth.InsecureSkipVerify {
7174
return errors.New("CA bundle contains no valid PEM certificates")
7275
}

internal/cmd/cli/gitcloner/cloner.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ var (
4141
// with a custom-CA override.
4242
var caCloneMutex sync.RWMutex
4343

44-
// withExclusiveCA installs a custom HTTPS transport that trusts only caBundle
45-
// (not the system cert pool) for the duration of fn, then restores the default.
46-
// The mutex is only taken for HTTPS URLs because only go-git's HTTPS protocol
47-
// registry is affected; SSH and other scheme clones can proceed concurrently.
44+
// withExclusiveCA installs a custom HTTPS transport that augments the system cert pool
45+
// with caBundle for the duration of fn, then restores the default. The mutex is only
46+
// taken for HTTPS URLs because only go-git's HTTPS protocol registry is affected; SSH
47+
// and other scheme clones can proceed concurrently.
4848
func withExclusiveCA(repoURL string, caBundle []byte, insecureSkipTLS bool, fn func() error) error {
4949
if !strings.HasPrefix(repoURL, "https://") {
5050
// Non-HTTPS clone: no protocol registry mutation, no lock needed.
@@ -58,7 +58,10 @@ func withExclusiveCA(repoURL string, caBundle []byte, insecureSkipTLS bool, fn f
5858
caCloneMutex.Lock()
5959
defer caCloneMutex.Unlock()
6060

61-
certPool := x509.NewCertPool()
61+
certPool, err := x509.SystemCertPool()
62+
if err != nil {
63+
certPool = x509.NewCertPool()
64+
}
6265
if ok := certPool.AppendCertsFromPEM(caBundle); !ok && !insecureSkipTLS {
6366
return errors.New("CA bundle contains no valid PEM certificates")
6467
}

0 commit comments

Comments
 (0)