From 9be7ab877cfb6ccc067cb81ff13847d100371e7c Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 29 May 2020 15:33:38 +0200 Subject: [PATCH] [Ingest Manager] When not port are specified and the https is used fallback to 443 (#18844) [Ingest Manager] When not port are specified and the https is used fallback to 443 (#18844) --- x-pack/elastic-agent/CHANGELOG.asciidoc | 1 + x-pack/elastic-agent/pkg/kibana/client.go | 12 +++++-- .../elastic-agent/pkg/kibana/client_test.go | 34 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index eebffc211d2..a9917740ca8 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -65,3 +65,4 @@ - Enable debug log level for Metricbeat and Filebeat when run under the Elastic Agent. {pull}17935[17935] - Pick up version from libbeat {pull}18350[18350] - More clear output of inspect command {pull}18405[18405] +- When not port are specified and the https is used fallback to 443 {pull}18844[18844] diff --git a/x-pack/elastic-agent/pkg/kibana/client.go b/x-pack/elastic-agent/pkg/kibana/client.go index 9e7b7dfcd0d..0a94bca2f1d 100644 --- a/x-pack/elastic-agent/pkg/kibana/client.go +++ b/x-pack/elastic-agent/pkg/kibana/client.go @@ -21,7 +21,10 @@ import ( "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger" ) -const kibanaPort = 5601 +const ( + kibanaPort = 5601 + kibanaHTTPSPort = 443 +) type requestFunc func(string, string, url.Values, io.Reader) (*http.Request, error) type wrapperFunc func(rt http.RoundTripper) (http.RoundTripper, error) @@ -144,7 +147,12 @@ func NewWithConfig(log *logger.Logger, cfg *Config, wrapper wrapperFunc) (*Clien p = p + "/" } - kibanaURL, err := common.MakeURL(string(cfg.Protocol), p, cfg.Host, kibanaPort) + usedDefaultPort := kibanaPort + if cfg.Protocol == "https" { + usedDefaultPort = kibanaHTTPSPort + } + + kibanaURL, err := common.MakeURL(string(cfg.Protocol), p, cfg.Host, usedDefaultPort) if err != nil { return nil, errors.Wrap(err, "invalid Kibana endpoint") } diff --git a/x-pack/elastic-agent/pkg/kibana/client_test.go b/x-pack/elastic-agent/pkg/kibana/client_test.go index c8af7b7b151..c860d62d88d 100644 --- a/x-pack/elastic-agent/pkg/kibana/client_test.go +++ b/x-pack/elastic-agent/pkg/kibana/client_test.go @@ -11,6 +11,7 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "strings" "sync" "testing" @@ -32,6 +33,39 @@ func addCatchAll(mux *http.ServeMux, t *testing.T) *http.ServeMux { return mux } +func TestPortDefaults(t *testing.T) { + l, err := logger.New() + require.NoError(t, err) + + testCases := []struct { + Name string + URI string + ExpectedPort int + ExpectedScheme string + }{ + {"no scheme uri", "test.url", kibanaPort, "http"}, + {"default kibana port", "http://test.url", kibanaPort, "http"}, + {"specified kibana port", "http://test.url:123", 123, "http"}, + {"default kibana https port", "https://test.url", kibanaHTTPSPort, "https"}, + {"specified kibana https port", "https://test.url:123", 123, "https"}, + } + for _, tc := range testCases { + t.Run(tc.Name, func(t *testing.T) { + cfg, err := NewConfigFromURL(tc.URI) + require.NoError(t, err) + + c, err := NewWithConfig(l, cfg, nil) + require.NoError(t, err) + + r, err := c.request("GET", "/", nil, strings.NewReader("")) + require.NoError(t, err) + + assert.True(t, strings.HasSuffix(r.Host, fmt.Sprintf(":%d", tc.ExpectedPort))) + assert.Equal(t, tc.ExpectedScheme, r.URL.Scheme) + }) + } +} + // - Prefix. func TestHTTPClient(t *testing.T) { ctx := context.Background()