mirror of
https://github.com/coreos/prometheus-operator.git
synced 2026-02-05 06:45:27 +01:00
fix: support IPv6 addresses in prober URL validation (#8354)
* fix: support IPv6 addresses in prober URL validation Signed-off-by: Sanchit2662 <sanchit2662@gmail.com> * docs: add comment explaining govalidator usage over stdlib Signed-off-by: Sanchit2662 <sanchit2662@gmail.com> --------- Signed-off-by: Sanchit2662 <sanchit2662@gmail.com>
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/url"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -491,18 +492,29 @@ func (rs *ResourceSelector) checkProbe(ctx context.Context, probe *monitoringv1.
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateProberURL(url string) error {
|
||||
hostPort := strings.Split(url, ":")
|
||||
|
||||
if !govalidator.IsHost(hostPort[0]) {
|
||||
return fmt.Errorf("invalid host: %q", hostPort[0])
|
||||
// validateProberURL checks that the prober URL is a valid host or host:port.
|
||||
// We use govalidator.IsHost() because the standard library doesn't offer a
|
||||
// single function that validates a string as an IP (v4/v6) or DNS hostname.
|
||||
// Similarly, govalidator.IsPort() validates that a string is a numeric port
|
||||
// in the valid range (1-65535), which has no stdlib equivalent.
|
||||
func validateProberURL(proberURL string) error {
|
||||
// Try to parse as host:port (handles IPv6 in [bracket]:port format correctly)
|
||||
host, port, err := net.SplitHostPort(proberURL)
|
||||
if err != nil {
|
||||
// No port specified - validate the entire input as a host.
|
||||
// This handles bare hostnames, IPv4, and IPv6 addresses without ports.
|
||||
if !govalidator.IsHost(proberURL) {
|
||||
return fmt.Errorf("invalid host: %q", proberURL)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// handling cases with url specified as host:port
|
||||
if len(hostPort) > 1 {
|
||||
if !govalidator.IsPort(hostPort[1]) {
|
||||
return fmt.Errorf("invalid port: %q", hostPort[1])
|
||||
}
|
||||
// Validate the extracted host and port
|
||||
if !govalidator.IsHost(host) {
|
||||
return fmt.Errorf("invalid host: %q", host)
|
||||
}
|
||||
if !govalidator.IsPort(port) {
|
||||
return fmt.Errorf("invalid port: %q", port)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -114,6 +114,34 @@ func TestSelectProbes(t *testing.T) {
|
||||
},
|
||||
valid: true,
|
||||
},
|
||||
{
|
||||
scenario: "ipv6 address as prober url",
|
||||
updateSpec: func(ps *monitoringv1.ProbeSpec) {
|
||||
ps.ProberSpec.URL = "::1"
|
||||
},
|
||||
valid: true,
|
||||
},
|
||||
{
|
||||
scenario: "ipv6 full address as prober url",
|
||||
updateSpec: func(ps *monitoringv1.ProbeSpec) {
|
||||
ps.ProberSpec.URL = "2001:db8::1"
|
||||
},
|
||||
valid: true,
|
||||
},
|
||||
{
|
||||
scenario: "ipv6 address with port as prober url",
|
||||
updateSpec: func(ps *monitoringv1.ProbeSpec) {
|
||||
ps.ProberSpec.URL = "[::1]:9090"
|
||||
},
|
||||
valid: true,
|
||||
},
|
||||
{
|
||||
scenario: "ipv6 full address with port as prober url",
|
||||
updateSpec: func(ps *monitoringv1.ProbeSpec) {
|
||||
ps.ProberSpec.URL = "[2001:db8::1]:9090"
|
||||
},
|
||||
valid: true,
|
||||
},
|
||||
{
|
||||
scenario: "invalid proxyconfig due to invalid proxyurl",
|
||||
updateSpec: func(ps *monitoringv1.ProbeSpec) {
|
||||
|
||||
Reference in New Issue
Block a user