Skip to content

Commit

Permalink
Cherry-pick #3671 to 5.x: Fix elasticsearch url parsing (#3849)
Browse files Browse the repository at this point in the history
* Fix elasticsearch url parsing (#3671)

use regex to check if host given contains the protocol scheme. If scheme is
missing, prepend before attempting to parse the complete URL
(cherry picked from commit 1391731)
  • Loading branch information
monicasarbu authored and andrewkroh committed Mar 31, 2017
1 parent 36adeee commit 28f76b8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ https://github.com/elastic/beats/compare/v5.3.0...master[Check the HEAD diff]
*Affecting all Beats*

- Update index mappings to support future Elasticsearch 6.X. {pull}3778[3778]
- Fix potential elasticsearch output URL parsing error if protocol scheme is missing. {pull}3671[3671]

*Filebeat*

Expand Down
23 changes: 7 additions & 16 deletions libbeat/outputs/elasticsearch/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"fmt"
"net"
"net/url"
"regexp"
"strings"
)

var hasScheme = regexp.MustCompile(`^([a-z][a-z0-9+\-.]*)://`)

// Creates the url based on the url configuration.
// Adds missing parts with defaults (scheme, host, port)
func getURL(defaultScheme string, defaultPath string, rawURL string) (string, error) {
Expand All @@ -15,6 +18,10 @@ func getURL(defaultScheme string, defaultPath string, rawURL string) (string, er
defaultScheme = "http"
}

if !hasScheme.MatchString(rawURL) {
rawURL = fmt.Sprintf("%v://%v", defaultScheme, rawURL)
}

addr, err := url.Parse(rawURL)
if err != nil {
return "", err
Expand All @@ -24,22 +31,6 @@ func getURL(defaultScheme string, defaultPath string, rawURL string) (string, er
host := addr.Host
port := "9200"

// sanitize parse errors if url does not contain scheme
// if parse url looks funny, prepend schema and try again:
if addr.Scheme == "" || (addr.Host == "" && addr.Path == "" && addr.Opaque != "") {
rawURL = fmt.Sprintf("%v://%v", defaultScheme, rawURL)
if tmpAddr, err := url.Parse(rawURL); err == nil {
addr = tmpAddr
scheme = addr.Scheme
host = addr.Host
} else {
// If url doesn't have a scheme, host is written into path. For example: 192.168.3.7
scheme = defaultScheme
host = addr.Path
addr.Path = ""
}
}

if host == "" {
host = "localhost"
} else {
Expand Down
2 changes: 2 additions & 0 deletions libbeat/outputs/elasticsearch/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func TestGetUrl(t *testing.T) {
"http://localhost/": "http://localhost:9200/",

// no schema + hostname
"33f3600fd5c1bb599af557c36a4efb08.host": "http://33f3600fd5c1bb599af557c36a4efb08.host:9200",
"33f3600fd5c1bb599af557c36a4efb08.host:12345": "http://33f3600fd5c1bb599af557c36a4efb08.host:12345",
"localhost": "http://localhost:9200",
"localhost:80": "http://localhost:80",
"localhost:80/": "http://localhost:80/",
Expand Down

0 comments on commit 28f76b8

Please sign in to comment.