Skip to content

Commit

Permalink
fix(core): custom resource providers cannot be used in CDK Pipelines (#…
Browse files Browse the repository at this point in the history
…11807)

The Custom Resource Provider was using an absolute path staging assets.

Needed to be a relative path.

Fixes #11760.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rix0rrr committed Dec 2, 2020
1 parent 6bf0da0 commit 48b3fa9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/@aws-cdk/core/lib/asset-staging.ts
Expand Up @@ -93,9 +93,27 @@ export class AssetStaging extends CoreConstruct {
* a temporary directory used for bundling.
*
* If asset staging is enabled it will be the staged path.
*
* IMPORTANT: If you are going to call `addFileAsset()`, use
* `relativeStagedPath()` instead.
*
* @deprecated - Use `absoluteStagedPath` instead.
*/
public readonly stagedPath: string;

/**
* Absolute path to the asset data.
*
* If asset staging is disabled, this will just be the source path or
* a temporary directory used for bundling.
*
* If asset staging is enabled it will be the staged path.
*
* IMPORTANT: If you are going to call `addFileAsset()`, use
* `relativeStagedPath()` instead.
*/
public readonly absoluteStagedPath: string;

/**
* The absolute path of the asset as it was referenced by the user.
*/
Expand Down Expand Up @@ -172,6 +190,7 @@ export class AssetStaging extends CoreConstruct {

const staged = AssetStaging.assetCache.obtain(this.cacheKey, stageThisAsset);
this.stagedPath = staged.stagedPath;
this.absoluteStagedPath = staged.stagedPath;
this.assetHash = staged.assetHash;
}

Expand Down
Expand Up @@ -139,7 +139,7 @@ export class CustomResourceProvider extends CoreConstruct {
});

const asset = stack.addFileAsset({
fileName: staging.stagedPath,
fileName: staging.relativeStagedPath(stack),
sourceHash: staging.sourceHash,
packaging: FileAssetPackaging.ZIP_DIRECTORY,
});
Expand Down
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { AssetStaging, CustomResourceProvider, CustomResourceProviderRuntime, Duration, Size, Stack } from '../../lib';
import { App, AssetStaging, CustomResourceProvider, CustomResourceProviderRuntime, DockerImageAssetLocation, DockerImageAssetSource, Duration, FileAssetLocation, FileAssetSource, ISynthesisSession, Size, Stack } from '../../lib';
import { toCloudFormation } from '../util';

const TEST_HANDLER = `${__dirname}/mock-provider`;
Expand Down Expand Up @@ -123,6 +123,43 @@ nodeunitShim({
test.done();
},

'custom resource provided creates asset in new-style synthesis with relative path'(test: Test) {
// GIVEN

let assetFilename : string | undefined;

const app = new App();
const stack = new Stack(app, 'Stack', {
synthesizer: {
bind(_stack: Stack): void { },

addFileAsset(asset: FileAssetSource): FileAssetLocation {
assetFilename = asset.fileName;
return { bucketName: '', httpUrl: '', objectKey: '', s3ObjectUrl: '', s3Url: '', kmsKeyArn: '' };
},

addDockerImageAsset(_asset: DockerImageAssetSource): DockerImageAssetLocation {
return { imageUri: '', repositoryName: '' };
},

synthesize(_session: ISynthesisSession): void { },
},
});

// WHEN
CustomResourceProvider.getOrCreate(stack, 'Custom:MyResourceType', {
codeDirectory: TEST_HANDLER,
runtime: CustomResourceProviderRuntime.NODEJS_12,
});

// THEN -- no exception
if (!assetFilename || assetFilename.startsWith(path.sep)) {
throw new Error(`Asset filename must be a relative path, got: ${assetFilename}`);
}

test.done();
},

'policyStatements can be used to add statements to the inline policy'(test: Test) {
// GIVEN
const stack = new Stack();
Expand Down

0 comments on commit 48b3fa9

Please sign in to comment.