Debian Codegraph @

internal/config/validate_test.go

raw
1 package config_test
2
3 import (
4 "html/template"
5 "net/url"
6 "testing"
7 "testing/fstest"
8
9 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config"
10 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types"
11 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/test/testsuite"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 )
15
16 func TestValidate(t *testing.T) {
17 t.Parallel()
18
19 for _, tc := range []struct {
20 name string
21 conf config.Config
22 err string
23 }{
24 {
25 "missing oauth2 issuer",
26 config.Config{},
27 "oauth2.issuer is required",
28 },
29 {
30 "missing oauth2 client id",
31 config.Config{
32 OAuth2: config.OAuth2{
33 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
34 },
35 },
36 "oauth2.client.id is required",
37 },
38 {
39 "missing http secret",
40 config.Config{
41 OAuth2: config.OAuth2{
42 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
43 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
44 },
45 },
46 "http.secret is required",
47 },
48 {
49 "missing oauth2 client private key and secret",
50 config.Config{
51 HTTP: config.HTTP{
52 BaseURL: types.URL{},
53 Secret: testsuite.Secret,
54 Template: config.Defaults.HTTP.Template,
55 },
56 OAuth2: config.OAuth2{
57 Client: config.OAuth2Client{ID: "ID"},
58 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
59 },
60 },
61 "one of oauth2.client.private-key or oauth2.client.secret is required",
62 },
63 {
64 "missing http base url",
65 config.Config{
66 HTTP: config.HTTP{
67 BaseURL: types.URL{},
68 Secret: testsuite.Secret,
69 Template: config.Defaults.HTTP.Template,
70 },
71 OAuth2: config.OAuth2{
72 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
73 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
74 },
75 },
76 "http.baseurl is required",
77 },
78 {
79 "invalid http template rendering",
80 config.Config{
81 HTTP: config.HTTP{
82 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
83 Secret: testsuite.Secret,
84 Template: types.Template{Template: template.Must(template.New("index.gohtml").Parse("{{ slice .invalid.error 1 2 }}"))},
85 },
86 OAuth2: config.OAuth2{
87 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
88 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
89 },
90 OpenVPN: config.OpenVPN{
91 Addr: types.URL{URL: &url.URL{Scheme: "tcp", Host: "127.0.0.1:9000"}},
92 },
93 },
94 "invalid rendering http.template: template: index.gohtml:1:3: executing \"index.gohtml\" at <slice .invalid.error 1 2>: error calling slice: slice of untyped nil",
95 },
96 {
97 "missing oauth2 issuer after http configured",
98 config.Config{
99 HTTP: config.HTTP{
100 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
101 Secret: testsuite.Secret,
102 Template: config.Defaults.HTTP.Template,
103 },
104 OAuth2: config.OAuth2{
105 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
106 Issuer: types.URL{},
107 },
108 },
109 "oauth2.issuer is required",
110 },
111 {
112 "missing openvpn address",
113 config.Config{
114 HTTP: config.HTTP{
115 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
116 Secret: testsuite.Secret,
117 Template: config.Defaults.HTTP.Template,
118 },
119 OAuth2: config.OAuth2{
120 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
121 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
122 },
123 },
124 "openvpn.addr is required",
125 },
126 {
127 "invalid http secret length",
128 config.Config{
129 HTTP: config.HTTP{
130 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
131 Secret: "invalid",
132 Template: config.Defaults.HTTP.Template,
133 },
134 OAuth2: config.OAuth2{
135 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
136 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
137 },
138 OpenVPN: config.OpenVPN{
139 Addr: types.URL{URL: &url.URL{Scheme: "tcp", Host: "127.0.0.1:9000"}},
140 },
141 },
142 "http.secret requires a length of 16, 24 or 32",
143 },
144 {
145 "invalid http base url scheme",
146 config.Config{
147 HTTP: config.HTTP{
148 BaseURL: types.URL{URL: &url.URL{Scheme: "invalid", Host: "localhost"}},
149 Secret: testsuite.Secret,
150 Template: config.Defaults.HTTP.Template,
151 },
152 OAuth2: config.OAuth2{
153 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
154 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
155 },
156 OpenVPN: config.OpenVPN{
157 Addr: types.URL{URL: &url.URL{Scheme: "tcp", Host: "127.0.0.1:9000"}},
158 },
159 },
160 "http.baseurl: invalid URL. only http:// or https:// scheme supported",
161 },
162 {
163 "invalid openvpn address scheme",
164 config.Config{
165 HTTP: config.HTTP{
166 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
167 Secret: testsuite.Secret,
168 Template: config.Defaults.HTTP.Template,
169 },
170 OAuth2: config.OAuth2{
171 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
172 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
173 },
174 OpenVPN: config.OpenVPN{
175 Addr: types.URL{URL: &url.URL{Scheme: "quic", Host: "127.0.0.1:9000"}},
176 },
177 },
178 "openvpn.addr: invalid URL. only tcp://addr or unix://addr scheme supported",
179 },
180 {
181 "invalid oauth2 refresh secret length",
182 config.Config{
183 HTTP: config.HTTP{
184 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
185 Secret: testsuite.Secret,
186 Template: config.Defaults.HTTP.Template,
187 },
188 OAuth2: config.OAuth2{
189 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
190 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
191 Refresh: config.OAuth2Refresh{
192 Enabled: true,
193 },
194 },
195 OpenVPN: config.OpenVPN{
196 Addr: types.URL{URL: &url.URL{Scheme: "tcp", Host: "127.0.0.1:9000"}},
197 },
198 },
199 "oauth2.refresh.secret requires a length of 16, 24 or 32",
200 },
201 {
202 "missing trusted proxies with proxy headers enabled",
203 func() config.Config {
204 conf := validConfig()
205 conf.HTTP.EnableProxyHeaders = true
206
207 return conf
208 }(),
209 "http.trusted-proxies is required when http.enable-proxy-headers is true",
210 },
211 {
212 "invalid trusted proxy cidr",
213 func() config.Config {
214 conf := validConfig()
215 conf.HTTP.EnableProxyHeaders = true
216 conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1"}
217
218 return conf
219 }(),
220 `http.trusted-proxies: invalid CIDR "127.0.0.1": netip.ParsePrefix("127.0.0.1"): no '/'`,
221 },
222 {
223 "valid trusted proxies",
224 func() config.Config {
225 conf := validConfig()
226 conf.HTTP.EnableProxyHeaders = true
227 conf.HTTP.TrustedProxies = types.StringSlice{"127.0.0.1/32", "2001:db8::/64"}
228
229 return conf
230 }(),
231 "",
232 },
233 {
234 "valid refresh discovery endpoint",
235 config.Config{
236 HTTP: config.HTTP{
237 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
238 Secret: testsuite.Secret,
239 Template: config.Defaults.HTTP.Template,
240 },
241 OAuth2: config.OAuth2{
242 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
243 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
244 Refresh: config.OAuth2Refresh{
245 Enabled: true,
246 Secret: testsuite.Secret,
247 },
248 Endpoints: config.OAuth2Endpoints{
249 Discovery: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
250 },
251 },
252 OpenVPN: config.OpenVPN{
253 Addr: types.URL{URL: &url.URL{Scheme: "tcp", Host: "127.0.0.1:9000"}},
254 },
255 },
256 "",
257 },
258 {
259 "userinfo conflicts with auth and token endpoints",
260 config.Config{
261 HTTP: config.HTTP{
262 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
263 Secret: testsuite.Secret,
264 Template: config.Defaults.HTTP.Template,
265 },
266 OAuth2: config.OAuth2{
267 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
268 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
269 Refresh: config.OAuth2Refresh{
270 Enabled: true,
271 Secret: testsuite.Secret,
272 },
273 UserInfo: true,
274 Endpoints: config.OAuth2Endpoints{
275 Discovery: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
276 Auth: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
277 Token: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
278 },
279 },
280 OpenVPN: config.OpenVPN{
281 Addr: types.URL{URL: &url.URL{Scheme: "tcp", Host: "127.0.0.1:9000"}},
282 },
283 },
284 "oauth2.userinfo: cannot be used if oauth2.endpoint.auth and oauth2.endpoint.token is set",
285 },
286 {
287 "missing client config expression",
288 func() config.Config {
289 conf := validConfig()
290 conf.OpenVPN.ClientConfig.Enabled = true
291 conf.OpenVPN.ClientConfig.Path = types.FS{FS: fstest.MapFS{}}
292
293 return conf
294 }(),
295 "openvpn.client-config.expression is required when openvpn.client-config.enabled is true",
296 },
297 {
298 "valid single active session configuration",
299 func() config.Config {
300 conf := validConfig()
301 conf.OpenVPN.EnforceUniqueUser = true
302 conf.OpenVPN.OverrideUsername = true
303
304 return conf
305 }(),
306 "",
307 },
308 {
309 "valid openvpn passthrough",
310 func() config.Config {
311 conf := validConfig()
312 conf.OpenVPN.Passthrough.Enabled = true
313
314 return conf
315 }(),
316 "",
317 },
318 } {
319 t.Run(tc.name, func(t *testing.T) {
320 t.Parallel()
321
322 err := config.Validate(config.ManagementClient, &tc.conf)
323 if tc.err == "" {
324 require.NoError(t, err)
325 } else {
326 require.Error(t, err)
327
328 if tc.err != "-" {
329 assert.EqualError(t, err, tc.err)
330 }
331 }
332 })
333 }
334 }
335
336 func validConfig() config.Config {
337 return config.Config{
338 HTTP: config.HTTP{
339 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
340 Secret: testsuite.Secret,
341 Template: config.Defaults.HTTP.Template,
342 },
343 OAuth2: config.OAuth2{
344 Client: config.OAuth2Client{ID: "ID", Secret: testsuite.Secret},
345 Issuer: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
346 },
347 OpenVPN: config.OpenVPN{
348 Addr: types.URL{URL: &url.URL{Scheme: "tcp", Host: "127.0.0.1:9000"}},
349 },
350 }
351 }
352
353 func TestValidateEnforceUniqueUserRequiresManagementClient(t *testing.T) {
354 t.Parallel()
355
356 conf := validConfig()
357 conf.OpenVPN.EnforceUniqueUser = true
358 conf.OpenVPN.OverrideUsername = true
359
360 err := config.Validate(config.Plugin, &conf)
361
362 require.EqualError(t, err, "openvpn.enforce-unique-user requires the OpenVPN management interface")
363 }