Debian Codegraph @

internal/openvpn/main_test.go

raw
1 package openvpn_test
2
3 import (
4 "context"
5 "errors"
6 "io"
7 "net/url"
8 "os"
9 "regexp"
10 "strconv"
11 "strings"
12 "testing"
13 "time"
14
15 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config"
16 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types"
17 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/openvpn"
18 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/openvpn/connection"
19 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/state"
20 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/test/testsuite"
21 "github.com/stretchr/testify/require"
22 )
23
24 func TestClientInvalidServer(t *testing.T) {
25 t.Parallel()
26
27 ctx, cancel := context.WithCancel(t.Context())
28 t.Cleanup(cancel)
29
30 conf := config.Config{
31 HTTP: config.HTTP{
32 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
33 Secret: testsuite.Secret,
34 },
35 OpenVPN: config.OpenVPN{
36 Addr: types.URL{URL: &url.URL{Scheme: "tcp", Host: "127.0.0.1:1"}},
37 Bypass: config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)},
38 },
39 }
40
41 suite := testsuite.New(&conf)
42 _, openVPNClient := suite.SetupOpenVPNOAuth2Clients(ctx, t, nil)
43
44 err := openVPNClient.Connect(t.Context())
45 require.Error(t, err)
46 require.Contains(t, err.Error(), "unable to connect to openvpn management interface tcp://127.0.0.1:1: dial tcp 127.0.0.1:1: connect")
47 }
48
49 func TestClientFull(t *testing.T) {
50 t.Parallel()
51
52 for _, tc := range []struct {
53 name string
54 conf config.Config
55 client string
56 expect string
57 err error
58 }{
59 {
60 name: "without password",
61 conf: func() config.Config {
62 conf := config.Defaults
63 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
64 conf.HTTP.Secret = testsuite.Secret
65 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
66 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)}
67 conf.OAuth2.Validate.Expression = "true"
68
69 return conf
70 }(),
71 client: ">CLIENT:CONNECT,1,2\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=test\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END\r\n",
72 expect: "client-pending-auth 1 2 \"WEB_AUTH::",
73 },
74 {
75 name: "with http ip check and privacy logging disabled",
76 conf: func() config.Config {
77 conf := config.Defaults
78 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
79 conf.HTTP.Secret = testsuite.Secret
80 conf.HTTP.Check.IPAddr = true
81 conf.Log.VPNClientIP = false
82 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
83 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)}
84
85 return conf
86 }(),
87 client: ">CLIENT:CONNECT,1,2\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=test\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END\r\n",
88 expect: "client-pending-auth 1 2 \"WEB_AUTH::",
89 },
90 {
91 "with password",
92 func() config.Config {
93 conf := config.Defaults
94 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
95 conf.HTTP.Secret = testsuite.Secret
96 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
97 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)}
98 conf.OpenVPN.Password = testsuite.Password
99 conf.OAuth2.Validate.Expression = "true"
100
101 return conf
102 }(),
103 ">CLIENT:CONNECT,1,2\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=test\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END\r\n",
104 "client-pending-auth 1 2 \"WEB_AUTH::",
105 nil,
106 },
107 {
108 name: "with username",
109 conf: func() config.Config {
110 conf := config.Defaults
111 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
112 conf.HTTP.Secret = testsuite.Secret
113 conf.OpenVPN.CommonName.EnvironmentVariableName = "username"
114 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)}
115 conf.OAuth2.Validate.Expression = "true"
116
117 return conf
118 }(),
119 client: ">CLIENT:CONNECT,1,2\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,username=test\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END\r\n",
120 expect: "client-pending-auth 1 2 \"WEB_AUTH::",
121 },
122 {
123 name: "with invalid state",
124 conf: func() config.Config {
125 conf := config.Defaults
126 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
127 conf.HTTP.Secret = "012345678910111"
128 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
129 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)}
130 conf.OpenVPN.Password = testsuite.Password
131
132 return conf
133 }(),
134 client: ">CLIENT:CONNECT,1,2\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=test\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END\r\n",
135 expect: "",
136 err: nil,
137 },
138 {
139 name: "client without IV_SSO",
140 conf: func() config.Config {
141 conf := config.Defaults
142 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
143 conf.HTTP.Secret = testsuite.Secret
144 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
145 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)}
146 conf.OpenVPN.Password = testsuite.Password
147
148 return conf
149 }(),
150 client: ">CLIENT:CONNECT,0,1\r\n>CLIENT:ENV,daemon=0\r\n>CLIENT:ENV,END\r\n",
151 expect: "client-deny 0 1 \"OpenVPN Client does not support SSO authentication via webauth\"",
152 err: nil,
153 },
154 {
155 "to long base url",
156 func() config.Config {
157 conf := config.Defaults
158 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost", Path: strings.Repeat("a", 1024)}}
159 conf.HTTP.Secret = testsuite.Secret
160 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
161 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)}
162 conf.OpenVPN.Password = testsuite.Password
163
164 return conf
165 }(),
166 ">CLIENT:CONNECT,1,2\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=test\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END\r\n",
167 `client-deny 1 2 "internal error"`,
168 nil,
169 },
170 {
171 "client bypass",
172 func() config.Config {
173 conf := config.Defaults
174 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
175 conf.HTTP.Secret = testsuite.Secret
176 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
177 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: types.RegexpSlice{regexp.MustCompile(`^bypass$`)}}
178 conf.OpenVPN.Password = testsuite.Password
179 conf.OpenVPN.AuthTokenUser = false
180
181 return conf
182 }(),
183 ">CLIENT:CONNECT,0,1\r\n>CLIENT:ENV,common_name=bypass\r\n>CLIENT:ENV,END\r\n",
184 "client-auth-nt 0 1",
185 nil,
186 },
187 {
188 name: "client password mask",
189 conf: func() config.Config {
190 conf := config.Defaults
191 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
192 conf.HTTP.Secret = testsuite.Secret
193 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
194 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: types.RegexpSlice{regexp.MustCompile(`^bypass$`)}}
195 conf.OpenVPN.Password = testsuite.Password
196 conf.OpenVPN.AuthTokenUser = false
197
198 return conf
199 }(),
200 client: ">CLIENT:CONNECT,0,1\r\n>CLIENT:ENV,common_name=bypass\r\nCLIENT:ENV,password=important value\n>CLIENT:ENV,END\r\n",
201 expect: "client-auth-nt 0 1",
202 },
203 {
204 "client established",
205 func() config.Config {
206 conf := config.Defaults
207 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
208 conf.HTTP.Secret = testsuite.Secret
209 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
210 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: types.RegexpSlice{regexp.MustCompile(`^bypass$`)}}
211 conf.OpenVPN.Password = testsuite.Password
212
213 return conf
214 }(),
215 ">CLIENT:ESTABLISHED,0\r\n>CLIENT:ENV,common_name=bypass\r\n>CLIENT:ENV,END\r\n",
216 "",
217 nil,
218 },
219 {
220 "client disconnected",
221 func() config.Config {
222 conf := config.Defaults
223 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
224 conf.HTTP.Secret = testsuite.Secret
225 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
226 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: types.RegexpSlice{regexp.MustCompile(`^bypass$`)}}
227 conf.OpenVPN.Password = testsuite.Password
228
229 return conf
230 }(),
231 ">CLIENT:DISCONNECT,0\r\n>CLIENT:ENV,common_name=bypass\r\n>CLIENT:ENV,END\r\n",
232 "",
233 nil,
234 },
235 {
236 "client invalid reason 1",
237 func() config.Config {
238 conf := config.Defaults
239 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
240 conf.HTTP.Secret = testsuite.Secret
241 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
242 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: types.RegexpSlice{regexp.MustCompile(`^bypass$`)}}
243 conf.OpenVPN.Password = testsuite.Password
244
245 return conf
246 }(),
247 ">CLIENT:FOO,0\r\n>CLIENT:ENV,common_name=bypass\r\n>CLIENT:ENV,END\r\n",
248 "",
249 connection.ErrParseErrorClientReason,
250 },
251 {
252 "client invalid reason 2",
253
254 func() config.Config {
255 conf := config.Defaults
256 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
257 conf.HTTP.Secret = testsuite.Secret
258 conf.OpenVPN.CommonName.EnvironmentVariableName = config.CommonName
259 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: types.RegexpSlice{regexp.MustCompile(`^bypass$`)}}
260 conf.OpenVPN.Password = testsuite.Password
261
262 return conf
263 }(),
264 ">CLIENT:CONNECT1,0,1\r\n>CLIENT:ENV,common_name=bypass\r\n>CLIENT:ENV,END\r\n",
265 "",
266 openvpn.ErrUnknownClientReason,
267 },
268 } {
269 t.Run(tc.name, func(t *testing.T) {
270 t.Parallel()
271
272 ctx, cancel := context.WithCancel(t.Context())
273 t.Cleanup(cancel)
274
275 suite := testsuite.New(&tc.conf)
276 errOpenVPNClientCh := suite.SetupManagementEnvironment(ctx, t, nil)
277 t.Cleanup(func() {
278 require.NoError(t, suite.GetManagementInterfaceConn().Close())
279
280 select {
281 case err := <-errOpenVPNClientCh:
282 if err != nil && !errors.Is(err, io.EOF) {
283 if tc.err != nil {
284 require.ErrorIs(t, err, tc.err)
285 } else {
286 require.NoError(t, err)
287 }
288 }
289 case <-time.After(1 * time.Second):
290 t.Fatalf("timeout waiting for connection to close. Logs:\n\n%s", suite.Logs())
291 }
292 })
293
294 if tc.conf.OpenVPN.Password != "" {
295 suite.SendAndExpectMessage(t, "ENTER PASSWORD:", tc.conf.OpenVPN.Password.String())
296 suite.SendMessagef(t, "SUCCESS: password is correct")
297 }
298
299 suite.ExpectVersionAndReleaseHold(t)
300 suite.SendMessagef(t, tc.client)
301
302 if tc.err != nil {
303 _, _ = suite.GetManagementInterfaceConnReader().ReadString('\n')
304
305 return
306 } else if tc.expect == "" {
307 return
308 }
309
310 auth := suite.ReadLine(t)
311
312 if strings.Contains(tc.expect, "WEB_AUTH") {
313 require.Contains(t, auth, tc.expect)
314 } else {
315 require.Equal(t, tc.expect, auth, suite.Logs())
316 }
317
318 if strings.Contains(tc.client, "CLIENT:ENV,password=") {
319 require.Contains(t, suite.Logs(), `CLIENT:ENV,password=***`, suite.Logs())
320 }
321
322 suite.SendMessagef(t, "SUCCESS: %s command succeeded\r\n", strings.SplitN(auth, " ", 2)[0])
323
324 if strings.Contains(auth, "client-deny") {
325 suite.SendMessagef(t, ">CLIENT:DISCONNECT,0\r\n>CLIENT:ENV,END")
326 } else if strings.Contains(auth, "WEB_AUTH::") {
327 matches := regexp.MustCompile(`state=(.+)"`).FindStringSubmatch(auth)
328 require.Len(t, matches, 2)
329
330 sessionState, err := state.Decrypt(testsuite.NewCipher(t), matches[1])
331 require.NoError(t, err)
332
333 require.Equal(t, uint64(1), sessionState.Client.CID)
334 require.Equal(t, uint64(2), sessionState.Client.KID)
335 require.Equal(t, "test", sessionState.Client.CommonName)
336 require.Equal(t, "127.0.0.1", sessionState.IPAddr)
337 }
338 })
339 }
340 }
341
342 func TestClientInvalidPassword(t *testing.T) {
343 t.Parallel()
344
345 ctx, cancel := context.WithCancel(t.Context())
346 t.Cleanup(cancel)
347
348 conf := config.Defaults
349 conf.OpenVPN.Password = "invalid"
350
351 suite := testsuite.New(&conf)
352 errOpenVPNClientCh := suite.SetupMockEnvironment(ctx, t, nil)
353 suite.SendMessagef(t, "ENTER PASSWORD:")
354 suite.ExpectMessage(t, conf.OpenVPN.Password.String())
355 suite.SendMessagef(t, "ERROR: bad password")
356
357 select {
358 case err := <-errOpenVPNClientCh:
359 require.ErrorIs(t, err, openvpn.ErrInvalidPassword)
360 case <-time.After(1 * time.Second):
361 t.Fatal("timeout waiting for connection to close")
362 }
363 }
364
365 func TestClientInvalidVersion(t *testing.T) {
366 t.Parallel()
367
368 for _, tc := range []struct {
369 name string
370 version string
371 err error
372 }{
373 {
374 name: "invalid parts",
375 version: "OpenVPN Version: OpenVPN Mock\r\nEND\r\n",
376 err: openvpn.ErrUnexpectedResponseFromVersionCommand,
377 },
378 {
379 name: "empty version line",
380 version: "OpenVPN Version: OpenVPN Mock\r\n\r\nEND\r\n",
381 err: openvpn.ErrUnexpectedResponseFromVersionCommand,
382 },
383 {
384 name: "invalid version",
385 version: "OpenVPN Version: OpenVPN Mock\r\nManagement Version: invalid\r\nEND\r\n",
386 err: strconv.ErrSyntax,
387 },
388 {
389 name: "version too low",
390 version: "OpenVPN Version: OpenVPN Mock\r\nManagement Version: 4\r\nEND\r\n",
391 err: openvpn.ErrRequireManagementInterfaceVersion5,
392 },
393 } {
394 t.Run(tc.name, func(t *testing.T) {
395 t.Parallel()
396
397 ctx, cancel := context.WithCancel(t.Context())
398 t.Cleanup(cancel)
399
400 suite := testsuite.New(new(config.Defaults))
401 errOpenVPNClientCh := suite.SetupMockEnvironment(ctx, t, nil)
402 suite.SendMessagef(t, openvpn.WelcomeBanner)
403 suite.ExpectMessage(t, "version")
404 suite.SendMessagef(t, tc.version)
405
406 select {
407 case err := <-errOpenVPNClientCh:
408 require.ErrorIs(t, err, tc.err)
409 case <-time.After(1 * time.Second):
410 t.Fatalf("timeout waiting for connection to close. Logs:\n\n%s", suite.Logs())
411 }
412 })
413 }
414 }
415
416 func TestClientMultiDigitManagementVersion(t *testing.T) {
417 t.Parallel()
418
419 ctx, cancel := context.WithCancel(t.Context())
420 t.Cleanup(cancel)
421
422 suite := testsuite.New(new(config.Defaults))
423 errOpenVPNClientCh := suite.SetupMockEnvironment(ctx, t, nil)
424 suite.SendMessagef(t, openvpn.WelcomeBanner)
425 suite.ExpectMessage(t, "version")
426 suite.SendMessagef(
427 t,
428 "OpenVPN Version: OpenVPN Mock\r\nManagement Version: 10\r\nEND\r\n",
429 )
430 require.NoError(t, suite.GetManagementInterfaceConn().Close())
431
432 select {
433 case err := <-errOpenVPNClientCh:
434 require.NoError(t, err)
435 case <-time.After(time.Second):
436 t.Fatalf("timeout waiting for connection to close. Logs:\n\n%s", suite.Logs())
437 }
438 }
439
440 func TestClientEnforceUniqueUserRejectsPluginShim(t *testing.T) {
441 t.Parallel()
442
443 ctx, cancel := context.WithCancel(t.Context())
444 t.Cleanup(cancel)
445
446 conf := config.Defaults
447 conf.OpenVPN.EnforceUniqueUser = true
448 conf.OpenVPN.OverrideUsername = true
449
450 suite := testsuite.New(&conf)
451 errOpenVPNClientCh := suite.SetupMockEnvironment(ctx, t, nil)
452 suite.SendMessagef(t, openvpn.WelcomeBanner)
453 suite.ExpectMessage(t, "version")
454 suite.SendMessagef(
455 t,
456 "OpenVPN Version: openvpn-auth-oauth2 2.0.0\r\nManagement Version: 5\r\nEND",
457 )
458
459 select {
460 case err := <-errOpenVPNClientCh:
461 require.ErrorIs(t, err, openvpn.ErrEnforceUniqueUserUnsupported)
462 case <-time.After(time.Second):
463 t.Fatalf("timeout waiting for connection to close. Logs:\n\n%s", suite.Logs())
464 }
465 }
466
467 func TestHoldRelease(t *testing.T) {
468 t.Parallel()
469
470 ctx, cancel := context.WithCancel(t.Context())
471 t.Cleanup(cancel)
472
473 conf := config.Config{
474 HTTP: config.HTTP{
475 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
476 Secret: testsuite.Secret,
477 },
478 OpenVPN: config.OpenVPN{
479 Bypass: config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)},
480 },
481 }
482
483 suite := testsuite.New(&conf)
484 errOpenVPNClientCh := suite.SetupManagementEnvironment(ctx, t, nil)
485
486 suite.ExpectVersionAndReleaseHold(t)
487
488 for range 10 {
489 suite.SendAndExpectMessage(t, ">HOLD:Waiting for hold release:0", "hold release")
490 }
491
492 require.NoError(t, suite.GetManagementInterfaceConn().Close())
493
494 select {
495 case err := <-errOpenVPNClientCh:
496 require.NoError(t, err)
497 case <-time.After(1 * time.Second):
498 t.Fatal("timeout waiting for connection to close")
499 }
500 }
501
502 func TestCommandTimeout(t *testing.T) {
503 t.Parallel()
504
505 ctx, cancel := context.WithCancel(t.Context())
506 t.Cleanup(cancel)
507
508 conf := config.Config{
509 HTTP: config.HTTP{
510 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
511 Secret: testsuite.Secret,
512 },
513 OpenVPN: config.OpenVPN{
514 Bypass: config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)},
515 CommandTimeout: time.Millisecond * 300,
516 },
517 }
518
519 suite := testsuite.New(&conf)
520 errOpenVPNClientCh := suite.SetupManagementEnvironment(ctx, t, nil)
521 openVPNClient := suite.GetOpenVPNClient()
522
523 t.Cleanup(func() {
524 openVPNClient.Shutdown(t.Context())
525
526 select {
527 case err := <-errOpenVPNClientCh:
528 require.NoError(t, err, suite.Logs())
529 case <-time.After(1 * time.Second):
530 t.Fatal("timeout waiting for connection to close")
531 }
532 })
533
534 suite.ExpectVersionAndReleaseHold(t)
535
536 defer func() {
537 suite.ExpectMessage(t, "help")
538 suite.SendMessagef(t, "")
539 }()
540
541 _, err := openVPNClient.SendCommandf(t.Context(), "help")
542 require.ErrorIs(t, err, openvpn.ErrTimeout)
543 }
544
545 func TestCommandErrorResponse(t *testing.T) {
546 t.Parallel()
547
548 ctx, cancel := context.WithCancel(t.Context())
549 t.Cleanup(cancel)
550
551 conf := config.Defaults
552 suite := testsuite.New(&conf)
553 errOpenVPNClientCh := suite.SetupManagementEnvironment(ctx, t, nil)
554 openVPNClient := suite.GetOpenVPNClient()
555
556 t.Cleanup(func() {
557 openVPNClient.Shutdown(t.Context())
558
559 select {
560 case err := <-errOpenVPNClientCh:
561 require.NoError(t, err, suite.Logs())
562 case <-time.After(time.Second):
563 t.Fatal("timeout waiting for connection to close")
564 }
565 })
566
567 suite.ExpectVersionAndReleaseHold(t)
568
569 type commandResult struct {
570 err error
571 response string
572 }
573
574 resultCh := make(chan commandResult, 1)
575
576 go func() {
577 response, err := openVPNClient.SendCommandf(t.Context(), "help")
578 resultCh <- commandResult{response: response, err: err}
579 }()
580
581 suite.ExpectMessage(t, "help")
582 suite.SendMessagef(t, "ERROR: help command failed")
583
584 result := <-resultCh
585 require.Equal(t, "ERROR: help command failed\r\n", result.response)
586 require.ErrorIs(t, result.err, openvpn.ErrErrorResponse)
587
588 commandErr, ok := errors.AsType[*openvpn.ManagementCommandError](result.err)
589 require.True(t, ok)
590 require.Equal(t, "help", commandErr.Command)
591 require.Equal(t, "ERROR: help command failed", commandErr.Response)
592 }
593
594 func TestSendCommandSerializesConcurrentCallers(t *testing.T) {
595 t.Parallel()
596
597 ctx, cancel := context.WithCancel(t.Context())
598 t.Cleanup(cancel)
599
600 conf := config.Config{
601 HTTP: config.HTTP{
602 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
603 Secret: testsuite.Secret,
604 },
605 OpenVPN: config.OpenVPN{
606 Bypass: config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)},
607 CommandTimeout: time.Second,
608 },
609 }
610
611 suite := testsuite.New(&conf)
612 errOpenVPNClientCh := suite.SetupManagementEnvironment(ctx, t, nil)
613 openVPNClient := suite.GetOpenVPNClient()
614 managementInterfaceConn := suite.GetManagementInterfaceConn()
615 reader := suite.GetManagementInterfaceConnReader()
616
617 t.Cleanup(func() {
618 openVPNClient.Shutdown(t.Context())
619
620 select {
621 case err := <-errOpenVPNClientCh:
622 require.NoError(t, err, suite.Logs())
623 case <-time.After(time.Second):
624 t.Fatal("timeout waiting for connection to close")
625 }
626 })
627
628 suite.ExpectVersionAndReleaseHold(t)
629
630 firstErrCh := make(chan error, 1)
631 secondErrCh := make(chan error, 1)
632
633 go func() {
634 _, err := openVPNClient.SendCommandf(t.Context(), "first")
635 firstErrCh <- err
636 }()
637
638 suite.ExpectMessage(t, "first")
639
640 go func() {
641 _, err := openVPNClient.SendCommandf(t.Context(), "second")
642 secondErrCh <- err
643 }()
644
645 require.NoError(t, managementInterfaceConn.SetReadDeadline(time.Now().Add(100*time.Millisecond)))
646
647 _, err := reader.ReadString('\n')
648 require.ErrorIs(t, err, os.ErrDeadlineExceeded)
649
650 suite.SendMessagef(t, "SUCCESS: first command succeeded")
651
652 select {
653 case err := <-firstErrCh:
654 require.NoError(t, err)
655 case <-time.After(time.Second):
656 t.Fatal("timeout waiting for first command")
657 }
658
659 suite.ExpectMessage(t, "second")
660 suite.SendMessagef(t, "SUCCESS: second command succeeded")
661
662 select {
663 case err := <-secondErrCh:
664 require.NoError(t, err)
665 case <-time.After(time.Second):
666 t.Fatal("timeout waiting for second command")
667 }
668 }
669
670 func TestDeadLocks(t *testing.T) {
671 t.Parallel()
672
673 for _, tc := range []struct {
674 name string
675 message string
676 }{
677 {
678 name: "explicit-exit-notify",
679 message: ">NOTIFY:info,remote-exit,EXIT",
680 },
681 {
682 name: "empty-lines",
683 message: "\r\n",
684 },
685 {
686 name: "short event marker",
687 message: ">",
688 },
689 {
690 name: "short hold event",
691 message: ">H",
692 },
693 {
694 name: "short info event",
695 message: ">I",
696 },
697 {
698 name: "short notify event",
699 message: ">N",
700 },
701 {
702 name: "short line",
703 message: "S",
704 },
705 } {
706 t.Run(tc.name, func(t *testing.T) {
707 t.Parallel()
708
709 ctx, cancel := context.WithCancel(t.Context())
710 t.Cleanup(cancel)
711
712 conf := config.Defaults
713 conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}
714 conf.HTTP.Secret = testsuite.Secret
715 conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)}
716
717 suite := testsuite.New(&conf)
718 errOpenVPNClientCh := suite.SetupManagementEnvironment(ctx, t, nil)
719
720 suite.ExpectVersionAndReleaseHold(t)
721
722 for range 12 {
723 suite.SendMessagef(t, tc.message)
724 }
725
726 require.NoError(t, suite.GetManagementInterfaceConn().Close())
727
728 select {
729 case err := <-errOpenVPNClientCh:
730 require.NoError(t, err)
731 case <-time.After(1 * time.Second):
732 t.Fatalf("timeout waiting for connection to close. Logs:\n\n%s", suite.Logs())
733 }
734 })
735 }
736 }
737
738 func TestInvalidCommandResponses(t *testing.T) {
739 t.Parallel()
740
741 for _, tc := range []struct {
742 name string
743 message string
744 }{
745 {
746 name: "empty SUCCESS",
747 message: "SUCCESS: ",
748 },
749 {
750 name: "empty ERROR",
751 message: "ERROR: ",
752 },
753 } {
754 t.Run(tc.name, func(t *testing.T) {
755 t.Parallel()
756
757 ctx, cancel := context.WithCancel(t.Context())
758 t.Cleanup(cancel)
759
760 conf := config.Config{
761 HTTP: config.HTTP{
762 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
763 Secret: testsuite.Secret,
764 },
765 OpenVPN: config.OpenVPN{
766 Bypass: config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)},
767 },
768 }
769
770 suite := testsuite.New(&conf)
771 errOpenVPNClientCh := suite.SetupManagementEnvironment(ctx, t, nil)
772
773 suite.ExpectVersionAndReleaseHold(t)
774 suite.SendAndExpectMessage(t, ">HOLD:Waiting for hold release:0", "hold release")
775
776 suite.SendMessagef(t, tc.message)
777
778 require.NoError(t, suite.GetManagementInterfaceConn().Close())
779
780 select {
781 case err := <-errOpenVPNClientCh:
782 require.NoError(t, err)
783 require.Contains(t, suite.Logs(), "command response not accepted")
784 case <-time.After(3 * time.Second):
785 t.Fatalf("timeout waiting for connection to close. Logs:\n\n%s", suite.Logs())
786 }
787 })
788 }
789 }