configuration_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package config
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. yaml "gopkg.in/yaml.v3"
  9. )
  10. func TestConfigFileSettings(t *testing.T) {
  11. var (
  12. firstIngress = UnvalidatedIngressRule{
  13. Hostname: "tunnel1.example.com",
  14. Path: "/id",
  15. Service: "https://localhost:8000",
  16. }
  17. secondIngress = UnvalidatedIngressRule{
  18. Hostname: "*",
  19. Path: "",
  20. Service: "https://localhost:8001",
  21. }
  22. warpRouting = WarpRoutingConfig{
  23. ConnectTimeout: &CustomDuration{Duration: 2 * time.Second},
  24. TCPKeepAlive: &CustomDuration{Duration: 10 * time.Second},
  25. }
  26. )
  27. rawYAML := `
  28. tunnel: config-file-test
  29. originRequest:
  30. ipRules:
  31. - prefix: "10.0.0.0/8"
  32. ports:
  33. - 80
  34. - 8080
  35. allow: false
  36. - prefix: "fc00::/7"
  37. ports:
  38. - 443
  39. - 4443
  40. allow: true
  41. ingress:
  42. - hostname: tunnel1.example.com
  43. path: /id
  44. service: https://localhost:8000
  45. - hostname: "*"
  46. service: https://localhost:8001
  47. warp-routing:
  48. enabled: true
  49. connectTimeout: 2s
  50. tcpKeepAlive: 10s
  51. retries: 5
  52. grace-period: 30s
  53. percentage: 3.14
  54. hostname: example.com
  55. tag:
  56. - test
  57. - central-1
  58. counters:
  59. - 123
  60. - 456
  61. `
  62. var config configFileSettings
  63. err := yaml.Unmarshal([]byte(rawYAML), &config)
  64. assert.NoError(t, err)
  65. assert.Equal(t, "config-file-test", config.TunnelID)
  66. assert.Equal(t, firstIngress, config.Ingress[0])
  67. assert.Equal(t, secondIngress, config.Ingress[1])
  68. assert.Equal(t, warpRouting, config.WarpRouting)
  69. privateV4 := "10.0.0.0/8"
  70. privateV6 := "fc00::/7"
  71. ipRules := []IngressIPRule{
  72. {
  73. Prefix: &privateV4,
  74. Ports: []int{80, 8080},
  75. Allow: false,
  76. },
  77. {
  78. Prefix: &privateV6,
  79. Ports: []int{443, 4443},
  80. Allow: true,
  81. },
  82. }
  83. assert.Equal(t, ipRules, config.OriginRequest.IPRules)
  84. retries, err := config.Int("retries")
  85. assert.NoError(t, err)
  86. assert.Equal(t, 5, retries)
  87. gracePeriod, err := config.Duration("grace-period")
  88. assert.NoError(t, err)
  89. assert.Equal(t, time.Second*30, gracePeriod)
  90. percentage, err := config.Float64("percentage")
  91. assert.NoError(t, err)
  92. assert.Equal(t, 3.14, percentage)
  93. hostname, err := config.String("hostname")
  94. assert.NoError(t, err)
  95. assert.Equal(t, "example.com", hostname)
  96. tags, err := config.StringSlice("tag")
  97. assert.NoError(t, err)
  98. assert.Equal(t, "test", tags[0])
  99. assert.Equal(t, "central-1", tags[1])
  100. counters, err := config.IntSlice("counters")
  101. assert.NoError(t, err)
  102. assert.Equal(t, 123, counters[0])
  103. assert.Equal(t, 456, counters[1])
  104. }
  105. var rawJsonConfig = []byte(`
  106. {
  107. "connectTimeout": 10,
  108. "tlsTimeout": 30,
  109. "tcpKeepAlive": 30,
  110. "noHappyEyeballs": true,
  111. "keepAliveTimeout": 60,
  112. "keepAliveConnections": 10,
  113. "httpHostHeader": "app.tunnel.com",
  114. "originServerName": "app.tunnel.com",
  115. "caPool": "/etc/capool",
  116. "noTLSVerify": true,
  117. "disableChunkedEncoding": true,
  118. "bastionMode": true,
  119. "proxyAddress": "127.0.0.3",
  120. "proxyPort": 9000,
  121. "proxyType": "socks",
  122. "ipRules": [
  123. {
  124. "prefix": "10.0.0.0/8",
  125. "ports": [80, 8080],
  126. "allow": false
  127. },
  128. {
  129. "prefix": "fc00::/7",
  130. "ports": [443, 4443],
  131. "allow": true
  132. }
  133. ],
  134. "http2Origin": true
  135. }
  136. `)
  137. func TestMarshalUnmarshalOriginRequest(t *testing.T) {
  138. testCases := []struct {
  139. name string
  140. marshalFunc func(in interface{}) (out []byte, err error)
  141. unMarshalFunc func(in []byte, out interface{}) (err error)
  142. }{
  143. {"json", json.Marshal, json.Unmarshal},
  144. {"yaml", yaml.Marshal, yaml.Unmarshal},
  145. }
  146. for _, tc := range testCases {
  147. t.Run(tc.name, func(t *testing.T) {
  148. assertConfig(t, tc.marshalFunc, tc.unMarshalFunc)
  149. })
  150. }
  151. }
  152. func assertConfig(
  153. t *testing.T,
  154. marshalFunc func(in interface{}) (out []byte, err error),
  155. unMarshalFunc func(in []byte, out interface{}) (err error),
  156. ) {
  157. var config OriginRequestConfig
  158. var config2 OriginRequestConfig
  159. assert.NoError(t, json.Unmarshal(rawJsonConfig, &config))
  160. assert.Equal(t, time.Second*10, config.ConnectTimeout.Duration)
  161. assert.Equal(t, time.Second*30, config.TLSTimeout.Duration)
  162. assert.Equal(t, time.Second*30, config.TCPKeepAlive.Duration)
  163. assert.Equal(t, true, *config.NoHappyEyeballs)
  164. assert.Equal(t, time.Second*60, config.KeepAliveTimeout.Duration)
  165. assert.Equal(t, 10, *config.KeepAliveConnections)
  166. assert.Equal(t, "app.tunnel.com", *config.HTTPHostHeader)
  167. assert.Equal(t, "app.tunnel.com", *config.OriginServerName)
  168. assert.Equal(t, "/etc/capool", *config.CAPool)
  169. assert.Equal(t, true, *config.NoTLSVerify)
  170. assert.Equal(t, true, *config.DisableChunkedEncoding)
  171. assert.Equal(t, true, *config.BastionMode)
  172. assert.Equal(t, "127.0.0.3", *config.ProxyAddress)
  173. assert.Equal(t, true, *config.NoTLSVerify)
  174. assert.Equal(t, uint(9000), *config.ProxyPort)
  175. assert.Equal(t, "socks", *config.ProxyType)
  176. assert.Equal(t, true, *config.Http2Origin)
  177. privateV4 := "10.0.0.0/8"
  178. privateV6 := "fc00::/7"
  179. ipRules := []IngressIPRule{
  180. {
  181. Prefix: &privateV4,
  182. Ports: []int{80, 8080},
  183. Allow: false,
  184. },
  185. {
  186. Prefix: &privateV6,
  187. Ports: []int{443, 4443},
  188. Allow: true,
  189. },
  190. }
  191. assert.Equal(t, ipRules, config.IPRules)
  192. // validate that serializing and deserializing again matches the deserialization from raw string
  193. result, err := marshalFunc(config)
  194. require.NoError(t, err)
  195. err = unMarshalFunc(result, &config2)
  196. require.NoError(t, err)
  197. require.Equal(t, config2, config)
  198. }