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

chore(cloudfront): prevent WebACL from being created in regions other than us-east-1 #32252

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,20 @@ export class Distribution extends Resource implements IDistribution {
/**
* Attach WAF WebACL to this CloudFront distribution
*
* 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');
}
if (webAclId.startsWith('arn:')) {
const webAclRegion = Stack.of(this).splitArn(webAclId, ArnFormat.SLASH_RESOURCE_NAME).region;
if (!Token.isUnresolved(webAclRegion) && webAclRegion !== 'us-east-1') {
throw new Error(`WebACL for CloudFront distributions must be created in the us-east-1 region; received ${webAclRegion}`);
}
}
this.webAclId = webAclId;
}

Expand Down
13 changes: 13 additions & 0 deletions packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1364,4 +1364,17 @@ describe('attachWebAclId', () => {
distribution.attachWebAclId('473e64fd-f30b-4765-81a0-62ad96dd167b');
}).toThrow(/A WebACL has already been attached to this distribution/);
});

test('throws if the WebAcl is not in us-east-1 region', () => {
const origin = defaultOrigin();

const distribution = new Distribution(stack, 'MyDist', {
defaultBehavior: { origin },
});

expect(() => {
distribution.attachWebAclId('arn:aws:wafv2:ap-northeast-1:123456789012:global/web-acl/MyWebAcl/473e64fd-f30b-4765-81a0-62ad96dd167a');
}).toThrow(/WebACL for CloudFront distributions must be created in the us-east-1 region; received ap-northeast-1/);
});

});
Loading