Skip to content

Commit 51aadf2

Browse files
committed
throw database and AWS-related errors; don't log the STAGE on critical errors, instead catch them and store a log
1 parent f36eef4 commit 51aadf2

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

worker/domainVerification.js

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,22 @@ async function verifyDomain (domain, models) {
117117
let certificateArn = domain.certificate?.certificateArn || null
118118
if (!certificateArn) {
119119
certificateArn = await requestCertificate(domain, models)
120-
if (!certificateArn) return { status, message: 'Certificate issuance has failed.' }
121120
}
122121

123122
// STEP 2b: Get the validation values for the certificate
124123
if (certificateArn && !recordMap.SSL) {
125-
const validationValues = await getACMValidationValues(domain, models, certificateArn)
126-
if (!validationValues) return { status, message: 'Could not get validation values.' }
124+
await getACMValidationValues(domain, models, certificateArn)
127125

128126
// return PENDING to check ACM validation later
129127
return { status, message: 'Certificate issued and validation values stored.' }
130128
}
131129

132130
// STEP 2c: Check ACM validation
133131
const sslVerified = await checkACMValidation(domain, models, recordMap.SSL)
134-
if (!sslVerified) return { status, message: 'ACM validation has failed.' }
132+
if (!sslVerified) return { status, message: 'ACM validation is still pending.' }
135133

136134
// STEP 2d: Attach the certificate to the ELB listener
137-
const elbAttached = await attachACMCertificateToELB(domain, models, certificateArn)
138-
if (!elbAttached) return { status, message: 'ELB attachment has failed.' }
135+
await attachACMCertificateToELB(domain, models, certificateArn)
139136
} catch (error) {
140137
await logAttempt({ domain, models, stage: 'GENERAL', status, message: 'ACM services error: ' + error.message })
141138
throw error
@@ -181,19 +178,31 @@ async function requestCertificate (domain, models) {
181178
const { certStatus, error: checkError } = await checkCertificateStatus(certificateArn)
182179
if (checkError) {
183180
message = 'Could not check certificate status: ' + checkError
181+
throw new Error(message)
184182
} else {
185-
// store the certificate in the database with its status
186-
await models.domainCertificate.create({
187-
data: {
188-
domain: { connect: { id: domain.id } },
189-
certificateArn,
190-
status: certStatus
183+
try {
184+
// store the certificate in the database with its status
185+
await models.domainCertificate.create({
186+
data: {
187+
domain: { connect: { id: domain.id } },
188+
certificateArn,
189+
status: certStatus
190+
}
191+
})
192+
message = 'An ACM certificate with arn ' + certificateArn + ' has been successfully requested'
193+
} catch (e) {
194+
// if record already exists, move on
195+
if (e.code === 'P2002') {
196+
message = 'Certificate already stored'
197+
} else {
198+
message = 'Could not store certificate in the database: ' + e.message
199+
throw new Error(message)
191200
}
192-
})
193-
message = 'An ACM certificate with arn ' + certificateArn + ' has been successfully requested'
201+
}
194202
}
195203
} else {
196204
message = 'Could not request an ACM certificate: ' + error
205+
throw new Error(message)
197206
}
198207

199208
const status = certificateArn ? 'PENDING' : 'FAILED'
@@ -207,18 +216,29 @@ async function getACMValidationValues (domain, models, certificateArn) {
207216
// get the validation values for the certificate
208217
const { cname, value, error } = await getValidationValues(certificateArn)
209218
if (cname && value) {
210-
// store the validation values in the database
211-
await models.domainVerificationRecord.create({
212-
data: {
213-
domain: { connect: { id: domain.id } },
214-
type: 'SSL',
215-
recordName: cname,
216-
recordValue: value
219+
try {
220+
// store the validation values in the database
221+
await models.domainVerificationRecord.create({
222+
data: {
223+
domain: { connect: { id: domain.id } },
224+
type: 'SSL',
225+
recordName: cname,
226+
recordValue: value
227+
}
228+
})
229+
message = 'Validation values stored'
230+
} catch (e) {
231+
// if record already exists, move on
232+
if (e.code === 'P2002') {
233+
message = 'Validation values already stored'
234+
} else {
235+
message = 'Could not store validation values: ' + e.message
236+
throw new Error(message)
217237
}
218-
})
219-
message = 'Validation values stored'
238+
}
220239
} else {
221240
message = 'Could not get validation values: ' + error
241+
throw new Error(message)
222242
}
223243

224244
const status = cname && value ? 'PENDING' : 'FAILED'
@@ -241,6 +261,7 @@ async function checkACMValidation (domain, models, record) {
241261
message = `Certificate status is: ${certStatus}`
242262
} else {
243263
message = 'Could not check certificate status: ' + error
264+
throw new Error(message)
244265
}
245266

246267
const status = certStatus === 'ISSUED' ? 'VERIFIED' : 'PENDING'
@@ -257,6 +278,7 @@ async function attachACMCertificateToELB (domain, models, certificateArn) {
257278
message = `Certificate ${certificateArn} is now attached to ELB listener`
258279
} else {
259280
message = `Could not attach certificate ${certificateArn} to ELB listener: ${error.message}`
281+
throw new Error(message)
260282
}
261283

262284
const status = !error ? 'VERIFIED' : 'FAILED'

0 commit comments

Comments
 (0)