Skip to content

Commit

Permalink
fix: userPool group template fixes (aws-amplify#8515)
Browse files Browse the repository at this point in the history
* fix: template and naming fixed

* fix: addressed comments
  • Loading branch information
akshbhu committed Oct 27, 2021
1 parent c2d9465 commit 94d054a
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
},
Expand All @@ -281,6 +281,7 @@ export class AmplifyUserPoolGroupStack extends cdk.Stack implements AmplifyUserP
env: cdk.Fn.ref('env'),
},
});
this.roleMapCustomResource.node.addDependency(this.roleMapLambdaFunction);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -116,6 +116,7 @@ export class AmplifyUserPoolGroupTransform extends AmplifyCategoryTransform {
this._userPoolGroupTemplateObj.addCfnParameter(
{
type: 'String',
default: `auth${props.cognitoResourceName}UserPoolId`,
},
`auth${props.cognitoResourceName}UserPoolId`,
);
Expand All @@ -124,6 +125,7 @@ export class AmplifyUserPoolGroupTransform extends AmplifyCategoryTransform {
this._userPoolGroupTemplateObj.addCfnParameter(
{
type: 'String',
default: `auth${props.cognitoResourceName}IdentityPoolId`,
},
`auth${props.cognitoResourceName}IdentityPoolId`,
);
Expand All @@ -132,13 +134,15 @@ export class AmplifyUserPoolGroupTransform extends AmplifyCategoryTransform {
this._userPoolGroupTemplateObj.addCfnParameter(
{
type: 'String',
default: `auth${props.cognitoResourceName}AppClientID`,
},
`auth${props.cognitoResourceName}AppClientID`,
);

this._userPoolGroupTemplateObj.addCfnParameter(
{
type: 'String',
default: `auth${props.cognitoResourceName}AppClientIDWeb`,
},
`auth${props.cognitoResourceName}AppClientIDWeb`,
);
Expand Down Expand Up @@ -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);
};
}
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit 94d054a

Please sign in to comment.