Skip to content

Commit a80bb06

Browse files
authored
Merge pull request #8 from cloudgraphdev/feature/CG-1059
Feature/cg 1059
2 parents e34636a + 625701e commit a80bb06

File tree

9 files changed

+146
-35
lines changed

9 files changed

+146
-35
lines changed

src/services/cloudFormationStack/connections.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { ServiceConnection } from '@cloudgraph/sdk'
22
import { Stack } from 'aws-sdk/clients/cloudformation'
33
import isEmpty from 'lodash/isEmpty'
4-
import resources from '../../enums/resources'
54
import services from '../../enums/services'
65
import { RawAwsCloudFormationStack } from './data'
76
import { RawAwsIamRole } from '../iamRole/data'
87
import { TagMap } from '../../types'
9-
import { getIamId } from '../../utils/ids'
108
import { globalRegionName } from '../../enums/regions'
119

1210
/**
@@ -84,14 +82,10 @@ export default ({
8482
)
8583
if (!isEmpty(dataAtRegion)) {
8684
for (const instance of dataAtRegion) {
87-
const { RoleId: roleId, RoleName: roleName } = instance
85+
const { Arn: arn }: RawAwsIamRole = instance
8886

8987
connections.push({
90-
id: getIamId({
91-
resourceId: roleId,
92-
resourceName: roleName,
93-
resourceType: resources.iamRole,
94-
}),
88+
id: arn,
9589
resourceType: services.iamRole,
9690
relation: 'child',
9791
field: 'iamRole',

src/services/cloudFormationStack/format.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export default ({
124124
timeoutInMinutes,
125125
capabilities,
126126
outputs: outputsList,
127-
roleArn: roleArn || '', // TODO: create connection to IAM role if possible
127+
roleArn: roleArn || '',
128128
tags: formatTagsFromMap(tags),
129129
enableTerminationProtection: enableTerminationProtection ? t.yes : t.no,
130130
parentId: parentId || '',
Lines changed: 126 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
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+
}

src/services/cognitoUserPool/schema.graphql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ type awsCognitoUserPool implements awsBaseService @key(fields: "id") {
119119
usernameConfigurationCaseSensitive: String @search(by: [hash, regexp])
120120
accountRecoverySettings: [awsAccountRecoverySetting]
121121
tags: [awsRawTag]
122-
lambda: [awsLambda] @hasInverse(field: cognitoUserPool) #change to plural
122+
lambdas: [awsLambda] @hasInverse(field: cognitoUserPools)
123123
appSync: [awsAppSync] @hasInverse(field: cognitoUserPool)
124-
}
125-
126-
# TODO: add connetion to kms
127-
# TODO: add connection to iamRole using SmsConfiguration.SnsCallerArn
124+
kms: [awsKms] @hasInverse(field: cognitoUserPools)
125+
ses: [awsSes] @hasInverse(field: cognitoUserPools)
126+
iamRole: [awsIamRole] @hasInverse(field: cognitoUserPools)
127+
}

src/services/iamRole/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ type awsIamRole implements awsBaseService @key(fields: "id") {
2222
systemsManagerInstances: [awsSystemsManagerInstance]
2323
@hasInverse(field: iamRole)
2424
iamInstanceProfiles: [awsIamInstanceProfile] @hasInverse(field: iamRole)
25+
cognitoUserPools: [awsCognitoUserPool] @hasInverse(field: iamRole)
2526
}

src/services/kms/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ type awsKms implements awsBaseService @key(fields: "id") {
2828
sageMakerNotebookInstances: [awsSageMakerNotebookInstance]
2929
@hasInverse(field: kms)
3030
rdsClusterSnapshots: [awsRdsClusterSnapshot] @hasInverse(field: kms)
31+
cognitoUserPools: [awsCognitoUserPool] @hasInverse(field: kms)
3132
}

src/services/lambda/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type awsLambda implements awsBaseService @key(fields: "arn") {
2020
securityGroups: [awsSecurityGroup] @hasInverse(field: lambda)
2121
subnet: [awsSubnet] @hasInverse(field: lambda) #change to plural
2222
vpc: [awsVpc] @hasInverse(field: lambda)
23-
cognitoUserPool: [awsCognitoUserPool] @hasInverse(field: lambda) #change to plural
23+
cognitoUserPools: [awsCognitoUserPool] @hasInverse(field: lambdas)
2424
appSync: [awsAppSync] @hasInverse(field: lambda)
2525
}
2626

src/services/ses/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
type awsSes implements awsBaseService @key(fields: "arn") {
22
email: String @search(by: [hash, regexp])
33
verificationStatus: String @search(by: [hash, regexp])
4+
cognitoUserPools: [awsCognitoUserPool] @hasInverse(field: ses)
45
}

src/types/generated.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,13 +1152,16 @@ export type AwsCognitoUserPool = AwsBaseService & {
11521152
emailVerificationMessage?: Maybe<Scalars['String']>;
11531153
emailVerificationSubject?: Maybe<Scalars['String']>;
11541154
estimatedNumberOfUsers?: Maybe<Scalars['Int']>;
1155-
lambda?: Maybe<Array<Maybe<AwsLambda>>>;
1155+
iamRole?: Maybe<Array<Maybe<AwsIamRole>>>;
1156+
kms?: Maybe<Array<Maybe<AwsKms>>>;
11561157
lambdaConfig?: Maybe<AwsCognitoUserPoolLambdaConfig>;
1158+
lambdas?: Maybe<Array<Maybe<AwsLambda>>>;
11571159
lastModifiedDate?: Maybe<Scalars['String']>;
11581160
mfaConfiguration?: Maybe<Scalars['String']>;
11591161
name?: Maybe<Scalars['String']>;
11601162
policies?: Maybe<AwsCognitoUserPoolPasswordPolicy>;
11611163
schemaAttributes?: Maybe<Array<Maybe<AwsCognitoUserPoolSchemaAttribute>>>;
1164+
ses?: Maybe<Array<Maybe<AwsSes>>>;
11621165
smsAuthenticationMessage?: Maybe<Scalars['String']>;
11631166
smsConfigurationExternalId?: Maybe<Scalars['String']>;
11641167
smsConfigurationFailure?: Maybe<Scalars['String']>;
@@ -3011,6 +3014,7 @@ export type AwsIamRole = AwsBaseService & {
30113014
assumeRolePolicy?: Maybe<AwsIamJsonPolicy>;
30123015
cloudFormationStack?: Maybe<Array<Maybe<AwsCloudFormationStack>>>;
30133016
codebuilds?: Maybe<Array<Maybe<AwsCodebuild>>>;
3017+
cognitoUserPools?: Maybe<Array<Maybe<AwsCognitoUserPool>>>;
30143018
configurationRecorder?: Maybe<Array<Maybe<AwsConfigurationRecorder>>>;
30153019
createdAt?: Maybe<Scalars['String']>;
30163020
description?: Maybe<Scalars['String']>;
@@ -3134,6 +3138,7 @@ export type AwsKms = AwsBaseService & {
31343138
cloudtrail?: Maybe<Array<Maybe<AwsCloudtrail>>>;
31353139
cloudwatchLog?: Maybe<Array<Maybe<AwsCloudwatchLog>>>;
31363140
codebuilds?: Maybe<Array<Maybe<AwsCodebuild>>>;
3141+
cognitoUserPools?: Maybe<Array<Maybe<AwsCognitoUserPool>>>;
31373142
creationDate?: Maybe<Scalars['String']>;
31383143
customerMasterKeySpec?: Maybe<Scalars['String']>;
31393144
deletionDate?: Maybe<Scalars['String']>;
@@ -3162,7 +3167,7 @@ export type AwsKms = AwsBaseService & {
31623167

31633168
export type AwsLambda = AwsBaseService & {
31643169
appSync?: Maybe<Array<Maybe<AwsAppSync>>>;
3165-
cognitoUserPool?: Maybe<Array<Maybe<AwsCognitoUserPool>>>;
3170+
cognitoUserPools?: Maybe<Array<Maybe<AwsCognitoUserPool>>>;
31663171
description?: Maybe<Scalars['String']>;
31673172
environmentVariables?: Maybe<Array<Maybe<AwsLambdaEnvironmentVariable>>>;
31683173
handler?: Maybe<Scalars['String']>;
@@ -3761,6 +3766,7 @@ export type AwsServiceBillingInfo = {
37613766
};
37623767

37633768
export type AwsSes = AwsBaseService & {
3769+
cognitoUserPools?: Maybe<Array<Maybe<AwsCognitoUserPool>>>;
37643770
email?: Maybe<Scalars['String']>;
37653771
verificationStatus?: Maybe<Scalars['String']>;
37663772
};

0 commit comments

Comments
 (0)