Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue/234 - Add edge function for signing and Host header replacement #244

Merged
merged 11 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions configs/microapps-edge-to-origin/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
awsRegion: us-east-2
originRegion: us-east-2
signingMode: sign
replaceHostHeader: true
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = {
// "lcov",
// "clover"
// ],
coverageReporters: ['html', 'text'],
coverageReporters: ['html', 'text', 'clover'],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,
Expand All @@ -67,6 +67,8 @@ module.exports = {
globals: {
'ts-jest': {
tsconfig: 'tsconfig.packages.json',
// Reduces test time from 85 seconds to 20-25 seconds on a 2018 MacBook Pro 13" Core i7
isolatedModules: true
},
},

Expand Down Expand Up @@ -130,9 +132,7 @@ module.exports = {
// roots: [
// "<rootDir>"
// ],
roots: [
'<rootDir>packages/',
],
roots: ['<rootDir>packages/'],

// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",
Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
"format": "prettier --write \"packages/**/*.{js,ts}\"",
"start:publish": "node packages/microapps-publish/dist/src/index.js",
"start:publish:debug": "ts-node --inspect=5858 --debug-brk --ignore false packages/microapps-publish/src/index.ts",
"build": "tsc --build tsconfig.json",
"build": "yarn build:tsc && yarn esbuild:edge-to-origin",
"build:tsc": "tsc --build tsconfig.json",
"build:publish": "tsc --build tsconfig.publish.json",
"esbuild:deployer": "esbuild packages/microapps-deployer/src/index.ts --bundle --minify --sourcemap --platform=node --target=node14 --external:aws-sdk --outfile=packages/cdk/dist/microapps-deployer/index.js",
"esbuild:router": "esbuild packages/microapps-router/src/index.ts --bundle --minify --sourcemap --platform=node --target=node14 --external:aws-sdk --outfile=packages/cdk/dist/microapps-router/index.js",
"esbuild:edge-to-origin": "esbuild packages/microapps-edge-to-origin/src/index.ts --bundle --minify --sourcemap --platform=node --target=node14 --external:aws-sdk --outfile=packages/microapps-edge-to-origin/dist/index.js",
"build:deployer": "rollup --config rollup.deployer.js",
"build:router": "rollup --config rollup.router.js && cp packages/microapps-router/appFrame.html distb/microapps-router/",
"test": "AWS_PROFILE= AWS_EMF_ENVIRONMENT=Local jest",
Expand All @@ -45,7 +47,7 @@
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^10.0.0",
"@tsconfig/node14": "^1.0.0",
"@types/jest": "^26.0.0",
"@types/jest": "^27.0.0",
"@types/node": "^16.9.2",
"@typescript-eslint/eslint-plugin": "^4.9.0",
"@typescript-eslint/parser": "^4.9.0",
Expand All @@ -61,11 +63,11 @@
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^3.2.0",
"husky": "^4.2.5",
"jest": "^26.6.3",
"jest": "^27.0.0",
"jest-dynalite": "^3.4.4",
"lint-staged": "^10.2.11",
"patch-package": "^6.4.7",
"prettier": "^2.2.1",
"prettier": "^2.6.2",
"projen": "~0.56.19",
"replace-in-file": "^6.3.2",
"rimraf": "^3.0.2",
Expand All @@ -74,9 +76,9 @@
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.30.0",
"start-server-and-test": "^1.14.0",
"ts-jest": "^26.5.6",
"ts-node": "^9.1.1",
"typescript": "4.3.5"
"ts-jest": "^27.0.0",
"ts-node": "^10.7.0",
"typescript": "^4.6.0"
},
"husky": {
"hooks": {
Expand Down
9 changes: 9 additions & 0 deletions packages/cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ new MicroAppsStack(app, 'microapps-core', {
deployDemoApp: shared.deployDemoApp,
deployNextjsDemoApp: shared.deployNextjsDemoApp,
deployReleaseApp: shared.deployReleaseApp,
// We need to know the origin region for signing requests
// Accessing Aws.REGION will end up writing a Token into the config file
originRegion: shared.region,
});

new MicroAppsStack(app, 'microapps-basic', {
Expand All @@ -47,6 +50,9 @@ new MicroAppsStack(app, 'microapps-basic', {
deployDemoApp: shared.deployDemoApp,
deployNextjsDemoApp: shared.deployNextjsDemoApp,
deployReleaseApp: shared.deployReleaseApp,
// We need to know the origin region for signing requests
// Accessing Aws.REGION will end up writing a Token into the config file
originRegion: shared.region,
});

new MicroAppsStack(app, 'microapps-basic-prefix', {
Expand All @@ -58,6 +64,9 @@ new MicroAppsStack(app, 'microapps-basic-prefix', {
deployNextjsDemoApp: shared.deployNextjsDemoApp,
deployReleaseApp: shared.deployReleaseApp,
rootPathPrefix: 'prefix',
// We need to know the origin region for signing requests
// Accessing Aws.REGION will end up writing a Token into the config file
originRegion: shared.region,
});

// Note: This is only run manually once per env to create build user
Expand Down
11 changes: 11 additions & 0 deletions packages/cdk/lib/MicroApps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ export interface MicroAppsStackProps extends StackProps {
* @example dev/
*/
readonly rootPathPrefix?: string;

/**
* Origin region that API Gateway will be deployed to, used
* for the config.yml on the Edge function to sign requests for
* the correct region
*
* @default undefined
*/
readonly originRegion?: string;
}

export class MicroAppsStack extends Stack {
Expand Down Expand Up @@ -155,6 +164,7 @@ export class MicroAppsStack extends Stack {
certARNEdge,
certARNOrigin,
rootPathPrefix,
originRegion,
} = props;

let removalPolicy: RemovalPolicy | undefined = undefined;
Expand Down Expand Up @@ -228,6 +238,7 @@ export class MicroAppsStack extends Stack {
removalPolicy,
appEnv: nodeEnv,
rootPathPrefix,
originRegion,
...optionalAssetNameOpts,
...optionals3PolicyOpts,
...optionalCustomDomainOpts,
Expand Down
2 changes: 1 addition & 1 deletion packages/cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"constructs": "10.0.5",
"ts-node": "^9.0.0",
"tslib": "^2.1.0",
"typescript": "~4.0.0"
"typescript": "^4.6.0"
}
}
17 changes: 12 additions & 5 deletions packages/cdk/test/MicroApps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import { MicroAppsStack } from '../lib/MicroApps';
describe('MicroAppsStack', () => {
it('works with no params', () => {
const app = new App({});
const stackName = 'construct';
const stack = new MicroAppsStack(app, stackName, {});
const stackName = 'stack';
const stack = new MicroAppsStack(app, stackName, {
env: {
region: 'us-east-1',
},
});

expect(stack).toBeDefined();
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {});
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Function', 3);
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Function', 4);
Template.fromStack(stack).hasOutput('edgedomainname', {
Value: { 'Fn::GetAtt': ['microappscft5FDF8AB8', 'DomainName'] },
Export: { Name: `${stackName}-edge-domain-name` },
Expand All @@ -24,7 +28,10 @@ describe('MicroAppsStack', () => {
it('stable names with params', () => {
const app = new App({});
const stackName = 'microapps-stack-name';
const stack = new MicroAppsStack(app, 'construct', {
const stack = new MicroAppsStack(app, 'stack', {
env: {
region: 'us-east-1',
},
stackName,
domainNameEdge: 'appz.pwrdrvr.com',
domainNameOrigin: 'appz-origin.pwrdrvr.com',
Expand All @@ -38,7 +45,7 @@ describe('MicroAppsStack', () => {

expect(stack).toBeDefined();
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {});
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Function', 3);
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Function', 4);
Template.fromStack(stack).hasOutput('edgedomainname', {
Value: 'appz.pwrdrvr.com',
Export: { Name: `${stackName}-edge-domain-name` },
Expand Down
8 changes: 8 additions & 0 deletions packages/microapps-cdk/.projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions packages/microapps-cdk/.projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ const project = new awscdk.AwsCdkConstructLibrary({

// cdkTestDependencies: undefined, /* AWS CDK modules required for testing. */
// deps: [], /* Runtime dependencies of this module. */
deps: ['@aws-cdk/aws-apigatewayv2-alpha', '@aws-cdk/aws-apigatewayv2-integrations-alpha'],
deps: [
'@aws-cdk/aws-apigatewayv2-alpha',
'@aws-cdk/aws-apigatewayv2-authorizers-alpha',
'@aws-cdk/aws-apigatewayv2-integrations-alpha',
],

// description: undefined, /* The description is just a string that helps people understand the purpose of the package. */
// devDeps: [], /* Build dependencies for this module. */
Expand All @@ -49,7 +53,11 @@ const project = new awscdk.AwsCdkConstructLibrary({
// '@aws-cdk/[email protected]',
// 'patch-package@^6.4.7',
],
peerDeps: ['@aws-cdk/aws-apigatewayv2-alpha', '@aws-cdk/aws-apigatewayv2-integrations-alpha'],
peerDeps: [
'@aws-cdk/aws-apigatewayv2-alpha',
'@aws-cdk/aws-apigatewayv2-authorizers-alpha',
'@aws-cdk/aws-apigatewayv2-integrations-alpha',
],

// packageName: undefined, /* The "name" in package.json. */
// projectType: ProjectType.UNKNOWN, /* Which type of project this is (library/app). */
Expand Down
Loading