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

chore: Seperate WAF uri checks contexts between API and App #896

Merged
merged 10 commits into from
Nov 21, 2024

Conversation

bryan-robitaille
Copy link
Contributor

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

Summary | Résumé

This PR separates the regex patterns for the uri paths between the API and App services. Each WAF rule is responsible to to check against it's proper domain match. The rule is also ready to handle multiple App domains with the dynamic statement being generated for each passed in domain.

App regex has been updated to remove uri paths that are no longer valid

API regex is detailed enough to cover exact path matching for all valid routes vs the App that only takes into account the root path.

Test instructions | Instructions pour tester la modification

Difficult to test however you can take the regex statement for the API and enter it into an online regex tester with various known path strings to ensure they pass.

@bryan-robitaille bryan-robitaille marked this pull request as ready for review November 21, 2024 13:21
@bryan-robitaille bryan-robitaille mentioned this pull request Nov 21, 2024
4 tasks
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.

Still running some tests on the regex expressions

aws/load_balancer/waf.tf Outdated Show resolved Hide resolved
aws/load_balancer/waf.tf Outdated Show resolved Hide resolved
aws/load_balancer/waf.tf Outdated Show resolved Hide resolved
aws/load_balancer/waf.tf Outdated Show resolved Hide resolved
Co-authored-by: Clément JANIN <clement.janin@cds-snc.ca>
Copy link

⚠ Terrform update available

Terraform: 1.9.8 (using 1.9.2)
Terragrunt: 0.69.0 (using 0.63.2)

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.

LGTM :)

Copy link

Staging: load_balancer

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

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

Terraform will perform the following actions:

  # aws_wafv2_regex_pattern_set.valid_api_uri_paths will be created
  + resource "aws_wafv2_regex_pattern_set" "valid_api_uri_paths" {
      + arn         = (known after apply)
      + description = "Regex to match the api valid urls"
      + id          = (known after apply)
      + lock_token  = (known after apply)
      + name        = "valid_api_uri"
      + scope       = "REGIONAL"
      + tags_all    = {
          + "CostCentre" = "forms-platform-staging"
          + "Terraform"  = "true"
        }

      + regular_expression {
          + regex_string = "^(?:\\/v1)?\\/forms\\/(?:(\\w{25}))\\/(?:(template|(?:(submission\\/(?:(new|(?:(\\d{2}-\\d{2}-\\w{4})\\/?(?:(confirm\\/\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}|problem)?))))))))(?:\\/)?$"
        }
    }

  # aws_wafv2_regex_pattern_set.valid_app_uri_paths will be updated in-place
  ~ resource "aws_wafv2_regex_pattern_set" "valid_app_uri_paths" {
      ~ description = "Regex to match the app and api valid urls" -> "Regex to match the app valid urls"
        id          = "2020c3bc-eb87-45f3-8ee7-99cd707612e7"
        name        = "valid_app_uri_paths"
        tags        = {}
        # (4 unchanged attributes hidden)

      - regular_expression {
          - regex_string = "^\\/(?:en|fr)?\\/?(?:(admin|id|api|auth|signup|profile|forms|unsupported-browser|terms-of-use|contact|support|404)(?:\\/[\\w-]+)?)(?:\\/.*)?$" -> null
        }
      - regular_expression {
          - regex_string = "^\\/(?:en|fr)?\\/?(?:(form-builder|sla|unlock-publishing|terms-and-conditions|javascript-disabled)(?:\\/[\\w-]+)?)(?:\\/.*)?$" -> null
        }
      - regular_expression {
          - regex_string = "^\\/(?:v1)?\\/?(?:(docs|status))(?:\\/)?$" -> null
        }
      + regular_expression {
          + regex_string = "^\\/(?:en|fr)?\\/?(?:(admin|form-builder|forms|id|auth|profile|support|contact|unlock-publishing)(?:\\/[\\w-]+)?)(?:\\/.*)?$"
        }
      + regular_expression {
          + regex_string = "^\\/(?:en|fr)?\\/?(?:(sla|terms-and-conditions|terms-of-use|unsupported-browser|javascript-disabled|404)(?:\\/[\\w-]+)?)(?:\\/.*)?$"
        }

        # (3 unchanged blocks hidden)
    }

  # 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 {
                      - 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 {
              + not_statement {
                  + 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 {
                              + regex_pattern_set_reference_statement {
                                  + arn = (known after apply)

                                  + 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 {
              + not_statement {
                  + statement {
                      + and_statement {
                          + statement {
                              + or_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 {
                              + 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: 1 to add, 2 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...

@bryan-robitaille bryan-robitaille merged commit 116af75 into main Nov 21, 2024
11 checks passed
@bryan-robitaille bryan-robitaille deleted the chore/untangle_waf_rules branch November 21, 2024 18:48
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