Skip to content

Commit 61c03e1

Browse files
committed
fix(SNI): delete expired autogenerated SNI certificate
1 parent 40db519 commit 61c03e1

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

lib/cert-handler.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,34 @@ class CertHandler {
130130
{
131131
upsert: false,
132132
returnDocument: 'after',
133-
projection: { _id: true }
133+
projection: { _id: true, autogenerated: true, expires: true, servername: true }
134134
}
135135
);
136136

137-
if (r && r.value) {
137+
if (r?.value) {
138+
const certData = r.value;
139+
const now = new Date();
140+
141+
if (certData.autogenerated && certData.expires < now) {
142+
// delete expired automatic cert, do not try to renew it
143+
try {
144+
let r = await this.database.collection('certs').deleteOne({ _id: certData._id });
145+
if (r?.deletedCount) {
146+
this.loggelf({
147+
short_message: `Deleted autogenerated certificate ${certData.cervername}`,
148+
_sni_servername: certData.cervername,
149+
_cert_action: 'sni_autodelete'
150+
});
151+
}
152+
} catch (err) {
153+
//ignore
154+
}
155+
156+
return await this.getNextRenewal();
157+
}
158+
138159
// use getRecord to decrypt secrets
139-
return await this.getRecord({ _id: r.value._id }, true);
160+
return await this.getRecord({ _id: certData._id }, true);
140161
}
141162

142163
return false;
@@ -728,7 +749,7 @@ class CertHandler {
728749
log.verbose('Certs', 'ACME precheck passed. action=precheck domain=%s', domain);
729750

730751
this.loggelf({
731-
short_message: ` Autogenerating TLS certificate for ${domain}`,
752+
short_message: `Autogenerating TLS certificate for ${domain}`,
732753
_sni_servername: domain,
733754
_cert_action: 'sni_autogenerate'
734755
});

0 commit comments

Comments
 (0)