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 proxy environment variables in the influx client #9429

Merged
merged 1 commit into from
Mar 12, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[]