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

Merged
merged 12 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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:')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this validation also necessary in the place where the webacl props is being passed?

this.webAclId = props.webAclId;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mazyu36

Thank you very much. As you pointed out, validation was needed, so we have modified it!

(I have modified the policy to provide a validation method in the private method.)

4a02e21

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}`);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a Web ACL created using AWS WAF Classic is specified, this validation will not be performed because the ACL ID received is not arn.

this.webAclId = webAclId;
}

Expand Down
14 changes: 14 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,18 @@ 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 },
});

// ap-northeast-1リージョンのWebACL ARNを指定
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