Skip to content

Commit

Permalink
feat(cloudfront): fix validation logic and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ren-yamanashi committed Nov 22, 2024
1 parent 11e77c4 commit 8458596
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
11 changes: 7 additions & 4 deletions packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,16 +606,19 @@ export class Distribution extends Resource implements IDistribution {
/**
* Attach WAF WebACL to this CloudFront distribution
*
* WebACL must be in us-east-1 region
* WebACL must be in the us-east-1 region.
*
* @param webAclId The WAF WebACL to associate with this distribution
*/
public attachWebAclId(webAclId: string) {
if (this.webAclId) {
throw new Error('A WebACL has already been attached to this distribution');
}
const arnParts = Stack.of(this).splitArn(webAclId, ArnFormat.SLASH_RESOURCE_NAME);
if (!Token.isUnresolved(arnParts.region) && arnParts.region !== 'us-east-1') {
throw new Error(`WebACL for CloudFront distributions must be created in the us-east-1 region; received ${arnParts.region}`);
if (webAclId.startsWith('arn:')) {
const arnParts = Stack.of(this).splitArn(webAclId, ArnFormat.SLASH_RESOURCE_NAME);
if (!Token.isUnresolved(arnParts.region) && arnParts.region !== 'us-east-1') {
throw new Error(`WebACL for CloudFront distributions must be created in the us-east-1 region; received ${arnParts.region}`);
}
}
this.webAclId = webAclId;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1343,11 +1343,11 @@ describe('attachWebAclId', () => {
defaultBehavior: { origin },
});

distribution.attachWebAclId('arn:aws:wafv2:us-east-1:123456789012:global/web-acl/MyWebAcl/473e64fd-f30b-4765-81a0-62ad96dd167a');
distribution.attachWebAclId('473e64fd-f30b-4765-81a0-62ad96dd167a');

Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Distribution', {
DistributionConfig: {
WebACLId: 'arn:aws:wafv2:us-east-1:123456789012:global/web-acl/MyWebAcl/473e64fd-f30b-4765-81a0-62ad96dd167a',
WebACLId: '473e64fd-f30b-4765-81a0-62ad96dd167a',
},
});
});
Expand All @@ -1357,11 +1357,11 @@ describe('attachWebAclId', () => {

const distribution = new Distribution(stack, 'MyDist', {
defaultBehavior: { origin },
webAclId: 'arn:aws:wafv2:us-east-1:123456789012:global/web-acl/MyWebAcl/473e64fd-f30b-4765-81a0-62ad96dd167a',
webAclId: '473e64fd-f30b-4765-81a0-62ad96dd167a',
});

expect(() => {
distribution.attachWebAclId('arn:aws:wafv2:us-east-1:123456789012:global/web-acl/MyWebAcl/473e64fd-f30b-4765-81a0-62ad96dd167b');
distribution.attachWebAclId('473e64fd-f30b-4765-81a0-62ad96dd167b');
}).toThrow(/A WebACL has already been attached to this distribution/);
});

Expand Down

0 comments on commit 8458596

Please sign in to comment.