Skip to content
Closed
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
8 changes: 6 additions & 2 deletions elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ type Config struct {
Password string // Password for HTTP Basic Authentication.

CloudID string // Endpoint for the Elastic Service (https://elastic.co/cloud).
APIKey string // Base64-encoded token for authorization; if set, overrides username and password.

APIKey string // Base64-encoded token for authorization; if set, overrides username, password, and token
Token string // Token used for authorization. if set, overrides username and password.

Transport http.RoundTripper // The HTTP transport object.
Logger estransport.Logger // The logger object.
Expand Down Expand Up @@ -110,7 +112,9 @@ func NewClient(cfg Config) (*Client, error) {
URLs: urls,
Username: cfg.Username,
Password: cfg.Password,
APIKey: cfg.APIKey,

APIKey: cfg.APIKey,
Token: cfg.Token,

Transport: cfg.Transport,
Logger: cfg.Logger,
Expand Down
21 changes: 18 additions & 3 deletions estransport/estransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ type Config struct {
URLs []*url.URL
Username string
Password string
APIKey string

APIKey string
Token string

Transport http.RoundTripper
Logger Logger
Expand All @@ -52,7 +54,9 @@ type Client struct {
urls []*url.URL
username string
password string
apikey string

apikey string
token string

transport http.RoundTripper
selector Selector
Expand All @@ -72,7 +76,9 @@ func New(cfg Config) *Client {
urls: cfg.URLs,
username: cfg.Username,
password: cfg.Password,
apikey: cfg.APIKey,

apikey: cfg.APIKey,
token: cfg.Token,

transport: cfg.Transport,
selector: NewRoundRobinSelector(cfg.URLs...),
Expand Down Expand Up @@ -167,6 +173,15 @@ func (c *Client) setAuthorization(u *url.URL, req *http.Request) *http.Request {
return req
}

if c.token != "" {
var b bytes.Buffer
b.Grow(len("Bearer ") + len(c.apikey))
b.WriteString("Bearer ")
b.WriteString(c.token)
req.Header.Set("Authorization", b.String())
return req
}

if c.username != "" && c.password != "" {
req.SetBasicAuth(c.username, c.password)
return req
Expand Down