@@ -117,25 +117,22 @@ async function verifyDomain (domain, models) {
117
117
let certificateArn = domain . certificate ?. certificateArn || null
118
118
if ( ! certificateArn ) {
119
119
certificateArn = await requestCertificate ( domain , models )
120
- if ( ! certificateArn ) return { status, message : 'Certificate issuance has failed.' }
121
120
}
122
121
123
122
// STEP 2b: Get the validation values for the certificate
124
123
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 )
127
125
128
126
// return PENDING to check ACM validation later
129
127
return { status, message : 'Certificate issued and validation values stored.' }
130
128
}
131
129
132
130
// STEP 2c: Check ACM validation
133
131
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 .' }
135
133
136
134
// 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 )
139
136
} catch ( error ) {
140
137
await logAttempt ( { domain, models, stage : 'GENERAL' , status, message : 'ACM services error: ' + error . message } )
141
138
throw error
@@ -181,19 +178,31 @@ async function requestCertificate (domain, models) {
181
178
const { certStatus, error : checkError } = await checkCertificateStatus ( certificateArn )
182
179
if ( checkError ) {
183
180
message = 'Could not check certificate status: ' + checkError
181
+ throw new Error ( message )
184
182
} 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 )
191
200
}
192
- } )
193
- message = 'An ACM certificate with arn ' + certificateArn + ' has been successfully requested'
201
+ }
194
202
}
195
203
} else {
196
204
message = 'Could not request an ACM certificate: ' + error
205
+ throw new Error ( message )
197
206
}
198
207
199
208
const status = certificateArn ? 'PENDING' : 'FAILED'
@@ -207,18 +216,29 @@ async function getACMValidationValues (domain, models, certificateArn) {
207
216
// get the validation values for the certificate
208
217
const { cname, value, error } = await getValidationValues ( certificateArn )
209
218
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 )
217
237
}
218
- } )
219
- message = 'Validation values stored'
238
+ }
220
239
} else {
221
240
message = 'Could not get validation values: ' + error
241
+ throw new Error ( message )
222
242
}
223
243
224
244
const status = cname && value ? 'PENDING' : 'FAILED'
@@ -241,6 +261,7 @@ async function checkACMValidation (domain, models, record) {
241
261
message = `Certificate status is: ${ certStatus } `
242
262
} else {
243
263
message = 'Could not check certificate status: ' + error
264
+ throw new Error ( message )
244
265
}
245
266
246
267
const status = certStatus === 'ISSUED' ? 'VERIFIED' : 'PENDING'
@@ -257,6 +278,7 @@ async function attachACMCertificateToELB (domain, models, certificateArn) {
257
278
message = `Certificate ${ certificateArn } is now attached to ELB listener`
258
279
} else {
259
280
message = `Could not attach certificate ${ certificateArn } to ELB listener: ${ error . message } `
281
+ throw new Error ( message )
260
282
}
261
283
262
284
const status = ! error ? 'VERIFIED' : 'FAILED'
0 commit comments