【问题标题】:SAM API Gateway with Cloudformation WAFRegionalSAM API 网关与 Cloudformation WAFRegional
【发布时间】:2019-11-03 03:25:58
【问题描述】:

为了保护我们的 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


【问题讨论】:

  • 不相关,但WAFCamerasWebACL 中的两个连接可以使用 Sub:MetricName: !Sub IPBlockingMetric${EnvironmentType} 更简单清晰地编写
  • 当 Cloudformation 实现 WAFv2(他们建议您现在使用)时,问题将得到解决。最终,我们需要 Cloudformation 来支持创建和关联(即到 API 网关或负载均衡器),这样其他变通方法就不会到位,因为它们不是很容易转移。 GitHub 票是:github.com/aws-cloudformation/…

标签: amazon-cloudformation aws-sam amazon-waf


【解决方案1】:
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

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我用 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
    

    【讨论】:

      【解决方案3】:

      假设 AWS::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 时如何启用堆栈以删除规则的任何想法?

      【讨论】:

        【解决方案4】:

        我终于在 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 命令aws waf-regional update-web-acl 将 RateBasedRule 添加到 WebACL。

        【讨论】:

        • 感谢分享。我遇到了完全相同的问题。文档在这一点上并不是很清楚。
        • 谢谢!刚刚也遇到了同样的问题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-08
        • 1970-01-01
        • 2021-06-17
        • 2018-08-16
        • 2021-01-01
        • 2018-09-28
        • 2018-06-02
        相关资源
        最近更新 更多