mirror of
https://github.com/coredns/coredns.git
synced 2026-10-17 15:55:47 -04:00
plugin/proxyproto: expose PPv2 TLVs to downstream plugins as metadata (#8641)
The proxyproto plugin parsed PROXY protocol v2 headers only to recover the real client address; the TLV vector was discarded, so downstream plugins had no way to read values such as the AWS VPC endpoint ID, SSL client CN, or vendor-specific custom TLVs. Add an opt-in `tlv_metadata` option. When set, the connection's raw PPv2 TLV vector is captured and the plugin, acting as a metadata.Provider, publishes it under the `proxyproto/tlv` label as a hex-encoded string. Consumers decode and split it with proxyproto.SplitTLVs. The option is off by default, so existing deployments skip the extra per-connection processing and behave exactly as before. PROXY protocol v1 connections, which carry no TLVs, are unaffected. Capture spans all transports: dns:// (UDP and TCP), tls:// (DoT), https:// (DoH), grpc://, quic:// (DoQ), and https3:// (DoH3). QUIC-based transports require udp_session_tracking, since the PPv2 header arrives as a standalone first datagram ahead of the QUIC handshake. Includes unit tests for capture, the metadata provider (direct and through the real metadata chain), v1 compatibility, and option parsing, plus README and man page updates. Fixes #8633 Signed-off-by: Mekias Yohannes <mekias@amazon.com> Co-authored-by: Mekias Yohannes <mekias@amazon.com>
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
||||
"github.com/coredns/coredns/plugin/pkg/doh"
|
||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||
cproxyproto "github.com/coredns/coredns/plugin/pkg/proxyproto"
|
||||
"github.com/coredns/coredns/plugin/pkg/response"
|
||||
"github.com/coredns/coredns/plugin/pkg/reuseport"
|
||||
"github.com/coredns/coredns/plugin/pkg/transport"
|
||||
@@ -56,6 +57,10 @@ func (l *loggerAdapter) Write(p []byte) (n int, err error) {
|
||||
// Plugins can access the original HTTP request to retrieve headers, client IP, and metadata.
|
||||
type HTTPRequestKey struct{}
|
||||
|
||||
// connContextKey is the context key under which the accepted net.Conn is stashed
|
||||
// via http.Server.ConnContext, so ServeHTTP can recover PPv2 TLVs from it.
|
||||
type connContextKey struct{}
|
||||
|
||||
// NewServerHTTPS returns a new CoreDNS HTTPS server and compiles all plugins in to it.
|
||||
func NewServerHTTPS(addr string, group []*Config) (*ServerHTTPS, error) {
|
||||
tlsConfig, err := sharedTLSConfig(addr, group)
|
||||
@@ -129,6 +134,14 @@ func NewServerHTTPS(addr string, group []*Config) (*ServerHTTPS, error) {
|
||||
}
|
||||
sh.httpsServer.Handler = sh
|
||||
|
||||
// When PPv2 TLV metadata is enabled, stash the accepted connection in the
|
||||
// request context so ServeHTTP can recover the TLVs captured by the listener.
|
||||
if s.proxyProtoTLVMetadata {
|
||||
sh.httpsServer.ConnContext = func(ctx context.Context, c net.Conn) context.Context {
|
||||
return context.WithValue(ctx, connContextKey{}, c)
|
||||
}
|
||||
}
|
||||
|
||||
return sh, nil
|
||||
}
|
||||
|
||||
@@ -163,7 +176,11 @@ func (s *ServerHTTPS) Listen() (net.Listener, error) {
|
||||
return nil, err
|
||||
}
|
||||
if s.connPolicy != nil {
|
||||
l = &proxyproto.Listener{Listener: l, ConnPolicy: s.connPolicy}
|
||||
if s.proxyProtoTLVMetadata {
|
||||
l = cproxyproto.NewTLVListener(l, s.connPolicy)
|
||||
} else {
|
||||
l = &proxyproto.Listener{Listener: l, ConnPolicy: s.connPolicy}
|
||||
}
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
@@ -228,6 +245,14 @@ func (s *ServerHTTPS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
request: r,
|
||||
}
|
||||
|
||||
// If the listener captured PPv2 TLVs, surface them via RemoteAddr so the
|
||||
// proxyproto metadata provider exposes them, same as the other transports.
|
||||
if conn, ok := r.Context().Value(connContextKey{}).(net.Conn); ok {
|
||||
if a := cproxyproto.AddrFromConn(conn); a != nil {
|
||||
dw.raddr = a
|
||||
}
|
||||
}
|
||||
|
||||
if tsig := msg.IsTsig(); tsig != nil {
|
||||
if s.TsigSecret == nil {
|
||||
dw.tsigStatus = dns.ErrSecret
|
||||
|
||||
Reference in New Issue
Block a user