@@ -50,8 +50,8 @@ func genSelfSigned(t *testing.T, serial int64) (certPEM, keyPEM []byte) {
5050 return certPEM , keyPEM
5151}
5252
53- // writeKeyPair writes the cert/key files and bumps their mtime strictly forward,
54- // mirroring how cert-manager replaces the mounted Secret on renewal.
53+ // writeKeyPair writes the cert/key files and sets their mtime, mirroring how
54+ // cert-manager replaces the mounted Secret on renewal.
5555func writeKeyPair (t * testing.T , certFile , keyFile string , certPEM , keyPEM []byte , mtime time.Time ) {
5656 t .Helper ()
5757 if err := os .WriteFile (certFile , certPEM , 0o600 ); err != nil {
@@ -60,12 +60,36 @@ func writeKeyPair(t *testing.T, certFile, keyFile string, certPEM, keyPEM []byte
6060 if err := os .WriteFile (keyFile , keyPEM , 0o600 ); err != nil {
6161 t .Fatalf ("write key: %v" , err )
6262 }
63- if err := os .Chtimes (certFile , mtime , mtime ); err != nil {
64- t .Fatalf ("chtimes cert: %v" , err )
63+ setModTime (t , certFile , mtime )
64+ setModTime (t , keyFile , mtime )
65+ }
66+
67+ func setModTime (t * testing.T , name string , mtime time.Time ) {
68+ t .Helper ()
69+ if err := os .Chtimes (name , mtime , mtime ); err != nil {
70+ t .Fatalf ("chtimes %s: %v" , name , err )
6571 }
66- if err := os .Chtimes (keyFile , mtime , mtime ); err != nil {
67- t .Fatalf ("chtimes key: %v" , err )
72+ }
73+
74+ // certSerial returns the serial number of a *tls.Certificate leaf without mutating the
75+ // shared value returned by GetCertificate (it parses into a local when Leaf is unset).
76+ func certSerial (t * testing.T , c * tls.Certificate ) int64 {
77+ t .Helper ()
78+ if c == nil {
79+ t .Fatal ("nil certificate" )
80+ }
81+ leaf := c .Leaf
82+ if leaf == nil {
83+ if len (c .Certificate ) == 0 {
84+ t .Fatal ("certificate has no DER data" )
85+ }
86+ parsed , err := x509 .ParseCertificate (c .Certificate [0 ])
87+ if err != nil {
88+ t .Fatalf ("parse served cert: %v" , err )
89+ }
90+ leaf = parsed
6891 }
92+ return leaf .SerialNumber .Int64 ()
6993}
7094
7195// servedSerial completes a TLS handshake against addr and returns the serial number
@@ -84,10 +108,9 @@ func servedSerial(t *testing.T, addr string) int64 {
84108 return certs [0 ].SerialNumber .Int64 ()
85109}
86110
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.
111+ // TestReloadingTLSConfigServesRenewedCertificate is the core regression test: it fails
112+ // against the old code that loads the key pair once into tls.Config.Certificates, and
113+ // passes once the certificate is served via a reloading GetCertificate callback.
91114func TestReloadingTLSConfigServesRenewedCertificate (t * testing.T ) {
92115 dir := t .TempDir ()
93116 certFile := filepath .Join (dir , "tls.crt" )
@@ -136,9 +159,9 @@ func TestReloadingTLSConfigServesRenewedCertificate(t *testing.T) {
136159 }
137160}
138161
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.
162+ // TestCertReloaderKeepsLastGoodCertificate verifies that a failed reload (e.g. a torn
163+ // read while the volume is updated) does not take the webhook down: it keeps serving
164+ // the previously loaded certificate instead of erroring the handshake.
142165func TestCertReloaderKeepsLastGoodCertificate (t * testing.T ) {
143166 dir := t .TempDir ()
144167 certFile := filepath .Join (dir , "tls.crt" )
@@ -156,26 +179,67 @@ func TestCertReloaderKeepsLastGoodCertificate(t *testing.T) {
156179 if err := os .WriteFile (certFile , []byte ("not a certificate" ), 0o600 ); err != nil {
157180 t .Fatalf ("corrupt cert: %v" , err )
158181 }
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- }
182+ setModTime (t , certFile , time .Now ().Add (2 * time .Second ))
162183
163184 got , err := cr .GetCertificate (& tls.ClientHelloInfo {})
164185 if err != nil {
165186 t .Fatalf ("GetCertificate returned error instead of serving last good cert: %v" , err )
166187 }
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
188+ if certSerial (t , got ) != 1 {
189+ t .Fatalf ("expected to keep serving serial 1 after failed reload, got %d" , certSerial (t , got ))
190+ }
191+ }
192+
193+ // TestCertReloaderRetriesAfterTransientFailure is the regression test for the reloader
194+ // abandoning a renewal on a transient error. A failed reload must NOT advance the
195+ // recorded mtime, so once the (unchanged-mtime) file becomes readable the renewed
196+ // certificate is still picked up. Against the code that advanced the mtime on failure,
197+ // the renewal is permanently missed and this test fails.
198+ func TestCertReloaderRetriesAfterTransientFailure (t * testing.T ) {
199+ dir := t .TempDir ()
200+ certFile := filepath .Join (dir , "tls.crt" )
201+ keyFile := filepath .Join (dir , "tls.key" )
202+
203+ certA , keyA := genSelfSigned (t , 1 )
204+ writeKeyPair (t , certFile , keyFile , certA , keyA , time .Now ().Add (- 2 * time .Second ))
205+
206+ cr , err := newCertReloader (certFile , keyFile )
207+ if err != nil {
208+ t .Fatalf ("newCertReloader: %v" , err )
209+ }
210+ cr .retryInterval = 0 // retry immediately, no backoff wait in the test
211+
212+ // Renewal advances the mtime, but the first load fails (simulating a transient,
213+ // content-independent error) by writing an unreadable cert at the new mtime.
214+ renewMod := time .Now ().Add (2 * time .Second )
215+ if err := os .WriteFile (certFile , []byte ("transiently unreadable" ), 0o600 ); err != nil {
216+ t .Fatalf ("write bad cert: %v" , err )
217+ }
218+ setModTime (t , certFile , renewMod )
219+
220+ if got , gerr := cr .GetCertificate (& tls.ClientHelloInfo {}); gerr != nil {
221+ t .Fatalf ("GetCertificate errored during transient failure: %v" , gerr )
222+ } else if certSerial (t , got ) != 1 {
223+ t .Fatalf ("during transient failure: expected to keep serial 1, got %d" , certSerial (t , got ))
224+ }
225+
226+ // The renewed, valid certificate is now readable at the SAME mtime as the failed
227+ // attempt. The reloader must still pick it up on retry.
228+ certB , keyB := genSelfSigned (t , 2 )
229+ if err := os .WriteFile (certFile , certB , 0o600 ); err != nil {
230+ t .Fatalf ("write renewed cert: %v" , err )
231+ }
232+ if err := os .WriteFile (keyFile , keyB , 0o600 ); err != nil {
233+ t .Fatalf ("write renewed key: %v" , err )
234+ }
235+ setModTime (t , certFile , renewMod )
236+ setModTime (t , keyFile , renewMod )
237+
238+ got , gerr := cr .GetCertificate (& tls.ClientHelloInfo {})
239+ if gerr != nil {
240+ t .Fatalf ("GetCertificate errored after retry: %v" , gerr )
177241 }
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 ( ))
242+ if certSerial ( t , got ) != 2 {
243+ t .Fatalf ("after transient failure: expected renewed serial 2 on retry , got %d (mtime advanced on failure, renewal permanently missed) " , certSerial ( t , got ))
180244 }
181245}
0 commit comments