-
Notifications
You must be signed in to change notification settings - Fork 626
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
Add challenge and js_challenge rate-limit modes #172
Merged
patryk
merged 9 commits into
cloudflare:master
from
jdarley:add-rate-limit-challenge-support
Jan 7, 2019
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
77b98a3
Add challenge and js_challenge rate-limit modes
5aa27db
Attempt to raise errors on incorrectly defined ratelimit
c6f5e0a
Apply @jacobbednarz's patch, modified to stop timeouts being provided…
9b635c5
Simplify if statement
5e44796
Remove rogue ValidateAction
0b222ec
Add acceptance tests
11eb7ea
Fix formatting
9890792
Remove errant log message
e2f16c4
Address short timeout periods and mispelling of 'timeout'
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -50,6 +50,41 @@ func TestAccCloudflareRateLimit_Basic(t *testing.T) { | |||||
}) | ||||||
} | ||||||
|
||||||
func TestAccCloudflareRateLimitChallenge_Basic(t *testing.T) { | ||||||
t.Parallel() | ||||||
var rateLimit cloudflare.RateLimit | ||||||
zone := os.Getenv("CLOUDFLARE_DOMAIN") | ||||||
rnd := acctest.RandString(10) | ||||||
name := "cloudflare_rate_limit." + rnd | ||||||
|
||||||
resource.Test(t, resource.TestCase{ | ||||||
PreCheck: func() { testAccPreCheck(t) }, | ||||||
Providers: testAccProviders, | ||||||
CheckDestroy: testAccCheckCloudflareRateLimitDestroy, | ||||||
Steps: []resource.TestStep{ | ||||||
{ | ||||||
Config: testAccCheckCloudflareRateLimitChallengeConfigBasic(zone, rnd), | ||||||
Check: resource.ComposeTestCheckFunc( | ||||||
testAccCheckCloudflareRateLimitExists(name, &rateLimit), | ||||||
testAccCheckCloudflareRateLimitIDIsValid(name, zone), | ||||||
// check that the action challenge mode has been set | ||||||
resource.TestCheckResourceAttr(name, "action.0.mode", "challenge"), | ||||||
resource.TestCheckResourceAttr(name, "action.0.response.#", "0"), | ||||||
resource.TestCheckResourceAttr(name, "bypass_url_patterns.#", "0"), | ||||||
resource.TestCheckResourceAttr(name, "match.0.response.0.statuses.#", "0"), | ||||||
resource.TestCheckResourceAttr(name, "disabled", "false"), | ||||||
resource.TestCheckResourceAttr(name, "match.#", "1"), | ||||||
resource.TestCheckResourceAttr(name, "match.0.request.#", "1"), | ||||||
resource.TestCheckResourceAttr(name, "match.0.request.0.schemes.#", "1"), | ||||||
resource.TestCheckResourceAttr(name, "match.0.request.0.url_pattern", "*"), | ||||||
resource.TestCheckResourceAttr(name, "match.0.response.#", "1"), | ||||||
resource.TestCheckResourceAttr(name, "match.0.response.0.origin_traffic", "true"), | ||||||
), | ||||||
}, | ||||||
}, | ||||||
}) | ||||||
} | ||||||
|
||||||
func TestAccCloudflareRateLimit_FullySpecified(t *testing.T) { | ||||||
t.Parallel() | ||||||
var rateLimit cloudflare.RateLimit | ||||||
|
@@ -167,6 +202,42 @@ func TestAccCloudflareRateLimit_CreateAfterManualDestroy(t *testing.T) { | |||||
}) | ||||||
} | ||||||
|
||||||
func TestAccCloudflareRateLimit_WithoutTimeout(t *testing.T) { | ||||||
t.Parallel() | ||||||
zone := os.Getenv("CLOUDFLARE_DOMAIN") | ||||||
rnd := acctest.RandString(10) | ||||||
|
||||||
resource.Test(t, resource.TestCase{ | ||||||
PreCheck: func() { testAccPreCheck(t) }, | ||||||
Providers: testAccProviders, | ||||||
CheckDestroy: testAccCheckCloudflareRateLimitDestroy, | ||||||
Steps: []resource.TestStep{ | ||||||
{ | ||||||
Config: testAccCheckCloudflareRateLimitConfigWithoutTimeout(zone, rnd), | ||||||
ExpectError: regexp.MustCompile(regexp.QuoteMeta("rate limit timeout must be set if the 'mode' is simulate or ban")), | ||||||
}, | ||||||
}, | ||||||
}) | ||||||
} | ||||||
|
||||||
func TestAccCloudflareRateLimit_ChallengeWithTimeout(t *testing.T) { | ||||||
t.Parallel() | ||||||
zone := os.Getenv("CLOUDFLARE_DOMAIN") | ||||||
rnd := acctest.RandString(10) | ||||||
|
||||||
resource.Test(t, resource.TestCase{ | ||||||
PreCheck: func() { testAccPreCheck(t) }, | ||||||
Providers: testAccProviders, | ||||||
CheckDestroy: testAccCheckCloudflareRateLimitDestroy, | ||||||
Steps: []resource.TestStep{ | ||||||
{ | ||||||
Config: testAccCheckCloudflareRateLimitChallengeConfigWithTimeout(zone, rnd), | ||||||
ExpectError: regexp.MustCompile(regexp.QuoteMeta("rate limit timeout must not be set if the 'mode' is challenge or js_challenge")), | ||||||
}, | ||||||
}, | ||||||
}) | ||||||
} | ||||||
|
||||||
func testAccCheckCloudflareRateLimitDestroy(s *terraform.State) error { | ||||||
client := testAccProvider.Meta().(*cloudflare.API) | ||||||
|
||||||
|
@@ -314,3 +385,40 @@ resource "cloudflare_rate_limit" "%[1]s" { | |||||
bypass_url_patterns = ["%[2]s/bypass1","%[2]s/bypass2"] | ||||||
}`, id, zone) | ||||||
} | ||||||
|
||||||
func testAccCheckCloudflareRateLimitChallengeConfigBasic(zone, id string) string { | ||||||
return fmt.Sprintf(` | ||||||
resource "cloudflare_rate_limit" "%[1]s" { | ||||||
zone = "%[2]s" | ||||||
threshold = 1000 | ||||||
period = 1 | ||||||
action { | ||||||
mode = "challenge" | ||||||
} | ||||||
}`, id, zone) | ||||||
} | ||||||
|
||||||
func testAccCheckCloudflareRateLimitConfigWithoutTimeout(zone, id string) string { | ||||||
return fmt.Sprintf(` | ||||||
resource "cloudflare_rate_limit" "%[1]s" { | ||||||
zone = "%[2]s" | ||||||
threshold = 1000 | ||||||
period = 1 | ||||||
action { | ||||||
mode = "simulate" | ||||||
} | ||||||
}`, id, zone) | ||||||
} | ||||||
|
||||||
func testAccCheckCloudflareRateLimitChallengeConfigWithTimeout(zone, id string) string { | ||||||
return fmt.Sprintf(` | ||||||
resource "cloudflare_rate_limit" "%[1]s" { | ||||||
zone = "%[2]s" | ||||||
threshold = 1000 | ||||||
period = 1 | ||||||
action { | ||||||
mode = "challenge" | ||||||
timeoute = 60 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
}`, id, zone) | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is now optional based on the mode, we probably want to add a check in the
Create
/Update
that if the mode is simulate or ban, we check the value is defined to prevent people scratching their heads with unintended behaviour.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely agree. The Cloudflare API does give reasonable error messages around this though it should really be caught by the provider before the API is called.
I'm just working on it at the moment, though this is the first time I've written any Go so you may have to forgive my non-idiomatic code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After grappling with attempting to insert validation into
expandRateLimitAction
, I've realised that this won't behave quite as expected. I've attempted to implement aValidateFunc
into theaction
section of the schema itself (where I'll have access to bothmode
andtimeout
), but it appears that this is not supported on lists or sets at this point in time.I'd appreciate any pointers as to how to proceed with this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, the
ValidateFunc
is only supported on primitive types which throws it out a bit.Sure, I think this might do the job. You were on the right track with using
expandRateLimitAction
as it's used in both theCreate
andUpdate
. I haven't thoroughly vetted this though.Do you want to try this and let me know how you go?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's absolutely brilliant, thanks for your help. I'd used
fmt.Errorf
to raise errors, but hadn't wrapped them witherrors.Wrap
. This now raises errors correctly.I expanded the logic to throw an error when timeout values are provided for
challenge
andjs_challenge
modes as the Cloudflare API won't accept them.