From 71e3903b1ed63a3b64aa9ce119a67322546f2e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20P=C3=A9rez-Aradros=20Herce?= Date: Wed, 26 Jul 2017 18:13:48 +0200 Subject: [PATCH] Support `npipe` URLs in host parser (#4751) Docker for Windows uses URLs like `npipe://./pipe/docker_engine`. This change makes sure they are supported by our host validators (cherry picked from commit 5750cc0547303adceb6e434385f9ad8ecedcfb53) --- CHANGELOG.asciidoc | 2 ++ metricbeat/mb/parse/url.go | 7 ++++--- metricbeat/mb/parse/url_test.go | 12 ++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index ecbdddae722..cbb3b3cc118 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -34,6 +34,8 @@ https://github.com/elastic/beats/compare/v6.0.0-beta1...master[Check the HEAD di *Metricbeat* +- Support `npipe` protocol (Windows) in Docker module. {pull}4751[4751] + *Packetbeat* *Winlogbeat* diff --git a/metricbeat/mb/parse/url.go b/metricbeat/mb/parse/url.go index 6871c1f7d1f..b8f8f9dc40f 100644 --- a/metricbeat/mb/parse/url.go +++ b/metricbeat/mb/parse/url.go @@ -61,7 +61,8 @@ func (b URLHostParserBuilder) Build() mb.HostParser { // NewHostDataFromURL returns a new HostData based on the contents of the URL. // If the URLs scheme is "unix" or end is "unix" (e.g. "http+unix://") then -// the HostData.Host field is set to the URLs path instead of the URLs host. +// the HostData.Host field is set to the URLs path instead of the URLs host, +// the same happens for "npipe". func NewHostDataFromURL(u *url.URL) mb.HostData { var user, pass string if u.User != nil { @@ -70,7 +71,7 @@ func NewHostDataFromURL(u *url.URL) mb.HostData { } host := u.Host - if strings.HasSuffix(u.Scheme, "unix") { + if strings.HasSuffix(u.Scheme, "unix") || strings.HasSuffix(u.Scheme, "npipe") { host = u.Path } @@ -140,7 +141,7 @@ func getURL(rawURL, scheme, username, password, path, query string) (*url.URL, e SetURLUser(u, username, password) - if !strings.HasSuffix(u.Scheme, "unix") { + if !strings.HasSuffix(u.Scheme, "unix") && !strings.HasSuffix(u.Scheme, "npipe") { if u.Host == "" { return nil, fmt.Errorf("error parsing URL: empty host") } diff --git a/metricbeat/mb/parse/url_test.go b/metricbeat/mb/parse/url_test.go index 6571fb889d7..d585c53b8bf 100644 --- a/metricbeat/mb/parse/url_test.go +++ b/metricbeat/mb/parse/url_test.go @@ -41,6 +41,18 @@ func TestParseURL(t *testing.T) { } }) + t.Run("npipe", func(t *testing.T) { + rawURL := "npipe://./pipe/docker_engine" + hostData, err := ParseURL(rawURL, "tcp", "", "", "", "") + if assert.NoError(t, err) { + assert.Equal(t, "npipe://./pipe/docker_engine", hostData.URI) + assert.Equal(t, "npipe://./pipe/docker_engine", hostData.SanitizedURI) + assert.Equal(t, "/pipe/docker_engine", hostData.Host) + assert.Equal(t, "", hostData.User) + assert.Equal(t, "", hostData.Password) + } + }) + t.Run("set default user", func(t *testing.T) { rawURL := "http://:secret@localhost" h, err := ParseURL(rawURL, "https", "root", "passwd", "", "")