Skip to content

Commit

Permalink
http_config: Allow customizing TLS config and settings.
Browse files Browse the repository at this point in the history
Signed-off-by: bwplotka <[email protected]>
  • Loading branch information
bwplotka committed Jan 16, 2025
1 parent 8d916fa commit ca379f3
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,14 @@ func (a *BasicAuth) UnmarshalYAML(unmarshal func(interface{}) error) error {
type DialContextFunc func(context.Context, string, string) (net.Conn, error)

type httpClientOptions struct {
dialContextFunc DialContextFunc
keepAlivesEnabled bool
http2Enabled bool
idleConnTimeout time.Duration
userAgent string
host string
secretManager SecretManager
dialContextFunc DialContextFunc
keepAlivesEnabled bool
http2Enabled bool
idleConnTimeout time.Duration
userAgent string
host string
secretManager SecretManager
extendTLSConfigFunc TLSConfigExtension
}

// HTTPClientOption defines an option that can be applied to the HTTP client.
Expand Down Expand Up @@ -515,6 +516,17 @@ func WithHost(host string) HTTPClientOption {
})
}

// TLSConfigExtension modifies the given tls config and settings.
type TLSConfigExtension func(*tls.Config, TLSRoundTripperSettings) (*tls.Config, TLSRoundTripperSettings, error)

// WithTLSConfigExtension allows to insert extension function that can freely modify
// TLSConfig and TLSRoundTripperSettings used for the round tripper creation.
func WithTLSConfigExtension(extendTLSConfigFunc TLSConfigExtension) HTTPClientOption {
return httpClientOptionFunc(func(opts *httpClientOptions) {
opts.extendTLSConfigFunc = extendTLSConfigFunc
})
}

type secretManagerOption struct {
secretManager SecretManager
}
Expand Down Expand Up @@ -679,6 +691,15 @@ func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientCon
if err != nil {
return nil, err
}

// Allow customizing the TLS config and settings, if specified in opts.
if opts.extendTLSConfigFunc != nil {
tlsConfig, tlsSettings, err = opts.extendTLSConfigFunc(tlsConfig, tlsSettings)
if err != nil {
return nil, err
}
}

if tlsSettings.immutable() {
// No need for a RoundTripper that reloads the files automatically.
return newRT(tlsConfig)
Expand Down

0 comments on commit ca379f3

Please sign in to comment.