Skip to content

Commit f36eef4

Browse files
committed
use directly the interested ELB Listener ARN via env vars; get rid of functions that we don't need; consistency clean-up
1 parent bbf2b0a commit f36eef4

File tree

5 files changed

+31
-73
lines changed

5 files changed

+31
-73
lines changed

.env.development

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ PERSISTENCE=1
179179
SKIP_SSL_CERT_DOWNLOAD=1
180180
# custom domain ACM, ELBv2
181181
LOCALSTACK_ENDPOINT=http://aws:4566
182-
ELB_NAME=mock-lb
182+
ELB_NAME=sndev-lb
183+
ELB_LISTENER_ARN=arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/sndev-lb/1234567890abcdef/1234567890abcdef
183184

184185
# tor proxy
185186
TOR_PROXY=http://tor:7050/

api/elb/index.js

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,26 @@
11
import AWS from 'aws-sdk'
22
import { MockELBv2 } from './mocks'
33

4+
const ELB_LISTENER_ARN = process.env.ELB_LISTENER_ARN
5+
46
AWS.config.update({
57
region: 'us-east-1'
68
})
79

8-
async function getElb () {
9-
try {
10-
// get the load balancer
11-
const elbv2 = process.env.NODE_ENV === 'development'
12-
? new MockELBv2() // use the mocked elb for local development
13-
: new AWS.ELBv2()
14-
const { LoadBalancers } = await elbv2.describeLoadBalancers({ Names: [process.env.ELB_NAME] }).promise()
15-
console.log('[elbv2] LoadBalancers', LoadBalancers)
16-
17-
if (!LoadBalancers?.length) {
18-
throw new Error('Cannot find a load balancer, check the .env file')
19-
}
20-
21-
return LoadBalancers[0]
22-
} catch (error) {
23-
console.error('[elbv2] Error getting elb', error)
24-
throw error
25-
}
26-
}
27-
28-
async function getElbListener (elbArn) {
29-
try {
30-
const elbv2 = process.env.NODE_ENV === 'development'
31-
? new MockELBv2() // use the mocked elb for local development
32-
: new AWS.ELBv2()
33-
const { Listeners } = await elbv2.describeListeners({ LoadBalancerArn: elbArn, Filters: [{ Name: 'protocol', Values: ['HTTPS'] }] }).promise()
34-
console.log('[elbv2] Listeners', Listeners)
35-
36-
if (!Listeners?.length) {
37-
throw new Error('Cannot find a listener, check the .env file')
38-
}
39-
40-
return Listeners[0]
41-
} catch (error) {
42-
console.error('[elbv2] Error getting elb listener', error)
43-
throw error
44-
}
45-
}
46-
4710
// attach a certificate to the elb listener
4811
async function attachCertificateToElb (certificateArn) {
4912
const elbv2 = process.env.NODE_ENV === 'development'
5013
? new MockELBv2() // use the mocked elb for local development
5114
: new AWS.ELBv2()
52-
const elb = await getElb()
53-
const elbListener = await getElbListener(elb.LoadBalancerArn)
54-
const elbListenerArn = elbListener.ListenerArn
5515

5616
// attach the certificate
5717
// AWS doesn't throw an error if the certificate is already attached to the listener
5818
await elbv2.addListenerCertificates({
59-
ListenerArn: elbListenerArn,
19+
ListenerArn: ELB_LISTENER_ARN,
6020
Certificates: [{ CertificateArn: certificateArn }]
6121
}).promise()
6222

63-
console.log('[elbv2] Certificate', certificateArn, 'attached to listener', elbListenerArn)
23+
console.log('[elbv2] Certificate', certificateArn, 'attached to listener', ELB_LISTENER_ARN)
6424
return true
6525
}
6626

@@ -69,18 +29,15 @@ async function detachCertificateFromElb (certificateArn) {
6929
const elbv2 = process.env.NODE_ENV === 'development'
7030
? new MockELBv2() // use the mocked elb for local development
7131
: new AWS.ELBv2()
72-
const elb = await getElb()
73-
const elbListener = await getElbListener(elb.LoadBalancerArn)
74-
const elbListenerArn = elbListener.ListenerArn
7532

7633
// detach the certificate
7734
// AWS doesn't throw an error if the certificate is not attached to the listener
7835
await elbv2.removeListenerCertificates({
79-
ListenerArn: elbListenerArn,
36+
ListenerArn: ELB_LISTENER_ARN,
8037
Certificates: [{ CertificateArn: certificateArn }]
8138
}).promise()
8239

83-
console.log('[elbv2] Certificate', certificateArn, 'detached from listener', elbListenerArn)
40+
console.log('[elbv2] Certificate', certificateArn, 'detached from listener', ELB_LISTENER_ARN)
8441
return true
8542
}
8643

api/elb/mocks.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
const mockedElb = {
33
loadBalancers: [
44
{
5-
LoadBalancerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/mock-lb/1234567890abcdef',
6-
DNSName: 'mock-lb.us-east-1.elb.amazonaws.com',
7-
LoadBalancerName: 'mock-lb',
5+
LoadBalancerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/sndev-lb/1234567890abcdef',
6+
DNSName: 'sndev-lb.us-east-1.elb.amazonaws.com',
7+
LoadBalancerName: 'sndev-lb',
88
Type: 'application'
99
}
1010
],
1111
listeners: [
1212
{
13-
ListenerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/mock-lb/1234567890abcdef/1234567890abcdef',
14-
LoadBalancerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/mock-lb/1234567890abcdef',
13+
ListenerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/sndev-lb/1234567890abcdef/1234567890abcdef',
14+
LoadBalancerArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/sndev-lb/1234567890abcdef',
1515
Protocol: 'HTTPS',
1616
Port: 443
1717
}

lib/domain-verification.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ export async function getValidationValues (certificateArn) {
5656
// Attach a certificate to the ELB listener
5757
export async function attachDomainCertificate (certificateArn) {
5858
try {
59-
const result = await attachCertificateToElb(certificateArn)
60-
return { result, error: null }
59+
await attachCertificateToElb(certificateArn)
60+
return { error: null }
6161
} catch (error) {
6262
console.error(`Failed to attach certificate to elb: ${error.message}`)
63-
return { result: null, error: error.message }
63+
return { error: error.message }
6464
}
6565
}
6666

@@ -120,24 +120,24 @@ export async function verifyDNSRecord (type, recordName, recordValue) {
120120
return result
121121
}
122122

123-
// Delete a certificate for a custom domain
124-
export async function deleteDomainCertificate (certificateArn) {
123+
// Detach a certificate from the elb listener
124+
export async function detachDomainCertificate (certificateArn) {
125125
try {
126-
await deleteCertificate(certificateArn)
126+
await detachCertificateFromElb(certificateArn)
127127
return { error: null }
128128
} catch (error) {
129-
console.error(`Failed to delete certificate: ${error.message}`)
129+
console.error(`Failed to detach certificate from elb: ${error.message}`)
130130
return { error: error.message }
131131
}
132132
}
133133

134-
// Detach a certificate from the elb listener
135-
export async function detachDomainCertificate (certificateArn) {
134+
// Delete a certificate for a custom domain
135+
export async function deleteDomainCertificate (certificateArn) {
136136
try {
137-
await detachCertificateFromElb(certificateArn)
137+
await deleteCertificate(certificateArn)
138138
return { error: null }
139139
} catch (error) {
140-
console.error(`Failed to detach certificate from elb: ${error.message}`)
140+
console.error(`Failed to delete certificate: ${error.message}`)
141141
return { error: error.message }
142142
}
143143
}

worker/domainVerification.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,14 @@ async function attachACMCertificateToELB (domain, models, certificateArn) {
252252
let message = null
253253

254254
// attach the certificate to the ELB listener
255-
const { result, error } = await attachDomainCertificate(certificateArn)
256-
if (result) {
255+
const { error } = await attachDomainCertificate(certificateArn)
256+
if (!error) {
257257
message = `Certificate ${certificateArn} is now attached to ELB listener`
258258
} else {
259259
message = `Could not attach certificate ${certificateArn} to ELB listener: ${error.message}`
260260
}
261261

262-
const status = result ? 'VERIFIED' : 'FAILED'
262+
const status = !error ? 'VERIFIED' : 'FAILED'
263263
await logAttempt({ domain, models, stage: 'ELB_ATTACH_CERTIFICATE', status, message })
264264
return status !== 'FAILED'
265265
}
@@ -307,9 +307,9 @@ export async function deleteCertificateExternal ({ data: { certificateArn } }) {
307307
}
308308

309309
// delete the certificate from ACM
310-
const { error } = await deleteDomainCertificate(certificateArn)
311-
if (error) {
312-
console.error(`couldn't delete certificate with ARN ${certificateArn}: ${error.message}`)
313-
throw error
310+
const { error: deleteError } = await deleteDomainCertificate(certificateArn)
311+
if (deleteError) {
312+
console.error(`couldn't delete certificate with ARN ${certificateArn}: ${deleteError.message}`)
313+
throw deleteError
314314
}
315315
}

0 commit comments

Comments
 (0)