mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 01:10:23 +08:00
Clean up gateway API key whitelist handlers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use super::super::super::{
|
||||
build_admin_users_bad_request_response, build_admin_users_read_only_response,
|
||||
normalize_admin_feature_settings, normalize_admin_user_allowed_ips, AdminUpdateUserApiKeyRequest,
|
||||
normalize_admin_feature_settings, normalize_admin_user_allowed_ips,
|
||||
AdminUpdateUserApiKeyRequest,
|
||||
};
|
||||
use super::super::helpers::{
|
||||
attach_audit_response, build_admin_user_api_key_detail_payload,
|
||||
|
||||
@@ -56,7 +56,8 @@ use self::shared::{
|
||||
};
|
||||
pub(crate) use self::shared::{
|
||||
normalize_admin_list_policy_mode, normalize_admin_rate_limit_policy_mode,
|
||||
normalize_admin_user_allowed_ips, normalize_admin_user_api_formats, normalize_admin_user_string_list,
|
||||
normalize_admin_user_allowed_ips, normalize_admin_user_api_formats,
|
||||
normalize_admin_user_string_list,
|
||||
};
|
||||
pub(crate) use crate::handlers::shared::normalize_feature_settings as normalize_admin_feature_settings;
|
||||
|
||||
|
||||
@@ -228,7 +228,9 @@ fn users_me_validate_ip_or_cidr(value: &str) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_users_me_allowed_ips(values: Option<Vec<String>>) -> Result<Option<Vec<String>>, String> {
|
||||
fn normalize_users_me_allowed_ips(
|
||||
values: Option<Vec<String>>,
|
||||
) -> Result<Option<Vec<String>>, String> {
|
||||
let Some(values) = values else {
|
||||
return Ok(None);
|
||||
};
|
||||
@@ -682,33 +684,6 @@ pub(super) async fn handle_users_me_api_key_create(
|
||||
.into_response()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::normalize_users_me_allowed_ips;
|
||||
|
||||
#[test]
|
||||
fn normalize_allowed_ips_trims_ip_and_cidr_values() {
|
||||
let values = normalize_users_me_allowed_ips(Some(vec![
|
||||
" 203.0.113.10 ".to_string(),
|
||||
"10.0.0.0/24".to_string(),
|
||||
]))
|
||||
.expect("valid whitelist should normalize");
|
||||
|
||||
assert_eq!(
|
||||
values,
|
||||
Some(vec!["203.0.113.10".to_string(), "10.0.0.0/24".to_string()]),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_allowed_ips_rejects_invalid_cidr() {
|
||||
let err = normalize_users_me_allowed_ips(Some(vec!["10.0.0.0/99".to_string()]))
|
||||
.expect_err("invalid cidr should fail");
|
||||
|
||||
assert_eq!(err, "无效的 IP 地址或 CIDR: 10.0.0.0/99");
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn handle_users_me_api_key_update(
|
||||
state: &AppState,
|
||||
request_context: &GatewayPublicRequestContext,
|
||||
@@ -1152,3 +1127,30 @@ pub(super) async fn handle_users_me_api_key_capabilities_put(
|
||||
}))
|
||||
.into_response()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::normalize_users_me_allowed_ips;
|
||||
|
||||
#[test]
|
||||
fn normalize_allowed_ips_trims_ip_and_cidr_values() {
|
||||
let values = normalize_users_me_allowed_ips(Some(vec![
|
||||
" 203.0.113.10 ".to_string(),
|
||||
"10.0.0.0/24".to_string(),
|
||||
]))
|
||||
.expect("valid whitelist should normalize");
|
||||
|
||||
assert_eq!(
|
||||
values,
|
||||
Some(vec!["203.0.113.10".to_string(), "10.0.0.0/24".to_string()]),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_allowed_ips_rejects_invalid_cidr() {
|
||||
let err = normalize_users_me_allowed_ips(Some(vec!["10.0.0.0/99".to_string()]))
|
||||
.expect_err("invalid cidr should fail");
|
||||
|
||||
assert_eq!(err, "无效的 IP 地址或 CIDR: 10.0.0.0/99");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user