Files
mc-router/server/connector_test.go
T
Geoff Bourne af1e193958 Redo code cleanup (#429)
* Code cleanup of routes config loader and API server (#424)

(cherry picked from commit 1ee3eb4de3)

* Refactored server setup and run out of main (#425)

(cherry picked from commit 05c57c3b85)

* Code cleanup in and around connector (#427)

(cherry picked from commit b3e88db48c)

* Update away from deprecated k8s NewInformer

* Tidy up couple of k8s docs examples
2025-07-13 10:00:40 -05:00

80 lines
1.9 KiB
Go

package server
import (
"net"
"testing"
"github.com/pires/go-proxyproto"
"github.com/stretchr/testify/assert"
)
func TestTrustedProxyNetworkPolicy(t *testing.T) {
tests := []struct {
name string
trustedNets []string
upstreamIP string
expectedPolicy proxyproto.Policy
}{
{
name: "trusted IP",
trustedNets: []string{"10.0.0.0/8"},
upstreamIP: "10.0.0.1",
expectedPolicy: proxyproto.USE,
},
{
name: "untrusted IP",
trustedNets: []string{"10.0.0.0/8"},
upstreamIP: "192.168.1.1",
expectedPolicy: proxyproto.IGNORE,
},
{
name: "multiple trusted nets",
trustedNets: []string{"10.0.0.0/8", "172.16.0.0/12"},
upstreamIP: "172.16.0.1",
expectedPolicy: proxyproto.USE,
},
{
name: "no trusted nets",
trustedNets: []string{},
upstreamIP: "148.184.129.202",
expectedPolicy: proxyproto.USE,
},
{
name: "remote trusted IP",
trustedNets: []string{"203.0.113.0/24"},
upstreamIP: "203.0.113.10",
expectedPolicy: proxyproto.USE,
},
{
name: "remote untrusted IP",
trustedNets: []string{"203.0.113.0/24"},
upstreamIP: "198.51.100.1",
expectedPolicy: proxyproto.IGNORE,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
c := &Connector{
trustedProxyNets: parseTrustedProxyNets(test.trustedNets),
}
policy := c.createProxyProtoPolicy()
upstreamAddr := &net.TCPAddr{IP: net.ParseIP(test.upstreamIP)}
policyResult, _ := policy(proxyproto.ConnPolicyOptions{
Upstream: upstreamAddr,
})
assert.Equal(t, test.expectedPolicy, policyResult, "Unexpected policy result for %s", test.name)
})
}
}
func parseTrustedProxyNets(nets []string) []*net.IPNet {
parsedNets := make([]*net.IPNet, 0, len(nets))
for _, n := range nets {
_, ipNet, _ := net.ParseCIDR(n)
parsedNets = append(parsedNets, ipNet)
}
return parsedNets
}