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

Compare firewall descriptions after converting unicode + HTML entities #758

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cloudflare/resource_cloudflare_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudflare

import (
"fmt"
"html"
"log"
"strings"

Expand Down Expand Up @@ -41,6 +42,12 @@ func resourceCloudflareFilter() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 500),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if html.UnescapeString(old) == html.UnescapeString(new) {
return true
}
return false
},
},
"ref": {
Type: schema.TypeString,
Expand Down
33 changes: 33 additions & 0 deletions cloudflare/resource_cloudflare_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,36 @@ func TestAccFilterWhitespace(t *testing.T) {
},
})
}

func TestAccFilterHTMLEntity(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_filter." + rnd
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

filter := `(http.host eq \"` + domain + `\")`

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testFilterWithHTMLEntityConfig(rnd, zoneID, "true", "this is a 'test'", filter),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "description", "this is a \u0026#39;test\u0026#39;"),
),
ExpectNonEmptyPlan: false,
},
},
})
}

func testFilterWithHTMLEntityConfig(resourceID, zoneID, paused, description, expression string) string {
return fmt.Sprintf(`
resource "cloudflare_filter" "%[1]s" {
zone_id = "%[2]s"
paused = "%[3]s"
description = "%[4]s"
expression = "%[5]s"
}
`, resourceID, zoneID, paused, description, expression)
}
7 changes: 7 additions & 0 deletions cloudflare/resource_cloudflare_firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudflare

import (
"fmt"
"html"
"log"
"strings"

Expand Down Expand Up @@ -44,6 +45,12 @@ func resourceCloudflareFirewallRule() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 500),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if html.UnescapeString(old) == html.UnescapeString(new) {
return true
}
return false
},
},
"paused": {
Type: schema.TypeBool,
Expand Down
46 changes: 46 additions & 0 deletions cloudflare/resource_cloudflare_firewall_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,49 @@ func testFirewallRuleBypassConfig(resourceID, zoneID, paused, description, expre
}
`, resourceID, zoneID, paused, description, expression)
}

func TestAccFirewallRuleWithUnicodeAndHTMLEntity(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_firewall_rule." + rnd
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
domain := os.Getenv("CLOUDFLARE_DOMAIN")

expression := `(http.host eq \"` + domain + `\")`

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccFirewallRuleWithUnicodeAndHTMLEntityConfig(rnd, zoneID, "true", "this is a 'test'", expression, "allow", "1"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "description", "this is a \u0026#39;test\u0026#39;"),
resource.TestCheckResourceAttr(name, "paused", "true"),
resource.TestCheckResourceAttr(name, "action", "allow"),
resource.TestCheckResourceAttr(name, "priority", "1"),
resource.TestCheckResourceAttr(name, "zone_id", zoneID),
),
ExpectNonEmptyPlan: false,
},
},
})
}

func testAccFirewallRuleWithUnicodeAndHTMLEntityConfig(resourceID, zoneID, paused, description, expression, action, priority string) string {
return fmt.Sprintf(`
resource "cloudflare_filter" "%[1]s" {
zone_id = "%[2]s"
paused = "%[3]s"
description = "%[4]s"
expression = "%[5]s"
}
resource "cloudflare_firewall_rule" "%[1]s" {
zone_id = "%[2]s"
paused = "%[3]s"
description = "%[4]s"
filter_id = "${cloudflare_filter.%[1]s.id}"
action = "%[6]s"
priority = %[7]s
}
`, resourceID, zoneID, paused, description, expression, action, priority)
}