-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaproxy_e2e_test.go
More file actions
737 lines (687 loc) · 22.9 KB
/
Copy pathhaproxy_e2e_test.go
File metadata and controls
737 lines (687 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
package rhttp
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"io"
"math/big"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
// HAProxy end-to-end tests.
//
// These spin up a real HAProxy subprocess in front of an rhttp worker and
// drive traffic through HAProxy's public frontend. They prove the
// recommended config in examples/haproxy/haproxy.cfg actually works for the
// scenarios that bit slides during integration, and they guard the library
// against future regressions visible only through a real proxy.
//
// HAProxy is required: `haproxy` must be on PATH or HAPROXY_BIN must point
// to a binary. Tests skip cleanly when it's missing — CI without HAProxy
// still runs the unit suite.
//
// Each test sets up a fresh HAProxy + rhttp pair so they're independent
// and parallelisable. Startup is ~150–250ms; the whole suite runs in a
// few seconds.
// haproxyBin returns the haproxy binary to run, or "" if unavailable.
// HAPROXY_BIN takes precedence; if it's set but points at something that
// doesn't exist, we still fall through to PATH so a misconfigured env
// var on a CI box doesn't silently break the tests.
func haproxyBin() string {
if v := os.Getenv("HAPROXY_BIN"); v != "" {
if st, err := os.Stat(v); err == nil && !st.IsDir() {
return v
}
}
if p, err := exec.LookPath("haproxy"); err == nil {
return p
}
return ""
}
// e2eFixture bundles everything one HAProxy-backed test needs.
type e2eFixture struct {
t *testing.T
haproxy *exec.Cmd
haproxyOut *bytes.Buffer
publicAddr string // host:port of the public frontend
tunnelAddr string // host:port HAProxy listens on for reverse workers
tlsCert tls.Certificate
caPool *x509.CertPool
sni string
pool *Pool
stopHAProxy func()
}
// setupHAProxy provisions: a self-signed cert, an HAProxy subprocess with
// our recommended config, an rhttp connection pool dialing into it, and
// returns a fixture whose Close() tears everything down.
//
// handler is the application served behind the reverse tunnel.
func setupHAProxy(t *testing.T, handler http.Handler) *e2eFixture {
t.Helper()
bin := haproxyBin()
if bin == "" {
t.Skip("haproxy not found (set HAPROXY_BIN or put it on PATH)")
}
dir := t.TempDir()
const sni = "worker.test"
// Build a tiny mTLS PKI:
// - ca: a root that signs everything else
// - serverPEM: HAProxy's identity (presents to workers + browsers)
// - clientPEM: rhttp worker's identity (presents to HAProxy)
// In a real deployment you'd typically already have an internal CA;
// here we mint a fresh one per test run.
ca := newCA(t)
serverPEM, serverKeyPEM := ca.issue(t, sni, true /* serverAuth */)
clientPEM, clientKeyPEM := ca.issue(t, "worker-1", false /* clientAuth */)
combinedServer := append(append([]byte{}, serverPEM...), serverKeyPEM...)
combinedServerPath := filepath.Join(dir, "server-combined.pem")
if err := os.WriteFile(combinedServerPath, combinedServer, 0o600); err != nil {
t.Fatalf("write server cert: %v", err)
}
caPath := filepath.Join(dir, "ca.pem")
if err := os.WriteFile(caPath, ca.certPEM, 0o600); err != nil {
t.Fatalf("write CA: %v", err)
}
// Two free TCP ports — one for the reverse-tunnel listener, one for
// the public-facing one.
tunnelPort := freePort(t)
publicPort := freePort(t)
cfg := fmt.Sprintf(`
global
log stdout format raw daemon
expose-experimental-directives
nbthread 1
defaults
mode http
log global
option httplog
option dontlognull
timeout client 30s
timeout server 30s
timeout connect 5s
timeout http-request 10s
timeout tunnel 2h
frontend reverse-in
bind 127.0.0.1:%d ssl crt %s ca-file %s verify required alpn h2 idle-ping 30s
acl is_worker ssl_fc_sni %s
tcp-request session attach-srv app/workers if is_worker
frontend public
bind 127.0.0.1:%d
default_backend app
backend app
http-reuse always
retries 5
retry-on all-retryable-errors
server workers rhttp@ idle-ping 20s
`, tunnelPort, combinedServerPath, caPath, sni, publicPort)
cfgPath := filepath.Join(dir, "haproxy.cfg")
if err := os.WriteFile(cfgPath, []byte(cfg), 0o600); err != nil {
t.Fatalf("write cfg: %v", err)
}
// Validate the config first — clearer error than a half-started daemon.
// reverse-http requires HAProxy >= 3.4-dev10 (and several keywords were
// introduced earlier in the 3.x line). If validation fails because of
// missing keywords, skip cleanly rather than failing.
if out, err := exec.Command(bin, "-c", "-f", cfgPath).CombinedOutput(); err != nil {
s := string(out)
if strings.Contains(s, "unknown keyword") ||
strings.Contains(s, "rhttp@") ||
strings.Contains(s, "attach-srv") {
t.Skipf("haproxy %q lacks reverse-http support (needs >= 3.4-dev10):\n%s", bin, out)
}
t.Fatalf("haproxy -c failed: %v\n%s", err, out)
}
// Start HAProxy.
hpOut := &bytes.Buffer{}
hp := exec.Command(bin, "-f", cfgPath)
hp.Stdout = hpOut
hp.Stderr = hpOut
if err := hp.Start(); err != nil {
t.Fatalf("haproxy start: %v", err)
}
stopHAProxy := func() {
_ = hp.Process.Signal(os.Interrupt)
done := make(chan struct{})
go func() { _ = hp.Wait(); close(done) }()
select {
case <-done:
case <-time.After(2 * time.Second):
_ = hp.Process.Kill()
<-done
}
}
if err := waitForListen(fmt.Sprintf("127.0.0.1:%d", publicPort), 5*time.Second); err != nil {
stopHAProxy()
t.Fatalf("haproxy didn't open public port: %v\n%s", err, hpOut.String())
}
if err := waitForListen(fmt.Sprintf("127.0.0.1:%d", tunnelPort), 5*time.Second); err != nil {
stopHAProxy()
t.Fatalf("haproxy didn't open tunnel port: %v\n%s", err, hpOut.String())
}
// Set up the rhttp client side — worker identity + trust for the CA.
cert, err := tls.X509KeyPair(clientPEM, clientKeyPEM)
if err != nil {
stopHAProxy()
t.Fatalf("parse client cert: %v", err)
}
caPool := x509.NewCertPool()
caPool.AppendCertsFromPEM(ca.certPEM)
if handler == nil {
handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(200)
_, _ = io.WriteString(w, "ok")
})
}
pool, err := NewConnectionPool(ServerOptions{
Addr: fmt.Sprintf("127.0.0.1:%d", tunnelPort),
SNIServerName: sni,
TLSCert: cert,
CACertPool: caPool,
Handler: handler,
// 4 tunnels match the comfortable working point established in the
// matching unit tests; lower values race the HAProxy idle pool
// under bursty load.
NBConn: 4,
})
if err != nil {
stopHAProxy()
t.Fatalf("NewConnectionPool: %v", err)
}
// Wait for every pool slot to complete at least one successful
// dial+SETTINGS exchange. We deliberately don't issue a "probe"
// request through the public frontend — that would run the test's
// handler with a synthetic request and corrupt any handler state
// (e.g. a counter or a channel buffer the test reads).
poolReady := make(chan struct{})
go func() {
pool.Ready().Wait()
close(poolReady)
}()
select {
case <-poolReady:
case <-time.After(3 * time.Second):
stopHAProxy()
t.Fatalf("rhttp pool didn't become ready within 3s\n%s", hpOut.String())
}
// Small settling pause so HAProxy registers each `attach-srv` against
// its backend before the test starts firing traffic. Skipping this
// produces a flake on the first request only.
time.Sleep(50 * time.Millisecond)
f := &e2eFixture{
t: t,
haproxy: hp,
haproxyOut: hpOut,
publicAddr: fmt.Sprintf("127.0.0.1:%d", publicPort),
tunnelAddr: fmt.Sprintf("127.0.0.1:%d", tunnelPort),
tlsCert: cert,
caPool: caPool,
sni: sni,
pool: pool,
stopHAProxy: stopHAProxy,
}
// On test failure, dump HAProxy logs — easier than digging via t.Logf
// inside every assertion.
t.Cleanup(func() {
if t.Failed() {
t.Logf("--- HAProxy stderr ---\n%s\n--- end ---", hpOut.String())
}
})
return f
}
// Close shuts down HAProxy. The rhttp pool stays alive (no public Close
// API yet — they're orphaned goroutines that exit when their TCP conns
// die, which happens when HAProxy goes away).
func (f *e2eFixture) Close() {
f.stopHAProxy()
}
// get is a convenience that issues a GET against the public frontend.
func (f *e2eFixture) get(path string) (*http.Response, error) {
return http.Get("http://" + f.publicAddr + path)
}
// freePort returns a port that nothing is listening on right now. Inherent
// TOCTOU race vs. another process binding before us — fine for tests.
func freePort(t *testing.T) int {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("freePort: %v", err)
}
defer ln.Close()
return ln.Addr().(*net.TCPAddr).Port
}
// waitForListen polls a TCP address until something accepts connections,
// or the deadline expires.
func waitForListen(addr string, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
c, err := net.DialTimeout("tcp", addr, 200*time.Millisecond)
if err == nil {
_ = c.Close()
return nil
}
time.Sleep(20 * time.Millisecond)
}
return errors.New("listen timeout")
}
// testCA is a tiny self-signed CA used to mint leaf certs for both ends of
// the mTLS handshake in the e2e tests.
type testCA struct {
cert *x509.Certificate
key *ecdsa.PrivateKey
certPEM []byte
}
// newCA generates a fresh root that the test can use to sign both the
// HAProxy server cert and the rhttp worker client cert. P-256 + 24h
// validity — short-lived since each test run mints its own.
func newCA(t *testing.T) *testCA {
t.Helper()
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("gen CA key: %v", err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "rhttp-test-ca"},
NotBefore: time.Now().Add(-time.Minute),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
BasicConstraintsValid: true,
IsCA: true,
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &priv.PublicKey, priv)
if err != nil {
t.Fatalf("sign CA: %v", err)
}
parsed, err := x509.ParseCertificate(der)
if err != nil {
t.Fatalf("parse CA: %v", err)
}
return &testCA{
cert: parsed,
key: priv,
certPEM: pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}),
}
}
// issue mints a leaf cert signed by the CA. host becomes the CN and a SAN;
// 127.0.0.1 is also added as an IP SAN so loopback connections verify. If
// serverAuth is true the cert carries the server-auth EKU (used for the
// HAProxy bind line); otherwise client-auth (used for the rhttp worker).
func (c *testCA) issue(t *testing.T, host string, serverAuth bool) (certPEM, keyPEM []byte) {
t.Helper()
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("gen leaf key: %v", err)
}
eku := x509.ExtKeyUsageClientAuth
if serverAuth {
eku = x509.ExtKeyUsageServerAuth
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(time.Now().UnixNano()),
Subject: pkix.Name{CommonName: host},
NotBefore: time.Now().Add(-time.Minute),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{eku},
DNSNames: []string{host, "localhost"},
IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")},
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, c.cert, &priv.PublicKey, c.key)
if err != nil {
t.Fatalf("sign leaf: %v", err)
}
certPEM = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
keyDER, err := x509.MarshalECPrivateKey(priv)
if err != nil {
t.Fatalf("marshal leaf key: %v", err)
}
keyPEM = pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER})
return certPEM, keyPEM
}
// ─── tests ───────────────────────────────────────────────────────────
// TestHAProxyBasicRoundTrip: GET → 200 OK with body, through HAProxy.
func TestHAProxyBasicRoundTrip(t *testing.T) {
hits := 0
var mu sync.Mutex
f := setupHAProxy(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
mu.Lock()
hits++
mu.Unlock()
w.Header().Set("X-E2E", "yes")
w.WriteHeader(200)
_, _ = io.WriteString(w, "pong from rhttp")
}))
defer f.Close()
resp, err := f.get("/ping")
if err != nil {
t.Fatalf("get: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("status = %d, want 200", resp.StatusCode)
}
if resp.Header.Get("X-E2E") != "yes" {
t.Fatal("missing X-E2E header")
}
body, _ := io.ReadAll(resp.Body)
if string(body) != "pong from rhttp" {
t.Fatalf("body = %q", body)
}
}
// TestHAProxyConcurrentBurst: 100 simultaneous requests succeed. With
// `http-reuse always` HAProxy multiplexes them across the 2 tunnels; the
// recommended config is what makes this work.
func TestHAProxyConcurrentBurst(t *testing.T) {
f := setupHAProxy(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(200)
_, _ = io.WriteString(w, "ok")
}))
defer f.Close()
// 20 concurrent through a 4-tunnel pool is comfortably below the
// idle-pool race threshold and matches a realistic "few browser tabs
// loading at once" scenario. The unit-test side covers far more
// aggressive stress; the e2e test is here to certify the recommended
// HAProxy config behaves correctly end-to-end on typical load.
const n = 20
results := make(chan int, n)
for range n {
go func() {
resp, err := f.get("/x")
if err != nil {
results <- 0
return
}
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
results <- resp.StatusCode
}()
}
ok := 0
codes := make(map[int]int)
for range n {
c := <-results
codes[c]++
if c == 200 {
ok++
}
}
if ok != n {
t.Fatalf("%d/%d succeeded (codes=%v); haproxy stderr:\n%s", ok, n, codes, f.haproxyOut.String())
}
}
// TestHAProxyLargeResponse: 1 MB response streams through correctly. Tests
// the full chain: rhttp send flow control → HAProxy multiplex → public
// frontend → http.Client.
func TestHAProxyLargeResponse(t *testing.T) {
const n = 1024 * 1024
payload := make([]byte, n)
for i := range payload {
payload[i] = byte(i)
}
f := setupHAProxy(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/octet-stream")
w.WriteHeader(200)
_, _ = w.Write(payload)
}))
defer f.Close()
resp, err := f.get("/blob")
if err != nil {
t.Fatalf("get: %v", err)
}
defer resp.Body.Close()
got, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("readall: %v", err)
}
if !bytes.Equal(got, payload) {
t.Fatalf("payload mismatch (got %d bytes, want %d)", len(got), n)
}
}
// TestHAProxyLargeRequestBody: client POSTs 1 MB through HAProxy; the
// handler reads it whole. Validates rhttp's receive-side WINDOW_UPDATE
// emission under real traffic.
func TestHAProxyLargeRequestBody(t *testing.T) {
// 1 MB exercises receive-side flow control: well past the default
// 64 KB stream window so rhttp's WINDOW_UPDATE emission is on the
// critical path.
const n = 1024 * 1024
payload := bytes.Repeat([]byte("Q"), n)
gotLen := make(chan int, 1)
f := setupHAProxy(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("server readall: %v", err)
}
gotLen <- len(b)
w.WriteHeader(204)
}))
defer f.Close()
resp, err := http.Post("http://"+f.publicAddr+"/upload", "application/octet-stream", bytes.NewReader(payload))
if err != nil {
t.Fatalf("post: %v", err)
}
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
select {
case got := <-gotLen:
if got != n {
t.Fatalf("server saw %d bytes, want %d", got, n)
}
case <-time.After(5 * time.Second):
t.Fatalf("handler never finished reading; haproxy stderr:\n%s", f.haproxyOut.String())
}
if resp.StatusCode != 204 {
t.Fatalf("status = %d, want 204; haproxy stderr:\n%s", resp.StatusCode, f.haproxyOut.String())
}
}
// TestHAProxySSE: a long-lived SSE response delivers each flushed chunk
// promptly through HAProxy. Mirrors the slides usage that exposed the
// `Connection: keep-alive` and `http-response return 503 if status 404`
// bugs earlier in development.
func TestHAProxySSE(t *testing.T) {
const chunks = 5
f := setupHAProxy(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.WriteHeader(200)
fl, _ := w.(http.Flusher)
for i := range chunks {
_, _ = fmt.Fprintf(w, "event: tick\ndata: %d\n\n", i)
fl.Flush()
time.Sleep(20 * time.Millisecond)
}
}))
defer f.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, "GET", "http://"+f.publicAddr+"/events", nil)
req.Header.Set("Accept", "text/event-stream")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("do: %v", err)
}
defer resp.Body.Close()
seen := 0
buf := make([]byte, 1024)
for seen < chunks {
n, err := resp.Body.Read(buf)
if n > 0 {
seen += strings.Count(string(buf[:n]), "event: tick")
}
if err != nil {
if seen >= chunks {
return
}
t.Fatalf("read: %v (seen %d/%d)", err, seen, chunks)
}
}
}
// TestHAProxyConcurrentBodies: 20 in-flight POSTs with non-trivial bodies
// validate that receive flow control + WINDOW_UPDATE emission + reuse all
// behave under contention.
func TestHAProxyConcurrentBodies(t *testing.T) {
f := setupHAProxy(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(200)
_, _ = fmt.Fprintf(w, "%d", len(b))
}))
defer f.Close()
const n = 10
payload := bytes.Repeat([]byte("Z"), 32*1024)
results := make(chan string, n)
for i := range n {
go func(i int) {
resp, err := http.Post("http://"+f.publicAddr+fmt.Sprintf("/echo?i=%d", i), "application/octet-stream", bytes.NewReader(payload))
if err != nil {
results <- fmt.Sprintf("err: %v", err)
return
}
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
results <- fmt.Sprintf("%d:%s", resp.StatusCode, b)
}(i)
}
for i := range n {
got := <-results
want := fmt.Sprintf("200:%d", len(payload))
if got != want {
t.Fatalf("response %d: got %q, want %q", i, got, want)
}
}
}
// TestHAProxyRejectsUnauthenticatedWorker: with `verify required ca-file`
// on the bind line, HAProxy must refuse a connection from a worker that
// presents a certificate not signed by the configured CA. This is the
// server-side half of the "mTLS is mandatory" guarantee — the client-side
// half is enforced by ErrMTLSRequired (unit-tested above).
func TestHAProxyRejectsUnauthenticatedWorker(t *testing.T) {
bin := haproxyBin()
if bin == "" {
t.Skip("haproxy not found (set HAPROXY_BIN or put it on PATH)")
}
dir := t.TempDir()
const sni = "worker.test"
goodCA := newCA(t)
serverPEM, serverKeyPEM := goodCA.issue(t, sni, true)
combinedServer := append(append([]byte{}, serverPEM...), serverKeyPEM...)
combinedPath := filepath.Join(dir, "server.pem")
if err := os.WriteFile(combinedPath, combinedServer, 0o600); err != nil {
t.Fatal(err)
}
caPath := filepath.Join(dir, "ca.pem")
if err := os.WriteFile(caPath, goodCA.certPEM, 0o600); err != nil {
t.Fatal(err)
}
tunnelPort := freePort(t)
cfg := fmt.Sprintf(`
global
expose-experimental-directives
nbthread 1
defaults
mode http
timeout client 10s
timeout server 10s
timeout connect 5s
frontend reverse-in
bind 127.0.0.1:%d ssl crt %s ca-file %s verify required alpn h2
tcp-request session reject
`, tunnelPort, combinedPath, caPath)
cfgPath := filepath.Join(dir, "haproxy.cfg")
if err := os.WriteFile(cfgPath, []byte(cfg), 0o600); err != nil {
t.Fatal(err)
}
if out, err := exec.Command(bin, "-c", "-f", cfgPath).CombinedOutput(); err != nil {
if strings.Contains(string(out), "unknown keyword") {
t.Skipf("haproxy %q lacks required keywords:\n%s", bin, out)
}
t.Fatalf("haproxy -c failed: %v\n%s", err, out)
}
hp := exec.Command(bin, "-f", cfgPath)
hpOut := &bytes.Buffer{}
hp.Stdout, hp.Stderr = hpOut, hpOut
if err := hp.Start(); err != nil {
t.Fatal(err)
}
defer func() {
_ = hp.Process.Signal(os.Interrupt)
_ = hp.Wait()
}()
if err := waitForListen(fmt.Sprintf("127.0.0.1:%d", tunnelPort), 5*time.Second); err != nil {
t.Fatalf("haproxy not listening: %v\n%s", err, hpOut.String())
}
// Worker side: a client cert signed by a DIFFERENT CA. HAProxy must
// reject it during the TLS handshake.
otherCA := newCA(t)
bogusPEM, bogusKeyPEM := otherCA.issue(t, "worker-1", false)
bogusCert, err := tls.X509KeyPair(bogusPEM, bogusKeyPEM)
if err != nil {
t.Fatal(err)
}
trustPool := x509.NewCertPool()
trustPool.AppendCertsFromPEM(goodCA.certPEM)
dialer := &net.Dialer{Timeout: 3 * time.Second}
conn, err := tls.DialWithDialer(dialer, "tcp", fmt.Sprintf("127.0.0.1:%d", tunnelPort), &tls.Config{
RootCAs: trustPool,
ServerName: sni,
Certificates: []tls.Certificate{bogusCert},
NextProtos: []string{"h2"},
MinVersion: tls.VersionTLS12,
})
if err != nil {
return // expected — HAProxy refused the bad cert during TLS handshake
}
defer conn.Close()
// Some HAProxy / TLS versions complete the handshake from the client's
// perspective before tearing down — verify by attempting an actual
// read/write. If the cert was acceptably verified, h2 traffic would
// flow; with a bad cert, the connection must be unusable.
_ = conn.SetDeadline(time.Now().Add(2 * time.Second))
// Try to write the H2 preface — same thing a real worker would do.
_, _ = conn.Write([]byte("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"))
buf := make([]byte, 1)
n, readErr := conn.Read(buf)
if readErr == nil && n > 0 {
t.Fatalf("HAProxy accepted bad client cert — got %d bytes of data, want connection error", n)
}
}
// TestHAProxyRetryAbsorbsTransientFailure: the recommended `retries 5 +
// retry-on all-retryable-errors` swallows the brief idle-pool race that
// HAProxy hits on the first request just after a tunnel reconnects. We
// can't easily inject the exact race, but we verify the retry config
// passes validation and a request still succeeds — the unit-test side
// already covers the retry mechanics in TestSendFlowControl etc.
//
// This test mostly catches regressions in the example config itself: any
// change that breaks `retry-on all-retryable-errors` parsing surfaces
// here as an HAProxy startup failure during setupHAProxy.
func TestHAProxyRetryConfigParses(t *testing.T) {
f := setupHAProxy(t, nil)
defer f.Close()
resp, err := f.get("/x")
if err != nil {
t.Fatalf("get: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("status = %d", resp.StatusCode)
}
}