Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

feat(aws-cloudfront, nextjs-component): support setting WAF web ACL id #724

Merged
merged 2 commits into from
Oct 30, 2020
Merged
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
60 changes: 31 additions & 29 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,71 @@ describe("General options propagation", () => {
})
);
});

it("create distribution with web ACL id and update it", async () => {
// Create
await component.default({
webACLId:
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/ExampleWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a",
origins
});

expect(mockCreateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
WebACLId:
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/ExampleWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a"
})
})
);

// Update
await component.default({
webACLId:
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/UpdatedWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a",
origins
});

expect(mockUpdateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
WebACLId:
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/UpdatedWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a"
})
})
);
});

it("create distribution with web ACL id and delete it", async () => {
// Create
await component.default({
webACLId:
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/ExampleWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a",
origins
});

expect(mockCreateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
WebACLId:
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/ExampleWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a"
})
})
);

// Delete
// Per AWS, providing an empty ACLId will remove the WAF association: https://docs.aws.amazon.com/waf/latest/APIReference/API_DisassociateWebACL.html
await component.default({
webACLId: "",
origins
});

expect(mockUpdateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
WebACLId: ""
})
})
);
});
});
10 changes: 10 additions & 0 deletions packages/serverless-components/aws-cloudfront/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ const createCloudFrontDistribution = async (cf, s3, inputs) => {
const CustomErrorResponses = getCustomErrorResponses(inputs.errorPages);
distributionConfig.CustomErrorResponses = CustomErrorResponses;

// Set WAF web ACL id if defined
if (inputs.webACLId !== undefined && inputs.webACLId !== null) {
distributionConfig.WebACLId = inputs.webACLId;
}

const res = await cf.createDistribution(params).promise();

return {
Expand Down Expand Up @@ -136,6 +141,11 @@ const updateCloudFrontDistribution = async (cf, s3, distributionId, inputs) => {
};
}

// When updating, don't override any existing webACLId if not set in inputs
if (inputs.webACLId !== undefined && inputs.webACLId !== null) {
params.DistributionConfig.WebACLId = inputs.webACLId;
}

let s3CanonicalUserId;
let originAccessIdentityId;

Expand Down
3 changes: 2 additions & 1 deletion packages/serverless-components/aws-cloudfront/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class CloudFront extends Component {
!equals(this.state.comment, inputs.comment) ||
!equals(this.state.aliases, inputs.aliases) ||
!equals(this.state.priceClass, inputs.priceClass) ||
!equals(this.state.errorPages, inputs.errorPages)
!equals(this.state.errorPages, inputs.errorPages) ||
!equals(this.state.webACLId, inputs.webACLId)
) {
this.context.debug(
`Updating CloudFront distribution of ID ${this.state.id}.`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1075,5 +1075,14 @@ describe("Custom inputs", () => {
}
});
});

it("sets web ACL id for AWS WAF", async () => {
await createNextComponent().default({
cloudfront: {
webACLId:
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/ExampleWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a"
}
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ class NextjsComponent extends Component {
errorPages: cloudFrontErrorPagesInputs,
distributionId: cloudFrontDistributionId = null,
comment: cloudFrontComment,
webACLId: cloudFrontWebACLId,
...cloudFrontOtherInputs
} = inputs.cloudfront || {};

Expand Down Expand Up @@ -587,7 +588,8 @@ class NextjsComponent extends Component {
...(cloudFrontErrorPagesInputs && {
errorPages: cloudFrontErrorPagesInputs
}),
comment: cloudFrontComment
comment: cloudFrontComment,
webACLId: cloudFrontWebACLId
});

let appUrl = cloudFrontOutputs.url;
Expand Down