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

fix: WAF rule logic #900

Merged
merged 1 commit into from
Nov 22, 2024
Merged

fix: WAF rule logic #900

merged 1 commit into from
Nov 22, 2024

Conversation

bryan-robitaille
Copy link
Contributor

@bryan-robitaille bryan-robitaille commented Nov 22, 2024

Summary | Résumé

The previous logic was such that although an APP or API path was valid it would be blocked by the opposing service rule.

This fixes the logic to be: service_url && !uri instead of !(service_url && uri)

app && !uri
app = true && uri = false = block
app = false && uri = true = allow
app = true && uri = true = allow
app = false && uri = false = allow

and

api && !uri
api = true && uri = false = block
api = false && uri = true = allow
api = true && uri = true = allow
api = false && uri = false = allow

Test instructions | Instructions pour tester la modification

Have faith in my logic deduction abilities on a Friday morning running on only 2 cups of coffee.

Copy link

Staging: load_balancer

✅   Terraform Init: success
✅   Terraform Validate: success
✅   Terraform Format: success
✅   Terraform Plan: success
✅   Conftest: success

Plan: 0 to add, 1 to change, 0 to destroy
Show summary
CHANGE NAME
update aws_wafv2_web_acl.forms_acl
Show plan
Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # aws_wafv2_web_acl.forms_acl will be updated in-place
  ~ resource "aws_wafv2_web_acl" "forms_acl" {
        id                          = "e8fc1b67-9d4d-4a29-8c94-65b37b30a231"
        name                        = "GCForms"
        tags                        = {}
        # (9 unchanged attributes hidden)

      - rule {
          - name     = "AllowOnlyAppUrls" -> null
          - priority = 60 -> null

          - action {
              - block {
                }
            }

          - statement {
              - not_statement {
                  - statement {
                      - and_statement {
                          - statement {
                              - byte_match_statement {
                                  - positional_constraint = "EXACTLY" -> null
                                  - search_string         = "forms-staging.cdssandbox.xyz" -> null

                                  - field_to_match {
                                      - single_header {
                                          - name = "host" -> null
                                        }
                                    }

                                  - text_transformation {
                                      - priority = 1 -> null
                                      - type     = "LOWERCASE" -> null
                                    }
                                }
                            }
                          - statement {
                              - regex_pattern_set_reference_statement {
                                  - arn = "arn:aws:wafv2:ca-central-1:687401027353:regional/regexpatternset/valid_app_uri_paths/2020c3bc-eb87-45f3-8ee7-99cd707612e7" -> null

                                  - field_to_match {
                                      - uri_path {}
                                    }

                                  - text_transformation {
                                      - priority = 1 -> null
                                      - type     = "COMPRESS_WHITE_SPACE" -> null
                                    }
                                  - text_transformation {
                                      - priority = 2 -> null
                                      - type     = "LOWERCASE" -> null
                                    }
                                }
                            }
                        }
                    }
                }
            }

          - visibility_config {
              - cloudwatch_metrics_enabled = true -> null
              - metric_name                = "AllowOnlyAppUrls" -> null
              - sampled_requests_enabled   = false -> null
            }
        }
      - rule {
          - name     = "AWSManagedRulesAmazonIpReputationList" -> null
          - priority = 1 -> null

          - override_action {
              - none {}
            }

          - statement {
              - managed_rule_group_statement {
                  - name        = "AWSManagedRulesAmazonIpReputationList" -> null
                  - vendor_name = "AWS" -> null
                    # (1 unchanged attribute hidden)
                }
            }

          - visibility_config {
              - cloudwatch_metrics_enabled = true -> null
              - metric_name                = "AWSManagedRulesAmazonIpReputationList" -> null
              - sampled_requests_enabled   = true -> null
            }
        }
      - rule {
          - name     = "AWSManagedRulesKnownBadInputsRuleSet" -> null
          - priority = 40 -> null

          - override_action {
              - none {}
            }

          - statement {
              - managed_rule_group_statement {
                  - name        = "AWSManagedRulesKnownBadInputsRuleSet" -> null
                  - vendor_name = "AWS" -> null
                    # (1 unchanged attribute hidden)
                }
            }

          - visibility_config {
              - cloudwatch_metrics_enabled = true -> null
              - metric_name                = "AWSManagedRulesKnownBadInputsRuleSet" -> null
              - sampled_requests_enabled   = true -> null
            }
        }
      - rule {
          - name     = "AWSManagedRulesLinuxRuleSet" -> null
          - priority = 50 -> null

          - override_action {
              - none {}
            }

          - statement {
              - managed_rule_group_statement {
                  - name        = "AWSManagedRulesLinuxRuleSet" -> null
                  - vendor_name = "AWS" -> null
                    # (1 unchanged attribute hidden)
                }
            }

          - visibility_config {
              - cloudwatch_metrics_enabled = true -> null
              - metric_name                = "AWSManagedRulesLinuxRuleSet" -> null
              - sampled_requests_enabled   = true -> null
            }
        }
      + rule {
          + name     = "AllowOnlyApiUrls"
          + priority = 65

          + action {
              + block {
                }
            }

          + statement {
              + and_statement {
                  + statement {
                      + byte_match_statement {
                          + positional_constraint = "EXACTLY"
                          + search_string         = "api.forms-staging.cdssandbox.xyz"

                          + field_to_match {
                              + single_header {
                                  + name = "host"
                                }
                            }

                          + text_transformation {
                              + priority = 1
                              + type     = "LOWERCASE"
                            }
                        }
                    }
                  + statement {
                      + not_statement {
                          + statement {
                              + regex_pattern_set_reference_statement {
                                  + arn = "arn:aws:wafv2:ca-central-1:687401027353:regional/regexpatternset/valid_api_uri/3d646d8d-b357-4b54-9fe4-0083273f7989"

                                  + field_to_match {
                                      + uri_path {}
                                    }

                                  + text_transformation {
                                      + priority = 1
                                      + type     = "COMPRESS_WHITE_SPACE"
                                    }
                                  + text_transformation {
                                      + priority = 2
                                      + type     = "LOWERCASE"
                                    }
                                }
                            }
                        }
                    }
                }
            }

          + visibility_config {
              + cloudwatch_metrics_enabled = true
              + metric_name                = "AllowOnlyApiUrls"
              + sampled_requests_enabled   = false
            }
        }
      + rule {
          + name     = "AllowOnlyAppUrls"
          + priority = 60

          + action {
              + block {
                }
            }

          + statement {
              + and_statement {
                  + statement {
                      + byte_match_statement {
                          + positional_constraint = "EXACTLY"
                          + search_string         = "forms-staging.cdssandbox.xyz"

                          + field_to_match {
                              + single_header {
                                  + name = "host"
                                }
                            }

                          + text_transformation {
                              + priority = 1
                              + type     = "LOWERCASE"
                            }
                        }
                    }
                  + statement {
                      + not_statement {
                          + statement {
                              + regex_pattern_set_reference_statement {
                                  + arn = "arn:aws:wafv2:ca-central-1:687401027353:regional/regexpatternset/valid_app_uri_paths/2020c3bc-eb87-45f3-8ee7-99cd707612e7"

                                  + field_to_match {
                                      + uri_path {}
                                    }

                                  + text_transformation {
                                      + priority = 1
                                      + type     = "COMPRESS_WHITE_SPACE"
                                    }
                                  + text_transformation {
                                      + priority = 2
                                      + type     = "LOWERCASE"
                                    }
                                }
                            }
                        }
                    }
                }
            }

          + visibility_config {
              + cloudwatch_metrics_enabled = true
              + metric_name                = "AllowOnlyAppUrls"
              + sampled_requests_enabled   = false
            }
        }
      + rule {
          + name     = "AWSManagedRulesAmazonIpReputationList"
          + priority = 1

          + override_action {
              + none {}
            }

          + statement {
              + managed_rule_group_statement {
                  + name        = "AWSManagedRulesAmazonIpReputationList"
                  + vendor_name = "AWS"
                }
            }

          + visibility_config {
              + cloudwatch_metrics_enabled = true
              + metric_name                = "AWSManagedRulesAmazonIpReputationList"
              + sampled_requests_enabled   = true
            }
        }
      + rule {
          + name     = "AWSManagedRulesKnownBadInputsRuleSet"
          + priority = 40

          + override_action {
              + none {}
            }

          + statement {
              + managed_rule_group_statement {
                  + name        = "AWSManagedRulesKnownBadInputsRuleSet"
                  + vendor_name = "AWS"
                }
            }

          + visibility_config {
              + cloudwatch_metrics_enabled = true
              + metric_name                = "AWSManagedRulesKnownBadInputsRuleSet"
              + sampled_requests_enabled   = true
            }
        }
      + rule {
          + name     = "AWSManagedRulesLinuxRuleSet"
          + priority = 50

          + override_action {
              + none {}
            }

          + statement {
              + managed_rule_group_statement {
                  + name        = "AWSManagedRulesLinuxRuleSet"
                  + vendor_name = "AWS"
                }
            }

          + visibility_config {
              + cloudwatch_metrics_enabled = true
              + metric_name                = "AWSManagedRulesLinuxRuleSet"
              + sampled_requests_enabled   = true
            }
        }

        # (7 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

─────────────────────────────────────────────────────────────────────────────

Saved the plan to: plan.tfplan

To perform exactly these actions, run the following command to apply:
    terraform apply "plan.tfplan"
Show Conftest results
WARN - plan.json - main - Missing Common Tags: ["aws_acm_certificate.form_viewer"]
WARN - plan.json - main - Missing Common Tags: ["aws_acm_certificate.form_viewer_maintenance_mode"]
WARN - plan.json - main - Missing Common Tags: ["aws_acm_certificate.forms_api"]
WARN - plan.json - main - Missing Common Tags: ["aws_alb_listener_rule.forms_api"]
WARN - plan.json - main - Missing Common Tags: ["aws_cloudfront_distribution.maintenance_mode"]
WARN - plan.json - main - Missing Common Tags: ["aws_iam_role.firehose_waf_logs"]
WARN - plan.json - main - Missing Common Tags: ["aws_kinesis_firehose_delivery_stream.firehose_waf_logs"]
WARN - plan.json - main - Missing Common Tags: ["aws_lb.form_viewer"]
WARN - plan.json - main - Missing Common Tags: ["aws_lb_listener.form_viewer_http"]
WARN - plan.json - main - Missing Common Tags: ["aws_lb_listener.form_viewer_https"]
WARN - plan.json - main - Missing Common Tags: ["aws_lb_target_group.form_viewer_1"]
WARN - plan.json - main - Missing Common Tags: ["aws_lb_target_group.form_viewer_2"]
WARN - plan.json - main - Missing Common Tags: ["aws_lb_target_group.forms_api"]
WARN - plan.json - main - Missing Common Tags: ["aws_s3_bucket.maintenance_mode"]
WARN - plan.json - main - Missing Common Tags: ["aws_s3_object.maintenance_static_page_css_files[\"style.css\"]"]
WARN - plan.json - main - Missing Common Tags: ["aws_s3_object.maintenance_static_page_html_files[\"index-fr.html\"]"]
WARN - plan.json - main - Missing Common Tags: ["aws_s3_object.maintenance_static_page_html_files[\"index.html\"]"]
WARN - plan.json - main - Missing Common Tags: ["aws_s3_object.maintenance_static_page_ico_files[\"favicon.ico\"]"]
WARN - plan.json - main - Missing Common Tags: ["aws_s3_object.maintenance_static_page_svg_files[\"site-unavailable.svg\"]"]
WARN - plan.json - main - Missing Common Tags: ["aws_shield_protection.alb"]
WARN - plan.json - main - Missing Common Tags: ["aws_shield_protection.route53_hosted_zone[0]"]
WARN - plan.json - main - Missing...

Copy link
Contributor

@craigzour craigzour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense!

@bryan-robitaille bryan-robitaille merged commit 11b40f3 into main Nov 22, 2024
11 checks passed
@bryan-robitaille bryan-robitaille deleted the fix/waf_rule_logic branch November 22, 2024 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants