@@ -6,33 +6,32 @@ export async function domainVerification () {
6
6
const models = createPrisma ( { connectionParams : { connection_limit : 1 } } )
7
7
8
8
try {
9
- const domains = await models . customDomain . findMany ( )
9
+ const domains = await models . customDomain . findMany ( { where : { OR : [ { dnsState : 'PENDING' } , { sslState : 'PENDING' } ] } } )
10
10
11
11
for ( const domain of domains ) {
12
- const { domain : domainName , dnsState, sslState, certificateArn, verificationTxt, id } = domain
13
12
try {
14
- const data = { lastVerifiedAt : new Date ( ) }
13
+ const data = { ... domain , lastVerifiedAt : new Date ( ) }
15
14
// 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' } ` )
19
18
data . dnsState = txtValid && cnameValid ? 'VERIFIED' : 'FAILED'
20
19
}
21
- // TODO: make this consequential, don't wait for the next cron to issue the certificate
20
+
22
21
// 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 } ` )
26
25
if ( certificateArn ) {
27
26
const sslState = await checkCertificateStatus ( certificateArn )
28
- console . log ( `${ domainName } : Issued certificate status: ${ sslState } ` )
27
+ console . log ( `${ domain . name } : Issued certificate status: ${ sslState } ` )
29
28
if ( sslState === 'PENDING' ) {
30
29
try {
31
30
const { cname, value } = await getValidationValues ( certificateArn )
32
31
data . verificationCname = cname
33
32
data . verificationCnameValue = value
34
33
} 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 )
36
35
}
37
36
}
38
37
if ( sslState ) data . sslState = sslState
@@ -43,21 +42,21 @@ export async function domainVerification () {
43
42
}
44
43
45
44
// 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 } ` )
49
48
if ( sslState ) data . sslState = sslState
50
49
}
51
50
52
- await models . customDomain . update ( { where : { id } , data } )
51
+ await models . customDomain . update ( { where : { id : domain . id } , data } )
53
52
} catch ( error ) {
54
53
// 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 )
56
55
57
56
// TODO: DNS inconcistencies can happen, we should retry at least 3 times before marking it as FAILED
58
57
// Update to FAILED on any error
59
58
await models . customDomain . update ( {
60
- where : { id } ,
59
+ where : { id : domain . id } ,
61
60
data : { dnsState : 'FAILED' , lastVerifiedAt : new Date ( ) }
62
61
} )
63
62
}
0 commit comments