From 8178d61eca6fd5799c190ece27cfcf211f378cb1 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Fri, 2 Jun 2023 14:22:58 +0800 Subject: [PATCH] refactor(core): persist dns records to database (#3950) --- packages/core/src/libraries/domain.test.ts | 25 --------------------- packages/core/src/libraries/domain.ts | 23 ++++++++++--------- packages/core/src/utils/cloudflare/utils.ts | 11 +++++++++ 3 files changed, 23 insertions(+), 36 deletions(-) diff --git a/packages/core/src/libraries/domain.test.ts b/packages/core/src/libraries/domain.test.ts index 8b6ff0ea6..db3e9bd2e 100644 --- a/packages/core/src/libraries/domain.test.ts +++ b/packages/core/src/libraries/domain.test.ts @@ -99,16 +99,6 @@ describe('syncDomainStatus()', () => { getCustomHostname.mockResolvedValueOnce(mockCloudflareDataPendingSSL); const response = await syncDomainStatus(mockDomainWithCloudflareData); expect(response.status).toBe(DomainStatus.PendingSsl); - expect(response.dnsRecords).not.toContainEqual({ - type: 'CNAME', - name: mockDomainWithCloudflareData.domain, - value: fallbackOrigin, - }); - expect(response.dnsRecords).not.toContainEqual({ - type: 'TXT', - name: mockTxtName, - value: mockTxtValue, - }); expect(response.dnsRecords).toContainEqual({ type: 'TXT', name: mockSslTxtName, @@ -120,21 +110,6 @@ describe('syncDomainStatus()', () => { getCustomHostname.mockResolvedValueOnce(mockCloudflareDataActive); const response = await syncDomainStatus(mockDomainWithCloudflareData); expect(response.status).toBe(DomainStatus.Active); - expect(response.dnsRecords).not.toContainEqual({ - type: 'CNAME', - name: mockDomainWithCloudflareData.domain, - value: fallbackOrigin, - }); - expect(response.dnsRecords).not.toContainEqual({ - type: 'TXT', - name: mockTxtName, - value: mockTxtValue, - }); - expect(response.dnsRecords).not.toContainEqual({ - type: 'TXT', - name: mockSslTxtName, - value: mockSslTxtValue, - }); }); it('should sync and get verification error', async () => { diff --git a/packages/core/src/libraries/domain.ts b/packages/core/src/libraries/domain.ts index 8afddb487..40c49e2b6 100644 --- a/packages/core/src/libraries/domain.ts +++ b/packages/core/src/libraries/domain.ts @@ -5,12 +5,12 @@ import { DomainStatus, } from '@logto/schemas'; import { generateStandardId } from '@logto/shared'; -import { conditional } from '@silverhand/essentials'; import type Queries from '#src/tenants/Queries.js'; import SystemContext from '#src/tenants/SystemContext.js'; import assertThat from '#src/utils/assert-that.js'; import { getCustomHostname, createCustomHostname } from '#src/utils/cloudflare/index.js'; +import { findSslTxtRecord, findVerificationTxtRecord } from '#src/utils/cloudflare/utils.js'; export type DomainLibrary = ReturnType; @@ -49,19 +49,20 @@ export const createDomainLibrary = (queries: Queries) => { .filter(Boolean) .join('\n'); - const sslRecord = conditional( - txtName && txtValue && { type: 'TXT', name: txtName, value: txtValue } - ); - const cnameRecord = conditional( - (status === DomainStatus.PendingVerification || status === DomainStatus.Error) && { + const dnsRecords: DomainDnsRecords = [ + // Verification CNAME, fixed value, generated by us + { type: 'CNAME', name: domain.domain, value: origin, - } - ); - const dnsRecords: DomainDnsRecords = [cnameRecord, ownershipVerification, sslRecord].filter( - Boolean - ); + }, + // SSL TXT, generated by Cloudflare + txtName && txtValue + ? { type: 'TXT', name: txtName, value: txtValue } + : findSslTxtRecord(domain.dnsRecords), + // Ownership TXT, generated by Cloudflare + ownershipVerification ?? findVerificationTxtRecord(domain.dnsRecords), + ].filter(Boolean); return updateDomainById( domain.id, diff --git a/packages/core/src/utils/cloudflare/utils.ts b/packages/core/src/utils/cloudflare/utils.ts index 4f03e21fe..43a3625ba 100644 --- a/packages/core/src/utils/cloudflare/utils.ts +++ b/packages/core/src/utils/cloudflare/utils.ts @@ -1,4 +1,5 @@ import { parseJson } from '@logto/connector-kit'; +import { type DomainDnsRecords } from '@logto/schemas'; import assertThat from '../assert-that.js'; @@ -11,3 +12,13 @@ export const parseCloudflareResponse = (body: string) => { return result.data.result; }; + +export const findVerificationTxtRecord = (records: DomainDnsRecords) => + records.find( + ({ type, name }) => type.toUpperCase() === 'TXT' && name.includes('_cf-custom-hostname') + ); + +export const findSslTxtRecord = (records: DomainDnsRecords) => + records.find( + ({ type, name }) => type.toUpperCase() === 'TXT' && name.includes('_acme-challenge') + );