traefik/pkg/middlewares/tcp/ipallowlist/ip_allowlist_test.go

140 lines
2.8 KiB
Go
Raw Normal View History

2022-10-26 15:42:07 +00:00
package ipallowlist
2021-06-11 13:30:05 +00:00
import (
"context"
"io"
2021-06-11 13:30:05 +00:00
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2023-02-03 14:24:05 +00:00
"github.com/traefik/traefik/v3/pkg/config/dynamic"
"github.com/traefik/traefik/v3/pkg/tcp"
2021-06-11 13:30:05 +00:00
)
2022-10-26 15:16:05 +00:00
func TestNewIPAllowLister(t *testing.T) {
2021-06-11 13:30:05 +00:00
testCases := []struct {
desc string
2022-10-26 15:16:05 +00:00
allowList dynamic.TCPIPAllowList
2021-06-11 13:30:05 +00:00
expectedError bool
}{
{
desc: "Empty config",
2022-10-26 15:16:05 +00:00
allowList: dynamic.TCPIPAllowList{},
2021-06-11 13:30:05 +00:00
expectedError: true,
},
{
desc: "invalid IP",
2022-10-26 15:16:05 +00:00
allowList: dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"foo"},
},
expectedError: true,
},
{
desc: "valid IP",
2022-10-26 15:16:05 +00:00
allowList: dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"10.10.10.10"},
},
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
next := tcp.HandlerFunc(func(conn tcp.WriteCloser) {})
2022-10-26 15:16:05 +00:00
allowLister, err := New(context.Background(), next, test.allowList, "traefikTest")
2021-06-11 13:30:05 +00:00
if test.expectedError {
assert.Error(t, err)
} else {
require.NoError(t, err)
2022-10-26 15:16:05 +00:00
assert.NotNil(t, allowLister)
2021-06-11 13:30:05 +00:00
}
})
}
}
2022-10-26 15:16:05 +00:00
func TestIPAllowLister_ServeHTTP(t *testing.T) {
2021-06-11 13:30:05 +00:00
testCases := []struct {
desc string
2022-10-26 15:16:05 +00:00
allowList dynamic.TCPIPAllowList
2021-06-11 13:30:05 +00:00
remoteAddr string
expected string
}{
{
desc: "authorized with remote address",
2022-10-26 15:16:05 +00:00
allowList: dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"20.20.20.20"},
},
remoteAddr: "20.20.20.20:1234",
expected: "OK",
},
{
desc: "non authorized with remote address",
2022-10-26 15:16:05 +00:00
allowList: dynamic.TCPIPAllowList{
2021-06-11 13:30:05 +00:00
SourceRange: []string{"20.20.20.20"},
},
remoteAddr: "20.20.20.21:1234",
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
next := tcp.HandlerFunc(func(conn tcp.WriteCloser) {
write, err := conn.Write([]byte("OK"))
require.NoError(t, err)
assert.Equal(t, 2, write)
err = conn.Close()
require.NoError(t, err)
})
2022-10-26 15:16:05 +00:00
allowLister, err := New(context.Background(), next, test.allowList, "traefikTest")
2021-06-11 13:30:05 +00:00
require.NoError(t, err)
server, client := net.Pipe()
go func() {
2022-10-26 15:16:05 +00:00
allowLister.ServeTCP(&contextWriteCloser{client, addr{test.remoteAddr}})
2021-06-11 13:30:05 +00:00
}()
read, err := io.ReadAll(server)
2021-06-11 13:30:05 +00:00
require.NoError(t, err)
assert.Equal(t, test.expected, string(read))
})
}
}
type contextWriteCloser struct {
net.Conn
addr
}
type addr struct {
remoteAddr string
}
func (a addr) Network() string {
panic("implement me")
}
func (a addr) String() string {
return a.remoteAddr
}
func (c contextWriteCloser) CloseWrite() error {
panic("implement me")
}
func (c contextWriteCloser) RemoteAddr() net.Addr { return c.addr }
func (c contextWriteCloser) Context() context.Context {
return context.Background()
}