1
1
import createPrisma from '@/lib/create-prisma'
2
- import { verifyDomainDNS , issueDomainCertificate , checkCertificateStatus } from '@/lib/domains'
2
+ import { verifyDomainDNS , issueDomainCertificate , checkCertificateStatus , getValidationValues } from '@/lib/domains'
3
3
4
4
// TODO: Add comments
5
5
export async function domainVerification ( ) {
@@ -9,23 +9,32 @@ export async function domainVerification () {
9
9
const domains = await models . customDomain . findMany ( )
10
10
11
11
for ( const domain of domains ) {
12
- const { domain : domainName , dnsState, sslState, certificateArn, verificationTxt, cname , id } = domain
12
+ const { domain : domainName , dnsState, sslState, certificateArn, verificationTxt, id } = domain
13
13
try {
14
14
const data = { lastVerifiedAt : new Date ( ) }
15
15
// DNS verification
16
16
if ( dnsState === 'PENDING' || dnsState === 'FAILED' ) {
17
- const { txtValid, cnameValid } = await verifyDomainDNS ( domainName , verificationTxt , cname )
17
+ const { txtValid, cnameValid } = await verifyDomainDNS ( domainName , verificationTxt )
18
18
console . log ( `${ domainName } : TXT ${ txtValid ? 'valid' : 'invalid' } , CNAME ${ cnameValid ? 'valid' : 'invalid' } ` )
19
19
data . dnsState = txtValid && cnameValid ? 'VERIFIED' : 'FAILED'
20
20
}
21
-
21
+ // TODO: make this consequential, don't wait for the next cron to issue the certificate
22
22
// SSL issuing
23
23
if ( dnsState === 'VERIFIED' && ( ! certificateArn || sslState === 'FAILED' ) ) {
24
24
const certificateArn = await issueDomainCertificate ( domainName )
25
25
console . log ( `${ domainName } : Certificate issued: ${ certificateArn } ` )
26
26
if ( certificateArn ) {
27
27
const sslState = await checkCertificateStatus ( certificateArn )
28
28
console . log ( `${ domainName } : Issued certificate status: ${ sslState } ` )
29
+ if ( sslState === 'PENDING' ) {
30
+ try {
31
+ const { cname, value } = await getValidationValues ( certificateArn )
32
+ data . verificationCname = cname
33
+ data . verificationCnameValue = value
34
+ } catch ( error ) {
35
+ console . error ( `Failed to get validation values for domain ${ domainName } :` , error )
36
+ }
37
+ }
29
38
if ( sslState ) data . sslState = sslState
30
39
data . certificateArn = certificateArn
31
40
} else {
@@ -42,7 +51,7 @@ export async function domainVerification () {
42
51
43
52
await models . customDomain . update ( { where : { id } , data } )
44
53
} catch ( error ) {
45
- // TODO: this considers only DNS verification errors , we should also consider SSL verification errors
54
+ // TODO: this declares any error as a DNS verification error , we should also consider SSL verification errors
46
55
console . error ( `Failed to verify domain ${ domainName } :` , error )
47
56
48
57
// TODO: DNS inconcistencies can happen, we should retry at least 3 times before marking it as FAILED
0 commit comments