@@ -100,26 +100,41 @@ func initCertPool() *x509.CertPool {
100100 return nil
101101 }
102102
103- certPool := getCertPool ()
103+ useSystemCertPool , _ := strconv .ParseBool (os .Getenv (caSystemCertPool ))
104+
105+ caCerts := strings .Split (customCACertsPath , string (os .PathListSeparator ))
106+
107+ certPool , err := CreateCertPool (caCerts , useSystemCertPool )
108+ if err != nil {
109+ panic (fmt .Sprintf ("create certificates pool: %v" , err ))
110+ }
111+
112+ return certPool
113+ }
104114
105- for _ , customPath := range strings .Split (customCACertsPath , string (os .PathListSeparator )) {
115+ // CreateCertPool creates a *x509.CertPool populated with the PEM certificates.
116+ func CreateCertPool (caCerts []string , useSystemCertPool bool ) (* x509.CertPool , error ) {
117+ if len (caCerts ) == 0 {
118+ return nil , nil
119+ }
120+
121+ certPool := newCertPool (useSystemCertPool )
122+
123+ for _ , customPath := range caCerts {
106124 customCAs , err := os .ReadFile (customPath )
107125 if err != nil {
108- panic (fmt .Sprintf ("error reading %s=%q: %v" ,
109- caCertificatesEnvVar , customPath , err ))
126+ return nil , fmt .Errorf ("error reading %q: %w" , customPath , err )
110127 }
111128
112129 if ok := certPool .AppendCertsFromPEM (customCAs ); ! ok {
113- panic (fmt .Sprintf ("error creating x509 cert pool from %s=%q: %v" ,
114- caCertificatesEnvVar , customPath , err ))
130+ return nil , fmt .Errorf ("error creating x509 cert pool from %q: %w" , customPath , err )
115131 }
116132 }
117133
118- return certPool
134+ return certPool , nil
119135}
120136
121- func getCertPool () * x509.CertPool {
122- useSystemCertPool , _ := strconv .ParseBool (os .Getenv (caSystemCertPool ))
137+ func newCertPool (useSystemCertPool bool ) * x509.CertPool {
123138 if ! useSystemCertPool {
124139 return x509 .NewCertPool ()
125140 }
@@ -128,5 +143,6 @@ func getCertPool() *x509.CertPool {
128143 if err == nil {
129144 return pool
130145 }
146+
131147 return x509 .NewCertPool ()
132148}
0 commit comments