11import { ServiceConnection } from '@cloudgraph/sdk'
22import { isEmpty } from 'lodash'
33import { SecurityGroup } from 'aws-sdk/clients/ec2'
4- import { DBInstance , DBCluster } from 'aws-sdk/clients/rds'
4+ import { DBInstance } from 'aws-sdk/clients/rds'
55
66import services from '../../enums/services'
7+ import { RawAwsRdsCluster } from './data'
8+ import { RawAwsRoute53HostedZone } from '../route53HostedZone/data'
79import { RawAwsRdsClusterSnapshot } from '../rdsClusterSnapshot/data'
810import { RawAwsIamRole } from '../iamRole/data'
11+ import { RawAwsSubnet } from '../subnet/data'
912import { AwsKms } from '../kms/data'
1013import { globalRegionName } from '../../enums/regions'
1114
@@ -14,7 +17,7 @@ export default ({
1417 data,
1518 region,
1619} : {
17- service : DBCluster
20+ service : RawAwsRdsCluster
1821 data : Array < { name : string ; data : { [ property : string ] : any [ ] } } >
1922 region : string
2023} ) : {
@@ -24,12 +27,14 @@ export default ({
2427 const {
2528 DBClusterArn : id ,
2629 DBClusterIdentifier : clusterId ,
30+ dbSubnetGroups,
2731 MonitoringRoleArn : monitoringRoleArn ,
2832 AssociatedRoles : associatedRoles = [ ] ,
2933 KmsKeyId,
3034 ActivityStreamKmsKeyId,
3135 PerformanceInsightsKMSKeyId,
3236 VpcSecurityGroups,
37+ HostedZoneId : hostedZoneId ,
3338 } = service
3439 const sgIds = VpcSecurityGroups . map (
3540 ( { VpcSecurityGroupId } ) => VpcSecurityGroupId
@@ -122,47 +127,19 @@ export default ({
122127 } = data . find ( ( { name } ) => name === services . kms )
123128
124129 if ( kms ?. data ?. [ region ] ) {
125- // set storage encryption kms key
126- let kmsInRegion : AwsKms [ ] = kms . data [ region ] . filter (
127- ( { Arn } : AwsKms ) => Arn === KmsKeyId
130+ const kmsInRegion : AwsKms [ ] = kms . data [ region ] . filter (
131+ ( { Arn } : AwsKms ) =>
132+ Arn === KmsKeyId ||
133+ Arn === ActivityStreamKmsKeyId ||
134+ Arn === PerformanceInsightsKMSKeyId
128135 )
129136 if ( ! isEmpty ( kmsInRegion ) ) {
130137 for ( const instance of kmsInRegion ) {
131138 connections . push ( {
132139 id : instance . KeyId ,
133140 resourceType : services . kms ,
134141 relation : 'child' ,
135- field : 'storageEncryptedKms' ,
136- } )
137- }
138- }
139-
140- // set activity stream kms key
141- kmsInRegion = kms . data [ region ] . filter (
142- ( { Arn } : AwsKms ) => Arn === ActivityStreamKmsKeyId
143- )
144- if ( ! isEmpty ( kmsInRegion ) ) {
145- for ( const instance of kmsInRegion ) {
146- connections . push ( {
147- id : instance . KeyId ,
148- resourceType : services . kms ,
149- relation : 'child' ,
150- field : 'activityStreamKms' ,
151- } )
152- }
153- }
154-
155- // set performance insights kms key
156- kmsInRegion = kms . data [ region ] . filter (
157- ( { Arn } : AwsKms ) => Arn === PerformanceInsightsKMSKeyId
158- )
159- if ( ! isEmpty ( kmsInRegion ) ) {
160- for ( const instance of kmsInRegion ) {
161- connections . push ( {
162- id : instance . KeyId ,
163- resourceType : services . kms ,
164- relation : 'child' ,
165- field : 'performanceInsightsKms' ,
142+ field : 'kms' ,
166143 } )
167144 }
168145 }
@@ -178,10 +155,12 @@ export default ({
178155 } = data . find ( ( { name } ) => name === services . iamRole )
179156
180157 if ( iamRoles ?. data ?. [ globalRegionName ] ) {
181- let iamRolesInRegion : RawAwsIamRole [ ] = iamRoles . data [
158+ const iamRolesInRegion : RawAwsIamRole [ ] = iamRoles . data [
182159 globalRegionName
183- ] . filter ( ( { Arn } : RawAwsIamRole ) =>
184- associatedRoles . find ( r => r . RoleArn === Arn )
160+ ] . filter (
161+ ( { Arn } : RawAwsIamRole ) =>
162+ Arn === monitoringRoleArn ||
163+ associatedRoles . find ( r => r . RoleArn === Arn )
185164 )
186165 if ( ! isEmpty ( iamRolesInRegion ) ) {
187166 for ( const instance of iamRolesInRegion ) {
@@ -193,16 +172,56 @@ export default ({
193172 } )
194173 }
195174 }
196- iamRolesInRegion = iamRoles . data [ globalRegionName ] . filter (
197- ( { Arn } : RawAwsIamRole ) => Arn === monitoringRoleArn
175+ }
176+
177+ /**
178+ * Find Route53 hosted zones
179+ */
180+ const route53HostedZones : {
181+ name : string
182+ data : { [ property : string ] : RawAwsRoute53HostedZone [ ] }
183+ } = data . find ( ( { name } ) => name === services . route53HostedZone )
184+
185+ if ( route53HostedZones ?. data ?. [ region ] ) {
186+ const instancesInRegion : RawAwsRoute53HostedZone [ ] =
187+ route53HostedZones . data [ region ] . filter (
188+ ( { Id } : RawAwsRoute53HostedZone ) => Id === hostedZoneId
189+ )
190+ if ( ! isEmpty ( instancesInRegion ) ) {
191+ for ( const instance of instancesInRegion ) {
192+ const { Id : id } = instance
193+ connections . push ( {
194+ id,
195+ resourceType : services . route53HostedZone ,
196+ relation : 'child' ,
197+ field : 'route53HostedZone' ,
198+ } )
199+ }
200+ }
201+ }
202+
203+ /**
204+ * Find Subnets
205+ * related to this RDS Cluster
206+ */
207+ const subnets = data . find ( ( { name } ) => name === services . subnet )
208+ const subnetIds = dbSubnetGroups ?. map ( ( { Subnets } ) =>
209+ Subnets ?. map ( subnet => subnet . SubnetIdentifier )
210+ )
211+ if ( subnets ?. data ?. [ region ] ) {
212+ const subnetsInRegion = subnets . data [ region ] . filter (
213+ ( subnet : RawAwsSubnet ) =>
214+ subnetIds . some ( ids => ids . includes ( subnet . SubnetId ) )
198215 )
199- if ( ! isEmpty ( iamRolesInRegion ) ) {
200- for ( const instance of iamRolesInRegion ) {
216+ if ( ! isEmpty ( subnetsInRegion ) ) {
217+ for ( const subnet of subnetsInRegion ) {
218+ const { SubnetId } = subnet
219+
201220 connections . push ( {
202- id : instance . Arn ,
203- resourceType : services . iamRole ,
221+ id : SubnetId ,
222+ resourceType : services . subnet ,
204223 relation : 'child' ,
205- field : 'monitoringIamRole ' ,
224+ field : 'subnets ' ,
206225 } )
207226 }
208227 }
0 commit comments