Skip to content

Commit

Permalink
http_config: Allow customizing TLS config and settings. (#748)
Browse files Browse the repository at this point in the history
* http_config: Allow customizing TLS config and settings.


Signed-off-by: bwplotka <[email protected]>

* Switched to newTLSConfigFunc

Signed-off-by: bwplotka <[email protected]>

* Addressed comments.

Signed-off-by: bwplotka <[email protected]>

---------

Signed-off-by: bwplotka <[email protected]>
  • Loading branch information
bwplotka authored Jan 16, 2025
1 parent aea8919 commit 280b0e7
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ var (
http2Enabled: true,
// 5 minutes is typically above the maximum sane scrape interval. So we can
// use keepalive for all configurations.
idleConnTimeout: 5 * time.Minute,
idleConnTimeout: 5 * time.Minute,
newTLSConfigFunc: NewTLSConfigWithContext,
}
)

Expand Down Expand Up @@ -452,8 +453,12 @@ func (a *BasicAuth) UnmarshalYAML(unmarshal func(interface{}) error) error {
// by net.Dialer.
type DialContextFunc func(context.Context, string, string) (net.Conn, error)

// NewTLSConfigFunc returns tls.Config.
type NewTLSConfigFunc func(context.Context, *TLSConfig, ...TLSConfigOption) (*tls.Config, error)

type httpClientOptions struct {
dialContextFunc DialContextFunc
newTLSConfigFunc NewTLSConfigFunc
keepAlivesEnabled bool
http2Enabled bool
idleConnTimeout time.Duration
Expand All @@ -473,13 +478,23 @@ func (f httpClientOptionFunc) applyToHTTPClientOptions(options *httpClientOption
f(options)
}

// WithDialContextFunc allows you to override func gets used for the actual dialing. The default is `net.Dialer.DialContext`.
// WithDialContextFunc allows you to override the func gets used for the dialing.
// The default is `net.Dialer.DialContext`.
func WithDialContextFunc(fn DialContextFunc) HTTPClientOption {
return httpClientOptionFunc(func(opts *httpClientOptions) {
opts.dialContextFunc = fn
})
}

// WithNewTLSConfigFunc allows you to override the func that creates the TLS config
// from the prometheus http config.
// The default is `NewTLSConfigWithContext`.
func WithNewTLSConfigFunc(newTLSConfigFunc NewTLSConfigFunc) HTTPClientOption {
return httpClientOptionFunc(func(opts *httpClientOptions) {
opts.newTLSConfigFunc = newTLSConfigFunc
})
}

// WithKeepAlivesDisabled allows to disable HTTP keepalive.
func WithKeepAlivesDisabled() HTTPClientOption {
return httpClientOptionFunc(func(opts *httpClientOptions) {
Expand Down Expand Up @@ -670,7 +685,7 @@ func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientCon
return rt, nil
}

tlsConfig, err := NewTLSConfig(&cfg.TLSConfig, WithSecretManager(opts.secretManager))
tlsConfig, err := opts.newTLSConfigFunc(ctx, &cfg.TLSConfig, WithSecretManager(opts.secretManager))
if err != nil {
return nil, err
}
Expand All @@ -679,6 +694,7 @@ func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientCon
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 280b0e7

Please sign in to comment.