2020-10-21 10:11:35 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2022-01-28 14:37:17 +00:00
|
|
|
"encoding/json"
|
2020-10-21 10:11:35 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-03-08 16:10:24 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-05-30 15:19:51 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2020-10-21 10:11:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigFileSettings(t *testing.T) {
|
|
|
|
var (
|
|
|
|
firstIngress = UnvalidatedIngressRule{
|
|
|
|
Hostname: "tunnel1.example.com",
|
|
|
|
Path: "/id",
|
|
|
|
Service: "https://localhost:8000",
|
|
|
|
}
|
|
|
|
secondIngress = UnvalidatedIngressRule{
|
|
|
|
Hostname: "*",
|
|
|
|
Path: "",
|
|
|
|
Service: "https://localhost:8001",
|
|
|
|
}
|
2021-01-17 20:22:53 +00:00
|
|
|
warpRouting = WarpRoutingConfig{
|
2022-06-13 16:44:27 +00:00
|
|
|
Enabled: true,
|
|
|
|
ConnectTimeout: &CustomDuration{Duration: 2 * time.Second},
|
|
|
|
TCPKeepAlive: &CustomDuration{Duration: 10 * time.Second},
|
2021-01-17 20:22:53 +00:00
|
|
|
}
|
2020-10-21 10:11:35 +00:00
|
|
|
)
|
|
|
|
rawYAML := `
|
|
|
|
tunnel: config-file-test
|
2022-01-28 14:37:17 +00:00
|
|
|
originRequest:
|
|
|
|
ipRules:
|
|
|
|
- prefix: "10.0.0.0/8"
|
|
|
|
ports:
|
|
|
|
- 80
|
|
|
|
- 8080
|
|
|
|
allow: false
|
|
|
|
- prefix: "fc00::/7"
|
|
|
|
ports:
|
|
|
|
- 443
|
|
|
|
- 4443
|
|
|
|
allow: true
|
2020-10-21 10:11:35 +00:00
|
|
|
ingress:
|
|
|
|
- hostname: tunnel1.example.com
|
|
|
|
path: /id
|
|
|
|
service: https://localhost:8000
|
|
|
|
- hostname: "*"
|
|
|
|
service: https://localhost:8001
|
2021-01-17 20:22:53 +00:00
|
|
|
warp-routing:
|
|
|
|
enabled: true
|
2022-06-13 16:44:27 +00:00
|
|
|
connectTimeout: 2s
|
|
|
|
tcpKeepAlive: 10s
|
|
|
|
|
2020-10-21 10:11:35 +00:00
|
|
|
retries: 5
|
|
|
|
grace-period: 30s
|
|
|
|
percentage: 3.14
|
|
|
|
hostname: example.com
|
|
|
|
tag:
|
|
|
|
- test
|
|
|
|
- central-1
|
|
|
|
counters:
|
|
|
|
- 123
|
|
|
|
- 456
|
|
|
|
`
|
|
|
|
var config configFileSettings
|
|
|
|
err := yaml.Unmarshal([]byte(rawYAML), &config)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "config-file-test", config.TunnelID)
|
|
|
|
assert.Equal(t, firstIngress, config.Ingress[0])
|
|
|
|
assert.Equal(t, secondIngress, config.Ingress[1])
|
2021-01-17 20:22:53 +00:00
|
|
|
assert.Equal(t, warpRouting, config.WarpRouting)
|
2022-01-28 14:37:17 +00:00
|
|
|
privateV4 := "10.0.0.0/8"
|
|
|
|
privateV6 := "fc00::/7"
|
|
|
|
ipRules := []IngressIPRule{
|
|
|
|
{
|
|
|
|
Prefix: &privateV4,
|
|
|
|
Ports: []int{80, 8080},
|
|
|
|
Allow: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Prefix: &privateV6,
|
|
|
|
Ports: []int{443, 4443},
|
|
|
|
Allow: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, ipRules, config.OriginRequest.IPRules)
|
2020-10-21 10:11:35 +00:00
|
|
|
|
|
|
|
retries, err := config.Int("retries")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 5, retries)
|
|
|
|
|
|
|
|
gracePeriod, err := config.Duration("grace-period")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, time.Second*30, gracePeriod)
|
|
|
|
|
|
|
|
percentage, err := config.Float64("percentage")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 3.14, percentage)
|
|
|
|
|
|
|
|
hostname, err := config.String("hostname")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "example.com", hostname)
|
|
|
|
|
|
|
|
tags, err := config.StringSlice("tag")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test", tags[0])
|
|
|
|
assert.Equal(t, "central-1", tags[1])
|
|
|
|
|
|
|
|
counters, err := config.IntSlice("counters")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 123, counters[0])
|
|
|
|
assert.Equal(t, 456, counters[1])
|
2021-01-17 20:22:53 +00:00
|
|
|
|
2020-10-21 10:11:35 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
|
2022-05-30 15:19:51 +00:00
|
|
|
var rawJsonConfig = []byte(`
|
2022-01-28 14:37:17 +00:00
|
|
|
{
|
2022-03-08 16:10:24 +00:00
|
|
|
"connectTimeout": 10,
|
|
|
|
"tlsTimeout": 30,
|
|
|
|
"tcpKeepAlive": 30,
|
2022-01-28 14:37:17 +00:00
|
|
|
"noHappyEyeballs": true,
|
2022-03-08 16:10:24 +00:00
|
|
|
"keepAliveTimeout": 60,
|
2022-01-28 14:37:17 +00:00
|
|
|
"keepAliveConnections": 10,
|
|
|
|
"httpHostHeader": "app.tunnel.com",
|
|
|
|
"originServerName": "app.tunnel.com",
|
|
|
|
"caPool": "/etc/capool",
|
|
|
|
"noTLSVerify": true,
|
|
|
|
"disableChunkedEncoding": true,
|
|
|
|
"bastionMode": true,
|
|
|
|
"proxyAddress": "127.0.0.3",
|
|
|
|
"proxyPort": 9000,
|
|
|
|
"proxyType": "socks",
|
|
|
|
"ipRules": [
|
|
|
|
{
|
|
|
|
"prefix": "10.0.0.0/8",
|
|
|
|
"ports": [80, 8080],
|
|
|
|
"allow": false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"prefix": "fc00::/7",
|
|
|
|
"ports": [443, 4443],
|
|
|
|
"allow": true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`)
|
2022-03-08 16:10:24 +00:00
|
|
|
|
|
|
|
func TestMarshalUnmarshalOriginRequest(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
marshalFunc func(in interface{}) (out []byte, err error)
|
|
|
|
unMarshalFunc func(in []byte, out interface{}) (err error)
|
|
|
|
}{
|
2022-05-30 15:19:51 +00:00
|
|
|
{"json", json.Marshal, json.Unmarshal},
|
|
|
|
{"yaml", yaml.Marshal, yaml.Unmarshal},
|
2022-03-08 16:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-05-30 15:19:51 +00:00
|
|
|
assertConfig(t, tc.marshalFunc, tc.unMarshalFunc)
|
2022-03-08 16:10:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertConfig(
|
|
|
|
t *testing.T,
|
|
|
|
marshalFunc func(in interface{}) (out []byte, err error),
|
|
|
|
unMarshalFunc func(in []byte, out interface{}) (err error),
|
|
|
|
) {
|
2022-01-28 14:37:17 +00:00
|
|
|
var config OriginRequestConfig
|
2022-03-08 16:10:24 +00:00
|
|
|
var config2 OriginRequestConfig
|
|
|
|
|
2022-05-30 15:19:51 +00:00
|
|
|
assert.NoError(t, json.Unmarshal(rawJsonConfig, &config))
|
2022-03-08 16:10:24 +00:00
|
|
|
|
2022-05-30 15:19:51 +00:00
|
|
|
assert.Equal(t, time.Second*10, config.ConnectTimeout.Duration)
|
|
|
|
assert.Equal(t, time.Second*30, config.TLSTimeout.Duration)
|
|
|
|
assert.Equal(t, time.Second*30, config.TCPKeepAlive.Duration)
|
2022-01-28 14:37:17 +00:00
|
|
|
assert.Equal(t, true, *config.NoHappyEyeballs)
|
2022-05-30 15:19:51 +00:00
|
|
|
assert.Equal(t, time.Second*60, config.KeepAliveTimeout.Duration)
|
2022-01-28 14:37:17 +00:00
|
|
|
assert.Equal(t, 10, *config.KeepAliveConnections)
|
|
|
|
assert.Equal(t, "app.tunnel.com", *config.HTTPHostHeader)
|
|
|
|
assert.Equal(t, "app.tunnel.com", *config.OriginServerName)
|
|
|
|
assert.Equal(t, "/etc/capool", *config.CAPool)
|
|
|
|
assert.Equal(t, true, *config.NoTLSVerify)
|
|
|
|
assert.Equal(t, true, *config.DisableChunkedEncoding)
|
|
|
|
assert.Equal(t, true, *config.BastionMode)
|
|
|
|
assert.Equal(t, "127.0.0.3", *config.ProxyAddress)
|
|
|
|
assert.Equal(t, true, *config.NoTLSVerify)
|
|
|
|
assert.Equal(t, uint(9000), *config.ProxyPort)
|
|
|
|
assert.Equal(t, "socks", *config.ProxyType)
|
|
|
|
|
|
|
|
privateV4 := "10.0.0.0/8"
|
|
|
|
privateV6 := "fc00::/7"
|
|
|
|
ipRules := []IngressIPRule{
|
|
|
|
{
|
|
|
|
Prefix: &privateV4,
|
|
|
|
Ports: []int{80, 8080},
|
|
|
|
Allow: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Prefix: &privateV6,
|
|
|
|
Ports: []int{443, 4443},
|
|
|
|
Allow: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, ipRules, config.IPRules)
|
2022-03-08 16:10:24 +00:00
|
|
|
|
|
|
|
// validate that serializing and deserializing again matches the deserialization from raw string
|
|
|
|
result, err := marshalFunc(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = unMarshalFunc(result, &config2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, config2, config)
|
2022-01-28 14:37:17 +00:00
|
|
|
}
|