From df967f68843ed93d179b57ba052c6759e08be5ff Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Wed, 7 Aug 2024 15:14:26 +0100 Subject: [PATCH] fixup! Add configuration for rate-limiting of logins, replacing hardcoded limits --- crates/config/src/sections/rate_limiting.rs | 12 +++--------- docs/config.schema.json | 2 +- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/crates/config/src/sections/rate_limiting.rs b/crates/config/src/sections/rate_limiting.rs index 3c29b744d..51efdbe93 100644 --- a/crates/config/src/sections/rate_limiting.rs +++ b/crates/config/src/sections/rate_limiting.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::{num::NonZero, time::Duration}; +use std::{num::NonZeroU32, time::Duration}; use governor::Quota; use schemars::JsonSchema; @@ -54,8 +54,7 @@ pub struct LoginRateLimitingConfig { pub struct RateLimiterConfiguration { /// A one-off burst of actions that the user can perform /// in one go without waiting. - /// Replenishes at the rate. - pub burst: u32, + pub burst: NonZeroU32, /// How quickly the allowance replenishes, in number of actions per second. /// Can be fractional to replenish slower. pub per_second: f64, @@ -82,10 +81,6 @@ impl ConfigurationSection for RateLimitingConfig { // Check one limiter's configuration for errors let error_on_limiter = |limiter: &RateLimiterConfiguration| -> Option { - if limiter.burst == 0 { - return Some(figment::error::Error::custom("`burst` must not be zero, as this would mean the action could never be performed")); - } - let recip = limiter.per_second.recip(); // period must be at least 1 nanosecond according to the governor library if recip < 1.0e-9 || !recip.is_finite() { @@ -120,8 +115,7 @@ impl RateLimiterConfiguration { if !reciprocal.is_finite() { return None; } - let burst = NonZero::new(self.burst)?; - Some(Quota::with_period(Duration::from_secs_f64(reciprocal))?.allow_burst(burst)) + Some(Quota::with_period(Duration::from_secs_f64(reciprocal))?.allow_burst(self.burst)) } } diff --git a/docs/config.schema.json b/docs/config.schema.json index 653972528..9232e190f 100644 --- a/docs/config.schema.json +++ b/docs/config.schema.json @@ -1726,7 +1726,7 @@ "description": "A one-off burst of actions that the user can perform in one go without waiting. Replenishes at the rate.", "type": "integer", "format": "uint32", - "minimum": 0.0 + "minimum": 1.0 }, "per_second": { "description": "How quickly the allowance replenishes, in number of actions per second. Can be fractional to replenish slower.",