Skip to content

Commit 1d04948

Browse files
committed
adjust schema and worker validation
1 parent 9552bf8 commit 1d04948

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

api/resolvers/sub.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ export default {
311311
return await models.customDomain.create({
312312
data: {
313313
domain,
314+
dnsState: 'PENDING',
314315
cname: 'todo', // TODO: explore other options
315316
verificationTxt: randomBytes(32).toString('base64'), // TODO: explore other options
316317
sub: {

api/typeDefs/sub.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ export default gql`
1414
updatedAt: Date!
1515
domain: String!
1616
subName: String!
17-
dnsState: String!
18-
sslState: String!
17+
dnsState: String
18+
sslState: String
1919
certificateArn: String
2020
lastVerifiedAt: Date
21-
cname: String!
22-
verificationTxt: String!
21+
cname: String
22+
verificationTxt: String
2323
}
2424
2525
type Subs {

prisma/migrations/20250304121322_custom_domains/migration.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ CREATE TABLE "CustomDomain" (
55
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
66
"domain" TEXT NOT NULL,
77
"subName" CITEXT NOT NULL,
8-
"dnsState" TEXT NOT NULL DEFAULT 'PENDING',
9-
"sslState" TEXT NOT NULL DEFAULT 'PENDING',
8+
"dnsState" TEXT,
9+
"sslState" TEXT,
1010
"certificateArn" TEXT,
1111
"lastVerifiedAt" TIMESTAMP(3),
12-
"cname" TEXT NOT NULL,
13-
"verificationTxt" TEXT NOT NULL,
12+
"cname" TEXT,
13+
"verificationTxt" TEXT,
1414

1515
CONSTRAINT "CustomDomain_pkey" PRIMARY KEY ("id")
1616
);

prisma/schema.prisma

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,12 +1207,12 @@ model CustomDomain {
12071207
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
12081208
domain String @unique
12091209
subName String @unique @db.Citext
1210-
dnsState String @default("PENDING")
1211-
sslState String @default("PENDING")
1212-
certificateArn String
1210+
dnsState String?
1211+
sslState String?
1212+
certificateArn String?
12131213
lastVerifiedAt DateTime?
1214-
cname String
1215-
verificationTxt String
1214+
cname String?
1215+
verificationTxt String?
12161216
sub Sub @relation(fields: [subName], references: [name], onDelete: Cascade, onUpdate: Cascade)
12171217
12181218
@@index([domain])

worker/domainVerification.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,40 @@ export async function domainVerification () {
99
const domains = await models.customDomain.findMany()
1010

1111
for (const domain of domains) {
12-
const { domain: domainName, verificationTxt, cname, id } = domain
12+
const { domain: domainName, dnsState, sslState, certificateArn, verificationTxt, cname, id } = domain
1313
try {
1414
const data = { lastVerifiedAt: new Date() }
1515
// DNS verification
16-
if (domain.dnsState === 'PENDING' || domain.dnsState === 'FAILED') {
16+
if (dnsState === 'PENDING' || dnsState === 'FAILED') {
1717
const { txtValid, cnameValid } = await verifyDomainDNS(domainName, verificationTxt, cname)
1818
console.log(`${domainName}: TXT ${txtValid ? 'valid' : 'invalid'}, CNAME ${cnameValid ? 'valid' : 'invalid'}`)
1919
data.dnsState = txtValid && cnameValid ? 'VERIFIED' : 'FAILED'
2020
}
2121

2222
// SSL issuing
23-
if (domain.dnsState === 'VERIFIED' && (domain.sslState === 'NOT_ISSUED' || domain.sslState === 'FAILED')) {
23+
if (dnsState === 'VERIFIED' && (!certificateArn || sslState === 'FAILED')) {
2424
const certificateArn = await issueDomainCertificate(domainName)
2525
console.log(`${domainName}: Certificate issued: ${certificateArn}`)
2626
if (certificateArn) {
2727
const sslState = await checkCertificateStatus(certificateArn)
2828
console.log(`${domainName}: Issued certificate status: ${sslState}`)
2929
if (sslState) data.sslState = sslState
3030
data.certificateArn = certificateArn
31+
} else {
32+
data.sslState = 'FAILED'
3133
}
3234
}
3335

3436
// SSL checking
35-
if (domain.dnsState === 'VERIFIED' && domain.sslState === 'PENDING') {
36-
const sslState = await checkCertificateStatus(domain.certificateArn)
37+
if (dnsState === 'VERIFIED' && sslState === 'PENDING') {
38+
const sslState = await checkCertificateStatus(certificateArn)
3739
console.log(`${domainName}: Certificate status: ${sslState}`)
3840
if (sslState) data.sslState = sslState
3941
}
4042

4143
await models.customDomain.update({ where: { id }, data })
4244
} catch (error) {
45+
// TODO: this considers only DNS verification errors, we should also consider SSL verification errors
4346
console.error(`Failed to verify domain ${domainName}:`, error)
4447

4548
// TODO: DNS inconcistencies can happen, we should retry at least 3 times before marking it as FAILED

0 commit comments

Comments
 (0)