Skip to content

Commit

Permalink
Fixes #52 - Ensure an error during cert renewal does not leave the sy…
Browse files Browse the repository at this point in the history
…stem in a failure state
  • Loading branch information
zsteinkamp committed Mar 25, 2024
1 parent c0342f8 commit 4465c6f
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ async function clientAutoModeInternal(
}

const pkeyPath = joinPaths(prefix, commonName + KEY_SUFFIX)
const tempPkeyPath = pkeyPath + '.tmp'
const csrPath = joinPaths(prefix, commonName + CERTIFICATE_REQ_SUFFIX)
const certPath = joinPaths(prefix, commonName + CERTIFICATE_SUFFIX)

Expand Down Expand Up @@ -168,8 +169,8 @@ async function clientAutoModeInternal(
csr.keys.privateKey
)) as ArrayBuffer
pkeyPem = toPEM(privKey, 'PRIVATE KEY')
fs.writeFileSync(pkeyPath, pkeyPem)
log.info(`Wrote private key to ${pkeyPath}`)
fs.writeFileSync(tempPkeyPath, pkeyPem)
log.info(`Wrote private key to ${tempPkeyPath}`)

const challengePath = acmeChallengeDir(r)

Expand Down Expand Up @@ -202,8 +203,11 @@ async function clientAutoModeInternal(
},
})
certInfo = await readCertificateInfo(certificatePem)

fs.writeFileSync(certPath, certificatePem)
log.info(`Wrote certificate to ${certPath}`)
fs.renameSync(tempPkeyPath, pkeyPath)
log.info(`Renamed ${tempPkeyPath} to ${pkeyPath}`)

// Purge the cert/key in the shared dict zone if applicable
purgeCachedCertKey(r)
Expand Down

0 comments on commit 4465c6f

Please sign in to comment.