@@ -17,6 +17,8 @@ limitations under the License.
1717package gitprovider
1818
1919import (
20+ "crypto/tls"
21+ "crypto/x509"
2022 "fmt"
2123 "net/http"
2224
@@ -59,6 +61,9 @@ type CommonClientOptions struct {
5961
6062 // Logger allows the caller to pass a logger for use by the provider
6163 Logger * logr.Logger
64+
65+ // CABundle is a []byte containing the CA bundle to use for the client.
66+ CABundle []byte
6267}
6368
6469// ApplyToCommonClientOptions applies the currently set fields in opts to target. If both opts and
@@ -106,6 +111,14 @@ func (opts *CommonClientOptions) ApplyToCommonClientOptions(target *CommonClient
106111 }
107112 target .Logger = opts .Logger
108113 }
114+
115+ if opts .CABundle != nil {
116+ if target .CABundle != nil {
117+ return fmt .Errorf ("option CABundle already configured: %w" , ErrInvalidClientOptions )
118+ }
119+ target .CABundle = opts .CABundle
120+ }
121+
109122 return nil
110123}
111124
@@ -292,3 +305,32 @@ func MakeClientOptions(opts ...ClientOption) (*ClientOptions, error) {
292305 }
293306 return o , nil
294307}
308+
309+ // WithCustomCAPostChainTransportHook registers a ChainableRoundTripperFunc "after" the cache and authentication
310+ // transports in the chain.
311+ func WithCustomCAPostChainTransportHook (caBundle []byte ) ClientOption {
312+ // Don't allow an empty value
313+ if len (caBundle ) == 0 {
314+ return optionError (fmt .Errorf ("caBundle cannot be empty: %w" , ErrInvalidClientOptions ))
315+ }
316+
317+ return buildCommonOption (CommonClientOptions {CABundle : caBundle , PostChainTransportHook : caCustomTransport (caBundle )})
318+ }
319+
320+ func caCustomTransport (caBundle []byte ) ChainableRoundTripperFunc {
321+ return func (_ http.RoundTripper ) http.RoundTripper {
322+ // discard error, as we're only using it to check if rootCA is empty
323+ rootCAs , _ := x509 .SystemCertPool ()
324+ if rootCAs == nil {
325+ rootCAs = x509 .NewCertPool ()
326+ }
327+
328+ rootCAs .AppendCertsFromPEM (caBundle )
329+
330+ return & http.Transport {
331+ TLSClientConfig : & tls.Config {
332+ RootCAs : rootCAs ,
333+ },
334+ }
335+ }
336+ }
0 commit comments