Skip to content

Commit

Permalink
fix(s3-deployment): BucketDeployment doesn't validate that distributi…
Browse files Browse the repository at this point in the history
…on paths start with "/" (aws#15865)

Cloudfront invalidation paths must start with a leading "/". This pull requests adds a corresponding validation to fail during build instead of during the Cloudformation deployment.

closes aws#9317

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
SvenKirschbaum authored and hollanddd committed Aug 26, 2021
1 parent 8a8827a commit a78e1f2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/@aws-cdk/aws-s3-deployment/lib/bucket-deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,15 @@ export class BucketDeployment extends CoreConstruct {
constructor(scope: Construct, id: string, props: BucketDeploymentProps) {
super(scope, id);

if (props.distributionPaths && !props.distribution) {
throw new Error('Distribution must be specified if distribution paths are specified');
if (props.distributionPaths) {
if (!props.distribution) {
throw new Error('Distribution must be specified if distribution paths are specified');
}
if (!cdk.Token.isUnresolved(props.distributionPaths)) {
if (!props.distributionPaths.every(distributionPath => cdk.Token.isUnresolved(distributionPath) || distributionPath.startsWith('/'))) {
throw new Error('Distribution paths must start with "/"');
}
}
}

const handler = new lambda.SingletonFunction(this, 'CustomResourceHandler', {
Expand Down
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-s3-deployment/test/bucket-deployment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,30 @@ test('fails if distribution paths provided but not distribution ID', () => {

});

test('fails if distribution paths don\'t start with "/"', () => {
// GIVEN
const stack = new cdk.Stack();
const bucket = new s3.Bucket(stack, 'Dest');
const distribution = new cloudfront.CloudFrontWebDistribution(stack, 'Distribution', {
originConfigs: [
{
s3OriginSource: {
s3BucketSource: bucket,
},
behaviors: [{ isDefaultBehavior: true }],
},
],
});

// THEN
expect(() => new s3deploy.BucketDeployment(stack, 'Deploy', {
sources: [s3deploy.Source.asset(path.join(__dirname, 'my-website.zip'))],
destinationBucket: bucket,
distribution,
distributionPaths: ['images/*'],
})).toThrow(/Distribution paths must start with "\/"/);
});

testFutureBehavior('lambda execution role gets permissions to read from the source bucket and read/write in destination', s3GrantWriteCtx, cdk.App, (app) => {
// GIVEN
const stack = new cdk.Stack(app);
Expand Down

0 comments on commit a78e1f2

Please sign in to comment.