From 94d054a9a54bd61cf551776629df16e30bcef751 Mon Sep 17 00:00:00 2001 From: akshbhu <39866697+akshbhu@users.noreply.github.com> Date: Thu, 21 Oct 2021 14:21:03 -0700 Subject: [PATCH] fix: userPool group template fixes (#8515) * fix: template and naming fixed * fix: addressed comments --- ...er-pool-group-stack-transform.test.ts.snap | 166 ++++++++++-------- .../utils/migrate-override-resource.test.ts | 4 +- .../auth-user-pool-group-stack-builder.ts | 3 +- .../user-pool-group-stack-transform.ts | 34 +++- .../utils/migrate-override-resource.ts | 8 +- 5 files changed, 132 insertions(+), 83 deletions(-) diff --git a/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/auth-stack-builder/__snapshots__/user-pool-group-stack-transform.test.ts.snap b/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/auth-stack-builder/__snapshots__/user-pool-group-stack-transform.test.ts.snap index efa217da6ae..85e611f3864 100644 --- a/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/auth-stack-builder/__snapshots__/user-pool-group-stack-transform.test.ts.snap +++ b/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/auth-stack-builder/__snapshots__/user-pool-group-stack-transform.test.ts.snap @@ -32,15 +32,19 @@ Object { "Type": "String", }, "authmockResourceAppClientID": Object { + "Default": "authmockResourceAppClientID", "Type": "String", }, "authmockResourceAppClientIDWeb": Object { + "Default": "authmockResourceAppClientIDWeb", "Type": "String", }, "authmockResourceIdentityPoolId": Object { + "Default": "authmockResourceIdentityPoolId", "Type": "String", }, "authmockResourceUserPoolId": Object { + "Default": "authmockResourceUserPoolId", "Type": "String", }, "env": Object { @@ -144,44 +148,7 @@ Object { }, "Type": "AWS::IAM::Role", }, - "RoleMapFunctionInput": Object { - "DeletionPolicy": "Delete", - "Properties": Object { - "AuthRoleArn": Object { - "Ref": "AuthRoleArn", - }, - "ServiceToken": Object { - "Fn::GetAtt": Array [ - "RoleMapLambdaFunction", - "Arn", - ], - }, - "UnauthRoleArn": Object { - "Ref": "UnauthRoleArn", - }, - "appClientID": Object { - "Ref": "authmockResourceAppClientID", - }, - "appClientIDWeb": Object { - "Ref": "authmockResourceAppClientIDWeb", - }, - "env": Object { - "Ref": "env", - }, - "identityPoolId": Object { - "Ref": "authmockResourceIdentityPoolId", - }, - "region": Object { - "Ref": "AWS::Region", - }, - "userPoolId": Object { - "Ref": "authmockResourceUserPoolId", - }, - }, - "Type": "Custom::LambdaCallout", - "UpdateReplacePolicy": "Delete", - }, - "RoleMapLambdaFunction": Object { + "RoleMapFunction": Object { "Properties": Object { "Code": Object { "ZipFile": "const response = require('cfn-response'); @@ -235,6 +202,46 @@ exports.handler = (event, context) => { }, "Type": "AWS::Lambda::Function", }, + "RoleMapFunctionInput": Object { + "DeletionPolicy": "Delete", + "DependsOn": Array [ + "RoleMapFunction", + ], + "Properties": Object { + "AuthRoleArn": Object { + "Ref": "AuthRoleArn", + }, + "ServiceToken": Object { + "Fn::GetAtt": Array [ + "RoleMapFunction", + "Arn", + ], + }, + "UnauthRoleArn": Object { + "Ref": "UnauthRoleArn", + }, + "appClientID": Object { + "Ref": "authmockResourceAppClientID", + }, + "appClientIDWeb": Object { + "Ref": "authmockResourceAppClientIDWeb", + }, + "env": Object { + "Ref": "env", + }, + "identityPoolId": Object { + "Ref": "authmockResourceIdentityPoolId", + }, + "region": Object { + "Ref": "AWS::Region", + }, + "userPoolId": Object { + "Ref": "authmockResourceUserPoolId", + }, + }, + "Type": "Custom::LambdaCallout", + "UpdateReplacePolicy": "Delete", + }, "adinGroup": Object { "Properties": Object { "Description": "override success", @@ -327,15 +334,19 @@ Object { "Type": "String", }, "authmockResourceAppClientID": Object { + "Default": "authmockResourceAppClientID", "Type": "String", }, "authmockResourceAppClientIDWeb": Object { + "Default": "authmockResourceAppClientIDWeb", "Type": "String", }, "authmockResourceIdentityPoolId": Object { + "Default": "authmockResourceIdentityPoolId", "Type": "String", }, "authmockResourceUserPoolId": Object { + "Default": "authmockResourceUserPoolId", "Type": "String", }, "env": Object { @@ -439,44 +450,7 @@ Object { }, "Type": "AWS::IAM::Role", }, - "RoleMapFunctionInput": Object { - "DeletionPolicy": "Delete", - "Properties": Object { - "AuthRoleArn": Object { - "Ref": "AuthRoleArn", - }, - "ServiceToken": Object { - "Fn::GetAtt": Array [ - "RoleMapLambdaFunction", - "Arn", - ], - }, - "UnauthRoleArn": Object { - "Ref": "UnauthRoleArn", - }, - "appClientID": Object { - "Ref": "authmockResourceAppClientID", - }, - "appClientIDWeb": Object { - "Ref": "authmockResourceAppClientIDWeb", - }, - "env": Object { - "Ref": "env", - }, - "identityPoolId": Object { - "Ref": "authmockResourceIdentityPoolId", - }, - "region": Object { - "Ref": "AWS::Region", - }, - "userPoolId": Object { - "Ref": "authmockResourceUserPoolId", - }, - }, - "Type": "Custom::LambdaCallout", - "UpdateReplacePolicy": "Delete", - }, - "RoleMapLambdaFunction": Object { + "RoleMapFunction": Object { "Properties": Object { "Code": Object { "ZipFile": "const response = require('cfn-response'); @@ -530,6 +504,46 @@ exports.handler = (event, context) => { }, "Type": "AWS::Lambda::Function", }, + "RoleMapFunctionInput": Object { + "DeletionPolicy": "Delete", + "DependsOn": Array [ + "RoleMapFunction", + ], + "Properties": Object { + "AuthRoleArn": Object { + "Ref": "AuthRoleArn", + }, + "ServiceToken": Object { + "Fn::GetAtt": Array [ + "RoleMapFunction", + "Arn", + ], + }, + "UnauthRoleArn": Object { + "Ref": "UnauthRoleArn", + }, + "appClientID": Object { + "Ref": "authmockResourceAppClientID", + }, + "appClientIDWeb": Object { + "Ref": "authmockResourceAppClientIDWeb", + }, + "env": Object { + "Ref": "env", + }, + "identityPoolId": Object { + "Ref": "authmockResourceIdentityPoolId", + }, + "region": Object { + "Ref": "AWS::Region", + }, + "userPoolId": Object { + "Ref": "authmockResourceUserPoolId", + }, + }, + "Type": "Custom::LambdaCallout", + "UpdateReplacePolicy": "Delete", + }, "adinmockGroup": Object { "Properties": Object { "Description": "override success", diff --git a/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/utils/migrate-override-resource.test.ts b/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/utils/migrate-override-resource.test.ts index 6bdf69b5545..e4d93d423ed 100644 --- a/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/utils/migrate-override-resource.test.ts +++ b/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/utils/migrate-override-resource.test.ts @@ -6,9 +6,11 @@ jest.mock('amplify-prompts'); jest.mock('fs-extra'); jest.mock('amplify-cli-core', () => ({ + ...(jest.requireActual('amplify-cli-core') as {}), pathManager: { findProjectRoot: jest.fn().mockReturnValue('somePath'), getBackendDirPath: jest.fn().mockReturnValue('mockProjectPath'), + getResourceDirectoryPath: jest.fn().mockReturnValue('mockProjectPath'), }, JSONUtilities: { readJson: jest.fn().mockReturnValue({ @@ -82,7 +84,7 @@ jest.mock('amplify-cli-core', () => ({ test('migrate resource', async () => { const resourceName = 'mockResource'; migrateResourceToSupportOverride(resourceName); - const expectedPath = path.join('mockProjectPath', 'auth', resourceName, 'cli-inputs.json'); + const expectedPath = path.join('mockProjectPath', 'cli-inputs.json'); const expectedPayload = { version: '1', cognitoConfig: { diff --git a/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/auth-user-pool-group-stack-builder.ts b/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/auth-user-pool-group-stack-builder.ts index 6f3ab33225d..629b8b0eade 100644 --- a/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/auth-user-pool-group-stack-builder.ts +++ b/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/auth-user-pool-group-stack-builder.ts @@ -256,7 +256,7 @@ export class AmplifyUserPoolGroupStack extends cdk.Stack implements AmplifyUserP ], }); // lambda function for RoleMap Custom Resource - this.roleMapLambdaFunction = new lambda.CfnFunction(this, 'RoleMapLambdaFunction', { + this.roleMapLambdaFunction = new lambda.CfnFunction(this, 'RoleMapFunction', { code: { zipFile: fs.readFileSync(roleMapLambdaFilePath, 'utf-8'), }, @@ -281,6 +281,7 @@ export class AmplifyUserPoolGroupStack extends cdk.Stack implements AmplifyUserP env: cdk.Fn.ref('env'), }, }); + this.roleMapCustomResource.node.addDependency(this.roleMapLambdaFunction); } }; } diff --git a/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/user-pool-group-stack-transform.ts b/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/user-pool-group-stack-transform.ts index 27371a49170..2232c9c3fa8 100644 --- a/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/user-pool-group-stack-transform.ts +++ b/packages/amplify-category-auth/src/provider-utils/awscloudformation/auth-stack-builder/user-pool-group-stack-transform.ts @@ -51,7 +51,7 @@ export class AmplifyUserPoolGroupTransform extends AmplifyCategoryTransform { constructor(resourceName: string) { super(resourceName); this._authResourceName = resourceName; - this._resourceName = 'UserPoolGroups'; + this._resourceName = 'userPoolGroups'; this._synthesizer = new AuthStackSythesizer(); this._synthesizerOutputs = new AuthStackSythesizer(); this._app = new cdk.App(); @@ -116,6 +116,7 @@ export class AmplifyUserPoolGroupTransform extends AmplifyCategoryTransform { this._userPoolGroupTemplateObj.addCfnParameter( { type: 'String', + default: `auth${props.cognitoResourceName}UserPoolId`, }, `auth${props.cognitoResourceName}UserPoolId`, ); @@ -124,6 +125,7 @@ export class AmplifyUserPoolGroupTransform extends AmplifyCategoryTransform { this._userPoolGroupTemplateObj.addCfnParameter( { type: 'String', + default: `auth${props.cognitoResourceName}IdentityPoolId`, }, `auth${props.cognitoResourceName}IdentityPoolId`, ); @@ -132,6 +134,7 @@ export class AmplifyUserPoolGroupTransform extends AmplifyCategoryTransform { this._userPoolGroupTemplateObj.addCfnParameter( { type: 'String', + default: `auth${props.cognitoResourceName}AppClientID`, }, `auth${props.cognitoResourceName}AppClientID`, ); @@ -139,6 +142,7 @@ export class AmplifyUserPoolGroupTransform extends AmplifyCategoryTransform { this._userPoolGroupTemplateObj.addCfnParameter( { type: 'String', + default: `auth${props.cognitoResourceName}AppClientIDWeb`, }, `auth${props.cognitoResourceName}AppClientIDWeb`, ); @@ -237,5 +241,33 @@ export class AmplifyUserPoolGroupTransform extends AmplifyCategoryTransform { writeCFNTemplate(template, cognitostackFilePath, { templateFormat: CFNTemplateFormat.JSON, }); + // write parameters.json file + this.writeBuildFiles(context); + }; + + private writeBuildFiles = async (context: $TSContext) => { + const parametersJSONFilePath = path.join( + pathManager.getBackendDirPath(), + this._category, + this._resourceName, + 'build', + 'parameters.json', + ); + + const roles = { + AuthRoleArn: { + 'Fn::GetAtt': ['AuthRole', 'Arn'], + }, + UnauthRoleArn: { + 'Fn::GetAtt': ['UnauthRole', 'Arn'], + }, + }; + + //save parameters + let parameters = { + ...roles, + }; + //save parameters + JSONUtilities.writeJson(parametersJSONFilePath, parameters); }; } diff --git a/packages/amplify-category-auth/src/provider-utils/awscloudformation/utils/migrate-override-resource.ts b/packages/amplify-category-auth/src/provider-utils/awscloudformation/utils/migrate-override-resource.ts index cbde6d2f354..d5955838c97 100644 --- a/packages/amplify-category-auth/src/provider-utils/awscloudformation/utils/migrate-override-resource.ts +++ b/packages/amplify-category-auth/src/provider-utils/awscloudformation/utils/migrate-override-resource.ts @@ -1,4 +1,4 @@ -import { $TSObject, JSONUtilities, NotInitializedError, pathManager } from 'amplify-cli-core'; +import { $TSObject, AmplifyCategories, JSONUtilities, NotInitializedError, pathManager } from 'amplify-cli-core'; import { printer } from 'amplify-prompts'; import * as path from 'path'; import uuid from 'uuid'; @@ -30,10 +30,10 @@ export const migrateResourceToSupportOverride = async (resourceName: string) => // New project, hence not able to find the amplify dir throw new NotInitializedError(); } - const authresourceDirPath = path.join(pathManager.getBackendDirPath(), 'auth', resourceName); - const userPoolGroupResourceDirPath = path.join(pathManager.getBackendDirPath(), 'auth', 'UserPoolGroups'); + const authresourceDirPath = pathManager.getResourceDirectoryPath(undefined, AmplifyCategories.AUTH, resourceName); + const userPoolGroupResourceDirPath = pathManager.getResourceDirectoryPath(undefined, AmplifyCategories.AUTH, 'userPoolGroups'); const backupAuthResourceFolder = backup(authresourceDirPath, projectPath, resourceName); - const backupUserPoolGroupResourceFolder = backup(userPoolGroupResourceDirPath, projectPath, 'UserPoolGroups'); + const backupUserPoolGroupResourceFolder = backup(userPoolGroupResourceDirPath, projectPath, 'userPoolGroups'); try { const parameters = JSONUtilities.readJson<$TSObject>(path.join(authresourceDirPath, 'parameters.json'), { throwIfNotExist: true });