Skip to content

fix(ssl): deploy nginx-proxy certs atomically with checked copies - #491

Open
mrrobot47 wants to merge 3 commits into
EasyEngine:developfrom
mrrobot47:fix/atomic-cert-deploy
Open

mrrobot47 wants to merge 3 commits into
EasyEngine:developfrom
mrrobot47:fix/atomic-cert-deploy

Conversation

@mrrobot47

@mrrobot47 mrrobot47 commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

Problem

moveCertsToNginxProxy() deployed the freshly-issued cert with three sequential bare copy() calls (key, crt, chain) into services/nginx-proxy/certs/, ignoring every return value and with no temp-file + rename. A failure between copies (disk full, permissions, crash) left nginx-proxy pointing at a mismatched key/cert pair. This runs on both first issuance and renewal (the unattended cron path).

Fix

Copy each source to a temp file in the destination directory (checking copy()), then rename() each into place (checking rename()). rename() is atomic on the same filesystem, so a failed copy can no longer leave a half-written live key/cert. On any failure the temp files are cleaned up and an exception is thrown, rather than silently leaving a partial deploy.

Existing deployed files keep their mode on renewal (the temp file takes the live file's permissions before the rename), so a hardened chmod 600 key stays 0600.

Notes

  • The three renames are each atomic per-file but not collectively atomic; the code does not roll back an already-renamed file (a same-filesystem rename failing after a prior success is near-impossible). The comment states this precisely.
  • The method stays void and throws on failure: executeRenewal() already wraps the call in try/catch (surfaces as a renewal warning); on first issuance the exception propagates as a loud failure — preferable to the previous silent broken-cert outcome.
  • Known, pre-existing: if the deploy fails on renewal, acme-conf already holds the new cert, so the next scheduled ssl-renew says "renewal is not necessary" and doesn't redeploy; only ssl-renew --force recovers. Until fix(ssl): include ACME error message in operator-facing warnings #486 lands, the renewal warning still reads "Challenge Authorization failed…"; the real reason is in ee.log.

Testing

Manual: issue/renew a cert and confirm .key/.crt/.chain.pem deploy together with no .tmp leftovers. To force a failure, make the certs dir immutable (chattr +i "$(readlink -f /opt/easyengine/services/nginx-proxy/certs)"; plain permissions don't stop root), run ee site ssl-renew <site> --force, and confirm it fails with Failed to copy certificate file … .tmp in ee.log, no "Certificate renewed successfully!", the live key/cert unchanged and no .tmp files; then chattr -i.

Tested on Ubuntu 26.04 with EasyEngine 4.12.0 with real Let's Encrypt certificates (first issuance, and a forced renewal with the certs dir immutable).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

rename() replaces the live file with a fresh temp created under the umask, so a renewal reset any tightened mode on the deployed key/cert (e.g. 0600 to 0644). The previous in-place copy() kept it; copy the live file's mode onto the temp before renaming.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants