|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/ecdsa" |
| 5 | + "crypto/elliptic" |
| 6 | + "crypto/rand" |
| 7 | + "crypto/tls" |
| 8 | + "crypto/x509" |
| 9 | + "crypto/x509/pkix" |
| 10 | + "encoding/pem" |
| 11 | + "math/big" |
| 12 | + "os" |
| 13 | + "path/filepath" |
| 14 | + "testing" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +// genSelfSigned returns a PEM-encoded self-signed cert/key pair carrying the given |
| 19 | +// serial number, so tests can tell two generations of the certificate apart. |
| 20 | +func genSelfSigned(t *testing.T, serial int64) (certPEM, keyPEM []byte) { |
| 21 | + t.Helper() |
| 22 | + |
| 23 | + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("generate key: %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + tmpl := &x509.Certificate{ |
| 29 | + SerialNumber: big.NewInt(serial), |
| 30 | + Subject: pkix.Name{CommonName: "kube-ovn-webhook-test"}, |
| 31 | + NotBefore: time.Now().Add(-time.Hour), |
| 32 | + NotAfter: time.Now().Add(time.Hour), |
| 33 | + KeyUsage: x509.KeyUsageDigitalSignature, |
| 34 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 35 | + DNSNames: []string{"localhost"}, |
| 36 | + BasicConstraintsValid: true, |
| 37 | + } |
| 38 | + |
| 39 | + der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key) |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("create certificate: %v", err) |
| 42 | + } |
| 43 | + keyDER, err := x509.MarshalPKCS8PrivateKey(key) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("marshal key: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + certPEM = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}) |
| 49 | + keyPEM = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyDER}) |
| 50 | + return certPEM, keyPEM |
| 51 | +} |
| 52 | + |
| 53 | +// writeKeyPair writes the cert/key files and bumps their mtime strictly forward, |
| 54 | +// mirroring how cert-manager replaces the mounted Secret on renewal. |
| 55 | +func writeKeyPair(t *testing.T, certFile, keyFile string, certPEM, keyPEM []byte, mtime time.Time) { |
| 56 | + t.Helper() |
| 57 | + if err := os.WriteFile(certFile, certPEM, 0o600); err != nil { |
| 58 | + t.Fatalf("write cert: %v", err) |
| 59 | + } |
| 60 | + if err := os.WriteFile(keyFile, keyPEM, 0o600); err != nil { |
| 61 | + t.Fatalf("write key: %v", err) |
| 62 | + } |
| 63 | + if err := os.Chtimes(certFile, mtime, mtime); err != nil { |
| 64 | + t.Fatalf("chtimes cert: %v", err) |
| 65 | + } |
| 66 | + if err := os.Chtimes(keyFile, mtime, mtime); err != nil { |
| 67 | + t.Fatalf("chtimes key: %v", err) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// servedSerial completes a TLS handshake against addr and returns the serial number |
| 72 | +// of the leaf certificate the server actually presented. |
| 73 | +func servedSerial(t *testing.T, addr string) int64 { |
| 74 | + t.Helper() |
| 75 | + conn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: true}) //nolint:gosec // test-only, inspecting the served cert |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("dial: %v", err) |
| 78 | + } |
| 79 | + defer conn.Close() |
| 80 | + certs := conn.ConnectionState().PeerCertificates |
| 81 | + if len(certs) == 0 { |
| 82 | + t.Fatalf("server presented no certificate") |
| 83 | + } |
| 84 | + return certs[0].SerialNumber.Int64() |
| 85 | +} |
| 86 | + |
| 87 | +// TestReloadingTLSConfigServesRenewedCertificate is the regression test for the |
| 88 | +// cert-manager renewal not being picked up: it fails against the old code that |
| 89 | +// loads the key pair once into tls.Config.Certificates, and passes once the |
| 90 | +// certificate is served via a reloading GetCertificate callback. |
| 91 | +func TestReloadingTLSConfigServesRenewedCertificate(t *testing.T) { |
| 92 | + dir := t.TempDir() |
| 93 | + certFile := filepath.Join(dir, "tls.crt") |
| 94 | + keyFile := filepath.Join(dir, "tls.key") |
| 95 | + |
| 96 | + certA, keyA := genSelfSigned(t, 1) |
| 97 | + writeKeyPair(t, certFile, keyFile, certA, keyA, time.Now().Add(-2*time.Second)) |
| 98 | + |
| 99 | + tlsConfig, err := newReloadingTLSConfig(certFile, keyFile) |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("newReloadingTLSConfig: %v", err) |
| 102 | + } |
| 103 | + |
| 104 | + ln, err := tls.Listen("tcp", "127.0.0.1:0", tlsConfig) |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("listen: %v", err) |
| 107 | + } |
| 108 | + defer ln.Close() |
| 109 | + go func() { |
| 110 | + for { |
| 111 | + conn, err := ln.Accept() |
| 112 | + if err != nil { |
| 113 | + return |
| 114 | + } |
| 115 | + go func() { |
| 116 | + if tc, ok := conn.(*tls.Conn); ok { |
| 117 | + _ = tc.Handshake() |
| 118 | + } |
| 119 | + conn.Close() |
| 120 | + }() |
| 121 | + } |
| 122 | + }() |
| 123 | + |
| 124 | + addr := ln.Addr().String() |
| 125 | + |
| 126 | + if got := servedSerial(t, addr); got != 1 { |
| 127 | + t.Fatalf("before renewal: expected serial 1, got %d", got) |
| 128 | + } |
| 129 | + |
| 130 | + // cert-manager renews the Secret: the mounted files are replaced in place. |
| 131 | + certB, keyB := genSelfSigned(t, 2) |
| 132 | + writeKeyPair(t, certFile, keyFile, certB, keyB, time.Now().Add(2*time.Second)) |
| 133 | + |
| 134 | + if got := servedSerial(t, addr); got != 2 { |
| 135 | + t.Fatalf("after renewal: expected renewed serial 2, got %d (certificate was not reloaded)", got) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// TestCertReloaderKeepsLastGoodCertificate verifies that a failed reload (e.g. a |
| 140 | +// torn read while the volume is updated) does not take the webhook down: it keeps |
| 141 | +// serving the previously loaded certificate instead of erroring the handshake. |
| 142 | +func TestCertReloaderKeepsLastGoodCertificate(t *testing.T) { |
| 143 | + dir := t.TempDir() |
| 144 | + certFile := filepath.Join(dir, "tls.crt") |
| 145 | + keyFile := filepath.Join(dir, "tls.key") |
| 146 | + |
| 147 | + certA, keyA := genSelfSigned(t, 1) |
| 148 | + writeKeyPair(t, certFile, keyFile, certA, keyA, time.Now().Add(-2*time.Second)) |
| 149 | + |
| 150 | + cr, err := newCertReloader(certFile, keyFile) |
| 151 | + if err != nil { |
| 152 | + t.Fatalf("newCertReloader: %v", err) |
| 153 | + } |
| 154 | + |
| 155 | + // Corrupt the cert file and advance its mtime so a reload is attempted and fails. |
| 156 | + if err := os.WriteFile(certFile, []byte("not a certificate"), 0o600); err != nil { |
| 157 | + t.Fatalf("corrupt cert: %v", err) |
| 158 | + } |
| 159 | + if err := os.Chtimes(certFile, time.Now().Add(2*time.Second), time.Now().Add(2*time.Second)); err != nil { |
| 160 | + t.Fatalf("chtimes: %v", err) |
| 161 | + } |
| 162 | + |
| 163 | + got, err := cr.GetCertificate(&tls.ClientHelloInfo{}) |
| 164 | + if err != nil { |
| 165 | + t.Fatalf("GetCertificate returned error instead of serving last good cert: %v", err) |
| 166 | + } |
| 167 | + if got == nil || got.Leaf == nil { |
| 168 | + // Leaf may be nil depending on the Go version; fall back to parsing. |
| 169 | + if got == nil || len(got.Certificate) == 0 { |
| 170 | + t.Fatalf("GetCertificate returned no certificate") |
| 171 | + } |
| 172 | + leaf, perr := x509.ParseCertificate(got.Certificate[0]) |
| 173 | + if perr != nil { |
| 174 | + t.Fatalf("parse served cert: %v", perr) |
| 175 | + } |
| 176 | + got.Leaf = leaf |
| 177 | + } |
| 178 | + if got.Leaf.SerialNumber.Int64() != 1 { |
| 179 | + t.Fatalf("expected to keep serving serial 1 after failed reload, got %d", got.Leaf.SerialNumber.Int64()) |
| 180 | + } |
| 181 | +} |
0 commit comments