Skip to content

Commit 749e83c

Browse files
committed
Add configuration options for HTTP Basic Authentication
This patch adds two fields, `Username` and `Password`, to the client configuration, allowing to set a common set of credentials for the client. Related: #31, #32 Closes: #32 (cherry picked from commit ec8c147)
1 parent 6ccdb98 commit 749e83c

5 files changed

+54
-8
lines changed

doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ To configure the client, pass a Config object to the NewClient function:
1515
"http://localhost:9200",
1616
"http://localhost:9201",
1717
},
18+
Username: "foo",
19+
Password: "bar",
1820
Transport: &http.Transport{
1921
MaxIdleConnsPerHost: 10,
2022
ResponseHeaderTimeout: time.Second,

elasticsearch.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ const (
1919
// Config represents the client configuration.
2020
//
2121
type Config struct {
22-
Addresses []string // A list of Elasticsearch nodes to use.
22+
Addresses []string // A list of Elasticsearch nodes to use.
23+
Username string // Username for HTTP Basic Authentication.
24+
Password string // Password for HTTP Basic Authentication.
25+
2326
Transport http.RoundTripper // The HTTP transport object.
2427
}
2528

@@ -70,9 +73,15 @@ func NewClient(cfg Config) (*Client, error) {
7073
urls = append(urls, u)
7174
}
7275

73-
tran := estransport.New(estransport.Config{URLs: urls, Transport: cfg.Transport})
76+
tp := estransport.New(estransport.Config{
77+
URLs: urls,
78+
Username: cfg.Username,
79+
Password: cfg.Password,
80+
81+
Transport: cfg.Transport,
82+
})
7483

75-
return &Client{Transport: tran, API: esapi.New(tran)}, nil
84+
return &Client{Transport: tp, API: esapi.New(tp)}, nil
7685
}
7786

7887
// Perform delegates to Transport to execute a request and return a response.

elasticsearch_example_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ func ExampleNewClient() {
3737
Addresses: []string{
3838
"http://localhost:9200",
3939
},
40+
Username: "foo",
41+
Password: "bar",
4042
Transport: &http.Transport{
4143
MaxIdleConnsPerHost: 10,
4244
ResponseHeaderTimeout: time.Second,
4345
DialContext: (&net.Dialer{Timeout: time.Second}).DialContext,
4446
TLSClientConfig: &tls.Config{
45-
MinVersion: tls.VersionTLS11,
47+
MinVersion: tls.VersionTLS11,
4648
},
4749
},
4850
}

estransport/estransport.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ type Interface interface {
1616
// Config represents the configuration of HTTP client.
1717
//
1818
type Config struct {
19-
URLs []*url.URL
19+
URLs []*url.URL
20+
Username string
21+
Password string
22+
2023
Transport http.RoundTripper
2124
}
2225

2326
// Client represents the HTTP client.
2427
//
2528
type Client struct {
26-
urls []*url.URL
29+
urls []*url.URL
30+
username string
31+
password string
32+
2733
transport http.RoundTripper
2834
selector Selector
2935
}
@@ -39,7 +45,10 @@ func New(cfg Config) *Client {
3945
}
4046

4147
return &Client{
42-
urls: cfg.URLs,
48+
urls: cfg.URLs,
49+
username: cfg.Username,
50+
password: cfg.Password,
51+
4352
transport: cfg.Transport,
4453
selector: NewRoundRobinSelector(cfg.URLs...),
4554
}
@@ -89,6 +98,13 @@ func (c *Client) setBasicAuth(u *url.URL, req *http.Request) *http.Request {
8998
if u.User != nil {
9099
password, _ := u.User.Password()
91100
req.SetBasicAuth(u.User.Username(), password)
101+
return req
92102
}
103+
104+
if c.username != "" && c.password != "" {
105+
req.SetBasicAuth(c.username, c.password)
106+
return req
107+
}
108+
93109
return req
94110
}

estransport/estransport_internal_test.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestTransportPerform(t *testing.T) {
9191
}
9292
})
9393

94-
t.Run("Sets HTTP Basic Auth", func(t *testing.T) {
94+
t.Run("Sets HTTP Basic Auth from URL", func(t *testing.T) {
9595
u, _ := url.Parse("https://foo:bar@example.com")
9696
tp := New(Config{URLs: []*url.URL{u}})
9797

@@ -108,6 +108,23 @@ func TestTransportPerform(t *testing.T) {
108108
}
109109
})
110110

111+
t.Run("Sets HTTP Basic Auth from configuration", func(t *testing.T) {
112+
u, _ := url.Parse("http://example.com")
113+
tp := New(Config{URLs: []*url.URL{u}, Username: "foo", Password: "bar"})
114+
115+
req, _ := http.NewRequest("GET", "/", nil)
116+
tp.setBasicAuth(u, req)
117+
118+
username, password, ok := req.BasicAuth()
119+
if !ok {
120+
t.Errorf("Expected the request to have Basic Auth set")
121+
}
122+
123+
if username != "foo" || password != "bar" {
124+
t.Errorf("Unexpected values for username and password: %s:%s", username, password)
125+
}
126+
})
127+
111128
t.Run("Error No URL", func(t *testing.T) {
112129
tp := New(Config{
113130
URLs: []*url.URL{},

0 commit comments

Comments
 (0)