Skip to content

Commit 5f36758

Browse files
committed
feat: support some alb services
1 parent 565a408 commit 5f36758

3 files changed

Lines changed: 146 additions & 10 deletions

File tree

src/properties/logger.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ export default {
209209
`Fetched ${num} Attributes for ${albArn}`,
210210
fetchedAlbListeners: (num: number, albArn: string): string =>
211211
`Fetched ${num} ALB Listeners for ${albArn}`,
212+
fetchedAlbListenerCertificates: (num: number, listenerArn: string): string =>
213+
`Fetched ${num} ALB Listener Certificates for ${listenerArn}`,
212214
fetchedAlbTargetGroups: (num: number, albArn: string): string =>
213215
`Fetched ${num} ALB Target Groups for ${albArn}`,
214216
fetchedAlbTargetIds: (num: number, albArn: string): string =>

src/services/alb/data.ts

Lines changed: 138 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ import ELBV2, {
99
DescribeTargetGroupsInput,
1010
DescribeTargetGroupsOutput,
1111
DescribeTargetHealthOutput,
12+
DescribeListenerCertificatesInput,
13+
DescribeListenerCertificatesOutput,
1214
Listeners,
1315
LoadBalancer,
1416
LoadBalancerAttributeValue,
1517
TargetGroups,
18+
Certificate,
19+
TargetHealthDescription,
1620
} from 'aws-sdk/clients/elbv2'
1721
import CloudGraph, { Opts } from '@cloudgraph/sdk'
1822

@@ -41,13 +45,101 @@ const customRetrySettings = setAwsRetryOptions({ baseDelay: ALB_CUSTOM_DELAY })
4145

4246
export type RawAwsAlb = LoadBalancer & {
4347
listeners: Listeners
48+
listenerCertificates: Array<Certificate & { listenerArn: string }>
4449
targetIds: Array<string>
50+
targetHealth: Array<TargetHealthDescription & { targetGroupArn: string }>
4551
attributes: { [property: string]: LoadBalancerAttributeValue }
4652
targetGroups: TargetGroups
4753
region: string
4854
Tags: TagMap
4955
}
5056

57+
const describeListenerCertificatesForAlb = async ({
58+
alb,
59+
elbv2,
60+
marker: Marker = '',
61+
ListenerArn,
62+
resolveListenerCertificates,
63+
}: {
64+
alb: RawAwsAlb
65+
elbv2: ELBV2
66+
marker?: string
67+
ListenerArn: string
68+
resolveListenerCertificates: () => void
69+
}): Promise<Request<DescribeListenerCertificatesOutput, AWSError>> => {
70+
let args: DescribeListenerCertificatesInput = { ListenerArn }
71+
72+
if (Marker) {
73+
args = {
74+
...args,
75+
Marker,
76+
}
77+
}
78+
79+
return elbv2.describeListenerCertificates(args, async (err, data) => {
80+
if (err) {
81+
errorLog.generateAwsErrorLog({
82+
functionName: 'elbv2:describeListenerCertificates',
83+
err,
84+
})
85+
}
86+
87+
/**
88+
* No certificates
89+
*/
90+
91+
if (isEmpty(data)) {
92+
return resolveListenerCertificates()
93+
}
94+
95+
const { Certificates: certificates = [], NextMarker: marker } = data || {}
96+
97+
logger.debug(
98+
lt.fetchedAlbListenerCertificates(certificates.length, ListenerArn)
99+
)
100+
101+
/**
102+
* No certificates found
103+
*/
104+
105+
if (isEmpty(certificates)) {
106+
return resolveListenerCertificates()
107+
}
108+
109+
/**
110+
* Check to see if there are more
111+
*/
112+
113+
if (marker) {
114+
describeListenerCertificatesForAlb({
115+
alb,
116+
elbv2,
117+
marker,
118+
ListenerArn,
119+
resolveListenerCertificates,
120+
})
121+
}
122+
123+
/**
124+
* If there are not, then add the targetGroups to the alb's targetGroups
125+
*/
126+
alb.listenerCertificates.push(
127+
...certificates.map(certificate => ({
128+
listenerArn: ListenerArn,
129+
...certificate,
130+
}))
131+
)
132+
133+
/**
134+
* If this is the last page of data then return
135+
*/
136+
137+
if (!marker) {
138+
resolveListenerCertificates()
139+
}
140+
})
141+
}
142+
51143
export default async ({
52144
regions,
53145
config,
@@ -64,6 +156,7 @@ export default async ({
64156
const listenerPromises = []
65157
const targetGroupPromises = []
66158
const targetHealthPromises = []
159+
const listenerCertificatesPromises = []
67160

68161
/**
69162
* Step 1) for all regions, list all the albs
@@ -131,7 +224,9 @@ export default async ({
131224
region,
132225
Tags: {},
133226
listeners: [],
227+
listenerCertificates: [],
134228
targetIds: [],
229+
targetHealth: [],
135230
attributes: {},
136231
targetGroups: [],
137232
}))
@@ -147,7 +242,7 @@ export default async ({
147242
})
148243
}
149244

150-
regions.split(',').map(region =>
245+
regions.split(',').forEach(region =>
151246
regionPromises.push(
152247
new Promise<void>(resolveRegion =>
153248
describeAlbs({
@@ -217,7 +312,7 @@ export default async ({
217312

218313
const result = {}
219314

220-
tags.map(({ Key, Value }) => {
315+
tags.forEach(({ Key, Value }) => {
221316
result[Key] = Value
222317
})
223318

@@ -226,7 +321,7 @@ export default async ({
226321
resolveTags()
227322
})
228323

229-
albData.map(alb => {
324+
albData.forEach(alb => {
230325
const { LoadBalancerArn: arn, region } = alb
231326
const elbv2 = new ELBV2({
232327
...config,
@@ -302,7 +397,7 @@ export default async ({
302397

303398
const result = {}
304399

305-
attributes.map(({ Key, Value }) => {
400+
attributes.forEach(({ Key, Value }) => {
306401
result[Key] = Value
307402
})
308403

@@ -311,7 +406,7 @@ export default async ({
311406
}
312407
)
313408

314-
albData.map(alb => {
409+
albData.forEach(alb => {
315410
const { LoadBalancerArn, region } = alb
316411
const elbv2 = new ELBV2({
317412
...config,
@@ -416,7 +511,7 @@ export default async ({
416511
})
417512
}
418513

419-
albData.map(alb => {
514+
albData.forEach(alb => {
420515
const { LoadBalancerArn, region } = alb
421516
const elbv2 = new ELBV2({
422517
...config,
@@ -523,7 +618,7 @@ export default async ({
523618
})
524619
}
525620

526-
albData.map(alb => {
621+
albData.forEach(alb => {
527622
const { LoadBalancerArn, region } = alb
528623
const elbv2 = new ELBV2({
529624
...config,
@@ -592,23 +687,28 @@ export default async ({
592687
/**
593688
* Add the ids to the alb's targetIds
594689
*/
595-
596690
alb.targetIds.push(
597691
...targetHealth.map(({ Target: { Id = '' } = {} }) => Id)
598692
)
693+
alb.targetHealth.push(
694+
...targetHealth.map(({ Target }) => ({
695+
targetGroupArn: TargetGroupArn,
696+
id: Target.Id,
697+
}))
698+
)
599699

600700
resolveTargetHealth()
601701
})
602702

603-
albData.map(alb => {
703+
albData.forEach(alb => {
604704
const { region, targetGroups = [] } = alb
605705
const elbv2 = new ELBV2({
606706
...config,
607707
region,
608708
endpoint,
609709
...customRetrySettings,
610710
})
611-
targetGroups.map(({ TargetGroupArn }) => {
711+
targetGroups.forEach(({ TargetGroupArn }) => {
612712
targetHealthPromises.push(
613713
new Promise<void>(resolveTargetHealth => {
614714
describeTargetHealth({
@@ -623,6 +723,34 @@ export default async ({
623723
})
624724

625725
await Promise.all(targetHealthPromises)
726+
727+
/**
728+
* Step 7, use the describeListenerCertificates method to get the certificates IDs
729+
*/
730+
albData.forEach(alb => {
731+
const { listeners = [], region } = alb
732+
const elbv2 = new ELBV2({
733+
...config,
734+
region,
735+
endpoint,
736+
...customRetrySettings,
737+
})
738+
const listenerCertificatesPromise = new Promise<void>(
739+
resolveListenerCertificates => {
740+
listeners.forEach(({ ListenerArn }) => {
741+
describeListenerCertificatesForAlb({
742+
alb,
743+
elbv2,
744+
ListenerArn,
745+
resolveListenerCertificates,
746+
})
747+
})
748+
}
749+
)
750+
listenerCertificatesPromises.push(listenerCertificatesPromise)
751+
})
752+
753+
await Promise.all(listenerCertificatesPromises)
626754
errorLog.reset()
627755

628756
resolve(groupBy(albData, 'region'))

src/services/alb/format.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ export default ({
7171
},
7272
CreatedTime: createdAt,
7373
listeners = [],
74+
targetGroups,
75+
listenerCertificates,
76+
targetHealth,
7477
}: // attributes = {},
7578
// SecurityGroups: securityGroups = [],
7679
// AvailabilityZones: azs = [],
@@ -102,6 +105,9 @@ export default ({
102105
createdAt: createdAt.toISOString(),
103106
status,
104107
listeners: listeners.map(awsAlbListernerGraphFormat),
108+
targetGroups,
109+
listenerCertificates,
110+
targetGroupAttachment: targetHealth,
105111
}
106112
return albResult
107113
}

0 commit comments

Comments
 (0)