Skip to content

Commit ee2ca2e

Browse files
committed
test: make GODEBUG=tlsmlkem=0 PQC opt-out subtests non-vacuous on Go 1.26
1 parent c8bcc08 commit ee2ca2e

2 files changed

Lines changed: 155 additions & 88 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,11 @@ The Cloud SQL Auth Proxy encrypts connections to Cloud SQL instances using TLS
715715
### Automatic Fallback
716716
717717
No flags or configuration changes are required. Both hybrid `X25519MLKEM768` and
718-
classical `X25519` key shares are offered in the TLS 1.3 `ClientHello`. If a
719-
Cloud SQL instance does not yet support hybrid PQC, the TLS 1.3 handshake
720-
automatically falls back to classical `X25519` without connection failures.
718+
classical `X25519` key shares are offered in the TLS 1.3 `ClientHello` (and Go
719+
1.26+ builds also advertise `SecP256r1MLKEM768` and `SecP384r1MLKEM1024` in
720+
`supported_groups`). If a Cloud SQL instance does not yet support hybrid PQC,
721+
the TLS 1.3 handshake automatically falls back to classical `X25519` without
722+
connection failures.
721723
722724
### Opting Out
723725

‎internal/proxy/pqc_test.go‎

Lines changed: 150 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ import (
3131
"net"
3232
"net/http"
3333
"net/http/httptest"
34+
"os"
35+
"os/exec"
3436
"slices"
37+
"strings"
3538
"sync"
3639
"testing"
3740
"time"
@@ -326,6 +329,95 @@ func freeTCPPort(t *testing.T) int {
326329
return port
327330
}
328331

332+
func runProxyRoundTrip(ctx context.Context, t *testing.T, adminURL, tlsAddr, icn string) (string, error) {
333+
t.Helper()
334+
proxyPort := freeTCPPort(t)
335+
336+
// Disable built-in Cloud Monitoring metric export (BuiltInMetrics = false)
337+
// so this hermetic unit test does not dial monitoring.googleapis.com on client.Close().
338+
builtInMetrics := false
339+
conf := &proxy.Config{
340+
Addr: "127.0.0.1",
341+
Port: proxyPort,
342+
APIEndpointURL: adminURL,
343+
Token: "test-oauth2-token",
344+
UserAgent: "cloud-sql-proxy/test",
345+
BuiltInMetrics: &builtInMetrics,
346+
Instances: []proxy.InstanceConnConfig{
347+
{Name: icn},
348+
},
349+
}
350+
351+
dialerOpts, err := conf.DialerOptions(testLogger)
352+
if err != nil {
353+
t.Fatalf("conf.DialerOptions: %v", err)
354+
}
355+
// Redirect the server-side proxy TCP dial (127.0.0.1:3307) to our
356+
// ephemeral mock Cloud SQL TLS 1.3 listener to avoid port 3307 collisions.
357+
dialerOpts = append(dialerOpts, cloudsqlconn.WithDialFunc(func(ctx context.Context, network, addr string) (net.Conn, error) {
358+
if addr != "127.0.0.1:3307" {
359+
return nil, fmt.Errorf("unexpected backend dial address %q, want 127.0.0.1:3307", addr)
360+
}
361+
var d net.Dialer
362+
return d.DialContext(ctx, network, tlsAddr)
363+
}))
364+
365+
dialer, err := cloudsqlconn.NewDialer(ctx, dialerOpts...)
366+
if err != nil {
367+
t.Fatalf("cloudsqlconn.NewDialer: %v", err)
368+
}
369+
370+
client, err := proxy.NewClient(ctx, dialer, testLogger, conf, nil)
371+
if err != nil {
372+
_ = dialer.Close()
373+
t.Fatalf("proxy.NewClient: %v", err)
374+
}
375+
defer func() { _ = client.Close() }()
376+
377+
ready := make(chan struct{})
378+
go func() {
379+
_ = client.Serve(ctx, func() { close(ready) })
380+
}()
381+
select {
382+
case <-ready:
383+
case <-ctx.Done():
384+
t.Fatal("timed out waiting for proxy Serve() readiness")
385+
}
386+
387+
localAddr := fmt.Sprintf("127.0.0.1:%d", proxyPort)
388+
conn, err := net.DialTimeout("tcp", localAddr, 5*time.Second)
389+
if err != nil {
390+
t.Fatalf("net.DialTimeout(%s): %v", localAddr, err)
391+
}
392+
defer func() { _ = conn.Close() }()
393+
_ = conn.SetDeadline(time.Now().Add(5 * time.Second))
394+
395+
if _, err := conn.Write([]byte("ping-pqc\n")); err != nil {
396+
return "", err
397+
}
398+
return bufio.NewReader(conn).ReadString('\n')
399+
}
400+
401+
func TestProxyPQCOptOutSubprocessHelper(t *testing.T) {
402+
tlsAddr := os.Getenv("CSQL_PQC_HELPER_TLS_ADDR")
403+
if tlsAddr == "" {
404+
return
405+
}
406+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
407+
defer cancel()
408+
409+
reply, err := runProxyRoundTrip(
410+
ctx,
411+
t,
412+
os.Getenv("CSQL_PQC_HELPER_ADMIN_URL"),
413+
tlsAddr,
414+
os.Getenv("CSQL_PQC_HELPER_ICN"),
415+
)
416+
if err == nil {
417+
t.Fatalf("expected local client connection to close when backend TLS handshake fails, got reply %q", reply)
418+
}
419+
}
420+
329421
func TestProxyPQCKeyExchange(t *testing.T) {
330422
// IANA codepoint for X25519MLKEM768 hybrid post-quantum key exchange.
331423
const wantX25519MLKEM768Codepoint = tls.CurveID(0x11ec)
@@ -343,12 +435,13 @@ func TestProxyPQCKeyExchange(t *testing.T) {
343435
adminSrv := startMockSQLAdminServer(t, pki, project, region, instance)
344436

345437
tcs := []struct {
346-
desc string
347-
godebug string
348-
serverCurves []tls.CurveID
349-
wantClientMLKEM bool
350-
wantNegotiatedID tls.CurveID
351-
wantHandshakeErr bool
438+
desc string
439+
godebug string
440+
clientSubprocessGODEBUG string
441+
serverCurves []tls.CurveID
442+
wantClientMLKEM bool
443+
wantNegotiatedID tls.CurveID
444+
wantHandshakeErr bool
352445
}{
353446
{
354447
desc: "default hybrid PQC handshake negotiates X25519MLKEM768",
@@ -375,10 +468,12 @@ func TestProxyPQCKeyExchange(t *testing.T) {
375468
wantNegotiatedID: tls.CurveP256,
376469
},
377470
{
378-
desc: "GODEBUG=tlsmlkem=0 opt-out negotiates classical X25519 against PQC server",
379-
godebug: "tlsmlkem=0",
380-
// Explicitly configure X25519MLKEM768 first on the server so process-wide
381-
// GODEBUG=tlsmlkem=0 does not disable PQC on the mock server itself.
471+
desc: "GODEBUG=tlsmlkem=0 opt-out omits X25519MLKEM768 from ClientHello and negotiates X25519",
472+
// Note: In Go 1.26+, process-wide GODEBUG=tlsmlkem=0 filters ML-KEM groups
473+
// out of an in-process tls.Server's CurvePreferences as well. Client
474+
// opt-out is verified non-vacuously via GetConfigForClient inspecting the
475+
// raw wire ClientHelloInfo.SupportedCurves (wantClientMLKEM = false).
476+
godebug: "tlsmlkem=0",
382477
serverCurves: []tls.CurveID{tls.X25519MLKEM768, tls.X25519, tls.CurveP256},
383478
wantClientMLKEM: false,
384479
wantNegotiatedID: tls.X25519,
@@ -391,11 +486,15 @@ func TestProxyPQCKeyExchange(t *testing.T) {
391486
wantNegotiatedID: tls.X25519MLKEM768,
392487
},
393488
{
394-
desc: "GODEBUG=tlsmlkem=0 fails against PQC-only server",
395-
godebug: "tlsmlkem=0",
396-
serverCurves: []tls.CurveID{tls.X25519MLKEM768},
397-
wantClientMLKEM: false,
398-
wantHandshakeErr: true,
489+
desc: "GODEBUG=tlsmlkem=0 fails against PQC-only server",
490+
// Keep tlsmlkem=1 in the parent process so Go 1.26 does not filter
491+
// X25519MLKEM768 out of the PQC-only mock server's CurvePreferences,
492+
// and run the proxy client in a subprocess with GODEBUG=tlsmlkem=0.
493+
godebug: "tlsmlkem=1",
494+
clientSubprocessGODEBUG: "tlsmlkem=0",
495+
serverCurves: []tls.CurveID{tls.X25519MLKEM768},
496+
wantClientMLKEM: false,
497+
wantHandshakeErr: true,
399498
},
400499
}
401500

@@ -409,72 +508,32 @@ func TestProxyPQCKeyExchange(t *testing.T) {
409508
}
410509

411510
tlsAddr, rec := startMockCloudSQLTLSServer(t, pki, tc.serverCurves)
412-
proxyPort := freeTCPPort(t)
413-
414-
// Disable built-in Cloud Monitoring metric export (BuiltInMetrics = false)
415-
// so this hermetic unit test does not dial monitoring.googleapis.com on client.Close().
416-
builtInMetrics := false
417-
conf := &proxy.Config{
418-
Addr: "127.0.0.1",
419-
Port: proxyPort,
420-
APIEndpointURL: adminSrv.URL,
421-
Token: "test-oauth2-token",
422-
UserAgent: "cloud-sql-proxy/test",
423-
BuiltInMetrics: &builtInMetrics,
424-
Instances: []proxy.InstanceConnConfig{
425-
{Name: icn},
426-
},
427-
}
428-
429-
dialerOpts, err := conf.DialerOptions(testLogger)
430-
if err != nil {
431-
t.Fatalf("conf.DialerOptions: %v", err)
432-
}
433-
// Redirect the server-side proxy TCP dial (127.0.0.1:3307) to our
434-
// ephemeral mock Cloud SQL TLS 1.3 listener to avoid port 3307 collisions.
435-
dialerOpts = append(dialerOpts, cloudsqlconn.WithDialFunc(func(ctx context.Context, network, addr string) (net.Conn, error) {
436-
if addr != "127.0.0.1:3307" {
437-
return nil, fmt.Errorf("unexpected backend dial address %q, want 127.0.0.1:3307", addr)
438-
}
439-
var d net.Dialer
440-
return d.DialContext(ctx, network, tlsAddr)
441-
}))
442-
443511
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
444512
defer cancel()
445513

446-
dialer, err := cloudsqlconn.NewDialer(ctx, dialerOpts...)
447-
if err != nil {
448-
t.Fatalf("cloudsqlconn.NewDialer: %v", err)
449-
}
450-
451-
client, err := proxy.NewClient(ctx, dialer, testLogger, conf, nil)
452-
if err != nil {
453-
_ = dialer.Close()
454-
t.Fatalf("proxy.NewClient: %v", err)
455-
}
456-
defer func() { _ = client.Close() }()
457-
458-
ready := make(chan struct{})
459-
go func() {
460-
_ = client.Serve(ctx, func() { close(ready) })
461-
}()
462-
select {
463-
case <-ready:
464-
case <-ctx.Done():
465-
t.Fatal("timed out waiting for proxy Serve() readiness")
466-
}
467-
468-
localAddr := fmt.Sprintf("127.0.0.1:%d", proxyPort)
469-
conn, err := net.DialTimeout("tcp", localAddr, 5*time.Second)
470-
if err != nil {
471-
t.Fatalf("net.DialTimeout(%s): %v", localAddr, err)
472-
}
473-
defer func() { _ = conn.Close() }()
474-
_ = conn.SetDeadline(time.Now().Add(5 * time.Second))
475-
476-
if _, err := conn.Write([]byte("ping-pqc\n")); err != nil {
477-
t.Fatalf("conn.Write: %v", err)
514+
var (
515+
reply string
516+
readErr error
517+
)
518+
if tc.clientSubprocessGODEBUG != "" {
519+
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestProxyPQCOptOutSubprocessHelper$")
520+
env := make([]string, 0, len(os.Environ())+4)
521+
for _, kv := range os.Environ() {
522+
if !strings.HasPrefix(kv, "GODEBUG=") {
523+
env = append(env, kv)
524+
}
525+
}
526+
cmd.Env = append(env,
527+
"GODEBUG="+tc.clientSubprocessGODEBUG,
528+
"CSQL_PQC_HELPER_ADMIN_URL="+adminSrv.URL,
529+
"CSQL_PQC_HELPER_TLS_ADDR="+tlsAddr,
530+
"CSQL_PQC_HELPER_ICN="+icn,
531+
)
532+
if out, err := cmd.CombinedOutput(); err != nil {
533+
t.Fatalf("subprocess client helper failed: %v\n%s", err, out)
534+
}
535+
} else {
536+
reply, readErr = runProxyRoundTrip(ctx, t, adminSrv.URL, tlsAddr, icn)
478537
}
479538

480539
select {
@@ -494,17 +553,24 @@ func TestProxyPQCKeyExchange(t *testing.T) {
494553
t.Fatalf("ClientHello SupportedCurves = %v: contains X25519MLKEM768 (%#x) = %v, want %v",
495554
gotCurves, tls.X25519MLKEM768, gotHasMLKEM, tc.wantClientMLKEM)
496555
}
556+
if !tc.wantClientMLKEM {
557+
for _, pqGroup := range []tls.CurveID{tls.SecP256r1MLKEM768, tls.SecP384r1MLKEM1024} {
558+
if slices.Contains(gotCurves, pqGroup) {
559+
t.Fatalf("ClientHello SupportedCurves = %v: unexpected hybrid group %v (%#x) when ML-KEM is disabled",
560+
gotCurves, pqGroup, uint16(pqGroup))
561+
}
562+
}
563+
}
497564
if !slices.Contains(gotCurves, tls.X25519) {
498565
t.Fatalf("ClientHello SupportedCurves = %v: expected classical X25519 fallback curve", gotCurves)
499566
}
500567

501568
if tc.wantHandshakeErr {
502569
if gotHandshakeErr == nil {
503-
t.Fatalf("expected TLS handshake to fail against PQC-only server with GODEBUG=%q, but succeeded with CurveID=%v",
504-
tc.godebug, gotState.CurveID)
570+
t.Fatalf("expected TLS handshake to fail against PQC-only server, but succeeded with CurveID=%v",
571+
gotState.CurveID)
505572
}
506-
var buf [1]byte
507-
if _, rErr := conn.Read(buf[:]); rErr == nil {
573+
if tc.clientSubprocessGODEBUG == "" && readErr == nil {
508574
t.Fatal("expected local client connection to close when backend TLS handshake fails")
509575
}
510576
return
@@ -521,9 +587,8 @@ func TestProxyPQCKeyExchange(t *testing.T) {
521587
gotState.CurveID, uint16(gotState.CurveID), tc.wantNegotiatedID, uint16(tc.wantNegotiatedID))
522588
}
523589

524-
reply, err := bufio.NewReader(conn).ReadString('\n')
525-
if err != nil {
526-
t.Fatalf("reading proxied reply: %v", err)
590+
if readErr != nil {
591+
t.Fatalf("reading proxied reply: %v", readErr)
527592
}
528593
if wantReply := "pong:ping-pqc\n"; reply != wantReply {
529594
t.Fatalf("proxied reply = %q, want %q", reply, wantReply)

0 commit comments

Comments
 (0)