Tạo tích hợp Amazon API Gateway với AWS WAF
Mẫu này trong CDK cung cấp một ví dụ để tạo Cổng API Amazon với proxy tham lam (“{proxy +}”) và phương thức “BẤT KỲ” từ đường dẫn được chỉ định. Điều này có nghĩa là nó sẽ chấp nhận theo mặc định bất kỳ phương thức nào và bất kỳ đường dẫn nào. Hàm Lambda được cấu hình để truy cập VPC và chỉ trả về đường dẫn.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | import { CfnWebACL, CfnWebACLAssociation } from '@aws-cdk/aws-wafv2'; import * as cdk from "@aws-cdk/core"; export class WafStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string) { super(scope, id); // const CustomHeader = new cdk.CfnParameter(this, "CustomHeader", { // type: "String", // default: "x-key" // }); //Web ACL const APIGatewayWebACL = new CfnWebACL(this, "APIGatewayWebACL", { name: "demo-api-gateway-webacl", description: "This is WebACL for Auth APi Gateway", scope: "REGIONAL", defaultAction: { block: {} }, visibilityConfig: { metricName: "demo-APIWebACL", cloudWatchMetricsEnabled: true, sampledRequestsEnabled: true }, rules: [ { name: "demo-rateLimitRule", priority: 20, action: { block: {} }, visibilityConfig: { metricName: "demo-rateLimitRule", cloudWatchMetricsEnabled: true, sampledRequestsEnabled: false }, statement: { rateBasedStatement: { aggregateKeyType: "IP", limit: 100 } } }, { name: `demo-api-auth-gateway-geolocation-rule`, priority: 30, action: { allow: {} }, visibilityConfig: { metricName: `demo-AuthAPIGeoLocationUS`, cloudWatchMetricsEnabled: true, sampledRequestsEnabled: false }, statement: { geoMatchStatement: { countryCodes: ['US'] } } }, { name: `demo-api-auth-gateway-sqli-rule`, priority: 40, action: { block: {} }, visibilityConfig: { metricName: `demo-APIAuthGatewaySqliRule`, cloudWatchMetricsEnabled: true, sampledRequestsEnabled: false }, statement: { orStatement: { statements: [{ sqliMatchStatement: { fieldToMatch: { allQueryArguments: {} }, textTransformations: [{ priority: 1, type: "URL_DECODE" }, { priority: 2, type: "HTML_ENTITY_DECODE" }] } }, { sqliMatchStatement: { fieldToMatch: { body: {} }, textTransformations: [{ priority: 1, type: "URL_DECODE" }, { priority: 2, type: "HTML_ENTITY_DECODE" }] } }, { sqliMatchStatement: { fieldToMatch: { uriPath: {} }, textTransformations: [{ priority: 1, type: "URL_DECODE" }] } }] } } }, { name: `demo-detect-xss`, priority: 60, action: { block: {} }, visibilityConfig: { metricName: `demo-detect-xss`, cloudWatchMetricsEnabled: true, sampledRequestsEnabled: false }, statement: { orStatement: { statements: [ { xssMatchStatement: { fieldToMatch: { uriPath: {} }, textTransformations: [{ priority: 1, type: "URL_DECODE" }, { priority: 2, type: "HTML_ENTITY_DECODE" }] } }, { xssMatchStatement: { fieldToMatch: { allQueryArguments: {} }, textTransformations: [{ priority: 1, type: "URL_DECODE" }, { priority: 2, type: "HTML_ENTITY_DECODE" }] } }, ] } } } ] }); // Web ACL Association // const APIGatewayWebACLAssociation = new CfnWebACLAssociation(this, "APIGatewayWebACLAssociation", { webAclArn: APIGatewayWebACL.attrArn, resourceArn: cdk.Fn.join("", ["arn:aws:apigateway:us-east-1::/restapis/", cdk.Fn.importValue("demorestapiid"), "/stages/prod", ]) }); } } |
Tích hợp Amazon API Gateway với WAF
Mẫu này trong CDK cung cấp một ví dụ để tạo Cổng API Amazon với proxy tham lam (“{proxy +}”) và phương thức “BẤT KỲ” từ đường dẫn được chỉ định, có nghĩa là theo mặc định, nó sẽ chấp nhận bất kỳ phương thức nào và bất kỳ đường dẫn nào. Hàm VPC Lambda được cung cấp trong JavaScript chỉ trả về đường dẫn.
Tìm hiểu thêm về mô hình này tại Mô hình đất không máy chủ: https://serverlessland.com/patterns/apigw-waf-cdk
Quan trọng: ứng dụng này sử dụng các dịch vụ AWS khác nhau và có các chi phí liên quan đến các dịch vụ này sau khi sử dụng Bậc miễn phí – vui lòng xem trang Định giá AWS để biết chi tiết. Bạn chịu trách nhiệm cho mọi chi phí AWS phát sinh. Không có bảo hành nào được ngụ ý trong ví dụ này.
Yêu cầu
- Tạo tài khoản AWS nếu bạn chưa có và đăng nhập. Người dùng IAM mà bạn sử dụng phải có đủ quyền để thực hiện các cuộc gọi dịch vụ AWS cần thiết và quản lý tài nguyên AWS.
- AWS CLI đã được cài đặt và định cấu hình
- Git đã được cài đặt
- Đã cài đặt Node và NPM
- Bộ phát triển đám mây AWS (AWS CDK) đã được cài đặt
Hướng dẫn triển khai
- Tạo một thư mục mới, điều hướng đến thư mục đó trong một thiết bị đầu cuối và sao chép kho lưu trữ GitHub:12<span class="token function">git</span> clone https://github.com/aws-samples/serverless-patterns
- Thay đổi thư mục thành thư mục mẫu thư mục mã nguồn của nó:12<span class="token builtin class-name">cd</span> apig-waf/src
- Từ dòng lệnh, sử dụng npm để cài đặt các phụ thuộc phát triển:12<span class="token function">npm</span> <span class="token function">install</span>
- Để triển khai từ dòng lệnh, hãy sử dụng như sau:123npx cdk bootstrap aws://accountnumber/regionnpx cdk deploy --app <span class="token string">'ts-node .'</span> --all
Thử nghiệm
- Xác định vị trí WAFAPIGatewayApi.ApiUrl từ đầu ra được in bởi cdk, đây là điểm cuối api sẽ được gọi trong trình duyệt
1 2 3 4 | ```https://<api_id>.execute-api.<region>.amazonaws.com/prod``` You should see: ```Success path: "/"``` |
Dọn dẹp
- Từ dòng lệnh, sử dụng phần sau trong thư mục nguồn12npx cdk destroy --app <span class="token string">'ts-node .'</span> --all
- Xác nhận việc xóa và đợi quá trình xóa tài nguyên hoàn tất.