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

resource/cloudflare_ruleset: allow setting both URI query and path options #1271

Merged
merged 1 commit into from
Oct 21, 2021
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
3 changes: 3 additions & 0 deletions .changelog/1271.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_ruleset: allow setting `uri` and `path` action parmeters together in a single rule
```
19 changes: 9 additions & 10 deletions cloudflare/resource_cloudflare_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,26 +747,25 @@ func buildRulesetRulesFromResource(phase string, r interface{}) ([]cloudflare.Ru
}

case "uri":
var uriParameterConfig cloudflare.RulesetRuleActionParametersURI
for _, uriValue := range pValue.([]interface{}) {
if val, ok := uriValue.(map[string]interface{})["path"]; ok && len(val.([]interface{})) > 0 {
uriPathConfig := val.([]interface{})[0].(map[string]interface{})
rule.ActionParameters.URI = &cloudflare.RulesetRuleActionParametersURI{
Path: &cloudflare.RulesetRuleActionParametersURIPath{
Value: uriPathConfig["value"].(string),
Expression: uriPathConfig["expression"].(string),
},
uriParameterConfig.Path = &cloudflare.RulesetRuleActionParametersURIPath{
Value: uriPathConfig["value"].(string),
Expression: uriPathConfig["expression"].(string),
}
}

if val, ok := uriValue.(map[string]interface{})["query"]; ok && len(val.([]interface{})) > 0 {
uriQueryConfig := val.([]interface{})[0].(map[string]interface{})
rule.ActionParameters.URI = &cloudflare.RulesetRuleActionParametersURI{
Query: &cloudflare.RulesetRuleActionParametersURIQuery{
Value: uriQueryConfig["value"].(string),
Expression: uriQueryConfig["expression"].(string),
},
uriParameterConfig.Query = &cloudflare.RulesetRuleActionParametersURIQuery{
Value: uriQueryConfig["value"].(string),
Expression: uriQueryConfig["expression"].(string),
}
}

rule.ActionParameters.URI = &uriParameterConfig
}

case "headers":
Expand Down
71 changes: 71 additions & 0 deletions cloudflare/resource_cloudflare_ruleset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,49 @@ func TestAccCloudflareRuleset_TransformationRuleURIQuery(t *testing.T) {
})
}

func TestAccCloudflareRuleset_TransformationRuleURIPathAndQueryCombination(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the WAF
// service does not yet support the API tokens and it results in
// misleading state error messages.
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" {
defer func(apiToken string) {
os.Setenv("CLOUDFLARE_API_TOKEN", apiToken)
}(os.Getenv("CLOUDFLARE_API_TOKEN"))
os.Setenv("CLOUDFLARE_API_TOKEN", "")
}

t.Parallel()
rnd := generateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
zoneName := os.Getenv("CLOUDFLARE_DOMAIN")
resourceName := "cloudflare_ruleset." + rnd

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareRulesetTransformationRuleURIPathAndQueryCombination(rnd, "uri combination of path and query", zoneID, zoneName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", "uri combination of path and query"),
resource.TestCheckResourceAttr(resourceName, "description", rnd+" ruleset description"),
resource.TestCheckResourceAttr(resourceName, "kind", "zone"),
resource.TestCheckResourceAttr(resourceName, "phase", "http_request_transform"),

resource.TestCheckResourceAttr(resourceName, "rules.#", "1"),

resource.TestCheckResourceAttr(resourceName, "rules.0.action", "rewrite"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.uri.0.path.0.value", "/path/to/url"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.uri.0.query.0.expression", "concat(\"requestUrl=\", http.request.full_uri)"),
resource.TestCheckResourceAttr(resourceName, "rules.0.expression", "true"),
resource.TestCheckResourceAttr(resourceName, "rules.0.description", "example for combining URI action parameters for path and query"),
resource.TestCheckResourceAttr(resourceName, "rules.0.enabled", "true"),
),
},
},
})
}

func TestAccCloudflareRuleset_TransformationRuleHeaders(t *testing.T) {
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the WAF
// service does not yet support the API tokens and it results in
Expand Down Expand Up @@ -1663,3 +1706,31 @@ func testAccCheckCloudflareRulesetAccountLevelCustomWAFRule(rnd, name, accountID
}
}`, rnd, name, accountID, zoneName)
}

func testAccCheckCloudflareRulesetTransformationRuleURIPathAndQueryCombination(rnd, name, zoneID, zoneName string) string {
return fmt.Sprintf(`
resource "cloudflare_ruleset" "%[1]s" {
zone_id = "%[3]s"
name = "%[2]s"
description = "%[1]s ruleset description"
kind = "zone"
phase = "http_request_transform"

rules {
action = "rewrite"
action_parameters {
uri {
query {
expression = "concat(\"requestUrl=\", http.request.full_uri)"
}
path {
value = "/path/to/url"
}
}
}
expression = "true"
description = "example for combining URI action parameters for path and query"
enabled = true
}
}`, rnd, name, zoneID, zoneName)
}