Skip to content

Commit

Permalink
feat: overrides uniformity accross all the categories (aws-amplify#8695)
Browse files Browse the repository at this point in the history
* feat: overrides uniformity accross all the categories

* chore: remove unneeded dependency

* fix: apply override unformity to root-stack

Co-authored-by: Ghosh <[email protected]>
  • Loading branch information
2 people authored and Sachin Panemangalore committed Nov 11, 2021
1 parent e1806d3 commit b3a79ef
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AmplifyAuthCognitoStackTemplate } from '@aws-amplify/cli-overrides-helper';

export function overrideProps(props: AmplifyAuthCognitoStackTemplate) {
return props;
}
export function override(resources: AmplifyAuthCognitoStackTemplate) {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@types/fs-extra": "^9.0.11",
"fs-extra": "^9.1.0",
"@aws-amplify/cli-overrides-helper": "1.1.0-ext11.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class AmplifyAuthTransform extends AmplifyCategoryTransform {
formatter.list(['No override File Found', `To override ${this.resourceName} run amplify override auth`]);
return '';
});
const cognitoStackTemplateObj = this._authTemplateObj as AmplifyAuthCognitoStack & AmplifyStackTemplate;

const sandboxNode = new vm.NodeVM({
console: 'inherit',
timeout: 5000,
Expand All @@ -122,9 +122,9 @@ export class AmplifyAuthTransform extends AmplifyCategoryTransform {
},
});
try {
this._authTemplateObj = await sandboxNode
await sandboxNode
.run(overrideCode, path.join(overrideDir, 'build', 'override.js'))
.overrideProps(cognitoStackTemplateObj);
.override(this._authTemplateObj as AmplifyAuthCognitoStack & AmplifyStackTemplate);
} catch (err: $TSAny) {
const error = new Error(`Skipping override due to ${err}${os.EOL}`);
printer.error(`${error}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,13 @@ export class AmplifyUserPoolGroupTransform extends AmplifyCategoryTransform {
formatter.list(['No override File Found', `To override ${this._resourceName} run amplify override auth`]);
return '';
});
const cognitoStackTemplateObj = this._userPoolGroupTemplateObj as AmplifyUserPoolGroupStack & AmplifyStackTemplate;
const sandboxNode = new vm.NodeVM({
console: 'inherit',
timeout: 5000,
sandbox: {},
});
try {
this._userPoolGroupTemplateObj = sandboxNode.run(overrideCode).overrideProps(cognitoStackTemplateObj);
sandboxNode.run(overrideCode).override(this._userPoolGroupTemplateObj as AmplifyUserPoolGroupStack & AmplifyStackTemplate);
} catch (err: $TSAny) {
const error = new Error(`Skipping override due to ${err}${os.EOL}`);
printer.error(`${error}`);
Expand Down
3 changes: 2 additions & 1 deletion packages/amplify-category-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"inquirer": "^7.3.3",
"lodash": "^4.17.21",
"promise-sequential": "^1.1.1",
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"vm2": "^3.9.3"
},
"devDependencies": {
"aws-sdk": "^2.963.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AmplifyDDBResourceTemplate } from '@aws-amplify/cli-overrides-helper';

export function overrideProps(props: AmplifyDDBResourceTemplate) {
return props;
}
export function override(resources: AmplifyDDBResourceTemplate) {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "overrides-for-root-stack",
"name": "overrides-dependencies",
"version": "1.0.0",
"description": "",
"scripts": {
Expand All @@ -8,8 +8,6 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@types/fs-extra": "^9.0.11",
"fs-extra": "^9.1.0",
"@aws-amplify/cli-overrides-helper": "1.1.0-ext11.0"

},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AmplifyS3ResourceTemplate } from '@aws-amplify/cli-overrides-helper';

export function overrideProps(props: AmplifyS3ResourceTemplate) {
return props;
}
export function override(resources: AmplifyS3ResourceTemplate) {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "overrides-for-root-stack",
"name": "overrides-dependencies",
"version": "1.0.0",
"description": "",
"scripts": {
Expand All @@ -8,8 +8,6 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@types/fs-extra": "^9.0.11",
"fs-extra": "^9.1.0",
"@aws-amplify/cli-overrides-helper": "1.1.0-ext11.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import * as fs from 'fs-extra';
import { JSONUtilities, pathManager, buildOverrideDir, $TSAny } from 'amplify-cli-core';
import * as path from 'path';
import { formatter, printer } from 'amplify-prompts';
import * as vm from 'vm2';
import os from 'os';

export class DDBStackTransform {
app: App;
Expand Down Expand Up @@ -168,32 +170,43 @@ export class DDBStackTransform {

async applyOverrides() {
const backendDir = pathManager.getBackendDirPath();
const overrideFilePath = pathManager.getResourceDirectoryPath(undefined, 'storage', this._resourceName);
const resourceDirPath = pathManager.getResourceDirectoryPath(undefined, 'storage', this._resourceName);
const overrideJSFilePath = path.resolve(path.join(resourceDirPath, 'build', 'override.js'));

const isBuild = await buildOverrideDir(backendDir, overrideFilePath).catch(error => {
const isBuild = await buildOverrideDir(backendDir, resourceDirPath).catch(error => {
printer.debug(`Skipping build as ${error.message}`);
return false;
});
// skip if packageManager or override.ts not found
if (isBuild) {
const { overrideProps } = await import(path.join(overrideFilePath, 'build', 'override.js')).catch(error => {
const { override } = await import(overrideJSFilePath).catch(error => {
formatter.list(['No override File Found', `To override ${this._resourceName} run amplify override auth ${this._resourceName} `]);
return undefined;
});

// pass stack object
const ddbStackTemplateObj = this._resourceTemplateObj as AmplifyDDBResourceTemplate;
//TODO: Check Script Options
if (typeof overrideProps === 'function' && overrideProps) {
if (typeof override === 'function' && override) {
const overrideCode: string = await fs.readFile(overrideJSFilePath, 'utf-8').catch(() => {
formatter.list(['No override File Found', `To override ${this._resourceName} run amplify override auth`]);
return '';
});

const sandboxNode = new vm.NodeVM({
console: 'inherit',
timeout: 5000,
sandbox: {},
require: {
context: 'sandbox',
builtin: ['path'],
external: true,
},
});
try {
this._resourceTemplateObj = overrideProps(this._resourceTemplateObj as AmplifyDDBResourceTemplate);

//The vm module enables compiling and running code within V8 Virtual Machine contexts. The vm module is not a security mechanism. Do not use it to run untrusted code.
// const script = new vm.Script(overrideCode);
// script.runInContext(vm.createContext(cognitoStackTemplateObj));
return;
} catch (error: $TSAny) {
throw new Error(error);
await sandboxNode.run(overrideCode, overrideJSFilePath).override(this._resourceTemplateObj as AmplifyDDBResourceTemplate);
} catch (err: $TSAny) {
const error = new Error(`Skipping override due to ${err}${os.EOL}`);
printer.error(`${error}`);
error.stack = undefined;
throw error;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
import { formatter, printer } from 'amplify-prompts';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as vm from 'vm2';
import os from 'os';
import { S3PermissionType, S3UserInputs } from '../service-walkthrough-types/s3-user-input-types';
import { canResourceBeTransformed, S3CFNDependsOn, S3CFNPermissionType, S3InputState } from '../service-walkthroughs/s3-user-input-state';
import { AmplifyS3ResourceCfnStack } from './s3-stack-builder';
Expand Down Expand Up @@ -167,27 +169,43 @@ export class AmplifyS3ResourceStackTransform {
// Modify cloudformation files based on overrides
async applyOverrides() {
const backendDir = pathManager.getBackendDirPath();
const overrideFilePath = pathManager.getResourceDirectoryPath(undefined, AmplifyCategories.STORAGE, this.resourceName);
const isBuild = await buildOverrideDir(backendDir, overrideFilePath).catch(error => {
const resourceDirPath = pathManager.getResourceDirectoryPath(undefined, AmplifyCategories.STORAGE, this.resourceName);
const overrideJSFilePath = path.resolve(path.join(resourceDirPath, 'build', 'override.js'));

const isBuild = await buildOverrideDir(backendDir, resourceDirPath).catch(error => {
printer.debug(`Skipping build as ${error.message}`);
return false;
});
//Skip if packageManager or override.ts not found
if (isBuild) {
const { overrideProps } = await import(path.join(overrideFilePath, 'build', 'override.js')).catch(error => {
const { override } = await import(overrideJSFilePath).catch(error => {
formatter.list(['No override File Found', `To override ${this.resourceName} run amplify override auth ${this.resourceName} `]);
return undefined;
});
// Pass stack object
if (overrideProps && typeof overrideProps === 'function') {
if (override && typeof override === 'function') {
const overrideCode: string = await fs.readFile(overrideJSFilePath, 'utf-8').catch(() => {
formatter.list(['No override File Found', `To override ${this.resourceName} run amplify override auth`]);
return '';
});

const sandboxNode = new vm.NodeVM({
console: 'inherit',
timeout: 5000,
sandbox: {},
require: {
context: 'sandbox',
builtin: ['path'],
external: true,
},
});
try {
this.resourceTemplateObj = overrideProps(this.resourceTemplateObj as AmplifyS3ResourceTemplate);
//The vm module enables compiling and running code within V8 Virtual Machine contexts. The vm module is not a security mechanism. Do not use it to run untrusted code.
// const script = new vm.Script(overrideCode);
// script.runInContext(vm.createContext(cognitoStackTemplateObj));
return;
} catch (error: $TSAny) {
throw new Error(error);
await sandboxNode.run(overrideCode, overrideJSFilePath).override(this.resourceTemplateObj as AmplifyS3ResourceTemplate);
} catch (err: $TSAny) {
const error = new Error(`Skipping override due to ${err}${os.EOL}`);
printer.error(`${error}`);
error.stack = undefined;
throw error;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const generateOverrideSkeleton = async (context: $TSContext, srcResourceD
await buildOverrideDir(backendDir, destDirPath);

printer.success(`Successfully generated "override.ts" folder at ${destDirPath}`);
const isOpen = await prompter.confirmContinue('Do you want to edit override.ts file now?');
const isOpen = await prompter.yesOrNo('Do you want to edit override.ts file now?', true);
if (isOpen) {
await context.amplify.openEditor(context, overrideFile);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-cli/amplify-plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"uninstall",
"upgrade",
"version",
"build-override"
"build"
],
"commandAliases": {
"h": "help",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { run } from '../../commands/build-override';
import { run } from '../../commands/build';
import { $TSContext } from 'amplify-cli-core';

jest.mock('amplify-cli-core');
jest.mock('amplify-provider-awscloudformation');

describe('run build-override command', () => {
it('runs override command for only a resource', async () => {
describe('run build command', () => {
it('runs build command for only a resource', async () => {
const context_stub = {
amplify: {
getResourceStatus: jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('run build-override command', () => {
expect(context_stub_typed.amplify.invokePluginMethod).toBeCalledTimes(1);
});

it('runs override command for only all resources in a category', async () => {
it('runs build command for only all resources in a category', async () => {
const context_stub = {
amplify: {
getResourceStatus: jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('run build-override command', () => {
expect(context_stub_typed.amplify.invokePluginMethod).toBeCalledTimes(3);
});

it('runs override command successfully for all resources in all categories', async () => {
it('runs build command successfully for all resources in all categories', async () => {
const context_stub = {
amplify: {
getResourceStatus: jest.fn().mockResolvedValue({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { printer } from 'amplify-prompts';
/**
* Command to transform CFN with overrides
*/
const subcommand = 'build-override';
const subcommand = 'build';

export const run = async (context: $TSContext) => {
const categoryName = context?.input?.subCommands?.[0];
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-cli/src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { printer } from 'amplify-prompts';
import chalk from 'chalk';
import { getResourceOutputs } from '../extensions/amplify-helpers/get-resource-outputs';
import Ora from 'ora';
import { getResources } from './build-override';
import { getResources } from './build';
import * as _ from 'lodash';

export const run = async (context: $TSContext) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-e2e-core/src/categories/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export function overrideDDB(cwd: string, settings: {}) {
export function buildOverrideStorage(cwd: string, settings: {}) {
return new Promise((resolve, reject) => {
// Add 'storage' as a category param once implemented
const args = ['build-override'];
const args = ['build'];

spawn(getCLIPath(), args, { cwd, stripColors: true })
.sendEof()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AmplifyRootStackTemplate } from '@aws-amplify/cli-overrides-helper';

export function overrideProps(props: AmplifyRootStackTemplate) {
return props;
}
export function override(resources: AmplifyRootStackTemplate) {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "overrides-for-root-stack",
"name": "overrides-dependencies",
"version": "1.0.0",
"description": "",
"scripts": {
Expand All @@ -8,8 +8,6 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@types/fs-extra": "^9.0.11",
"fs-extra": "^9.1.0",
"@aws-amplify/cli-overrides-helper": "1.1.0-ext11.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ export class AmplifyRootStackTransform {
amplifyPrinter.formatter.list(['No override File Found', `To override ${this._resourceName} run amplify override auth`]);
return '';
});
const rootStackTemplateObj = this._rootTemplateObj as AmplifyRootStackTemplate;
const sandboxNode = new vm.NodeVM({
console: 'inherit',
timeout: 5000,
sandbox: {},
});
try {
this._rootTemplateObj = sandboxNode.run(overrideCode).overrideProps(rootStackTemplateObj);
sandboxNode.run(overrideCode).overrideProps(this._rootTemplateObj);
} catch (err: $TSAny) {
const error = new Error(`Skipping override due to ${err}${os.EOL}`);
printer.error(`${error}`);
Expand Down

0 comments on commit b3a79ef

Please sign in to comment.