Skip to content

Commit

Permalink
patch to support VAULT_HTTP_PROXY variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Du Cros committed Sep 10, 2021
1 parent c6d178d commit f6a553a
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions api/client.go
Expand Up @@ -42,6 +42,7 @@ const (
EnvVaultToken = "VAULT_TOKEN"
EnvVaultMFA = "VAULT_MFA"
EnvRateLimit = "VAULT_RATE_LIMIT"
EnvHttpProxy = "VAULT_HTTP_PROXY"
)

// Deprecated values
Expand Down Expand Up @@ -79,6 +80,8 @@ type Config struct {
// (or http.DefaultClient).
HttpClient *http.Client

HttpProxy string

// MinRetryWait controls the minimum time to wait before retrying when a 5xx
// error occurs. Defaults to 1000 milliseconds.
MinRetryWait time.Duration
Expand Down Expand Up @@ -173,6 +176,25 @@ func DefaultConfig() *Config {
Backoff: retryablehttp.LinearJitterBackoff,
}

if err := config.ReadEnvironment(); err != nil {
config.Error = err
return config
}

if config.HttpProxy != "" {
url, err := url.Parse(config.HttpProxy)
if err != nil {
config.Error = err
return config
}

proxied_transport := cleanhttp.DefaultPooledTransport()
proxied_transport.Proxy = http.ProxyURL(url)
config.HttpClient = &http.Client{
Transport: proxied_transport,
}
}

transport := config.HttpClient.Transport.(*http.Transport)
transport.TLSHandshakeTimeout = 10 * time.Second
transport.TLSClientConfig = &tls.Config{
Expand All @@ -183,11 +205,6 @@ func DefaultConfig() *Config {
return config
}

if err := config.ReadEnvironment(); err != nil {
config.Error = err
return config
}

// Ensure redirects are not automatically followed
// Note that this is sane for the API client as it has its own
// redirect handling logic (and thus also for command/meta),
Expand Down Expand Up @@ -271,6 +288,7 @@ func (c *Config) ReadEnvironment() error {
var envMaxRetries *uint64
var envSRVLookup bool
var limit *rate.Limiter
var envHttpProxy string

// Parse the environment variables
if v := os.Getenv(EnvVaultAddress); v != "" {
Expand Down Expand Up @@ -339,6 +357,10 @@ func (c *Config) ReadEnvironment() error {
envTLSServerName = v
}

if v := os.Getenv(EnvHttpProxy); v != "" {
envHttpProxy = v
}

// Configure the HTTP clients TLS configuration.
t := &TLSConfig{
CACert: envCACert,
Expand Down Expand Up @@ -375,6 +397,10 @@ func (c *Config) ReadEnvironment() error {
c.Timeout = envClientTimeout
}

if envHttpProxy != "" {
c.HttpProxy = envHttpProxy
}

return nil
}

Expand Down

0 comments on commit f6a553a

Please sign in to comment.