Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom transport dialer #1520

Merged
merged 4 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"runtime"
"time"

"github.com/hashicorp/consul-template/dependency"
)

const (
Expand Down Expand Up @@ -36,6 +38,11 @@ var (
// TransportConfig is the configuration to tune low-level APIs for the
// interactions on the wire.
type TransportConfig struct {
// CustomDialer overrides the default net.Dial with a custom dialer. This is
// useful for instance with Vault Agent Templating to direct Consul Template
// requests through an internal cache.
CustomDialer dependency.TransportDialer `mapstructure:"-"`

// DialKeepAlive is the amount of time for keep-alives.
DialKeepAlive *time.Duration `mapstructure:"dial_keep_alive"`

Expand Down Expand Up @@ -75,6 +82,7 @@ func (c *TransportConfig) Copy() *TransportConfig {

var o TransportConfig

o.CustomDialer = c.CustomDialer
o.DialKeepAlive = c.DialKeepAlive
o.DialTimeout = c.DialTimeout
o.DisableKeepAlives = c.DisableKeepAlives
Expand Down Expand Up @@ -104,6 +112,10 @@ func (c *TransportConfig) Merge(o *TransportConfig) *TransportConfig {

r := c.Copy()

if o.CustomDialer != nil {
r.CustomDialer = o.CustomDialer
}

if o.DialKeepAlive != nil {
r.DialKeepAlive = o.DialKeepAlive
}
Expand Down
36 changes: 36 additions & 0 deletions config/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"net"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -33,6 +34,17 @@ func TestTransportConfig_Copy(t *testing.T) {
TLSHandshakeTimeout: TimeDuration(30 * time.Second),
},
},
{
"same_enabled_custom_dialer",
&TransportConfig{
CustomDialer: &net.Dialer{Timeout: 10 * time.Second},
DisableKeepAlives: Bool(true),
IdleConnTimeout: TimeDuration(40 * time.Second),
MaxIdleConns: Int(150),
MaxIdleConnsPerHost: Int(15),
TLSHandshakeTimeout: TimeDuration(30 * time.Second),
},
},
}

for i, tc := range cases {
Expand Down Expand Up @@ -245,6 +257,30 @@ func TestTransportConfig_Merge(t *testing.T) {
&TransportConfig{TLSHandshakeTimeout: TimeDuration(10 * time.Second)},
&TransportConfig{TLSHandshakeTimeout: TimeDuration(10 * time.Second)},
},
{
"custom_transport_dialer",
&TransportConfig{CustomDialer: &net.Dialer{Timeout: 10 * time.Second}},
&TransportConfig{CustomDialer: &net.Dialer{Timeout: 20 * time.Second}},
&TransportConfig{CustomDialer: &net.Dialer{Timeout: 20 * time.Second}},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the second one or the one with the highest timeout win?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this test it's the second one, i.e. the "other" takes precedence in Merge:

// Merge combines all values in this configuration with the values in the other
// configuration, with values in the other configuration taking precedence.
// Maps and slices are merged, most other values are overwritten. Complex
// structs define their own merge functionality.
func (c *TransportConfig) Merge(o *TransportConfig) *TransportConfig {

},
{
"custom_transport_dialer_empty_one",
&TransportConfig{CustomDialer: &net.Dialer{Timeout: 10 * time.Second}},
&TransportConfig{},
&TransportConfig{CustomDialer: &net.Dialer{Timeout: 10 * time.Second}},
},
{
"custom_transport_dialer_empty_two",
&TransportConfig{},
&TransportConfig{CustomDialer: &net.Dialer{Timeout: 10 * time.Second}},
&TransportConfig{CustomDialer: &net.Dialer{Timeout: 10 * time.Second}},
},
{
"custom_transport_dialer_same",
&TransportConfig{CustomDialer: &net.Dialer{Timeout: 10 * time.Second}},
&TransportConfig{CustomDialer: &net.Dialer{Timeout: 10 * time.Second}},
&TransportConfig{CustomDialer: &net.Dialer{Timeout: 10 * time.Second}},
},
}

for i, tc := range cases {
Expand Down
29 changes: 24 additions & 5 deletions dependency/client_set.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dependency

import (
"context"
"crypto/tls"
"fmt"
"log"
Expand Down Expand Up @@ -35,6 +36,16 @@ type vaultClient struct {
httpClient *http.Client
}

// TransportDialer is an interface that allows passing a custom dialer function
// to an HTTP client's transport config
type TransportDialer interface {
// Dial is intended to match https://pkg.go.dev/net#Dialer.Dial
Dial(network, address string) (net.Conn, error)

// DialContext is intended to match https://pkg.go.dev/net#Dialer.DialContext
DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

// CreateConsulClientInput is used as input to the CreateConsulClient function.
type CreateConsulClientInput struct {
Address string
Expand Down Expand Up @@ -74,6 +85,7 @@ type CreateVaultClientInput struct {
SSLCAPath string
ServerName string

TransportCustomDialer TransportDialer
TransportDialKeepAlive time.Duration
TransportDialTimeout time.Duration
TransportDisableKeepAlives bool
Expand Down Expand Up @@ -202,12 +214,19 @@ func (c *ClientSet) CreateVaultClient(i *CreateVaultClientInput) error {
}

// This transport will attempt to keep connections open to the Vault server.
var dialer TransportDialer
dialer = &net.Dialer{
Timeout: i.TransportDialTimeout,
KeepAlive: i.TransportDialKeepAlive,
}

if i.TransportCustomDialer != nil {
dialer = i.TransportCustomDialer
}

transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: i.TransportDialTimeout,
KeepAlive: i.TransportDialKeepAlive,
}).Dial,
Proxy: http.ProxyFromEnvironment,
Dial: dialer.Dial,
DisableKeepAlives: i.TransportDisableKeepAlives,
MaxIdleConns: i.TransportMaxIdleConns,
IdleConnTimeout: i.TransportIdleConnTimeout,
Expand Down
1 change: 1 addition & 0 deletions manager/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,7 @@ func newClientSet(c *config.Config) (*dep.ClientSet, error) {
SSLCACert: config.StringVal(c.Vault.SSL.CaCert),
SSLCAPath: config.StringVal(c.Vault.SSL.CaPath),
ServerName: config.StringVal(c.Vault.SSL.ServerName),
TransportCustomDialer: c.Vault.Transport.CustomDialer,
TransportDialKeepAlive: config.TimeDurationVal(c.Vault.Transport.DialKeepAlive),
TransportDialTimeout: config.TimeDurationVal(c.Vault.Transport.DialTimeout),
TransportDisableKeepAlives: config.BoolVal(c.Vault.Transport.DisableKeepAlives),
Expand Down