-
Notifications
You must be signed in to change notification settings - Fork 71
Add a task to clean up expired JWT failure rate limits #380
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,70 @@ | ||
| use std::{collections::HashMap, time::Duration}; | ||
|
|
||
| use alloy::primitives::b256; | ||
| use cb_common::{ | ||
| commit::constants::GET_PUBKEYS_PATH, | ||
| config::{ModuleSigningConfig, load_module_signing_configs}, | ||
| types::ModuleId, | ||
| utils::create_jwt, | ||
| }; | ||
| use cb_tests::{ | ||
| signer_service::start_server, | ||
| utils::{self}, | ||
| }; | ||
| use eyre::Result; | ||
| use reqwest::StatusCode; | ||
|
|
||
| const JWT_MODULE: &str = "test-module"; | ||
| const JWT_SECRET: &str = "test-jwt-secret"; | ||
| const ADMIN_SECRET: &str = "test-admin-secret"; | ||
|
|
||
| async fn create_mod_signing_configs() -> HashMap<ModuleId, ModuleSigningConfig> { | ||
| let mut cfg = | ||
| utils::get_commit_boost_config(utils::get_pbs_static_config(utils::get_pbs_config(0))); | ||
|
|
||
| let module_id = ModuleId(JWT_MODULE.to_string()); | ||
| let signing_id = b256!("0101010101010101010101010101010101010101010101010101010101010101"); | ||
|
|
||
| cfg.modules = Some(vec![utils::create_module_config(module_id.clone(), signing_id)]); | ||
|
|
||
| let jwts = HashMap::from([(module_id.clone(), JWT_SECRET.to_string())]); | ||
|
|
||
| load_module_signing_configs(&cfg, &jwts).unwrap() | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[tracing_test::traced_test] | ||
| async fn test_signer_jwt_fail_cleanup() -> Result<()> { | ||
| // setup_test_env() isn't used because we want to capture logs with tracing_test | ||
| let module_id = ModuleId(JWT_MODULE.to_string()); | ||
| let mod_cfgs = create_mod_signing_configs().await; | ||
| let start_config = start_server(20102, &mod_cfgs, ADMIN_SECRET.to_string(), false).await?; | ||
| let mod_cfg = mod_cfgs.get(&module_id).expect("JWT config for test module not found"); | ||
|
|
||
| // Run as many pubkeys requests as the fail limit | ||
| let jwt = create_jwt(&module_id, "incorrect secret", GET_PUBKEYS_PATH, None)?; | ||
| let client = reqwest::Client::new(); | ||
| let url = format!("http://{}{}", start_config.endpoint, GET_PUBKEYS_PATH); | ||
| for _ in 0..start_config.jwt_auth_fail_limit { | ||
| let response = client.get(&url).bearer_auth(&jwt).send().await?; | ||
| assert!(response.status() == StatusCode::UNAUTHORIZED); | ||
| } | ||
|
|
||
| // Run another request - this should fail due to rate limiting now | ||
| let jwt = create_jwt(&module_id, &mod_cfg.jwt_secret, GET_PUBKEYS_PATH, None)?; | ||
| let response = client.get(&url).bearer_auth(&jwt).send().await?; | ||
| assert!(response.status() == StatusCode::TOO_MANY_REQUESTS); | ||
|
|
||
| // Wait until the cleanup task should have run properly, takes a while for the | ||
| // timing to work out | ||
| tokio::time::sleep(Duration::from_secs( | ||
| (start_config.jwt_auth_fail_timeout_seconds * 3) as u64, | ||
| )) | ||
| .await; | ||
|
|
||
| // Make sure the cleanup message was logged - it's all internal state so without | ||
| // refactoring or exposing it, this is the easiest way to check if it triggered | ||
| assert!(logs_contain("Cleaned up 1 old JWT auth failure entries")); | ||
|
|
||
| Ok(()) | ||
| } |
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.
Should we also add this note to
config.example.toml?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 in a39fb31.