Skip to content

Commit 775a966

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

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

lib/domain-verification.js

+16-11
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

+8-8
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,7 +157,7 @@ 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
@@ -172,7 +172,7 @@ async function requestCertificate (domain, models) {
172172
})
173173
message = 'An ACM certificate with arn ' + certificateArn + ' has been successfully requested'
174174
} else {
175-
message = 'Could not request an ACM certificate'
175+
message = 'Could not request an ACM certificate: ' + error
176176
}
177177

178178
const status = certificateArn ? 'PENDING' : 'FAILED'
@@ -184,7 +184,7 @@ async function getACMValidationValues (domain, models, certificateArn) {
184184
let message = null
185185

186186
// get the validation values for the certificate
187-
const validationValues = await getValidationValues(certificateArn)
187+
const { validationValues, error } = await getValidationValues(certificateArn)
188188
if (validationValues) {
189189
// store the validation values in the database
190190
await models.domainVerificationRecord.create({
@@ -197,7 +197,7 @@ async function getACMValidationValues (domain, models, certificateArn) {
197197
})
198198
message = 'Validation values stored'
199199
} else {
200-
message = 'Could not get validation values'
200+
message = 'Could not get validation values: ' + error
201201
}
202202

203203
const status = validationValues ? 'PENDING' : 'FAILED'
@@ -208,7 +208,7 @@ async function getACMValidationValues (domain, models, certificateArn) {
208208
async function checkACMValidation (domain, models, record) {
209209
let message = null
210210

211-
const certificateStatus = await checkCertificateStatus(domain.certificate.certificateArn)
211+
const { certificateStatus, error } = await checkCertificateStatus(domain.certificate.certificateArn)
212212
if (certificateStatus) {
213213
if (certificateStatus !== domain.certificate.status) {
214214
console.log(`certificate status for ${domain.domainName} has changed from ${domain.certificate.status} to ${certificateStatus}`)
@@ -219,7 +219,7 @@ async function checkACMValidation (domain, models, record) {
219219
}
220220
message = `Certificate status is: ${certificateStatus}`
221221
} else {
222-
message = 'Could not check certificate status'
222+
message = 'Could not check certificate status: ' + error
223223
}
224224

225225
const status = certificateStatus === 'ISSUED' ? 'VERIFIED' : 'PENDING'

worker/territory.js

+2-2
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)