Skip to content

Commit

Permalink
Support proxy environment variables in the influx client
Browse files Browse the repository at this point in the history
This also adds support for both the v1 and v2 APIs to set the Proxy
function on the underlying http.Transport.

The functionality for proxy environment variables is provided by the
[http.ProxyFromEnvironment](https://godoc.org/net/http#ProxyFromEnvironment).
  • Loading branch information
jsternberg committed Feb 12, 2018
1 parent e451c6c commit c67a054
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type Config struct {
Precision string
WriteConsistency string
UnsafeSsl bool
Proxy func(req *http.Request) (*url.URL, error)
}

// NewConfig will create a config to be used in connecting to the client
Expand Down Expand Up @@ -154,6 +155,7 @@ func NewClient(c Config) (*Client, error) {
}

tr := &http.Transport{
Proxy: c.Proxy,
TLSClientConfig: tlsConfig,
}

Expand Down
31 changes: 31 additions & 0 deletions client/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,3 +957,34 @@ func TestChunkedResponse(t *testing.T) {
t.Fatalf("unexpected response. expected %v, actual %v", nil, resp)
}
}

func TestClient_Proxy(t *testing.T) {
pinged := false
server := httptest.NewServer(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if got, want := req.URL.String(), "http://example.com:8086/ping"; got != want {
t.Errorf("invalid url in request: got=%s want=%s", got, want)
}
resp.WriteHeader(http.StatusNoContent)
pinged = true
}))
defer server.Close()

proxyURL, _ := url.Parse(server.URL)
c, err := client.NewClient(client.Config{
URL: url.URL{
Scheme: "http",
Host: "example.com:8086",
},
Proxy: http.ProxyURL(proxyURL),
})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if _, _, err := c.Ping(); err != nil {
t.Fatalf("could not ping server: %s", err)
}

if !pinged {
t.Fatalf("no http request was received")
}
}
4 changes: 4 additions & 0 deletions client/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type HTTPConfig struct {
// TLSConfig allows the user to set their own TLS config for the HTTP
// Client. If set, this option overrides InsecureSkipVerify.
TLSConfig *tls.Config

// Proxy configures the Proxy function on the HTTP client.
Proxy func(req *http.Request) (*url.URL, error)
}

// BatchPointsConfig is the config data needed to create an instance of the BatchPoints struct.
Expand Down Expand Up @@ -99,6 +102,7 @@ func NewHTTPClient(conf HTTPConfig) (Client, error) {
TLSClientConfig: &tls.Config{
InsecureSkipVerify: conf.InsecureSkipVerify,
},
Proxy: conf.Proxy,
}
if conf.TLSConfig != nil {
tr.TLSClientConfig = conf.TLSConfig
Expand Down
25 changes: 25 additions & 0 deletions client/v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,3 +859,28 @@ func TestClientConcatURLPath(t *testing.T) {
t.Errorf("unexpected error. expected %v, actual %v", nil, err)
}
}

func TestClientProxy(t *testing.T) {
pinged := false
ts := httptest.NewServer(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if got, want := req.URL.String(), "http://example.com:8086/ping"; got != want {
t.Errorf("invalid url in request: got=%s want=%s", got, want)
}
resp.WriteHeader(http.StatusNoContent)
pinged = true
}))
defer ts.Close()

proxyURL, _ := url.Parse(ts.URL)
c, _ := NewHTTPClient(HTTPConfig{
Addr: "http://example.com:8086",
Proxy: http.ProxyURL(proxyURL),
})
if _, _, err := c.Ping(0); err != nil {
t.Fatalf("could not ping server: %s", err)
}

if !pinged {
t.Fatalf("no http request was received")
}
}
2 changes: 2 additions & 0 deletions cmd/influx/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -333,6 +334,7 @@ func (c *CommandLine) Connect(cmd string) error {
ClientConfig := c.ClientConfig
ClientConfig.UserAgent = "InfluxDBShell/" + c.ClientVersion
ClientConfig.URL = URL
ClientConfig.Proxy = http.ProxyFromEnvironment

client, err := client.NewClient(ClientConfig)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions man/influx.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ OPTIONS
-version::
Outputs the version of the influx client.

ENVIRONMENT
-----------
The environment variables can be specified in lower case or upper case. The upper case version has precedence.

HTTP_PROXY [protocol://]<host>[:port]::
Sets the proxy server to use for HTTP.

HTTPS_PROXY [protocol://]<host>[:port]::
Sets the proxy server to use for HTTPS. Takes precedence over HTTP_PROXY for HTTPS.

NO_PROXY <comma-separated list of hosts>::
List of host names that shouldn't go through any proxy. If set to an asterisk \'*' only, it matches all hosts.

include::footer.txt[]

0 comments on commit c67a054

Please sign in to comment.