Support IPv6 service endpoints in trace plugin (#8410)

Use net.JoinHostPort rather than string concatenation to support
both ipv4, ipv6, and hostname service endpoints for the trace
plugin. Previously, ipv6 bind addresses in the coredns configuration
would fail to be parsed, as the ipv6 address was not surrounded in
brackets.

Signed-off-by: Michael Wolf <mwolf@cloudflare.com>

Closes #8409

Co-authored-by: Michael Wolf <mwolf@cloudflare.com>
This commit is contained in:
Michael Wolf
2026-08-05 21:10:34 -07:00
committed by GitHub
parent 419869ebac
commit c7e5424e7c
2 changed files with 56 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package trace
import ( import (
"fmt" "fmt"
"net"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -38,7 +39,7 @@ func traceParse(c *caddy.Controller) (*trace, error) {
cfg := dnsserver.GetConfig(c) cfg := dnsserver.GetConfig(c)
if len(cfg.ListenHosts) > 0 && cfg.ListenHosts[0] != "" { if len(cfg.ListenHosts) > 0 && cfg.ListenHosts[0] != "" {
tr.serviceEndpoint = cfg.ListenHosts[0] + ":" + cfg.Port tr.serviceEndpoint = net.JoinHostPort(cfg.ListenHosts[0], cfg.Port)
} }
for c.Next() { // trace for c.Next() { // trace

View File

@@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/coredns/caddy" "github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
) )
func TestTraceParse(t *testing.T) { func TestTraceParse(t *testing.T) {
@@ -87,3 +88,56 @@ func TestTraceParse(t *testing.T) {
} }
} }
} }
func TestParseServiceEndpoint(t *testing.T) {
tests := []struct {
name string
listenHosts []string
port string
expectedServiceEndpoint string
}{
{
name: "IPv4 address",
listenHosts: []string{"127.0.0.1"},
port: "8053",
expectedServiceEndpoint: "127.0.0.1:8053",
},
{
name: "IPv6 address",
listenHosts: []string{"3d47:98c0:b113::3"},
port: "8853",
expectedServiceEndpoint: "[3d47:98c0:b113::3]:8853",
},
{
name: "Hostname",
listenHosts: []string{"localhost"},
port: "8053",
expectedServiceEndpoint: "localhost:8053",
},
{
name: "Multiple addresses",
listenHosts: []string{"3d47:98c0:b113::3", "127.0.0.1"},
port: "8853",
expectedServiceEndpoint: "[3d47:98c0:b113::3]:8853",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
c := caddy.NewTestController("dns", "trace")
cfg := dnsserver.GetConfig(c)
cfg.ListenHosts = tc.listenHosts
cfg.Port = tc.port
tr, err := traceParse(c)
if err != nil {
t.Errorf("Error parsing test input: %s", err)
return
}
if tr.serviceEndpoint != tc.expectedServiceEndpoint {
t.Errorf("Expected serviceEndpoint %s, got %s", tc.expectedServiceEndpoint, tr.serviceEndpoint)
}
})
}
}