B站视频链接: https://www.bilibili.com/video/BV1LG411j7v4/?spm_id_from=333.999.0.0 微信公众号: 自刘地
上一篇文章讲过了AWS GWLB如何集成FortiGate防火墙,来对流量做集中检测。上一次实验是通过AWS 控制台操作的,这里分享一下实验环境的CloudFormation代码,帮助大家快速部署一下实验环境。
这里的CloudFormation代码在Tokyo区域部署的,如果要在其他Region部署,请修改FortiGate和Windows2022Base的AMI ID(参考我之前的文章《如何寻找EC2特定版本的AMI ID》)。
这次CloudFormation是全自动化代码,堆栈运行完成以后,可以直接测试现象,不需要再做任何额外的配置。默认防火墙使用6.4.10
的版本部署,如果要使用7.2.2
参考第四部分修改关于防火墙的代码。
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 AWSTemplateFormatVersion: "2010-09-09" Mappings: RegionMap: ap-northeast-1: FortiGate722: ami-08479d0bce02ca48b FortiGate6410: ami-0abf1a002258e8077 Windows2022Base: ami-06ac5e650e049a48f FirewallInstanceType: FortiGate722: InstanceType: c6i.xlarge FortiGate6410: InstanceType: c6i.xlarge Parameters: EC2InstanceAmiId: Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id> Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2' Environment: Type: String AllowedValues: - dev - prod Default: dev MyKeyPair: Description: Amazon EC2 Key Pair Type: AWS::EC2::KeyPair::KeyName Default: Global_Tokyo_KeyPair WebServerPort: Description: Apache Http Server Port Type: String Default: 8443 AllowedValues: - 8443 - 8888 - 8088 FortigateVersion: Description: Choice Fortigate Firewall Version Type Type: String Default: FortiGate722 AllowedValues: - FortiGate722 - FortiGate6410 Resources: SecVpc: Type: AWS::EC2::VPC Properties: CidrBlock: 10.20 .0 .0 /16 EnableDnsSupport: 'true' EnableDnsHostnames: 'true' Tags: - Key: Name Value: SecVpc SecVpcIGW: Type: "AWS::EC2::InternetGateway" Properties: Tags: - Key: Name Value: SecVpcIGW SecVpcAttachIgw: Type: "AWS::EC2::VPCGatewayAttachment" Properties: VpcId: !Ref SecVpc InternetGatewayId: !Ref SecVpcIGW Az1GwlbSubnet: Type: AWS::EC2::Subnet Properties: VpcId: !Ref SecVpc CidrBlock: 10.20 .10 .0 /24 AvailabilityZone: Fn::Select: - 0 - Fn::GetAZs: "" Tags: - Key: Name Value: SecVpc-GWLB-AZ1-GWLB-Subnet Az2GwlbSubnet: Type: AWS::EC2::Subnet Properties: VpcId: !Ref SecVpc CidrBlock: 10.20 .30 .0 /24 AvailabilityZone: Fn::Select: - 1 - Fn::GetAZs: "" Tags: - Key: Name Value: SecVpc-GWLB-AZ2-GWLB-Subnet Az1MgtSubnet: Type: AWS::EC2::Subnet Properties: VpcId: !Ref SecVpc CidrBlock: 10.20 .20 .0 /24 AvailabilityZone: Fn::Select: - 0 - Fn::GetAZs: "" Tags: - Key: Name Value: SecVpc-GWLB-AZ1-MGT-Subnet Az2MgtSubnet: Type: AWS::EC2::Subnet Properties: VpcId: !Ref SecVpc CidrBlock: 10.20 .40 .0 /24 AvailabilityZone: Fn::Select: - 1 - Fn::GetAZs: "" Tags: - Key: Name Value: SecVpc-GWLB-AZ2-MGT-Subnet MgtRouteTable: Type: "AWS::EC2::RouteTable" Properties: VpcId: !Ref SecVpc Tags: - Key: Name Value: SecVpc-Mgt-route-table Az1MgtSubnetAssociation: Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref MgtRouteTable SubnetId: !Ref Az1MgtSubnet Az2MgtSubnetAssociation: Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref MgtRouteTable SubnetId: !Ref Az2MgtSubnet GwlbRouteTable: Type: "AWS::EC2::RouteTable" Properties: VpcId: !Ref SecVpc Tags: - Key: Name Value: SecVpc-Gwlb-route-table Az1GwlbSubnetAssociation: Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref GwlbRouteTable SubnetId: !Ref Az1GwlbSubnet Az2GwlbSubnetAssociation: Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref GwlbRouteTable SubnetId: !Ref Az2GwlbSubnet MgtToInternetRoute: Type: "AWS::EC2::Route" DependsOn: SecVpcIGW Properties: RouteTableId: !Ref MgtRouteTable DestinationCidrBlock: 0.0 .0 .0 /0 GatewayId: !Ref SecVpcIGW SecVpcSg: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: SG to test ping VpcId: !Ref SecVpc SecurityGroupIngress: - IpProtocol: tcp FromPort: 22 ToPort: 22 CidrIp: 0.0 .0 .0 /0 - IpProtocol: icmp FromPort: -1 ToPort: -1 CidrIp: 0.0 .0 .0 /0 - IpProtocol: tcp FromPort: 443 ToPort: 443 CidrIp: 0.0 .0 .0 /0 - IpProtocol: tcp FromPort: 8443 ToPort: 8443 CidrIp: 0.0 .0 .0 /0 - IpProtocol: -1 FromPort: -1 ToPort: -1 CidrIp: 10.20 .0 .0 /16 - IpProtocol: -1 FromPort: -1 ToPort: -1 CidrIp: 10.10 .0 .0 /16 - IpProtocol: tcp FromPort: 3389 ToPort: 3389 CidrIp: 0.0 .0 .0 /0 Tags: - Key: Name Value: SecVpcSg Fortigate1MgmtEip: Type: "AWS::EC2::EIP" Properties: Tags: - Key: Name Value: SecVpc-fortigate1-mgmt-eip Fortigate1MgmtEni: Type: "AWS::EC2::NetworkInterface" Properties: SourceDestCheck: false PrivateIpAddress: 10.20 .20 .100 GroupSet: - Ref: "SecVpcSg" SubnetId: Ref: "Az1MgtSubnet" Tags: - Key: Name Value: SecVpc-fortigate1-mgmt-eni Fortigate1MgmtEniAssociation: Type: AWS::EC2::EIPAssociation Properties: AllocationId: !GetAtt Fortigate1MgmtEip.AllocationId NetworkInterfaceId: !Ref Fortigate1MgmtEni Fortigate1DataEni: Type: "AWS::EC2::NetworkInterface" Properties: SourceDestCheck: false PrivateIpAddress: 10.20 .10 .100 GroupSet: - Ref: "SecVpcSg" SubnetId: Ref: "Az1GwlbSubnet" Tags: - Key: Name Value: SecVpc-fortigate1-data-eni Fortigate2MgmtEip: Type: "AWS::EC2::EIP" Properties: Tags: - Key: Name Value: SecVpc-fortigate2-mgmt-eip Fortigate2MgmtEni: Type: "AWS::EC2::NetworkInterface" Properties: SourceDestCheck: false PrivateIpAddress: 10.20 .40 .100 GroupSet: - Ref: "SecVpcSg" SubnetId: Ref: "Az2MgtSubnet" Tags: - Key: Name Value: SecVpc-fortigate2-mgmt-eni Fortigate2MgmtEniAssociation: Type: AWS::EC2::EIPAssociation Properties: AllocationId: !GetAtt Fortigate2MgmtEip.AllocationId NetworkInterfaceId: !Ref Fortigate2MgmtEni Fortigate2DataEni: Type: "AWS::EC2::NetworkInterface" Properties: SourceDestCheck: false PrivateIpAddress: 10.20 .30 .100 GroupSet: - Ref: "SecVpcSg" SubnetId: Ref: "Az2GwlbSubnet" Tags: - Key: Name Value: SecVpc-fortigate2-data-eni Fortigate1: Type: AWS::EC2::Instance Properties: ImageId: !FindInMap [RegionMap , !Ref "AWS::Region" , !Ref FortigateVersion ] KeyName: !Ref MyKeyPair InstanceType: !FindInMap [FirewallInstanceType , !Ref "FortigateVersion" , InstanceType ] NetworkInterfaces: - NetworkInterfaceId: !Ref Fortigate1DataEni DeviceIndex: 0 - NetworkInterfaceId: !Ref Fortigate1MgmtEni DeviceIndex: 1 UserData: Fn::Base64: Fn::Sub: - | config system admin edit "labuser" set accprofile "super_admin" set vdom "root" set password FSr1Lliu1qiang2long3DemoZJG5 next end end config system interface edit "port1" set defaultgw disable set allowaccess ping https ssh fgfm probe-response next edit "port2" set mode dhcp set allowaccess ping https ssh set defaultgw enable next end config system global set vdom-mode split-vdom end config global config system interface edit "port1" set vdom "FG-traffic" end end config vdom edit FG-traffic config system geneve edit "geneve1" set interface "port1" set type ppp set remote-ip ${Az1GwlbIp} next edit "geneve2" set interface "port1" set type ppp set remote-ip ${Az2GwlbIp} next end config firewall policy edit 1 set name "1" set srcintf "geneve1" set dstintf "geneve1" set srcaddr "all" set dstaddr "all" set action accept set schedule "always" set service "ALL" set logtraffic all set logtraffic-start enable next edit 2 set name "2" set srcintf "geneve2" set dstintf "geneve2" set srcaddr "all" set dstaddr "all" set action accept set schedule "always" set service "ALL" set logtraffic all set logtraffic-start enable next end config log setting set local-in-allow enable set local-in-deny-unicast enable set local-in-deny-broadcast enable set local-out enable next end config router static edit 1 set device "geneve1" next edit 2 set device "geneve2" next edit 3 set dst 10.20 .0 .0 255.255 .0 .0 set gateway 10.20 .10 .1 set device "port1" next end config router policy edit 1 set input-device "geneve1" set gateway ${Az1GwlbIp} set output-device "geneve1" next edit 2 set input-device "geneve2" set gateway ${Az2GwlbIp} set output-device "geneve2" next end - Az1GwlbIp: !GetAtt 'CustomGwlbIpResource.Az1GwlbIp' Az2GwlbIp: !GetAtt 'CustomGwlbIpResource.Az2GwlbIp' Tags: - Key: Name Value: Fortigate-FW1 Fortigate2: Type: AWS::EC2::Instance DependsOn: CustomGwlbIpResource Properties: ImageId: !FindInMap [RegionMap , !Ref "AWS::Region" , !Ref FortigateVersion ] KeyName: !Ref MyKeyPair InstanceType: !FindInMap [FirewallInstanceType , !Ref "FortigateVersion" , InstanceType ] NetworkInterfaces: - NetworkInterfaceId: !Ref Fortigate2DataEni DeviceIndex: 0 - NetworkInterfaceId: !Ref Fortigate2MgmtEni DeviceIndex: 1 UserData: Fn::Base64: Fn::Sub: - | config system admin edit "labuser" set accprofile "super_admin" set vdom "root" set password FSr1Lliu1qiang2long3DemoZJG5 next end end config system interface edit "port1" set defaultgw disable set allowaccess ping https ssh fgfm probe-response next edit "port2" set mode dhcp set allowaccess ping https ssh set defaultgw enable next end config system global set vdom-mode split-vdom end config global config system interface edit "port1" set vdom "FG-traffic" end end config vdom edit FG-traffic config system geneve edit "geneve1" set interface "port1" set type ppp set remote-ip ${Az2GwlbIp} next edit "geneve2" set interface "port1" set type ppp set remote-ip ${Az1GwlbIp} next end config firewall policy edit 1 set name "1" set srcintf "geneve1" set dstintf "geneve1" set srcaddr "all" set dstaddr "all" set action accept set schedule "always" set service "ALL" set logtraffic all set logtraffic-start enable next edit 2 set name "2" set srcintf "geneve2" set dstintf "geneve2" set srcaddr "all" set dstaddr "all" set action accept set schedule "always" set service "ALL" set logtraffic all set logtraffic-start enable next end config log setting set local-in-allow enable set local-in-deny-unicast enable set local-in-deny-broadcast enable set local-out enable next end config router static edit 1 set device "geneve1" next edit 2 set device "geneve2" next edit 3 set dst 10.20 .0 .0 255.255 .0 .0 set gateway 10.20 .30 .1 set device "port1" next end config router policy edit 1 set input-device "geneve1" set gateway ${Az2GwlbIp} set output-device "geneve1" next edit 2 set input-device "geneve2" set gateway ${Az1GwlbIp} set output-device "geneve2" next end - Az1GwlbIp: !GetAtt 'CustomGwlbIpResource.Az1GwlbIp' Az2GwlbIp: !GetAtt 'CustomGwlbIpResource.Az2GwlbIp' Tags: - Key: Name Value: Fortigate-FW2 Gwlb: Type: AWS::ElasticLoadBalancingV2::LoadBalancer Properties: LoadBalancerAttributes: - Key: load_balancing.cross_zone.enabled Value: true Name: Fortigate-GWLB Type: gateway Subnets: - !Ref Az1GwlbSubnet - !Ref Az2GwlbSubnet Tags: - Key: Name Value: SecVpc-Fortigate-gwlb TargetGroup: Type: AWS::ElasticLoadBalancingV2::TargetGroup Properties: HealthCheckIntervalSeconds: 5 Name: Fortigate-Target Port: 6081 Protocol: GENEVE TargetGroupAttributes: - Key: deregistration_delay.timeout_seconds Value: 20 VpcId: !Ref SecVpc HealthCheckPort: 443 HealthCheckProtocol: HTTPS TargetType: instance Targets: - Id: !Ref Fortigate1 - Id: !Ref Fortigate2 Tags: - Key: Name Value: !Sub "${AWS::StackName}-gwlbtg" Listener: Type: AWS::ElasticLoadBalancingV2::Listener Properties: DefaultActions: - Type: forward TargetGroupArn: !Ref TargetGroup LoadBalancerArn: !Ref Gwlb VpcEndpointService: Type: AWS::EC2::VPCEndpointService Properties: GatewayLoadBalancerArns: - !Ref Gwlb AcceptanceRequired: false VpceServiceLambdaExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: - lambda.amazonaws.com Action: - sts:AssumeRole Path: / Policies: - PolicyName: root PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - logs:CreateLogStream - logs:PutLogEvents Resource: arn:aws:logs:*:*:* - Effect: Allow Action: - ec2:DescribeVpcEndpointServiceConfigurations - ec2:DescribeVpcEndpointServicePermissions - ec2:DescribeVpcEndpointServices Resource: "*" VpceServiceLogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub /aws/lambda/${AWS::StackName}-service RetentionInDays: 1 VpceServiceName: Type: AWS::Lambda::Function DependsOn: VpceServiceLogGroup Properties: FunctionName: !Sub ${AWS::StackName}-service Handler: "index.handler" Role: !GetAtt VpceServiceLambdaExecutionRole.Arn Code: ZipFile: | import json import logging import time import boto3 import cfnresponse from botocore.exceptions import ClientError try: ec2 = boto3.client('ec2') except ClientError as e: logger.error(f"ERROR: failed to connect to EC2 client: {e }") sys.exit(1) def handler(event, context): logger = logging.getLogger() logger.setLevel(logging.INFO) logger.info('Received event: {}'.format(json.dumps(event))) responseData = {} responseStatus = cfnresponse.FAILED try: serviceid = event[" ResourceProperties"]["VpceServiceId"] except Exception as e: logger.info('Attribute retrival failure: {}'.format(e)) try: if event["RequestType"] == "Delete": responseStatus = cfnresponse.SUCCESS cfnresponse.send(event, context, responseStatus, responseData) except Exception: logger.exception("Signaling failure to CloudFormation.") cfnresponse.send(event, context, cfnresponse.FAILED, {}) if event["RequestType"] == "Create": logger.info("Retrieving VPC Endpoint Service Name:") try: response = ec2.describe_vpc_endpoint_service_configurations( Filters=[ { ' Name': 'service-id' , 'Values': [serviceid ] } ] ) except Exception as e: logger.info('ec2.describe_vpc_endpoint_service_configurations failure: {}'.format(e)) service_name = response[' ServiceConfigurations'][0]['ServiceName'] time.sleep(120) responseData['ServiceName'] = service_name responseStatus = cfnresponse.SUCCESS cfnresponse.send(event, context, responseStatus, responseData) Runtime: python3.7 Timeout: 150 RetrieveVpceServiceName: Type: Custom::RetrieveAttributes Properties: ServiceToken: !GetAtt VpceServiceName.Arn VpceServiceId: !Ref VpcEndpointService App1Endpoint: Type: AWS::EC2::VPCEndpoint Properties: VpcId: !Ref AppVpc ServiceName: !GetAtt RetrieveVpceServiceName.ServiceName VpcEndpointType: GatewayLoadBalancer SubnetIds: - !Ref Gwlbe1Subnet App2Endpoint: Type: AWS::EC2::VPCEndpoint Properties: VpcId: !Ref AppVpc ServiceName: !GetAtt RetrieveVpceServiceName.ServiceName VpcEndpointType: GatewayLoadBalancer SubnetIds: - !Ref Gwlbe2Subnet AppVpc: Type: AWS::EC2::VPC Properties: CidrBlock: 10.10 .0 .0 /16 EnableDnsSupport: 'true' EnableDnsHostnames: 'true' Tags: - Key: Name Value: AppVpc AppVpcIGW: Type: "AWS::EC2::InternetGateway" Properties: Tags: - Key: Name Value: AppVpc-IGW AppVpcAttachIgw: Type: "AWS::EC2::VPCGatewayAttachment" Properties: VpcId: !Ref AppVpc InternetGatewayId: !Ref AppVpcIGW Gwlbe1Subnet: Type: AWS::EC2::Subnet Properties: VpcId: !Ref AppVpc CidrBlock: 10.10 .10 .0 /24 AvailabilityZone: Fn::Select: - 0 - Fn::GetAZs: "" Tags: - Key: Name Value: AppVpc-GWLBe1-Subnet App1Subnet: Type: AWS::EC2::Subnet Properties: VpcId: !Ref AppVpc CidrBlock: 10.10 .20 .0 /24 AvailabilityZone: Fn::Select: - 0 - Fn::GetAZs: "" Tags: - Key: Name Value: AppVpc-App1-Subnet Gwlbe2Subnet: Type: AWS::EC2::Subnet Properties: VpcId: !Ref AppVpc CidrBlock: 10.10 .30 .0 /24 AvailabilityZone: Fn::Select: - 1 - Fn::GetAZs: "" Tags: - Key: Name Value: AppVpc-GWLBe2-Subnet App2Subnet: Type: AWS::EC2::Subnet Properties: VpcId: !Ref AppVpc CidrBlock: 10.10 .40 .0 /24 AvailabilityZone: Fn::Select: - 1 - Fn::GetAZs: "" Tags: - Key: Name Value: AppVpc-App2-Subnet IgwIngressRouteTable: Type: "AWS::EC2::RouteTable" Properties: VpcId: !Ref AppVpc Tags: - Key: Name Value: AppVpc-Igw-Ingress-route-table IgwIngressAssociation: Type: "AWS::EC2::GatewayRouteTableAssociation" Properties: RouteTableId: !Ref IgwIngressRouteTable GatewayId: !Ref AppVpcIGW IgwIngressToApp1: Type: "AWS::EC2::Route" DependsOn: App1Endpoint Properties: RouteTableId: !Ref IgwIngressRouteTable DestinationCidrBlock: 10.10 .20 .0 /24 VpcEndpointId: !Ref App1Endpoint IgwIngressToApp2: Type: "AWS::EC2::Route" DependsOn: App2Endpoint Properties: RouteTableId: !Ref IgwIngressRouteTable DestinationCidrBlock: 10.10 .40 .0 /24 VpcEndpointId: !Ref App2Endpoint GwlbeRouteTable: Type: "AWS::EC2::RouteTable" Properties: VpcId: !Ref AppVpc Tags: - Key: Name Value: AppVpc-Gwlbe-route-table Gwlbe1RouteTableAssociation: Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref GwlbeRouteTable SubnetId: !Ref Gwlbe1Subnet Gwlbe2RouteTableAssociation: Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref GwlbeRouteTable SubnetId: !Ref Gwlbe2Subnet Gwlbe1ToInternetRoute: Type: "AWS::EC2::Route" DependsOn: AppVpcIGW Properties: RouteTableId: !Ref GwlbeRouteTable DestinationCidrBlock: 0.0 .0 .0 /0 GatewayId: !Ref AppVpcIGW App1RouteTable: Type: "AWS::EC2::RouteTable" Properties: VpcId: !Ref AppVpc Tags: - Key: Name Value: AppVpc-App1-route-table App2RouteTable: Type: "AWS::EC2::RouteTable" Properties: VpcId: !Ref AppVpc Tags: - Key: Name Value: AppVpc-App2-route-table App1RouteTableAssociation: Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref App1RouteTable SubnetId: !Ref App1Subnet App2RouteTableAssociation: Type: "AWS::EC2::SubnetRouteTableAssociation" Properties: RouteTableId: !Ref App2RouteTable SubnetId: !Ref App2Subnet App1ToEndpoint: Type: "AWS::EC2::Route" DependsOn: App1Endpoint Properties: RouteTableId: !Ref App1RouteTable DestinationCidrBlock: 0.0 .0 .0 /0 VpcEndpointId: !Ref App1Endpoint App2ToEndpoint: Type: "AWS::EC2::Route" DependsOn: App2Endpoint Properties: RouteTableId: !Ref App2RouteTable DestinationCidrBlock: 0.0 .0 .0 /0 VpcEndpointId: !Ref App2Endpoint AppVpcSg: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: SG to test ping VpcId: !Ref AppVpc SecurityGroupIngress: - IpProtocol: tcp FromPort: 22 ToPort: 22 CidrIp: 0.0 .0 .0 /0 - IpProtocol: icmp FromPort: -1 ToPort: -1 CidrIp: 0.0 .0 .0 /0 - IpProtocol: tcp FromPort: 8443 ToPort: 8443 CidrIp: 0.0 .0 .0 /0 - IpProtocol: tcp FromPort: 443 ToPort: 443 CidrIp: 0.0 .0 .0 /0 - IpProtocol: -1 FromPort: -1 ToPort: -1 CidrIp: 10.20 .0 .0 /16 - IpProtocol: -1 FromPort: -1 ToPort: -1 CidrIp: 10.10 .0 .0 /16 - IpProtocol: tcp FromPort: 3389 ToPort: 3389 CidrIp: 0.0 .0 .0 /0 Tags: - Key: Name Value: AppVpcSg App1Linux: Type: AWS::EC2::Instance DependsOn: [App1ToEndpoint , App2ToEndpoint ] Properties: ImageId: !Ref EC2InstanceAmiId KeyName: !Ref MyKeyPair InstanceType: t2.micro NetworkInterfaces: - AssociatePublicIpAddress: true DeviceIndex: 0 GroupSet: - Ref: AppVpcSg SubnetId: !Ref App1Subnet UserData: Fn::Base64: !Sub | yum update -y yum install -y httpd sed -i.bak 's/Listen 80/Listen 8443/g' /etc/httpd/conf/httpd.conf echo "<h2>Hello World from $(hostname -f)</h2>" > /var/www/html/index.html systemctl start httpd.service systemctl enable httpd.service Tags: - Key: Name Value: App1-Linux App2Windows: Type: AWS::EC2::Instance Properties: ImageId: !FindInMap [RegionMap , !Ref "AWS::Region" , Windows2022Base ] KeyName: !Ref MyKeyPair InstanceType: t2.xlarge NetworkInterfaces: - AssociatePublicIpAddress: true DeviceIndex: 0 GroupSet: - Ref: AppVpcSg SubnetId: !Ref App2Subnet Tags: - Key: Name Value: App2-Windows UserData: Fn::Base64: !Sub | <powershell> $PASSWORD = convertto-securestring "FSr1Lliu1qiang2long3DemoZJG5" -asplaintext -force New-LocalUser -Name "labuser" -Description "rdp user" -Password $Password Add-LocalGroupMember -Group 'Administrators' -Member 'labuser' net localgroup "Remote Desktop Users" /add labuser $file = "C:\" + (Get-Date).ToString(" MM-dd-yy-hh-mm") New-Item $file -ItemType file </powershell> GwlbIpLambdaExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: - lambda.amazonaws.com Action: - sts:AssumeRole Path: / Policies: - PolicyName: GwlbIpAccess PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - logs:CreateLogStream - logs:PutLogEvents Resource: arn:aws:logs:*:*:* - Effect: Allow Action: - ec2:DescribeNetworkInterfaces Resource: "*" CustomGwlbIpResource: DependsOn: GwlbIpAddressFunction Type: Custom::GwlbIp Properties: ServiceToken: !GetAtt 'GwlbIpAddressFunction.Arn' GwlbIpAddressFunction: Type: AWS::Lambda::Function DependsOn: Gwlb Properties: FunctionName: GwlbIpAddressFunction Handler: "index.handler" Role: !GetAtt GwlbIpLambdaExecutionRole.Arn Code: ZipFile: | import json import logging import time import boto3 import cfnresponse from botocore.exceptions import ClientError try: ec2 = boto3.client('ec2') except ClientError as e: logger.error(f"ERROR: failed to connect to EC2 client: {e }") sys.exit(1) def handler(event, context): logger = logging.getLogger() logger.setLevel(logging.INFO) logger.info('Received event: {}'.format(json.dumps(event))) responseData = {} responseStatus = cfnresponse.FAILED try: serviceid = event[" ResourceProperties"]["VpceServiceId"] except Exception as e: logger.info('Attribute retrival failure: {}'.format(e)) try: if event["RequestType"] == "Delete": responseStatus = cfnresponse.SUCCESS cfnresponse.send(event, context, responseStatus, responseData) except Exception: logger.exception("Signaling failure to CloudFormation.") cfnresponse.send(event, context, cfnresponse.FAILED, {}) if event["RequestType"] == "Create": logger.info("Retrieving VPC Endpoint Service Name:") try: response = ec2.describe_network_interfaces( Filters=[ { ' Name': 'interface-type' , 'Values': [ 'gateway_load_balancer' , ] }, ] ) for x in response['NetworkInterfaces']: if x['AvailabilityZone'] == 'ap-northeast-1a': az1_gwlb_ip = x['PrivateIpAddress'] if x['AvailabilityZone'] == 'ap-northeast-1c': az2_gwlb_ip = x['PrivateIpAddress'] except Exception as e: logger.info('ec2.describe_network_interfaces failure: {}'.format(e)) time.sleep(120) responseData = {} responseData[' Az1GwlbIp'] = az1_gwlb_ip responseData['Az2GwlbIp'] = az2_gwlb_ip responseStatus = cfnresponse.SUCCESS cfnresponse.send(event, context, responseStatus, responseData) Runtime: python3.7 Timeout: 150 Outputs: Az1GwlbIp: Description: Firewall AZ1 GWLB IP Address Value: !GetAtt 'CustomGwlbIpResource.Az1GwlbIp' Az2GwlbIp: Description: Firewall AZ2 GWLB IP Address Value: !GetAtt 'CustomGwlbIpResource.Az2GwlbIp'
堆栈创建完成之后,会输出GWLB的弹性接口IP地址。如果手动配置Fortigate防火墙,配置geneve时需要这个信息。在CloudFormation里面,已经通过脚本自动化把这个地址抓取并且配置了。
二、查看FortiGate防火墙配置 FortiGate防火墙的配置在CloudFormation代码里面已经配置完成了,这里通过图形化界面来看一下配置结果。
首次登陆FortiGate防火墙,默认的用户名是admin
,默认的密码是实例id。
我在CloudFormation代码里面,为防火墙新建了一个测试用户,用户名为labuser
,密码为FSr1Lliu1qiang2long3DemoZJG5
。
这里可以查看实例ID信息。
2.1 FortiGate FW1 配置 FortiGate-FW1启用了Split-VDOM,有两个VDOM,root用于防火墙管理,FG-traffic用于转发数据流量。
port1是主接口,用于转发数据流量。port2作为管理接口,用于管理流量。
静态路由配置,默认路由从geneve1和geneve2接口出去,配置本VPC CIDR网段从port1数据接口出去,下一跳指向子网网关。
因为配置了两条默认路由,有可能造成流量来回路径不一致的问题。所以通过配置策略路由,让来自geneve1的流量从genven1接口出去,来自geneve2的流量从genven2接口出去。
测试环境下,放行所有流量。
2.2 FortiGate FW2 配置 FortiGate-FW2启用了Split-Task VDOM,有两个VDOM,root和FG-traffic。
port1是主接口,用于转发数据流量。port2作为管理接口,用于管理流量。
静态路由配置,默认路由从geneve1和geneve2接口出去,配置本VPC CIDR网段从port1数据接口出去,下一跳指向子网网关。
配置策略路由,因为配置了两条默认路由,有可能造成流量来回路径不一致的问题。所以通过配置策略路由,让来自geneve1的流量从genven1接口出去,来自geneve2的流量从genven2接口出去。
测试环境下,放行所有流量。
三、现象测试 3.1 访问APP测试 连接上APP2 Window RDP桌面。可以使用我在代码里面新建的用户名labuser
,密码FSr1Lliu1qiang2long3DemoZJG5
进行连接。
查看防火墙连接日志信息。
3.2 防火墙抓包 FortiGate防火墙可以通过Debug packet flow [1]来查看防火墙对数据包执行的动作,这个抓包方法一般用于排错,输出的信息比较多。也可以通过自带的sniffer工具 [2]单纯的抓包,输出的信息比较简洁。
Debug packet flow
例如:这里首先暂停了之前的抓包进程,然后抓取地址包含114.114.114.114的10个报文。
1 2 3 4 5 6 diagnose debug flow trace stop diagnose debug enable diagnose debug flow filter addr 114.114.114.114 diagnose debug flow show function-name enable diagnose debug flow trace start 10
也可以通过协议来进行过滤,这里只查看icmp
协议的报文。
1 2 3 4 5 6 diagnose debug flow trace stop diagnose debug enable diagnose debug flow filter proto 1 diagnose debug flow show function-name enable diagnose debug flow trace start 10
运行的效果如下:
自带sniffer工具抓包
例如,这里抓取来自port1端口,端口号为6081的报文,其实就是GENVEN报文。
1 FGT-GWLB-1 (FG-traffic) # diagnose sniffer packet port1 'port 6081'
例如,这里抓取来所有的icmp报文。
1 FGT-GWLB-1 (FG-traffic) # diagnose sniffer packet any icmp 4
抓包命令结果如下:
可以利用sniff抓包来确认流量目前走哪一台防火墙,然后stop这个防火墙实例,查看流量多长时间能够切换成功,测试防火墙的高可用切换。我测试大致会在40秒左右切换成功。
四、部署Fortigate7.2.2版本代码参考 上述的CloudFormation代码中FortiGate防火墙的开机启动配置,是基于FortiGate6.4.10
版本配置的。如果选择7.2.2
版本部署,防火墙无法加载对应配置,可以将下面的代码替换上面关于防火墙的部分。
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 Fortigate1: Type: AWS::EC2::Instance Properties: ImageId: !FindInMap [RegionMap , !Ref "AWS::Region" , !Ref FortigateVersion ] KeyName: !Ref MyKeyPair InstanceType: !FindInMap [FirewallInstanceType , !Ref "FortigateVersion" , InstanceType ] NetworkInterfaces: - NetworkInterfaceId: !Ref Fortigate1DataEni DeviceIndex: 0 - NetworkInterfaceId: !Ref Fortigate1MgmtEni DeviceIndex: 1 UserData: Fn::Base64: Fn::Sub: - | config system admin edit "labuser" set accprofile "super_admin" set vdom "root" set password FSr1Lliu1qiang2long3DemoZJG5 next end end config system interface edit "port1" set defaultgw disable set allowaccess ping https ssh fgfm probe-response next edit "port2" set mode dhcp set allowaccess ping https ssh fgfm probe-response set defaultgw enable next end config system global set vdom-mode multi-vdom set timezone 08 end end config vdom edit root config system settings set vdom-type admin end end config vdom edit FG-traffic config system settings set vdom-type traffic end end config global config system interface edit "port1" set vdom "FG-traffic" set defaultgw disable set allowaccess ping https ssh fgfm probe-response next edit "port2" set vdom "root" set mode dhcp set allowaccess ping https ssh fgfm probe-response set defaultgw enable next end end config vdom edit FG-traffic config system geneve edit "geneve1" set interface "port1" set type ppp set remote-ip ${Az1GwlbIp} next edit "geneve2" set interface "port1" set type ppp set remote-ip ${Az2GwlbIp} next end config firewall policy edit 1 set name "1" set srcintf "geneve1" set dstintf "geneve1" set srcaddr "all" set dstaddr "all" set action accept set schedule "always" set service "ALL" set logtraffic all set logtraffic-start enable next edit 2 set name "2" set srcintf "geneve2" set dstintf "geneve2" set srcaddr "all" set dstaddr "all" set action accept set schedule "always" set service "ALL" set logtraffic all set logtraffic-start enable next end config log setting set local-in-allow enable set local-in-deny-unicast enable set local-in-deny-broadcast enable set local-out enable next end config router static edit 1 set device "geneve1" next edit 2 set device "geneve2" next edit 3 set dst 10.20 .0 .0 255.255 .0 .0 set gateway 10.20 .10 .1 set device "port1" next end config router policy edit 1 set input-device "geneve1" set gateway ${Az1GwlbIp} set output-device "geneve1" next edit 2 set input-device "geneve2" set gateway ${Az2GwlbIp} set output-device "geneve2" next end - Az1GwlbIp: !GetAtt 'CustomGwlbIpResource.Az1GwlbIp' Az2GwlbIp: !GetAtt 'CustomGwlbIpResource.Az2GwlbIp' Tags: - Key: Name Value: Fortigate-FW1 Fortigate2: Type: AWS::EC2::Instance DependsOn: CustomGwlbIpResource Properties: ImageId: !FindInMap [RegionMap , !Ref "AWS::Region" , !Ref FortigateVersion ] KeyName: !Ref MyKeyPair InstanceType: !FindInMap [FirewallInstanceType , !Ref "FortigateVersion" , InstanceType ] NetworkInterfaces: - NetworkInterfaceId: !Ref Fortigate2DataEni DeviceIndex: 0 - NetworkInterfaceId: !Ref Fortigate2MgmtEni DeviceIndex: 1 UserData: Fn::Base64: Fn::Sub: - | config system admin edit "labuser" set accprofile "super_admin" set vdom "root" set password FSr1Lliu1qiang2long3DemoZJG5 next end end config system interface edit "port1" set defaultgw disable set allowaccess ping https ssh fgfm probe-response next edit "port2" set mode dhcp set allowaccess ping https ssh fgfm probe-response set defaultgw enable next end config system global set vdom-mode multi-vdom set timezone 08 end end config vdom edit root config system settings set vdom-type admin end end config vdom edit FG-traffic config system settings set vdom-type traffic end end config global config system interface edit "port1" set vdom "FG-traffic" set defaultgw disable set allowaccess ping https ssh fgfm probe-response next edit "port2" set vdom "root" set mode dhcp set allowaccess ping https ssh fgfm probe-response set defaultgw enable next end end config vdom edit FG-traffic config system geneve edit "geneve1" set interface "port1" set type ppp set remote-ip ${Az2GwlbIp} next edit "geneve2" set interface "port1" set type ppp set remote-ip ${Az1GwlbIp} next end config firewall policy edit 1 set name "1" set srcintf "geneve1" set dstintf "geneve1" set srcaddr "all" set dstaddr "all" set action accept set schedule "always" set service "ALL" set logtraffic all set logtraffic-start enable next edit 2 set name "2" set srcintf "geneve2" set dstintf "geneve2" set srcaddr "all" set dstaddr "all" set action accept set schedule "always" set service "ALL" set logtraffic all set logtraffic-start enable next end config log setting set local-in-allow enable set local-in-deny-unicast enable set local-in-deny-broadcast enable set local-out enable next end config router static edit 1 set device "geneve1" next edit 2 set device "geneve2" next edit 3 set dst 10.20 .0 .0 255.255 .0 .0 set gateway 10.20 .30 .1 set device "port1" next end config router policy edit 1 set input-device "geneve1" set gateway ${Az2GwlbIp} set output-device "geneve1" next edit 2 set input-device "geneve2" set gateway ${Az1GwlbIp} set output-device "geneve2" next end end - Az1GwlbIp: !GetAtt 'CustomGwlbIpResource.Az1GwlbIp' Az2GwlbIp: !GetAtt 'CustomGwlbIpResource.Az2GwlbIp' Tags: - Key: Name Value: Fortigate-FW2
五、参考文档