Skip to content

Commit 2911751

Browse files
committed
feat: Handle TODOs for dynamoDB
1 parent d79a230 commit 2911751

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
@@ -115,9 +115,9 @@ type awsCognitoUserPool @key(fields: "id") {
115115
usernameConfigurationCaseSensitive: String @search(by: [hash, regexp])
116116
accountRecoverySettings: [awsAccountRecoverySetting]
117117
tags: [awsRawTag]
118-
lambda: [awsLambda] @hasInverse(field: cognitoUserPool) #change to plural
118+
lambdas: [awsLambda] @hasInverse(field: cognitoUserPools)
119119
appSync: [awsAppSync] @hasInverse(field: cognitoUserPool)
120-
}
121-
122-
# TODO: add connetion to kms
123-
# TODO: add connection to iamRole using SmsConfiguration.SnsCallerArn
120+
kms: [awsKms] @hasInverse(field: cognitoUserPools)
121+
ses: [awsSes] @hasInverse(field: cognitoUserPools)
122+
iamRole: [awsIamRole] @hasInverse(field: cognitoUserPools)
123+
}

src/services/iamRole/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ type awsIamRole @key(fields: "id") {
2323
sageMakerNotebookInstances: [awsSageMakerNotebookInstance] @hasInverse(field: iamRole)
2424
systemsManagerInstances: [awsSystemsManagerInstance] @hasInverse(field: iamRole)
2525
iamInstanceProfiles: [awsIamInstanceProfile] @hasInverse(field: iamRole)
26+
cognitoUserPools: [awsCognitoUserPool] @hasInverse(field: iamRole)
2627
}

src/services/kms/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ type awsKms @key(fields: "id"){
3030
dmsReplicationInstances: [awsDmsReplicationInstance] @hasInverse(field: kms)
3131
sageMakerNotebookInstances: [awsSageMakerNotebookInstance] @hasInverse(field: kms)
3232
rdsClusterSnapshots: [awsRdsClusterSnapshot] @hasInverse(field: kms)
33+
cognitoUserPools: [awsCognitoUserPool] @hasInverse(field: kms)
3334
}

src/services/lambda/schema.graphql

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

src/services/ses/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ type awsSes @key(fields: "arn") {
55
region: String @search(by: [hash, regexp])
66
email: String @search(by: [hash, regexp])
77
verificationStatus: String @search(by: [hash, regexp])
8+
cognitoUserPools: [awsCognitoUserPool] @hasInverse(field: ses)
89
}

src/types/generated.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,15 +1206,18 @@ export type AwsCognitoUserPool = {
12061206
emailVerificationMessage?: Maybe<Scalars['String']>;
12071207
emailVerificationSubject?: Maybe<Scalars['String']>;
12081208
estimatedNumberOfUsers?: Maybe<Scalars['Int']>;
1209+
iamRole?: Maybe<Array<Maybe<AwsIamRole>>>;
12091210
id: Scalars['String'];
1210-
lambda?: Maybe<Array<Maybe<AwsLambda>>>;
1211+
kms?: Maybe<Array<Maybe<AwsKms>>>;
12111212
lambdaConfig?: Maybe<AwsCognitoUserPoolLambdaConfig>;
1213+
lambdas?: Maybe<Array<Maybe<AwsLambda>>>;
12121214
lastModifiedDate?: Maybe<Scalars['String']>;
12131215
mfaConfiguration?: Maybe<Scalars['String']>;
12141216
name?: Maybe<Scalars['String']>;
12151217
policies?: Maybe<AwsCognitoUserPoolPasswordPolicy>;
12161218
region?: Maybe<Scalars['String']>;
12171219
schemaAttributes?: Maybe<Array<Maybe<AwsCognitoUserPoolSchemaAttribute>>>;
1220+
ses?: Maybe<Array<Maybe<AwsSes>>>;
12181221
smsAuthenticationMessage?: Maybe<Scalars['String']>;
12191222
smsConfigurationExternalId?: Maybe<Scalars['String']>;
12201223
smsConfigurationFailure?: Maybe<Scalars['String']>;
@@ -3187,6 +3190,7 @@ export type AwsIamRole = {
31873190
assumeRolePolicy?: Maybe<AwsIamJsonPolicy>;
31883191
cloudFormationStack?: Maybe<Array<Maybe<AwsCloudFormationStack>>>;
31893192
codebuilds?: Maybe<Array<Maybe<AwsCodebuild>>>;
3193+
cognitoUserPools?: Maybe<Array<Maybe<AwsCognitoUserPool>>>;
31903194
configurationRecorder?: Maybe<Array<Maybe<AwsConfigurationRecorder>>>;
31913195
createdAt?: Maybe<Scalars['String']>;
31923196
description?: Maybe<Scalars['String']>;
@@ -3338,6 +3342,7 @@ export type AwsKms = {
33383342
cloudtrail?: Maybe<Array<Maybe<AwsCloudtrail>>>;
33393343
cloudwatchLog?: Maybe<Array<Maybe<AwsCloudwatchLog>>>;
33403344
codebuilds?: Maybe<Array<Maybe<AwsCodebuild>>>;
3345+
cognitoUserPools?: Maybe<Array<Maybe<AwsCognitoUserPool>>>;
33413346
creationDate?: Maybe<Scalars['String']>;
33423347
customerMasterKeySpec?: Maybe<Scalars['String']>;
33433348
deletionDate?: Maybe<Scalars['String']>;
@@ -3370,7 +3375,7 @@ export type AwsLambda = {
33703375
accountId: Scalars['String'];
33713376
appSync?: Maybe<Array<Maybe<AwsAppSync>>>;
33723377
arn: Scalars['String'];
3373-
cognitoUserPool?: Maybe<Array<Maybe<AwsCognitoUserPool>>>;
3378+
cognitoUserPools?: Maybe<Array<Maybe<AwsCognitoUserPool>>>;
33743379
description?: Maybe<Scalars['String']>;
33753380
environmentVariables?: Maybe<Array<Maybe<AwsLambdaEnvironmentVariable>>>;
33763381
handler?: Maybe<Scalars['String']>;
@@ -4034,6 +4039,7 @@ export type AwsServiceBillingInfo = {
40344039
export type AwsSes = {
40354040
accountId: Scalars['String'];
40364041
arn: Scalars['String'];
4042+
cognitoUserPools?: Maybe<Array<Maybe<AwsCognitoUserPool>>>;
40374043
email?: Maybe<Scalars['String']>;
40384044
id: Scalars['String'];
40394045
region?: Maybe<Scalars['String']>;

0 commit comments

Comments
 (0)