-
Notifications
You must be signed in to change notification settings - Fork 8
feat(api): Add global IP rate limiting framework (ADR-0022 phase 1) #846
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
Open
ymh1874
wants to merge
2
commits into
openstack-experimental:main
Choose a base branch
from
ymh1874:feature/843-rate-limiting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Rate limiting configuration sections (ADR-0022). | ||
| //! | ||
| //! Each `[rate_limit_*]` INI section deserializes into a [`RateLimitSection`]. | ||
| //! The section carries only the *policy* scalars; the actual `governor` | ||
| //! [`RateLimiter`](https://docs.rs/governor) instances live in | ||
| //! [`crate::rate_limit::RateLimitState`] and are constructed once at startup | ||
| //! from these values. | ||
| //! | ||
| //! # Security invariant (ADR-0022 §2, config bounds) | ||
| //! | ||
| //! If `enabled = true` and either `burst_size` or `replenish_rate_per_second` | ||
| //! falls outside `[1, 100000]`, the application **must** refuse to start. This | ||
| //! is enforced in | ||
| //! [`RateLimitState::from_config`](openstack_keystone_core::rate_limit::RateLimitState::from_config), | ||
| //! not here — a disabled section with out-of-range values is harmless and must | ||
| //! not cause a startup failure, so the bound cannot be a field-level | ||
| //! `validator` range that would fire unconditionally. | ||
|
|
||
| use serde::Deserialize; | ||
|
|
||
| /// Default burst capacity when the key is absent from the config file. | ||
| fn default_burst_size() -> u32 { | ||
| 100 | ||
| } | ||
|
|
||
| /// Default replenishment rate when the key is absent from the config file. | ||
| fn default_replenish_rate_per_second() -> u32 { | ||
| 10 | ||
| } | ||
|
|
||
| /// A single rate-limiting bucket, mapped from one INI `[rate_limit_*]` section. | ||
| /// | ||
| /// The same struct is reused for every bucket (global-IP, per-user, per-domain | ||
| /// …) so operators see a consistent configuration shape across all limiters. | ||
| /// | ||
| /// ```ini | ||
| /// [rate_limit_global_ip] | ||
| /// enabled = true | ||
| /// burst_size = 100 | ||
| /// replenish_rate_per_second = 10 | ||
| /// ``` | ||
| #[derive(Debug, Deserialize, Clone)] | ||
| pub struct RateLimitSection { | ||
| /// When `false` (the default) the corresponding `governor` limiter is not | ||
| /// instantiated and the handler bypasses this check entirely. | ||
| #[serde(default)] | ||
| pub enabled: bool, | ||
|
|
||
| /// Maximum number of cells that can be consumed in a burst before | ||
| /// replenishment kicks in. Must be within `[1, 100000]` when | ||
| /// `enabled = true`. | ||
| #[serde(default = "default_burst_size")] | ||
| pub burst_size: u32, | ||
|
|
||
| /// How many cells are added back to the bucket per second. Must be within | ||
| /// `[1, 100000]` when `enabled = true`. | ||
| #[serde(default = "default_replenish_rate_per_second")] | ||
| pub replenish_rate_per_second: u32, | ||
| } | ||
|
|
||
| impl Default for RateLimitSection { | ||
| fn default() -> Self { | ||
| Self { | ||
| enabled: false, | ||
| burst_size: default_burst_size(), | ||
| replenish_rate_per_second: default_replenish_rate_per_second(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use serde_json::json; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn default_is_disabled() { | ||
| let s = RateLimitSection::default(); | ||
| assert!(!s.enabled); | ||
| assert_eq!(s.burst_size, 100); | ||
| assert_eq!(s.replenish_rate_per_second, 10); | ||
| } | ||
|
|
||
| #[test] | ||
| fn deserialize_enabled_section() { | ||
| let s: RateLimitSection = serde_json::from_value(json!({"enabled": true, "burst_size": 5, | ||
| "replenish_rate_per_second": 1})) | ||
| .unwrap(); | ||
| assert!(s.enabled); | ||
| assert_eq!(s.burst_size, 5); | ||
| assert_eq!(s.replenish_rate_per_second, 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn deserialize_disabled_ignores_zero_values() { | ||
| // Disabled sections with zero limits are valid config (no startup failure). | ||
| let s: RateLimitSection = serde_json::from_value(json!({"enabled": false, "burst_size": 0, | ||
| "replenish_rate_per_second": 0})) | ||
| .unwrap(); | ||
| assert!(!s.enabled); | ||
| } | ||
|
|
||
| #[test] | ||
| fn deserialize_defaults_when_fields_absent() { | ||
| let s: RateLimitSection = serde_json::from_value(json!({})).unwrap(); | ||
| assert!(!s.enabled); | ||
| assert_eq!(s.burst_size, 100); | ||
| assert_eq!(s.replenish_rate_per_second, 10); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
we should add validation for both values to be [1, 100000] - this is defined in the ADR
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.
Done — added the
[1, 100000]bound for bothburst_sizeandreplenish_rate_per_second(ADR-0022 config-bounds table). Enforced inbuild_limiter/from_configas a fail-hard startup error alongside the existing zero check, via a smallvalidated_scalarhelper. I kept it there rather than a field-levelvalidatorrange(min=1, max=100000)because a disabled section must be allowed to carry out-of-range/zero values without aborting startup (existing invariant + tests) — a field-level range would fire unconditionally. Added tests for the upper bound and the 100000 boundary; field docs updated.