|
| 1 | +import isEmpty from 'lodash/isEmpty' |
| 2 | + |
| 3 | +import { ServiceConnection } from '@cloudgraph/sdk' |
| 4 | + |
| 5 | +import services from '../../enums/services' |
| 6 | +import { RawAwsAlb } from '../alb/data' |
| 7 | +import { RawAwsApiGatewayStage } from '../apiGatewayStage/data' |
| 8 | +import { RawAwsAppSync } from '../appSync/data' |
| 9 | +import { RawAwsWafV2WebAcl } from './data' |
| 10 | + |
| 11 | +/** |
| 12 | + * WAF |
| 13 | + */ |
| 14 | + |
| 15 | +export default ({ |
| 16 | + service: waf, |
| 17 | + data, |
| 18 | + region, |
| 19 | +}: { |
| 20 | + account: string |
| 21 | + data: { name: string; data: { [property: string]: any[] } }[] |
| 22 | + service: RawAwsWafV2WebAcl |
| 23 | + region: string |
| 24 | +}): { [key: string]: ServiceConnection[] } => { |
| 25 | + const connections: ServiceConnection[] = [] |
| 26 | + const { Id: id, wafResources } = waf |
| 27 | + |
| 28 | + /** |
| 29 | + * Find ALBs |
| 30 | + * related to this WAF |
| 31 | + */ |
| 32 | + const albs: { |
| 33 | + name: string |
| 34 | + data: { [property: string]: any[] } |
| 35 | + } = data.find(({ name }) => name === services.alb) |
| 36 | + |
| 37 | + if (albs?.data?.[region] && wafResources?.elasticloadbalancing?.length > 0) { |
| 38 | + const associatedALBs: RawAwsAlb[] = albs.data[region].filter( |
| 39 | + ({ LoadBalancerArn }: RawAwsAlb) => |
| 40 | + wafResources?.elasticloadbalancing?.find(arn => arn === LoadBalancerArn) |
| 41 | + ) |
| 42 | + |
| 43 | + if (!isEmpty(associatedALBs)) { |
| 44 | + for (const { LoadBalancerArn } of associatedALBs) { |
| 45 | + connections.push({ |
| 46 | + id: LoadBalancerArn, |
| 47 | + resourceType: services.alb, |
| 48 | + relation: 'child', |
| 49 | + field: 'albs', |
| 50 | + }) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + /** |
| 55 | + * Find Rest API Stages |
| 56 | + * related to this WAF |
| 57 | + */ |
| 58 | + const stages: { |
| 59 | + name: string |
| 60 | + data: { [property: string]: any[] } |
| 61 | + } = data.find(({ name }) => name === services.apiGatewayStage) |
| 62 | + |
| 63 | + if (stages?.data?.[region] && wafResources?.apigateway?.length > 0) { |
| 64 | + const associatedStages: RawAwsApiGatewayStage[] = stages.data[ |
| 65 | + region |
| 66 | + ].filter(({ arn: stageArn }: RawAwsApiGatewayStage) => |
| 67 | + wafResources?.apigateway?.find(arn => arn === stageArn) |
| 68 | + ) |
| 69 | + |
| 70 | + if (!isEmpty(associatedStages)) { |
| 71 | + for (const { arn } of associatedStages) { |
| 72 | + connections.push({ |
| 73 | + id: arn, |
| 74 | + resourceType: services.apiGatewayStage, |
| 75 | + relation: 'child', |
| 76 | + field: 'apiGatewayStages', |
| 77 | + }) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Find Apps Sync |
| 84 | + * related to this WAF |
| 85 | + */ |
| 86 | + const apps: { |
| 87 | + name: string |
| 88 | + data: { [property: string]: any[] } |
| 89 | + } = data.find(({ name }) => name === services.appSync) |
| 90 | + |
| 91 | + if (apps?.data?.[region] && wafResources?.appsync?.length > 0) { |
| 92 | + const associatedApps: RawAwsAppSync[] = apps.data[region].filter( |
| 93 | + ({ arn: apiArn }: RawAwsAppSync) => |
| 94 | + wafResources?.appsync?.find(arn => arn === apiArn) |
| 95 | + ) |
| 96 | + |
| 97 | + if (!isEmpty(associatedApps)) { |
| 98 | + for (const { apiId } of associatedApps) { |
| 99 | + connections.push({ |
| 100 | + id: apiId, |
| 101 | + resourceType: services.appSync, |
| 102 | + relation: 'child', |
| 103 | + field: 'appSync', |
| 104 | + }) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return { |
| 110 | + [id]: connections, |
| 111 | + } |
| 112 | +} |
0 commit comments