Skip to content

Commit

Permalink
[Ingest Manager] When not port are specified and the https is used fa…
Browse files Browse the repository at this point in the history
…llback to 443 (elastic#18844)

[Ingest Manager] When not port are specified and the https is used fallback to 443 (elastic#18844)
  • Loading branch information
michalpristas committed Jun 1, 2020
1 parent bcff902 commit 595e376
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@
- More clear output of inspect command {pull}18405[18405]
- Pick up version from libbeat {pull}18350[18350]
- Use shorter hash for application differentiator {pull}18770[18770]
- When not port are specified and the https is used fallback to 443 {pull}18844[18844]
12 changes: 10 additions & 2 deletions x-pack/elastic-agent/pkg/kibana/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
Expand Down
34 changes: 34 additions & 0 deletions x-pack/elastic-agent/pkg/kibana/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"

Expand All @@ -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()
Expand Down

0 comments on commit 595e376

Please sign in to comment.