Skip to content

Commit fc9009f

Browse files
authored
[ AGNTLOG-700 ] Honor bind_host on UDP log listeners (DataDog#54487)
### What does this PR do? UDP log listeners now bind to the address configured in `bind_host` instead of always listening on the wildcard address. TCP log listeners already honored the setting; the UDP listener built its listen address from the port alone and silently discarded `bind_host`. Sources that leave `bind_host` unset are unaffected and continue to listen on all interfaces. ### Motivation `bind_host` is documented as applying to both TCP and UDP log sources, so an operator restricting a UDP syslog listener to a specific management interface would reasonably expect the port to be reachable only there. In practice the socket was bound to the IPv6 wildcard address and accepted datagrams on every interface, which makes this a silent exposure rather than merely an ignored option. The release note is updated as if this was a new feature for TCP AND UDP, because the specific bind host functionality can be separated from the preview feature the option was previously soft-gated behind. ### Describe how you validated your changes `TestUDPBindHost` was added, mirroring the existing `TestTCPBindHost`, and confirmed to fail against the previous behavior — the listener reported `[::]:<port>` rather than the configured address. The full package suite passes via `dda inv test --targets=./pkg/logs/launchers/listener` (31 tests), alongside `dda inv linter.go --only-modified-packages`. ### Additional Notes No configuration migration is required: an empty `bind_host` still resolves to `:<port>`, preserving the all-interfaces default. Co-authored-by: ryan.hall <ryan.hall@datadoghq.com>
1 parent 29de702 commit fc9009f

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

pkg/logs/launchers/listener/udp.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package listener
88
import (
99
"fmt"
1010
"net"
11+
"strconv"
1112

1213
"github.com/DataDog/datadog-agent/pkg/util/log"
1314

@@ -110,7 +111,8 @@ func (l *UDPListener) resetTailer() {
110111
// newUDPConnection returns a new UDP connection,
111112
// returns an error if the creation failed.
112113
func (l *UDPListener) newUDPConnection() (*net.UDPConn, error) {
113-
udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", l.source.Config.Port))
114+
bindAddr := net.JoinHostPort(l.source.Config.BindHost, strconv.Itoa(l.source.Config.Port))
115+
udpAddr, err := net.ResolveUDPAddr("udp", bindAddr)
114116
if err != nil {
115117
return nil, err
116118
}

pkg/logs/launchers/listener/udp_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package listener
88
import (
99
"fmt"
1010
"net"
11+
"strings"
1112
"testing"
1213
"time"
1314

@@ -47,6 +48,31 @@ func TestUDPShouldReceiveMessage(t *testing.T) {
4748
listener.Stop()
4849
}
4950

51+
func TestUDPBindHost(t *testing.T) {
52+
pp := mock.NewMockProvider()
53+
msgChan := pp.NextPipelineChan()
54+
listener, err := NewUDPListener(pp, sources.NewLogSource("", &config.LogsConfig{
55+
Port: udpTestPort,
56+
BindHost: "127.0.0.1",
57+
}), 9000)
58+
require.NoError(t, err)
59+
listener.Start()
60+
require.NotNil(t, listener.Conn)
61+
62+
addr := listener.Conn.LocalAddr().String()
63+
assert.True(t, strings.HasPrefix(addr, "127.0.0.1:"), "expected 127.0.0.1 bind, got %s", addr)
64+
65+
conn, err := net.Dial("udp", addr)
66+
require.NoError(t, err)
67+
defer conn.Close()
68+
69+
fmt.Fprint(conn, "bound msg\n")
70+
msg := <-msgChan
71+
assert.Equal(t, "bound msg", string(msg.GetContent()))
72+
73+
listener.Stop()
74+
}
75+
5076
func TestUDPShouldStopWhenNotStarted(t *testing.T) {
5177
pp := mock.NewMockProvider()
5278
listener, err := NewUDPListener(pp, sources.NewLogSource("", &config.LogsConfig{Port: udpTestPort}), 9000)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Add new ``bind_host`` configuration option to TCP and UDP log listeners.

0 commit comments

Comments
 (0)