1- import { UserPoolType } from 'aws-sdk/clients/cognitoidentityserviceprovider' ;
1+ import { UserPoolType , LambdaConfigType } from 'aws-sdk/clients/cognitoidentityserviceprovider'
22
3- import { ServiceConnection } from '@cloudgraph/sdk' ;
4- import services from '../../enums/services' ;
3+ import { ServiceConnection } from '@cloudgraph/sdk'
4+ import { isEmpty } from 'lodash'
5+ import services from '../../enums/services'
6+ import { sesArn } from '../../utils/generateArns'
7+ import { RawAwsLambdaFunction } from '../lambda/data'
8+ import { RawAwsSes } from '../ses/data'
9+ import { RawAwsIamRole } from '../iamRole/data'
10+ import { AwsKms } from '../kms/data'
11+
12+ const getLambdasArn = (
13+ lambdaConfig ?: LambdaConfigType
14+ ) : string [ ] => {
15+ if ( isEmpty ( lambdaConfig ) ) {
16+ return [ ]
17+ }
18+
19+ const {
20+ PreSignUp,
21+ CustomMessage,
22+ PostConfirmation,
23+ PreAuthentication,
24+ PostAuthentication,
25+ DefineAuthChallenge,
26+ CreateAuthChallenge,
27+ VerifyAuthChallengeResponse,
28+ PreTokenGeneration,
29+ UserMigration,
30+ } = lambdaConfig
31+
32+ return [
33+ PreSignUp ,
34+ CustomMessage ,
35+ PostConfirmation ,
36+ PreAuthentication ,
37+ PostAuthentication ,
38+ DefineAuthChallenge ,
39+ CreateAuthChallenge ,
40+ VerifyAuthChallengeResponse ,
41+ PreTokenGeneration ,
42+ UserMigration ,
43+ ] ?. filter ( l => l )
44+ }
545
646/**
747 * Cognito User Pool
@@ -11,7 +51,9 @@ export default ({
1151 service : userPool ,
1252 data,
1353 region,
54+ account,
1455} : {
56+ account : string
1557 data : { name : string ; data : { [ property : string ] : any [ ] } } [ ]
1658 service : UserPoolType & {
1759 region : string
@@ -23,38 +65,104 @@ export default ({
2365 const {
2466 Id : id ,
2567 LambdaConfig : lambdaConfig ,
68+ EmailConfiguration : emailConfiguration ,
69+ SmsConfiguration : smsConfiguration ,
2670 } = userPool
2771
28- const defineAuthChallengeArn = lambdaConfig ?. DefineAuthChallenge
29-
3072 /**
3173 * Find Lambda Functions
32- * related to this Auto Scaling Group
74+ * related to this cognito user pool
3375 */
76+ const lambdasArn : string [ ] = getLambdasArn ( lambdaConfig )
3477 const lambdas = data . find ( ( { name } ) => name === services . lambda )
3578
36- if ( defineAuthChallengeArn && lambdas ?. data ?. [ region ] ) {
37- const lambdaInRegion = lambdas . data [ region ] . find ( lambda =>
38- defineAuthChallengeArn === lambda . FunctionArn )
39-
40- if ( lambdaInRegion ) {
41- const lambdaFunctionArn = lambdaInRegion . FunctionArn
79+ if ( lambdasArn ?. length > 0 && lambdas ?. data ?. [ region ] ) {
80+ const lambdasInRegion : RawAwsLambdaFunction [ ] = lambdas . data [ region ] . filter (
81+ ( { FunctionArn } : RawAwsLambdaFunction ) =>
82+ lambdasArn . includes ( FunctionArn )
83+ )
4284
85+ if ( ! isEmpty ( lambdasInRegion ) ) {
86+ for ( const lambda of lambdasInRegion ) {
87+ connections . push ( {
88+ id : lambda . FunctionArn ,
89+ resourceType : services . lambda ,
90+ relation : 'child' ,
91+ field : 'lambdas' ,
92+ } )
93+ }
94+ }
95+ }
96+
97+ /**
98+ * Find MKS
99+ * related to this cognito user pool
100+ */
101+ const kmsKeyID = lambdaConfig ?. KMSKeyID
102+ const kms = data . find ( ( { name } ) => name === services . kms )
103+
104+ if ( kmsKeyID && kms ?. data ?. [ region ] ) {
105+ const kmsInRegion : AwsKms = kms . data [ region ] . find (
106+ ( { KeyId } : AwsKms ) => kmsKeyID === KeyId
107+ )
108+
109+ if ( kmsInRegion ) {
43110 connections . push ( {
44- id : lambdaFunctionArn ,
45- resourceType : services . lambda ,
111+ id : kmsInRegion . KeyId ,
112+ resourceType : services . kms ,
46113 relation : 'child' ,
47- field : 'lambda ' ,
114+ field : 'kms ' ,
48115 } )
49116 }
50117 }
51118
52- // TODO Email Sender
119+ /**
120+ * Find SES sender
121+ * related to this cognito user pool
122+ */
123+ const emailConfigSourceArn = emailConfiguration ?. SourceArn
124+ const emails = data . find ( ( { name } ) => name === services . ses )
53125
54- // TODO SMS Sender
126+ if ( emailConfigSourceArn && emails ?. data ?. [ region ] ) {
127+ const emailInRegion : RawAwsSes = emails . data [ region ] . find (
128+ ( { Identity } : RawAwsSes ) =>
129+ emailConfigSourceArn === sesArn ( { region, account, email : Identity } )
130+ )
131+
132+ if ( emailInRegion ) {
133+ connections . push ( {
134+ id : sesArn ( { region, account, email : emailInRegion . Identity } ) ,
135+ resourceType : services . ses ,
136+ relation : 'child' ,
137+ field : 'ses' ,
138+ } )
139+ }
140+ }
141+
142+ /**
143+ * Find SNS caller
144+ * related to this cognito user pool
145+ */
146+ const smsConfigSnsCallerArn = smsConfiguration ?. SnsCallerArn
147+ const iamRoles = data . find ( ( { name } ) => name === services . iamRole )
148+
149+ if ( smsConfigSnsCallerArn && iamRoles ?. data ?. [ region ] ) {
150+ const iamRoleInRegion : RawAwsIamRole = iamRoles . data [ region ] . find (
151+ ( { Arn } : RawAwsIamRole ) => smsConfigSnsCallerArn === Arn
152+ )
153+
154+ if ( iamRoleInRegion ) {
155+ connections . push ( {
156+ id : iamRoleInRegion . Arn ,
157+ resourceType : services . iamRole ,
158+ relation : 'child' ,
159+ field : 'iamRole' ,
160+ } )
161+ }
162+ }
55163
56164 const userPoolResult = {
57165 [ id ] : connections ,
58166 }
59167 return userPoolResult
60- }
168+ }
0 commit comments