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
Prev Previous commit
Compute entire API key auth header value up front
  • Loading branch information
ycombinator committed Jun 5, 2020
commit 9982b8d6ae7350a647c58a9946f20528d699ab87
12 changes: 6 additions & 6 deletions libbeat/esleg/eslegclient/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ type Connection struct {
Encoder BodyEncoder
HTTP esHTTPClient

encodedAPIKey string // Base64-encoded API key
version common.Version
log *logp.Logger
apiKeyAuthHeader string // Authorization HTTP request header with base64-encoded API key
version common.Version
log *logp.Logger
}

// ConnectionSettings are the settings needed for a Connection
Expand Down Expand Up @@ -167,7 +167,7 @@ func NewConnection(s ConnectionSettings) (*Connection, error) {
}

if s.APIKey != "" {
conn.encodedAPIKey = base64.StdEncoding.EncodeToString([]byte(s.APIKey))
conn.apiKeyAuthHeader = "ApiKey " + base64.StdEncoding.EncodeToString([]byte(s.APIKey))
}

return &conn, nil
Expand Down Expand Up @@ -443,8 +443,8 @@ func (conn *Connection) execHTTPRequest(req *http.Request) (int, []byte, error)
req.SetBasicAuth(conn.Username, conn.Password)
}

if conn.encodedAPIKey != "" {
req.Header.Add("Authorization", "ApiKey "+conn.encodedAPIKey)
if conn.apiKeyAuthHeader != "" {
req.Header.Add("Authorization", conn.apiKeyAuthHeader)
}

for name, value := range conn.Headers {
Expand Down