1
1
import createPrisma from '@/lib/create-prisma'
2
- import { verifyDNSRecord , issueDomainCertificate , checkCertificateStatus , getValidationValues , deleteCertificate } from '@/lib/domain-verification'
2
+ import { verifyDNSRecord , issueDomainCertificate , checkCertificateStatus , getValidationValues , deleteDomainCertificate } from '@/lib/domain-verification'
3
3
import { datePivot } from '@/lib/time'
4
4
5
5
const VERIFICATION_INTERVAL = 60 * 5 // 5 minutes
@@ -81,7 +81,7 @@ async function verifyDomain (domain, models) {
81
81
if ( datePivot ( new Date ( ) , { days : VERIFICATION_HOLD_THRESHOLD } ) > domain . updatedAt ) {
82
82
if ( domain . certificate ) {
83
83
// certificate would expire in 72 hours anyway, it's best to delete it
84
- await deleteCertificate ( domain . certificate . certificateArn )
84
+ await deleteDomainCertificate ( domain . certificate . certificateArn )
85
85
}
86
86
return { status : 'HOLD' , message : `Domain ${ domain . domainName } has been put on HOLD because we couldn't verify it in 48 hours` }
87
87
}
@@ -157,22 +157,26 @@ async function requestCertificate (domain, models) {
157
157
let message = null
158
158
159
159
// ask ACM to request a certificate for the domain
160
- const certificateArn = await issueDomainCertificate ( domain . domainName )
160
+ const { certificateArn, error } = await issueDomainCertificate ( domain . domainName )
161
161
162
162
if ( certificateArn ) {
163
163
// check the status of the just created certificate
164
- const certificateStatus = await checkCertificateStatus ( certificateArn )
165
- // store the certificate in the database with its status
166
- await models . domainCertificate . create ( {
167
- data : {
168
- domain : { connect : { id : domain . id } } ,
169
- certificateArn,
170
- status : certificateStatus
171
- }
172
- } )
173
- message = 'An ACM certificate with arn ' + certificateArn + ' has been successfully requested'
164
+ const { certStatus, error : checkError } = await checkCertificateStatus ( certificateArn )
165
+ if ( checkError ) {
166
+ message = 'Could not check certificate status: ' + checkError
167
+ } else {
168
+ // store the certificate in the database with its status
169
+ await models . domainCertificate . create ( {
170
+ data : {
171
+ domain : { connect : { id : domain . id } } ,
172
+ certificateArn,
173
+ status : certStatus
174
+ }
175
+ } )
176
+ message = 'An ACM certificate with arn ' + certificateArn + ' has been successfully requested'
177
+ }
174
178
} else {
175
- message = 'Could not request an ACM certificate'
179
+ message = 'Could not request an ACM certificate: ' + error
176
180
}
177
181
178
182
const status = certificateArn ? 'PENDING' : 'FAILED'
@@ -184,45 +188,45 @@ async function getACMValidationValues (domain, models, certificateArn) {
184
188
let message = null
185
189
186
190
// get the validation values for the certificate
187
- const validationValues = await getValidationValues ( certificateArn )
188
- if ( validationValues ) {
191
+ const { cname , value , error } = await getValidationValues ( certificateArn )
192
+ if ( cname && value ) {
189
193
// store the validation values in the database
190
194
await models . domainVerificationRecord . create ( {
191
195
data : {
192
196
domain : { connect : { id : domain . id } } ,
193
197
type : 'SSL' ,
194
- recordName : validationValues . cname ,
195
- recordValue : validationValues . value
198
+ recordName : cname ,
199
+ recordValue : value
196
200
}
197
201
} )
198
202
message = 'Validation values stored'
199
203
} else {
200
- message = 'Could not get validation values'
204
+ message = 'Could not get validation values: ' + error
201
205
}
202
206
203
- const status = validationValues ? 'PENDING' : 'FAILED'
207
+ const status = cname && value ? 'PENDING' : 'FAILED'
204
208
await logAttempt ( { domain, models, stage : 'ACM_REQUEST_VALIDATION_VALUES' , status, message } )
205
209
return status !== 'FAILED'
206
210
}
207
211
208
212
async function checkACMValidation ( domain , models , record ) {
209
213
let message = null
210
214
211
- const certificateStatus = await checkCertificateStatus ( domain . certificate . certificateArn )
212
- if ( certificateStatus ) {
213
- if ( certificateStatus !== domain . certificate . status ) {
214
- console . log ( `certificate status for ${ domain . domainName } has changed from ${ domain . certificate . status } to ${ certificateStatus } ` )
215
+ const { certStatus , error } = await checkCertificateStatus ( domain . certificate . certificateArn )
216
+ if ( certStatus ) {
217
+ if ( certStatus !== domain . certificate . status ) {
218
+ console . log ( `certificate status for ${ domain . domainName } has changed from ${ domain . certificate . status } to ${ certStatus } ` )
215
219
await models . domainCertificate . update ( {
216
220
where : { id : domain . certificate . id } ,
217
- data : { status : certificateStatus }
221
+ data : { status : certStatus }
218
222
} )
219
223
}
220
- message = `Certificate status is: ${ certificateStatus } `
224
+ message = `Certificate status is: ${ certStatus } `
221
225
} else {
222
- message = 'Could not check certificate status'
226
+ message = 'Could not check certificate status: ' + error
223
227
}
224
228
225
- const status = certificateStatus === 'ISSUED' ? 'VERIFIED' : 'PENDING'
229
+ const status = certStatus === 'ISSUED' ? 'VERIFIED' : 'PENDING'
226
230
await logAttempt ( { domain, models, record, stage : 'ACM_VALIDATION' , status, message } )
227
231
return status === 'VERIFIED'
228
232
}
0 commit comments