cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2023.2.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

config/configuration_test.go

220lines · modecode

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