Skip to content

Commit 0c79d9f

Browse files
committed
consequential domain verification flow
1 parent 390a15d commit 0c79d9f

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

worker/domainVerification.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,32 @@ export async function domainVerification () {
66
const models = createPrisma({ connectionParams: { connection_limit: 1 } })
77

88
try {
9-
const domains = await models.customDomain.findMany()
9+
const domains = await models.customDomain.findMany({ where: { OR: [{ dnsState: 'PENDING' }, { sslState: 'PENDING' }] } })
1010

1111
for (const domain of domains) {
12-
const { domain: domainName, dnsState, sslState, certificateArn, verificationTxt, id } = domain
1312
try {
14-
const data = { lastVerifiedAt: new Date() }
13+
const data = { ...domain, lastVerifiedAt: new Date() }
1514
// DNS verification
16-
if (dnsState === 'PENDING' || dnsState === 'FAILED') {
17-
const { txtValid, cnameValid } = await verifyDomainDNS(domainName, verificationTxt)
18-
console.log(`${domainName}: TXT ${txtValid ? 'valid' : 'invalid'}, CNAME ${cnameValid ? 'valid' : 'invalid'}`)
15+
if (data.dnsState === 'PENDING' || data.dnsState === 'FAILED') {
16+
const { txtValid, cnameValid } = await verifyDomainDNS(domain.name, domain.verificationTxt)
17+
console.log(`${domain.name}: TXT ${txtValid ? 'valid' : 'invalid'}, CNAME ${cnameValid ? 'valid' : 'invalid'}`)
1918
data.dnsState = txtValid && cnameValid ? 'VERIFIED' : 'FAILED'
2019
}
21-
// TODO: make this consequential, don't wait for the next cron to issue the certificate
20+
2221
// SSL issuing
23-
if (dnsState === 'VERIFIED' && (!certificateArn || sslState === 'FAILED')) {
24-
const certificateArn = await issueDomainCertificate(domainName)
25-
console.log(`${domainName}: Certificate issued: ${certificateArn}`)
22+
if (data.dnsState === 'VERIFIED' && (!data.certificateArn || data.sslState === 'FAILED')) {
23+
const certificateArn = await issueDomainCertificate(domain.name)
24+
console.log(`${domain.name}: Certificate issued: ${certificateArn}`)
2625
if (certificateArn) {
2726
const sslState = await checkCertificateStatus(certificateArn)
28-
console.log(`${domainName}: Issued certificate status: ${sslState}`)
27+
console.log(`${domain.name}: Issued certificate status: ${sslState}`)
2928
if (sslState === 'PENDING') {
3029
try {
3130
const { cname, value } = await getValidationValues(certificateArn)
3231
data.verificationCname = cname
3332
data.verificationCnameValue = value
3433
} catch (error) {
35-
console.error(`Failed to get validation values for domain ${domainName}:`, error)
34+
console.error(`Failed to get validation values for domain ${domain.name}:`, error)
3635
}
3736
}
3837
if (sslState) data.sslState = sslState
@@ -43,21 +42,21 @@ export async function domainVerification () {
4342
}
4443

4544
// SSL checking
46-
if (dnsState === 'VERIFIED' && sslState === 'PENDING') {
47-
const sslState = await checkCertificateStatus(certificateArn)
48-
console.log(`${domainName}: Certificate status: ${sslState}`)
45+
if (data.dnsState === 'VERIFIED' && data.sslState === 'PENDING') {
46+
const sslState = await checkCertificateStatus(data.certificateArn)
47+
console.log(`${domain.name}: Certificate status: ${sslState}`)
4948
if (sslState) data.sslState = sslState
5049
}
5150

52-
await models.customDomain.update({ where: { id }, data })
51+
await models.customDomain.update({ where: { id: domain.id }, data })
5352
} catch (error) {
5453
// TODO: this declares any error as a DNS verification error, we should also consider SSL verification errors
55-
console.error(`Failed to verify domain ${domainName}:`, error)
54+
console.error(`Failed to verify domain ${domain.name}:`, error)
5655

5756
// TODO: DNS inconcistencies can happen, we should retry at least 3 times before marking it as FAILED
5857
// Update to FAILED on any error
5958
await models.customDomain.update({
60-
where: { id },
59+
where: { id: domain.id },
6160
data: { dnsState: 'FAILED', lastVerifiedAt: new Date() }
6261
})
6362
}

0 commit comments

Comments
 (0)