@@ -13,7 +13,6 @@ const log = require('npmlog');
13
13
const { Resolver } = require ( 'dns' ) . promises ;
14
14
const resolver = new Resolver ( ) ;
15
15
const Joi = require ( 'joi' ) ;
16
- const db = require ( '../db' ) ;
17
16
const { SettingsHandler } = require ( '../settings-handler' ) ;
18
17
19
18
if ( config . resolver && config . resolver . ns && config . resolver . ns . length ) {
@@ -40,9 +39,9 @@ let acmeInitialized = false;
40
39
let acmeInitializing = false ;
41
40
let acmeInitPending = [ ] ;
42
41
43
- const ensureAcme = async acmeOptions => {
42
+ const ensureAcme = async ( acmeOptions , certHandler ) => {
44
43
if ( ! settings ) {
45
- settings = new SettingsHandler ( { db : db . database } ) ;
44
+ settings = new SettingsHandler ( { db : certHandler . database } ) ;
46
45
}
47
46
48
47
if ( acmeInitialized ) {
@@ -78,7 +77,7 @@ const ensureAcme = async acmeOptions => {
78
77
} ;
79
78
80
79
const getAcmeAccount = async ( acmeOptions , certHandler ) => {
81
- await ensureAcme ( acmeOptions ) ;
80
+ await ensureAcme ( acmeOptions , certHandler ) ;
82
81
83
82
const entryKey = `acme:account:${ acmeOptions . key } ` ;
84
83
@@ -121,7 +120,7 @@ const getAcmeAccount = async (acmeOptions, certHandler) => {
121
120
} ;
122
121
} ;
123
122
124
- const validateDomain = async domain => {
123
+ const validateDomain = async ( domain , certHandler ) => {
125
124
// check domain name format
126
125
const validation = Joi . string ( )
127
126
. domain ( { tlds : { allow : true } } )
@@ -136,9 +135,9 @@ const validateDomain = async domain => {
136
135
}
137
136
138
137
// check CAA support
139
- const caaDomains = config . acme . caaDomains . map ( normalizeDomain ) . filter ( d => d ) ;
138
+ const caaDomains = certHandler . acme ? .caaDomains . map ( normalizeDomain ) . filter ( d => d ) ;
140
139
141
- if ( caaDomains . length ) {
140
+ if ( caaDomains ? .length ) {
142
141
let parts = domain . split ( '.' ) ;
143
142
for ( let i = 0 ; i < parts . length - 1 ; i ++ ) {
144
143
let subdomain = parts . slice ( i ) . join ( '.' ) ;
@@ -169,7 +168,7 @@ const acquireCert = async (domain, acmeOptions, certificateData, certHandler) =>
169
168
const domainSafeLockKey = `d:lock:safe:${ domain } ` ;
170
169
const domainOpLockKey = `d:lock:op:${ domain } ` ;
171
170
172
- if ( await db . redis . exists ( domainSafeLockKey ) ) {
171
+ if ( await certHandler . redis . exists ( domainSafeLockKey ) ) {
173
172
// nothing to do here, renewal blocked
174
173
log . info ( 'ACME' , 'Renewal blocked by failsafe lock for %s' , domain ) ;
175
174
@@ -179,17 +178,17 @@ const acquireCert = async (domain, acmeOptions, certificateData, certHandler) =>
179
178
180
179
try {
181
180
// throws if can not validate domain
182
- await validateDomain ( domain ) ;
181
+ await validateDomain ( domain , certHandler ) ;
183
182
log . info ( 'ACME' , 'Domain validation for %s passed' , domain ) ;
184
183
} catch ( err ) {
185
- log . error ( 'ACME' , 'Failed to validate domain %s: %s' , domain , err . message ) ;
184
+ log . error ( 'ACME' , 'Failed to validate domain %s: %s' , domain , err . stack ) ;
186
185
return certificateData ;
187
186
}
188
187
189
188
// Use locking to avoid race conditions, first try gets the lock, others wait until first is finished
190
189
if ( ! getLock ) {
191
190
let lock = new Lock ( {
192
- redis : db . redis ,
191
+ redis : certHandler . redis ,
193
192
namespace : 'acme'
194
193
} ) ;
195
194
getLock = ( ...args ) => lock . waitAcquireLock ( ...args ) ;
@@ -212,7 +211,7 @@ const acquireCert = async (domain, acmeOptions, certificateData, certHandler) =>
212
211
if ( ! privateKey ) {
213
212
// generate new key
214
213
log . info ( 'ACME' , 'Provision new private key for %s' , domain ) ;
215
- privateKey = await certHandler . resetPrivateKey ( { _id : certificateData . _id } , config . acme ) ;
214
+ privateKey = await certHandler . resetPrivateKey ( { _id : certificateData . _id } , certHandler . acme ) ;
216
215
}
217
216
218
217
const jwkPrivateKey = pem2jwk ( privateKey ) ;
@@ -236,7 +235,7 @@ const acquireCert = async (domain, acmeOptions, certificateData, certHandler) =>
236
235
domains : [ domain ] ,
237
236
challenges : {
238
237
'http-01' : AcmeChallenge . create ( {
239
- db : db . database
238
+ db : certHandler . database
240
239
} )
241
240
}
242
241
} ;
@@ -290,16 +289,16 @@ const acquireCert = async (domain, acmeOptions, certificateData, certHandler) =>
290
289
return await certHandler . getRecord ( { _id : certificateData . _id } , true ) ;
291
290
} catch ( err ) {
292
291
try {
293
- await db . redis . multi ( ) . set ( domainSafeLockKey , 1 ) . expire ( domainSafeLockKey , BLOCK_RENEW_AFTER_ERROR_TTL ) . exec ( ) ;
292
+ await certHandler . redis . multi ( ) . set ( domainSafeLockKey , 1 ) . expire ( domainSafeLockKey , BLOCK_RENEW_AFTER_ERROR_TTL ) . exec ( ) ;
294
293
} catch ( err ) {
295
- log . error ( 'ACME' , 'Redis call failed key=%s domains=%s error=%s' , domainSafeLockKey , domain , err . message ) ;
294
+ log . error ( 'ACME' , 'Redis call failed key=%s domains=%s error=%s' , domainSafeLockKey , domain , err . stack ) ;
296
295
}
297
296
298
297
log . error ( 'ACME' , 'Failed to generate certificate domains=%s error=%s' , domain , err . stack ) ;
299
298
300
299
if ( certificateData && certificateData . _id ) {
301
300
try {
302
- await db . database . collection ( 'certs' ) . findOneAndUpdate (
301
+ await certHandler . database . collection ( 'certs' ) . findOneAndUpdate (
303
302
{ _id : certificateData . _id } ,
304
303
{
305
304
$set : {
@@ -312,7 +311,7 @@ const acquireCert = async (domain, acmeOptions, certificateData, certHandler) =>
312
311
}
313
312
) ;
314
313
} catch ( err ) {
315
- log . error ( 'ACME' , 'Failed to update certificate record domain=%s error=%s' , domain , err . message ) ;
314
+ log . error ( 'ACME' , 'Failed to update certificate record domain=%s error=%s' , domain , err . stack ) ;
316
315
}
317
316
318
317
certHandler . loggelf ( {
@@ -335,7 +334,7 @@ const acquireCert = async (domain, acmeOptions, certificateData, certHandler) =>
335
334
try {
336
335
await releaseLock ( lock ) ;
337
336
} catch ( err ) {
338
- log . error ( 'Lock' , 'Failed to release lock for %s: %s' , domainOpLockKey , err ) ;
337
+ log . error ( 'Lock' , 'Failed to release lock for %s: %s' , domainOpLockKey , err . stack ) ;
339
338
}
340
339
}
341
340
} ;
0 commit comments