Skip to content

Commit ea49073

Browse files
committed
BUG/MEDIUM: write certificates on disk even if the runtime update failed
If the runtime update of a certificate failed, we were not writing the certificate on disk (delayed or not delayed) and were triggering a reload (which is ok). But the content of the file disk was wrong. Now we properly writes the certificate on disk if it has changed. #721
1 parent eac5717 commit ea49073

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

pkg/haproxy/certs/main.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ func (c *certs) AddSecret(secret *store.Secret, secretType SecretType) (certPath
161161
}
162162

163163
func (c *certs) updateRuntime(filename string, payload []byte, isCa bool) (bool, error) {
164-
// if instance.NeedReload() {
165-
// return false, nil
166-
// }
167164
// Only 1 transaction in parallel is possible for now in haproxy
168165
// Keep this mutex for now to ensure that we perform 1 transaction at a time
169166
certType := "cert"
@@ -232,9 +229,6 @@ func (c *certs) updateRuntime(filename string, payload []byte, isCa bool) (bool,
232229
}
233230

234231
func (c *certs) deleteRuntime(crtList, filename string) error {
235-
if instance.NeedReload() {
236-
return nil
237-
}
238232
// Only 1 transaction in parallel is possible for now in haproxy
239233
// Keep this mutex for now to ensure that we perform 1 transaction at a time
240234
c.mu.Lock()
@@ -318,6 +312,13 @@ func (c *certs) refreshCerts(certs map[string]*cert, certDir string) {
318312
// certificate file name should be already in the format: certName.pem
319313
certName := strings.Split(filename, ".pem")[0]
320314
crt, crtOk := certs[certName]
315+
// SKIP temporary file created by renameio
316+
// fileName .e2e-tests-https-runtime_haproxy-offload-test.pem2179154433
317+
// revisit this, take time to think about another way
318+
if certName+".pem" != filename {
319+
// This happens with temp files: created by renameio
320+
continue
321+
}
321322
if !crtOk || !crt.inUse {
322323
err := c.deleteRuntime(certDir, filename)
323324
if err != nil {
@@ -398,18 +399,15 @@ func (c *certs) writeCert(cert *cert, filename string, content []byte, isCa bool
398399
cert.updated = true
399400
}
400401

401-
// If the certificate has been updated through the runtime, it needs to be written with the delayed function
402-
// to be written on disk before a reload.
403-
if cert.updated {
404-
fs.AddDelayedFunc(filename, func() {
405-
err := renameio.WriteFile(filename, content, 0o666)
406-
if err != nil {
407-
logger.Error(err)
408-
return
409-
}
410-
utils.GetLogger().Debugf("Delayed writing cert on disk ok [%s] ", filename)
411-
})
412-
}
402+
// In runtime failed or did succeed, it needs to be written on disk.
403+
fs.AddDelayedFunc(filename, func() {
404+
err := renameio.WriteFile(filename, content, 0o666)
405+
if err != nil {
406+
logger.Error(err)
407+
return
408+
}
409+
utils.GetLogger().Debugf("Delayed writing cert on disk ok [%s] ", filename)
410+
})
413411
})
414412

415413
return nil

0 commit comments

Comments
 (0)