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: migrate AWS SDK for JavaScript v2 APIs to v3 in static-website #632

Merged
merged 7 commits into from
Nov 5, 2023
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
8 changes: 4 additions & 4 deletions packages/pdk/.projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/pdk/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/static-website/.projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/static-website/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

215 changes: 91 additions & 124 deletions packages/static-website/src/webacl_event_handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
/*********************************************************************************************************************
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */

Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************************************************** */

import { WAFV2 } from 'aws-sdk'; // eslint-disable-line
import { Rule, WAFV2 } from "@aws-sdk/client-wafv2"; // eslint-disable-line

const DELIMITER = ":";
const SCOPE = "CLOUDFRONT";
Expand Down Expand Up @@ -78,8 +65,8 @@ const getWafRules = (
ipSetName: string,
managedRules?: any,
cidrAllowList?: any
): WAFV2.Rules => {
const rules: WAFV2.Rules = [];
): Array<Rule> => {
const rules: Array<Rule> = [];

if (cidrAllowList) {
rules.push({
Expand Down Expand Up @@ -132,33 +119,29 @@ const createWaf = async (
cidrAllowList?: any
) => {
const ipSetName = getIpSetName(id);
const createIpSetResponse = await client
.createIPSet({
Name: ipSetName,
Scope: SCOPE,
Addresses: cidrAllowList?.cidrRanges ?? [],
IPAddressVersion: cidrAllowList?.cidrType ?? "IPV4",
})
.promise();

const createWebAclResponse = await client
.createWebACL({
Name: id,
DefaultAction: { Allow: {} },
Scope: SCOPE,
VisibilityConfig: {
CloudWatchMetricsEnabled: true,
MetricName: id,
SampledRequestsEnabled: true,
},
Rules: getWafRules(
createIpSetResponse.Summary!.ARN!,
ipSetName,
managedRules,
cidrAllowList
),
})
.promise();
const createIpSetResponse = await client.createIPSet({
Name: ipSetName,
Scope: SCOPE,
Addresses: cidrAllowList?.cidrRanges ?? [],
IPAddressVersion: cidrAllowList?.cidrType ?? "IPV4",
});

const createWebAclResponse = await client.createWebACL({
Name: id,
DefaultAction: { Allow: {} },
Scope: SCOPE,
VisibilityConfig: {
CloudWatchMetricsEnabled: true,
MetricName: id,
SampledRequestsEnabled: true,
},
Rules: getWafRules(
createIpSetResponse.Summary!.ARN!,
ipSetName,
managedRules,
cidrAllowList
),
});

return {
PhysicalResourceId: `${createWebAclResponse.Summary?.Id}${DELIMITER}${createIpSetResponse.Summary?.Id}`,
Expand All @@ -179,52 +162,44 @@ const updateWaf = async (
managedRules?: any,
cidrAllowList?: any
) => {
const getIpSetResponse = await client
.getIPSet({
Id: ipSetId,
Name: ipSetName,
Scope: SCOPE,
})
.promise();

await client
.updateIPSet({
Id: ipSetId,
Name: ipSetName,
Addresses: cidrAllowList?.cidrRanges ?? [],
Scope: SCOPE,
LockToken: getIpSetResponse.LockToken!,
})
.promise();

const getWebAclResponse = await client
.getWebACL({
Id: webAclId,
Name: id,
Scope: SCOPE,
})
.promise();

await client
.updateWebACL({
Name: id,
DefaultAction: { Allow: {} },
Scope: SCOPE,
VisibilityConfig: {
CloudWatchMetricsEnabled: true,
MetricName: id,
SampledRequestsEnabled: true,
},
Rules: getWafRules(
getIpSetResponse.IPSet?.ARN!,
ipSetName,
managedRules,
cidrAllowList
),
Id: getWebAclResponse.WebACL?.Id!,
LockToken: getWebAclResponse.LockToken!,
})
.promise();
const getIpSetResponse = await client.getIPSet({
Id: ipSetId,
Name: ipSetName,
Scope: SCOPE,
});

await client.updateIPSet({
Id: ipSetId,
Name: ipSetName,
Addresses: cidrAllowList?.cidrRanges ?? [],
Scope: SCOPE,
LockToken: getIpSetResponse.LockToken!,
});

const getWebAclResponse = await client.getWebACL({
Id: webAclId,
Name: id,
Scope: SCOPE,
});

await client.updateWebACL({
Name: id,
DefaultAction: { Allow: {} },
Scope: SCOPE,
VisibilityConfig: {
CloudWatchMetricsEnabled: true,
MetricName: id,
SampledRequestsEnabled: true,
},
Rules: getWafRules(
getIpSetResponse.IPSet?.ARN!,
ipSetName,
managedRules,
cidrAllowList
),
Id: getWebAclResponse.WebACL?.Id!,
LockToken: getWebAclResponse.LockToken!,
});

return {
Data: {
Expand All @@ -242,39 +217,31 @@ const deleteWaf = async (
id: string,
ipSetName: string
) => {
const getWebAclResponse = await client
.getWebACL({
Id: webAclId,
Name: id,
Scope: SCOPE,
})
.promise();

await client
.deleteWebACL({
Id: webAclId,
Name: id,
Scope: SCOPE,
LockToken: getWebAclResponse.LockToken!,
})
.promise();

const getIpSetResponse = await client
.getIPSet({
Id: ipSetId,
Name: ipSetName,
Scope: SCOPE,
})
.promise();

await client
.deleteIPSet({
Id: ipSetId,
Name: ipSetName,
Scope: SCOPE,
LockToken: getIpSetResponse.LockToken!,
})
.promise();
const getWebAclResponse = await client.getWebACL({
Id: webAclId,
Name: id,
Scope: SCOPE,
});

await client.deleteWebACL({
Id: webAclId,
Name: id,
Scope: SCOPE,
LockToken: getWebAclResponse.LockToken!,
});

const getIpSetResponse = await client.getIPSet({
Id: ipSetId,
Name: ipSetName,
Scope: SCOPE,
});

await client.deleteIPSet({
Id: ipSetId,
Name: ipSetName,
Scope: SCOPE,
LockToken: getIpSetResponse.LockToken!,
});

return {
Data: {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/type-safe-api/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading