mirror of
https://github.com/prometheus/prometheus
synced 2024-12-25 07:52:28 +00:00
API: Fix global URL when external address has no port (#8359)
* API: Fix global URL when external address has no port Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu> * Update test Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu> * Handle IPv6 correctly. Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
This commit is contained in:
parent
b0642fb42f
commit
06e53b7ecf
@ -82,7 +82,7 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
LocalhostRepresentations = []string{"127.0.0.1", "localhost"}
|
||||
LocalhostRepresentations = []string{"127.0.0.1", "localhost", "::1"}
|
||||
remoteReadQueries = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: namespace,
|
||||
Subsystem: subsystem,
|
||||
@ -750,8 +750,24 @@ type GlobalURLOptions struct {
|
||||
Scheme string
|
||||
}
|
||||
|
||||
// sanitizeSplitHostPort acts like net.SplitHostPort.
|
||||
// Additionally, if there is no port in the host passed as input, we return the
|
||||
// original host, making sure that IPv6 addresses are not surrounded by square
|
||||
// brackets.
|
||||
func sanitizeSplitHostPort(input string) (string, string, error) {
|
||||
host, port, err := net.SplitHostPort(input)
|
||||
if err != nil && strings.HasSuffix(err.Error(), "missing port in address") {
|
||||
var errWithPort error
|
||||
host, _, errWithPort = net.SplitHostPort(input + ":80")
|
||||
if errWithPort == nil {
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
return host, port, err
|
||||
}
|
||||
|
||||
func getGlobalURL(u *url.URL, opts GlobalURLOptions) (*url.URL, error) {
|
||||
host, port, err := net.SplitHostPort(u.Host)
|
||||
host, port, err := sanitizeSplitHostPort(u.Host)
|
||||
if err != nil {
|
||||
return u, err
|
||||
}
|
||||
@ -778,11 +794,14 @@ func getGlobalURL(u *url.URL, opts GlobalURLOptions) (*url.URL, error) {
|
||||
// externally, so we replace only the hostname by the one in the
|
||||
// external URL. It could be the wrong hostname for the service on
|
||||
// this port, but it's still the best possible guess.
|
||||
host, _, err := net.SplitHostPort(opts.Host)
|
||||
host, _, err := sanitizeSplitHostPort(opts.Host)
|
||||
if err != nil {
|
||||
return u, err
|
||||
}
|
||||
u.Host = host + ":" + port
|
||||
u.Host = host
|
||||
if port != "" {
|
||||
u.Host = net.JoinHostPort(u.Host, port)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
@ -2926,3 +2926,111 @@ func BenchmarkRespond(b *testing.B) {
|
||||
api.respond(&testResponseWriter, response, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetGlobalURL(t *testing.T) {
|
||||
mustParseURL := func(t *testing.T, u string) *url.URL {
|
||||
parsed, err := url.Parse(u)
|
||||
require.NoError(t, err)
|
||||
return parsed
|
||||
}
|
||||
|
||||
testcases := []struct {
|
||||
input *url.URL
|
||||
opts GlobalURLOptions
|
||||
expected *url.URL
|
||||
errorful bool
|
||||
}{
|
||||
{
|
||||
mustParseURL(t, "http://127.0.0.1:9090"),
|
||||
GlobalURLOptions{
|
||||
ListenAddress: "127.0.0.1:9090",
|
||||
Host: "127.0.0.1:9090",
|
||||
Scheme: "http",
|
||||
},
|
||||
mustParseURL(t, "http://127.0.0.1:9090"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
mustParseURL(t, "http://127.0.0.1:9090"),
|
||||
GlobalURLOptions{
|
||||
ListenAddress: "127.0.0.1:9090",
|
||||
Host: "prometheus.io",
|
||||
Scheme: "https",
|
||||
},
|
||||
mustParseURL(t, "https://prometheus.io"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
mustParseURL(t, "http://exemple.com"),
|
||||
GlobalURLOptions{
|
||||
ListenAddress: "127.0.0.1:9090",
|
||||
Host: "prometheus.io",
|
||||
Scheme: "https",
|
||||
},
|
||||
mustParseURL(t, "http://exemple.com"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
mustParseURL(t, "http://localhost:8080"),
|
||||
GlobalURLOptions{
|
||||
ListenAddress: "127.0.0.1:9090",
|
||||
Host: "prometheus.io",
|
||||
Scheme: "https",
|
||||
},
|
||||
mustParseURL(t, "http://prometheus.io:8080"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
mustParseURL(t, "http://[::1]:8080"),
|
||||
GlobalURLOptions{
|
||||
ListenAddress: "127.0.0.1:9090",
|
||||
Host: "prometheus.io",
|
||||
Scheme: "https",
|
||||
},
|
||||
mustParseURL(t, "http://prometheus.io:8080"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
mustParseURL(t, "http://localhost"),
|
||||
GlobalURLOptions{
|
||||
ListenAddress: "127.0.0.1:9090",
|
||||
Host: "prometheus.io",
|
||||
Scheme: "https",
|
||||
},
|
||||
mustParseURL(t, "http://prometheus.io"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
mustParseURL(t, "http://localhost:9091"),
|
||||
GlobalURLOptions{
|
||||
ListenAddress: "[::1]:9090",
|
||||
Host: "[::1]",
|
||||
Scheme: "https",
|
||||
},
|
||||
mustParseURL(t, "http://[::1]:9091"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
mustParseURL(t, "http://localhost:9091"),
|
||||
GlobalURLOptions{
|
||||
ListenAddress: "[::1]:9090",
|
||||
Host: "[::1]:9090",
|
||||
Scheme: "https",
|
||||
},
|
||||
mustParseURL(t, "http://[::1]:9091"),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range testcases {
|
||||
t.Run(fmt.Sprintf("Test %d", i), func(t *testing.T) {
|
||||
output, err := getGlobalURL(tc.input, tc.opts)
|
||||
if tc.errorful {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expected, output)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user