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

fix(synthetics): asset code validation failed on bundled assets #26291

Merged
merged 3 commits into from
Jul 10, 2023
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
25 changes: 16 additions & 9 deletions packages/@aws-cdk/aws-synthetics-alpha/lib/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as s3 from 'aws-cdk-lib/aws-s3';
import * as s3_assets from 'aws-cdk-lib/aws-s3-assets';
import { Construct } from 'constructs';
import { RuntimeFamily } from './runtime';
import { Stage } from 'aws-cdk-lib/core';

/**
* The code the canary should execute
Expand Down Expand Up @@ -97,8 +98,6 @@ export class AssetCode extends Code {
}

public bind(scope: Construct, handler: string, family: RuntimeFamily): CodeConfig {
this.validateCanaryAsset(handler, family);

// If the same AssetCode is used multiple times, retain only the first instantiation.
if (!this.asset) {
this.asset = new s3_assets.Asset(scope, 'Code', {
Expand All @@ -107,6 +106,8 @@ export class AssetCode extends Code {
});
}

this.validateCanaryAsset(scope, handler, family);

return {
s3Location: {
bucketName: this.asset.s3BucketName,
Expand All @@ -124,21 +125,27 @@ export class AssetCode extends Code {
* Requires canary file to be directly inside node_modules folder.
* Requires canary file name matches the handler name.
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Canaries_WritingCanary.html
*
* @param handler the canary handler
*/
private validateCanaryAsset(handler: string, family: RuntimeFamily) {
if (path.extname(this.assetPath) !== '.zip') {
if (!fs.lstatSync(this.assetPath).isDirectory()) {
private validateCanaryAsset(scope: Construct, handler: string, family: RuntimeFamily) {
if (!this.asset) {
throw new Error("'validateCanaryAsset' must be called after 'this.asset' is instantiated");
}

// Get the staged (or copied) asset path.
const assetOutDir = Stage.of(scope)?.assetOutdir;
const assetPath = assetOutDir ? path.join(assetOutDir, this.asset.assetPath): this.assetPath;

if (path.extname(assetPath) !== '.zip') {
if (!fs.lstatSync(assetPath).isDirectory()) {
throw new Error(`Asset must be a .zip file or a directory (${this.assetPath})`);
}
const filename = handler.split('.')[0];
const nodeFilename = `${filename}.js`;
const pythonFilename = `${filename}.py`;
if (family === RuntimeFamily.NODEJS && !fs.existsSync(path.join(this.assetPath, 'nodejs', 'node_modules', nodeFilename))) {
if (family === RuntimeFamily.NODEJS && !fs.existsSync(path.join(assetPath, 'nodejs', 'node_modules', nodeFilename))) {
throw new Error(`The canary resource requires that the handler is present at "nodejs/node_modules/${nodeFilename}" but not found at ${this.assetPath} (https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Canaries_WritingCanary_Nodejs.html)`);
}
if (family === RuntimeFamily.PYTHON && !fs.existsSync(path.join(this.assetPath, 'python', pythonFilename))) {
if (family === RuntimeFamily.PYTHON && !fs.existsSync(path.join(assetPath, 'python', pythonFilename))) {
throw new Error(`The canary resource requires that the handler is present at "python/${pythonFilename}" but not found at ${this.assetPath} (https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Canaries_WritingCanary_Python.html)`);
}
}
Expand Down
52 changes: 51 additions & 1 deletion packages/@aws-cdk/aws-synthetics-alpha/test/code.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as fs from 'fs';
import * as path from 'path';
import { Template } from 'aws-cdk-lib/assertions';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { App, Stack } from 'aws-cdk-lib';
import { App, Stack, DockerImage } from 'aws-cdk-lib';
import * as cxapi from 'aws-cdk-lib/cx-api';
import * as synthetics from '../lib';
import { RuntimeFamily } from '../lib';
Expand Down Expand Up @@ -167,6 +168,55 @@ describe(synthetics.Code.fromAsset, () => {
expect(() => synthetics.Code.fromAsset(assetPath).bind(stack, 'incorrect.handler', synthetics.RuntimeFamily.NODEJS))
.toThrowError(`The canary resource requires that the handler is present at "nodejs/node_modules/incorrect.js" but not found at ${assetPath} (https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Canaries_WritingCanary_Nodejs.html)`);
});

test('passes if bundling is specified', () => {
// GIVEN
const stack = new Stack(new App(), 'canaries');

// WHEN
const assetPath = path.join(__dirname, 'canaries', 'nodejs', 'node_modules');
const code = synthetics.Code.fromAsset(assetPath, {
bundling: {
image: DockerImage.fromRegistry('dummy'),
local: {
tryBundle(outputDir) {
const stageDir = path.join(outputDir, 'nodejs', 'node_modules');
fs.mkdirSync(path.join(outputDir, 'nodejs'));
fs.mkdirSync(stageDir);
fs.copyFileSync(path.join(assetPath, 'canary.js'), path.join(stageDir, 'canary.js'));
return true;
},
},
},
});

// THEN
expect(() => code.bind(stack, 'canary.handler', synthetics.RuntimeFamily.NODEJS))
.not.toThrow();
});

test('fails if bundling is specified but folder structure is wrong', () => {
// GIVEN
const stack = new Stack(new App(), 'canaries');

// WHEN
const assetPath = path.join(__dirname, 'canaries', 'nodejs', 'node_modules');
const code = synthetics.Code.fromAsset(assetPath, {
bundling: {
image: DockerImage.fromRegistry('dummy'),
local: {
tryBundle(outputDir) {
fs.copyFileSync(path.join(assetPath, 'canary.js'), path.join(outputDir, 'canary.js'));
return true;
},
},
},
});

// THEN
expect(() => code.bind(stack, 'canary.handler', synthetics.RuntimeFamily.NODEJS))
.toThrowError(`The canary resource requires that the handler is present at "nodejs/node_modules/canary.js" but not found at ${assetPath} (https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Canaries_WritingCanary_Nodejs.html)`);
});
});

describe(synthetics.Code.fromBucket, () => {
Expand Down