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

Encode API key as base64 in common code #18945

Merged
merged 9 commits into from
Jun 9, 2020
Next Next commit
Encode API key as base64 in common code
  • Loading branch information
ycombinator committed Jun 5, 2020
commit 559eb3ef20cd59ac7e260b4e00b9e0eb2379aedf
4 changes: 3 additions & 1 deletion libbeat/esleg/eslegclient/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package eslegclient

import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -436,7 +437,8 @@ func (conn *Connection) execHTTPRequest(req *http.Request) (int, []byte, error)
}

if conn.APIKey != "" {
req.Header.Add("Authorization", "ApiKey "+conn.APIKey)
encoded := base64.StdEncoding.EncodeToString([]byte(conn.APIKey))
req.Header.Add("Authorization", "ApiKey "+encoded)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to cache the result in a private variable like conn.encodedAPIKey ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would. No need to encode it each time we make a request to ES. Thanks for the suggestion, will implement!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c3e5b43.

}

for name, value := range conn.Headers {
Expand Down
3 changes: 1 addition & 2 deletions libbeat/outputs/elasticsearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package elasticsearch

import (
"context"
"encoding/base64"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -84,7 +83,7 @@ func NewClient(
URL: s.URL,
Username: s.Username,
Password: s.Password,
APIKey: base64.StdEncoding.EncodeToString([]byte(s.APIKey)),
APIKey: s.APIKey,
Headers: s.Headers,
TLS: s.TLS,
Kerberos: s.Kerberos,
Expand Down