Skip to content

Commit 82a71f5

Browse files
committed
Log AWS-related error messages; fix deleteCertificate recursion
1 parent 9e96d7c commit 82a71f5

File tree

3 files changed

+50
-41
lines changed

3 files changed

+50
-41
lines changed

lib/domain-verification.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { requestCertificate, getCertificateStatus, describeCertificate } from '@/api/acm'
1+
import { requestCertificate, getCertificateStatus, describeCertificate, deleteCertificate } from '@/api/acm'
22
import { Resolver } from 'node:dns/promises'
33

44
// Issue a certificate for a custom domain
55
export async function issueDomainCertificate (domainName) {
66
try {
77
const certificateArn = await requestCertificate(domainName)
8-
return certificateArn
8+
return { certificateArn, error: null }
99
} catch (error) {
1010
console.error(`Failed to issue certificate for domain ${domainName}:`, error)
11-
return null
11+
return { certificateArn: null, error: error.message }
1212
}
1313
}
1414

@@ -17,31 +17,34 @@ export async function checkCertificateStatus (certificateArn) {
1717
let certStatus
1818
try {
1919
certStatus = await getCertificateStatus(certificateArn)
20+
return { certStatus, error: null }
2021
} catch (error) {
2122
console.error(`Certificate status check failed: ${error.message}`)
22-
return 'FAILED'
23+
return { certStatus: 'FAILED', error: error.message }
2324
}
24-
25-
return certStatus
2625
}
2726

2827
// Get the details of a certificate for a custom domain
2928
export async function certDetails (certificateArn) {
3029
try {
3130
const certificate = await describeCertificate(certificateArn)
32-
return certificate
31+
return { certificate, error: null }
3332
} catch (error) {
3433
console.error(`Certificate description failed: ${error.message}`)
35-
return null
34+
return { certificate: null, error: error.message }
3635
}
3736
}
3837

3938
// Get the validation values for a certificate for a custom domain
4039
// TODO: Test with real values, localstack don't have this info until the certificate is issued
4140
export async function getValidationValues (certificateArn) {
42-
const certificate = await certDetails(certificateArn)
41+
const { certificate, error } = await certDetails(certificateArn)
42+
if (error) {
43+
return { cname: null, value: null, error }
44+
}
45+
4346
if (!certificate || !certificate.Certificate || !certificate.Certificate.DomainValidationOptions) {
44-
return { cname: null, value: null }
47+
return { cname: null, value: null, error: 'Certificate not found' }
4548
}
4649

4750
return {
@@ -93,10 +96,12 @@ export async function verifyDNSRecord (type, recordName, recordValue) {
9396
}
9497

9598
// Delete a certificate for a custom domain
96-
export async function deleteCertificate (certificateArn) {
99+
export async function deleteDomainCertificate (certificateArn) {
97100
try {
98101
await deleteCertificate(certificateArn)
102+
return { error: null }
99103
} catch (error) {
100104
console.error(`Failed to delete certificate: ${error.message}`)
105+
return { error: error.message }
101106
}
102107
}

worker/domainVerification.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import createPrisma from '@/lib/create-prisma'
2-
import { verifyDNSRecord, issueDomainCertificate, checkCertificateStatus, getValidationValues, deleteCertificate } from '@/lib/domain-verification'
2+
import { verifyDNSRecord, issueDomainCertificate, checkCertificateStatus, getValidationValues, deleteDomainCertificate } from '@/lib/domain-verification'
33
import { datePivot } from '@/lib/time'
44

55
const VERIFICATION_INTERVAL = 60 * 5 // 5 minutes
@@ -81,7 +81,7 @@ async function verifyDomain (domain, models) {
8181
if (datePivot(new Date(), { days: VERIFICATION_HOLD_THRESHOLD }) > domain.updatedAt) {
8282
if (domain.certificate) {
8383
// certificate would expire in 72 hours anyway, it's best to delete it
84-
await deleteCertificate(domain.certificate.certificateArn)
84+
await deleteDomainCertificate(domain.certificate.certificateArn)
8585
}
8686
return { status: 'HOLD', message: `Domain ${domain.domainName} has been put on HOLD because we couldn't verify it in 48 hours` }
8787
}
@@ -157,22 +157,26 @@ async function requestCertificate (domain, models) {
157157
let message = null
158158

159159
// ask ACM to request a certificate for the domain
160-
const certificateArn = await issueDomainCertificate(domain.domainName)
160+
const { certificateArn, error } = await issueDomainCertificate(domain.domainName)
161161

162162
if (certificateArn) {
163163
// check the status of the just created certificate
164-
const certificateStatus = await checkCertificateStatus(certificateArn)
165-
// store the certificate in the database with its status
166-
await models.domainCertificate.create({
167-
data: {
168-
domain: { connect: { id: domain.id } },
169-
certificateArn,
170-
status: certificateStatus
171-
}
172-
})
173-
message = 'An ACM certificate with arn ' + certificateArn + ' has been successfully requested'
164+
const { certStatus, error: checkError } = await checkCertificateStatus(certificateArn)
165+
if (checkError) {
166+
message = 'Could not check certificate status: ' + checkError
167+
} else {
168+
// store the certificate in the database with its status
169+
await models.domainCertificate.create({
170+
data: {
171+
domain: { connect: { id: domain.id } },
172+
certificateArn,
173+
status: certStatus
174+
}
175+
})
176+
message = 'An ACM certificate with arn ' + certificateArn + ' has been successfully requested'
177+
}
174178
} else {
175-
message = 'Could not request an ACM certificate'
179+
message = 'Could not request an ACM certificate: ' + error
176180
}
177181

178182
const status = certificateArn ? 'PENDING' : 'FAILED'
@@ -184,45 +188,45 @@ async function getACMValidationValues (domain, models, certificateArn) {
184188
let message = null
185189

186190
// get the validation values for the certificate
187-
const validationValues = await getValidationValues(certificateArn)
188-
if (validationValues) {
191+
const { cname, value, error } = await getValidationValues(certificateArn)
192+
if (cname && value) {
189193
// store the validation values in the database
190194
await models.domainVerificationRecord.create({
191195
data: {
192196
domain: { connect: { id: domain.id } },
193197
type: 'SSL',
194-
recordName: validationValues.cname,
195-
recordValue: validationValues.value
198+
recordName: cname,
199+
recordValue: value
196200
}
197201
})
198202
message = 'Validation values stored'
199203
} else {
200-
message = 'Could not get validation values'
204+
message = 'Could not get validation values: ' + error
201205
}
202206

203-
const status = validationValues ? 'PENDING' : 'FAILED'
207+
const status = cname && value ? 'PENDING' : 'FAILED'
204208
await logAttempt({ domain, models, stage: 'ACM_REQUEST_VALIDATION_VALUES', status, message })
205209
return status !== 'FAILED'
206210
}
207211

208212
async function checkACMValidation (domain, models, record) {
209213
let message = null
210214

211-
const certificateStatus = await checkCertificateStatus(domain.certificate.certificateArn)
212-
if (certificateStatus) {
213-
if (certificateStatus !== domain.certificate.status) {
214-
console.log(`certificate status for ${domain.domainName} has changed from ${domain.certificate.status} to ${certificateStatus}`)
215+
const { certStatus, error } = await checkCertificateStatus(domain.certificate.certificateArn)
216+
if (certStatus) {
217+
if (certStatus !== domain.certificate.status) {
218+
console.log(`certificate status for ${domain.domainName} has changed from ${domain.certificate.status} to ${certStatus}`)
215219
await models.domainCertificate.update({
216220
where: { id: domain.certificate.id },
217-
data: { status: certificateStatus }
221+
data: { status: certStatus }
218222
})
219223
}
220-
message = `Certificate status is: ${certificateStatus}`
224+
message = `Certificate status is: ${certStatus}`
221225
} else {
222-
message = 'Could not check certificate status'
226+
message = 'Could not check certificate status: ' + error
223227
}
224228

225-
const status = certificateStatus === 'ISSUED' ? 'VERIFIED' : 'PENDING'
229+
const status = certStatus === 'ISSUED' ? 'VERIFIED' : 'PENDING'
226230
await logAttempt({ domain, models, record, stage: 'ACM_VALIDATION', status, message })
227231
return status === 'VERIFIED'
228232
}

worker/territory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import lnd from '@/api/lnd'
22
import performPaidAction from '@/api/paidAction'
3-
import { deleteCertificate } from '@/lib/domain-verification'
3+
import { deleteDomainCertificate } from '@/lib/domain-verification'
44
import { PAID_ACTION_PAYMENT_METHODS } from '@/lib/constants'
55
import { nextBillingWithGrace } from '@/lib/territory'
66
import { datePivot } from '@/lib/time'
@@ -18,7 +18,7 @@ export async function territoryBilling ({ data: { subName }, boss, models }) {
1818

1919
// make sure to delete the certificate from ACM if the sub is stopped, if we have it.
2020
if (nextStatus === 'STOPPED' && sub.domain?.certificate?.certificateArn) {
21-
await deleteCertificate(sub.domain.certificate.certificateArn)
21+
await deleteDomainCertificate(sub.domain.certificate.certificateArn)
2222
}
2323

2424
await models.sub.update({

0 commit comments

Comments
 (0)