6

为了保护我们的 API,我正在尝试使用 RateBasedRule 部署 WAFRegional。API 网关位于 SAM 模板中,其中我还有一个嵌套堆栈,用于保存 WAFRegional 配置的子模板。下面提供了 WAFRegional 配置的子模板。在 ExecuteChangeSet 阶段发生的情况如下:

  1. CamerasIpSet 已创建

  2. CamerasRateRule 已创建

  3. WAFCamerasWebACL CREATE_FAILED:引用的项目不存在。(服务:AWSWAFRegional;状态代码:400;错误代码:WAFNonexistentItemException

我在大约 2 个月前发现了以下帖子,其中有人在使用无服务器时遇到了同样的问题:https ://forum.serverless.com/t/dependon-api-gateway-deployment/7792

我在这里错过了什么?

AWSTemplateFormatVersion: '2010-09-09'
Description: 'Template for WAF Configuration'
Parameters:
  CamerasApi:
    Description: "Arn of the Cameras Api"
    Type: String
    Default: cameras-api-dev
  StageName:
    Description: "Stage name of the Cameras Api"
    Type: String
    Default: v
  Blocking:
    Description: "Number of calls per 5 minutes for WAF IP blocking."
    Type: Number
    Default: 2000
  EnvironmentType:
    Type: String
    Default: "dev"
    Description: "Type of environment: dev, staging or prod."


Resources:
  WAFCamerasWebACL:
    Type: AWS::WAFRegional::WebACL
    DependsOn: CamerasRateRule
    Properties:
      DefaultAction:
        Type: ALLOW
      MetricName: !Join ['', ['IPBlockingMetric', !Ref EnvironmentType]]
      Name: !Join ['', ['IPBlockingACL', !Ref EnvironmentType]]
      Rules:
        -
          Action:
            Type: "BLOCK"
          Priority: 1
          RuleId: !Ref CamerasRateRule

  CamerasRateRule:
    Type: AWS::WAFRegional::RateBasedRule
    Properties:
      MetricName: UnallowedAccessCount
      Name: FiveMinuteRule
      RateKey: IP
      RateLimit: !Ref Blocking
      MatchPredicates:
      -
        DataId: !Ref CamerasIpSet
        Negated: false
        Type: "IPMatch"

  CamerasIpSet:
    Type: AWS::WAFRegional::IPSet
    Properties:
      Name: !Join ['-', ['IpBlacklist', !Ref EnvironmentType]]


  MyWebACLAssociation:
    Type: AWS::WAFRegional::WebACLAssociation
    Properties:
      ResourceArn: !Sub arn:aws:apigateway:${AWS::Region}::/restapis/${CamerasApi}/stages/${StageName}
      WebACLId: !Ref WAFCamerasWebACL

Outputs:
  WebACL:
    Description: Name of the web ACL
    Value: !Ref WAFCamerasWebACL


4

4 回答 4

8

我终于在 AWS 客服的帮助下解决了这个问题。这是他们在处理 AWS::WAFRegional::RateBasedRule 时对 CloudFormation 的限制。

尽管 CloudFormation 支持创建基于 WAF 区域速率的规则,但目前不支持将它们与 Web ACL 关联。如果您观察下面的链接 [1],您会意识到:“要将通过 CloudFormation 创建的基于速率的规则添加到 Web ACL,请使用 AWS WAF 控制台、API 或命令行界面 (CLI)。”

[1] AWS::WAFRegional::RateBasedRule: https ://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-wafregional-ratebasedrule.html

我使用 Cloudformation 模板生成 WebACL、RateBasedRule 以及 WebACL 与我的 APIGW 的关联。在我们的 CI/CD 管道中使用 CodeBuild,我现在使用 CLI 命令将 RateBasedRule 添加到 WebACL aws waf-regional update-web-acl

于 2019-07-15T09:58:59.780 回答
1

我遇到了同样的问题,我用 WAFv2 解决了这个问题

AWSTemplateFormatVersion: '2010-09-09'
Description: 'Template for WAF Configuration'
Parameters:
  CamerasApi:
    Description: "Arn of the Cameras Api"
    Type: String
    Default: YOUR-API-ID
  StageName:
    Description: "Stage name of the Cameras Api"
    Type: String
    Default: YOUR-Stage
  Blocking:
    Description: "Number of calls per 5 minutes for WAF IP blocking."
    Type: Number
    Default: 2000
  EnvironmentType:
    Type: String
    Default: Prod
    Description: "Type of environment: dev, staging or prod."


Resources:
  WAFCamerasWebACL:
    Type: AWS::WAFv2::WebACL
    Properties:
      Name: ExampleWebACL
      Description: This is an example WebACL
      Scope: REGIONAL
      DefaultAction: 
        Allow: {}
      VisibilityConfig:
        SampledRequestsEnabled: true
        CloudWatchMetricsEnabled: true
        MetricName: ExampleWebACLMetric
      Rules:
        - Name: RulesTest
          Priority: 0
          Action:
           Block: {}
          VisibilityConfig:
            SampledRequestsEnabled: true
            CloudWatchMetricsEnabled: true
            MetricName: test
          Statement:
            RateBasedStatement:
              Limit: 100
              AggregateKeyType: IP

  MyWebACLAssociation:
    Type: AWS::WAFv2::WebACLAssociation
    Properties:
      ResourceArn: !Sub arn:aws:apigateway:${AWS::Region}::/restapis/${CamerasApi}/stages/${StageName}
      WebACLArn: !GetAtt  WAFCamerasWebACL.Arn

Outputs:
  WebACL:
    Description: Name of the web ACL
    Value: !Ref WAFCamerasWebACL
于 2020-06-04T07:27:25.823 回答
0

假设 aAWS::WAFRegional::WebACLAWS::WAFRegional::RateBasedRule在 Cloudformation 堆栈中定义,可以使用以下 bash 脚本附加它们:

CHANGE_TOKEN=$(aws waf-regional get-change-token --output text)
WEBACL_ID=$(aws waf-regional list-web-acls --query WebACLs[0].WebACLId --output text)
RULE_ID=$(aws waf-regional list-rate-based-rules --query Rules[0].RuleId --output text)
aws waf-regional update-web-acl --web-acl-id $WEBACL_ID --change-token $CHANGE_TOKEN \
    --updates Action="INSERT",ActivatedRule='{Priority=1,RuleId="'$RULE_ID'",Action={Type="BLOCK"},Type="RATE_BASED"}'

然而不幸的是,这会在删除 Cloudformation 堆栈时导致问题

未能删除以下资源:[RateBasedRuleName]。

任何想法如何使堆栈在发布时删除规则aws cloudformation delete-stack

于 2019-07-30T17:46:36.900 回答
0
Resources:
  BlueWafAlbAssociation:
    Type: "AWS::WAFv2::WebACLAssociation"
    Properties:
      WebACLArn: arn:aws:wafv2:us-east-1:1234567890:regional/webacl/name-of-webacl/id-of-webacl
      ResourceArn: arn:aws:elasticloadbalancing:us-east-1:1234567890:loadbalancer/app/load-balancer-name/xxxxxxxxxxx
于 2021-11-23T11:11:42.497 回答