2017-09-15 18:56:04 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_parseEntryPointsConfiguration(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
expectedResult map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "all parameters",
|
2017-10-16 10:46:03 +00:00
|
|
|
value: "Name:foo TLS:goo TLS CA:car Redirect.EntryPoint:RedirectEntryPoint Redirect.Regex:RedirectRegex Redirect.Replacement:RedirectReplacement Compress:true WhiteListSourceRange:WhiteListSourceRange ProxyProtocol.TrustedIPs:192.168.0.1 ProxyProtocol.Insecure:false Address::8000",
|
2017-09-15 18:56:04 +00:00
|
|
|
expectedResult: map[string]string{
|
2017-10-13 13:04:02 +00:00
|
|
|
"name": "foo",
|
|
|
|
"address": ":8000",
|
|
|
|
"ca": "car",
|
|
|
|
"tls": "goo",
|
|
|
|
"tls_acme": "TLS",
|
|
|
|
"redirect_entrypoint": "RedirectEntryPoint",
|
|
|
|
"redirect_regex": "RedirectRegex",
|
|
|
|
"redirect_replacement": "RedirectReplacement",
|
|
|
|
"whitelistsourcerange": "WhiteListSourceRange",
|
|
|
|
"proxyprotocol_trustedips": "192.168.0.1",
|
2017-10-16 10:46:03 +00:00
|
|
|
"proxyprotocol_insecure": "false",
|
2017-10-13 13:04:02 +00:00
|
|
|
"compress": "true",
|
2017-09-15 18:56:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "compress on",
|
2017-10-13 13:04:02 +00:00
|
|
|
value: "name:foo Compress:on",
|
2017-09-15 18:56:04 +00:00
|
|
|
expectedResult: map[string]string{
|
2017-10-13 13:04:02 +00:00
|
|
|
"name": "foo",
|
|
|
|
"compress": "on",
|
2017-09-15 18:56:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "TLS",
|
|
|
|
value: "Name:foo TLS:goo TLS",
|
|
|
|
expectedResult: map[string]string{
|
2017-10-13 13:04:02 +00:00
|
|
|
"name": "foo",
|
|
|
|
"tls": "goo",
|
|
|
|
"tls_acme": "TLS",
|
2017-09-15 18:56:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-10-13 13:04:02 +00:00
|
|
|
conf := parseEntryPointsConfiguration(test.value)
|
2017-09-15 18:56:04 +00:00
|
|
|
|
|
|
|
assert.Len(t, conf, len(test.expectedResult))
|
|
|
|
assert.Equal(t, test.expectedResult, conf)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_toBool(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
key string
|
|
|
|
expectedBool bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "on",
|
|
|
|
value: "on",
|
|
|
|
key: "foo",
|
|
|
|
expectedBool: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "true",
|
|
|
|
value: "true",
|
|
|
|
key: "foo",
|
|
|
|
expectedBool: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "enable",
|
|
|
|
value: "enable",
|
|
|
|
key: "foo",
|
|
|
|
expectedBool: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "arbitrary string",
|
|
|
|
value: "bar",
|
|
|
|
key: "foo",
|
|
|
|
expectedBool: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no existing entry",
|
|
|
|
value: "bar",
|
|
|
|
key: "fii",
|
|
|
|
expectedBool: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
conf := map[string]string{
|
|
|
|
"foo": test.value,
|
|
|
|
}
|
|
|
|
|
|
|
|
result := toBool(conf, test.key)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expectedBool, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEntryPoints_Set(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
expression string
|
|
|
|
expectedEntryPointName string
|
|
|
|
expectedEntryPoint *EntryPoint
|
|
|
|
}{
|
|
|
|
{
|
2017-10-13 13:04:02 +00:00
|
|
|
name: "all parameters camelcase",
|
2017-10-16 10:46:03 +00:00
|
|
|
expression: "Name:foo Address::8000 TLS:goo,gii TLS CA:car Redirect.EntryPoint:RedirectEntryPoint Redirect.Regex:RedirectRegex Redirect.Replacement:RedirectReplacement Compress:true WhiteListSourceRange:Range ProxyProtocol.TrustedIPs:192.168.0.1 ForwardedHeaders.TrustedIPs:10.0.0.3/24,20.0.0.3/24",
|
2017-10-13 13:04:02 +00:00
|
|
|
expectedEntryPointName: "foo",
|
|
|
|
expectedEntryPoint: &EntryPoint{
|
|
|
|
Address: ":8000",
|
|
|
|
Redirect: &Redirect{
|
|
|
|
EntryPoint: "RedirectEntryPoint",
|
|
|
|
Regex: "RedirectRegex",
|
|
|
|
Replacement: "RedirectReplacement",
|
|
|
|
},
|
|
|
|
Compress: true,
|
|
|
|
ProxyProtocol: &ProxyProtocol{
|
|
|
|
TrustedIPs: []string{"192.168.0.1"},
|
|
|
|
},
|
2017-10-16 10:46:03 +00:00
|
|
|
ForwardedHeaders: &ForwardedHeaders{
|
|
|
|
TrustedIPs: []string{"10.0.0.3/24", "20.0.0.3/24"},
|
|
|
|
},
|
2017-10-13 13:04:02 +00:00
|
|
|
WhitelistSourceRange: []string{"Range"},
|
|
|
|
TLS: &TLS{
|
|
|
|
ClientCAFiles: []string{"car"},
|
|
|
|
Certificates: Certificates{
|
|
|
|
{
|
|
|
|
CertFile: FileOrContent("goo"),
|
|
|
|
KeyFile: FileOrContent("gii"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all parameters lowercase",
|
2017-10-16 10:46:03 +00:00
|
|
|
expression: "name:foo address::8000 tls:goo,gii tls ca:car redirect.entryPoint:RedirectEntryPoint redirect.regex:RedirectRegex redirect.replacement:RedirectReplacement compress:true whiteListSourceRange:Range proxyProtocol.trustedIPs:192.168.0.1 forwardedHeaders.trustedIPs:10.0.0.3/24,20.0.0.3/24",
|
2017-09-15 18:56:04 +00:00
|
|
|
expectedEntryPointName: "foo",
|
|
|
|
expectedEntryPoint: &EntryPoint{
|
2017-10-13 13:04:02 +00:00
|
|
|
Address: ":8000",
|
2017-09-15 18:56:04 +00:00
|
|
|
Redirect: &Redirect{
|
|
|
|
EntryPoint: "RedirectEntryPoint",
|
|
|
|
Regex: "RedirectRegex",
|
|
|
|
Replacement: "RedirectReplacement",
|
|
|
|
},
|
2017-10-10 12:50:03 +00:00
|
|
|
Compress: true,
|
|
|
|
ProxyProtocol: &ProxyProtocol{
|
|
|
|
TrustedIPs: []string{"192.168.0.1"},
|
|
|
|
},
|
2017-10-16 10:46:03 +00:00
|
|
|
ForwardedHeaders: &ForwardedHeaders{
|
|
|
|
TrustedIPs: []string{"10.0.0.3/24", "20.0.0.3/24"},
|
|
|
|
},
|
2017-09-15 18:56:04 +00:00
|
|
|
WhitelistSourceRange: []string{"Range"},
|
|
|
|
TLS: &TLS{
|
|
|
|
ClientCAFiles: []string{"car"},
|
|
|
|
Certificates: Certificates{
|
|
|
|
{
|
|
|
|
CertFile: FileOrContent("goo"),
|
|
|
|
KeyFile: FileOrContent("gii"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-16 10:46:03 +00:00
|
|
|
{
|
|
|
|
name: "default",
|
|
|
|
expression: "Name:foo",
|
|
|
|
expectedEntryPointName: "foo",
|
|
|
|
expectedEntryPoint: &EntryPoint{
|
|
|
|
WhitelistSourceRange: []string{},
|
|
|
|
ForwardedHeaders: &ForwardedHeaders{Insecure: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ForwardedHeaders insecure true",
|
|
|
|
expression: "Name:foo ForwardedHeaders.Insecure:true",
|
|
|
|
expectedEntryPointName: "foo",
|
|
|
|
expectedEntryPoint: &EntryPoint{
|
|
|
|
WhitelistSourceRange: []string{},
|
|
|
|
ForwardedHeaders: &ForwardedHeaders{Insecure: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ForwardedHeaders insecure false",
|
|
|
|
expression: "Name:foo ForwardedHeaders.Insecure:false",
|
|
|
|
expectedEntryPointName: "foo",
|
|
|
|
expectedEntryPoint: &EntryPoint{
|
|
|
|
WhitelistSourceRange: []string{},
|
|
|
|
ForwardedHeaders: &ForwardedHeaders{Insecure: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ForwardedHeaders TrustedIPs",
|
|
|
|
expression: "Name:foo ForwardedHeaders.TrustedIPs:10.0.0.3/24,20.0.0.3/24",
|
|
|
|
expectedEntryPointName: "foo",
|
|
|
|
expectedEntryPoint: &EntryPoint{
|
|
|
|
WhitelistSourceRange: []string{},
|
|
|
|
ForwardedHeaders: &ForwardedHeaders{
|
|
|
|
TrustedIPs: []string{"10.0.0.3/24", "20.0.0.3/24"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ProxyProtocol insecure true",
|
|
|
|
expression: "Name:foo ProxyProtocol.Insecure:true",
|
|
|
|
expectedEntryPointName: "foo",
|
|
|
|
expectedEntryPoint: &EntryPoint{
|
|
|
|
WhitelistSourceRange: []string{},
|
|
|
|
ForwardedHeaders: &ForwardedHeaders{Insecure: true},
|
|
|
|
ProxyProtocol: &ProxyProtocol{Insecure: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ProxyProtocol insecure false",
|
|
|
|
expression: "Name:foo ProxyProtocol.Insecure:false",
|
|
|
|
expectedEntryPointName: "foo",
|
|
|
|
expectedEntryPoint: &EntryPoint{
|
|
|
|
WhitelistSourceRange: []string{},
|
|
|
|
ForwardedHeaders: &ForwardedHeaders{Insecure: true},
|
|
|
|
ProxyProtocol: &ProxyProtocol{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ProxyProtocol TrustedIPs",
|
|
|
|
expression: "Name:foo ProxyProtocol.TrustedIPs:10.0.0.3/24,20.0.0.3/24",
|
|
|
|
expectedEntryPointName: "foo",
|
|
|
|
expectedEntryPoint: &EntryPoint{
|
|
|
|
WhitelistSourceRange: []string{},
|
|
|
|
ForwardedHeaders: &ForwardedHeaders{Insecure: true},
|
|
|
|
ProxyProtocol: &ProxyProtocol{
|
|
|
|
TrustedIPs: []string{"10.0.0.3/24", "20.0.0.3/24"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-09-15 18:56:04 +00:00
|
|
|
{
|
|
|
|
name: "compress on",
|
|
|
|
expression: "Name:foo Compress:on",
|
|
|
|
expectedEntryPointName: "foo",
|
|
|
|
expectedEntryPoint: &EntryPoint{
|
|
|
|
Compress: true,
|
|
|
|
WhitelistSourceRange: []string{},
|
2017-10-16 10:46:03 +00:00
|
|
|
ForwardedHeaders: &ForwardedHeaders{Insecure: true},
|
2017-09-15 18:56:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "compress true",
|
|
|
|
expression: "Name:foo Compress:true",
|
|
|
|
expectedEntryPointName: "foo",
|
|
|
|
expectedEntryPoint: &EntryPoint{
|
|
|
|
Compress: true,
|
|
|
|
WhitelistSourceRange: []string{},
|
2017-10-16 10:46:03 +00:00
|
|
|
ForwardedHeaders: &ForwardedHeaders{Insecure: true},
|
2017-09-15 18:56:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
eps := EntryPoints{}
|
|
|
|
err := eps.Set(test.expression)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ep := eps[test.expectedEntryPointName]
|
|
|
|
assert.EqualValues(t, test.expectedEntryPoint, ep)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|