Skip to content

Commit d827cd6

Browse files
authored
Fixes #52 - Ensure key/cert are in a consistent state despite errors (#53)
* Fixes #52 - Ensure an error during cert renewal does not leave the system in a failure state * fixup CHANGELOG.md
1 parent c0342f8 commit d827cd6

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Changelog
22

3-
## 1.0.0 (Month Date, Year)
3+
## 1.0.0 (March 25, 2024)
44

5-
Initial release of the NGINX template repository.
5+
Initial release of njs-acme.

src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ async function clientAutoModeInternal(
8888
}
8989

9090
const pkeyPath = joinPaths(prefix, commonName + KEY_SUFFIX)
91+
const tempPkeyPath = pkeyPath + '.tmp'
9192
const csrPath = joinPaths(prefix, commonName + CERTIFICATE_REQ_SUFFIX)
9293
const certPath = joinPaths(prefix, commonName + CERTIFICATE_SUFFIX)
9394

@@ -168,8 +169,8 @@ async function clientAutoModeInternal(
168169
csr.keys.privateKey
169170
)) as ArrayBuffer
170171
pkeyPem = toPEM(privKey, 'PRIVATE KEY')
171-
fs.writeFileSync(pkeyPath, pkeyPem)
172-
log.info(`Wrote private key to ${pkeyPath}`)
172+
fs.writeFileSync(tempPkeyPath, pkeyPem)
173+
log.info(`Wrote private key to ${tempPkeyPath}`)
173174

174175
const challengePath = acmeChallengeDir(r)
175176

@@ -204,6 +205,8 @@ async function clientAutoModeInternal(
204205
certInfo = await readCertificateInfo(certificatePem)
205206
fs.writeFileSync(certPath, certificatePem)
206207
log.info(`Wrote certificate to ${certPath}`)
208+
fs.renameSync(tempPkeyPath, pkeyPath)
209+
log.info(`Renamed ${tempPkeyPath} to ${pkeyPath}`)
207210

208211
// Purge the cert/key in the shared dict zone if applicable
209212
purgeCachedCertKey(r)

0 commit comments

Comments
 (0)