Enforce API key IP restrictions in proxy auth

This commit is contained in:
RWDai
2026-05-18 20:47:28 +08:00
parent 276d19b63c
commit fc12cc8a36
4 changed files with 81 additions and 3 deletions

View File

@@ -17,6 +17,7 @@ pub(crate) enum GatewayLocalAuthRejection {
ProviderNotAllowed { provider: String },
ApiFormatNotAllowed { api_format: String },
ModelNotAllowed { model: String },
IpNotAllowed { remote_ip: String },
}
pub(crate) fn trusted_auth_local_rejection(
@@ -566,6 +567,7 @@ mod tests {
admin_bypass_limits: false,
local_rejection: None,
allowed_models: Some(allowed_models),
allowed_ips: None,
});
decision
}

View File

@@ -48,6 +48,8 @@ pub(crate) struct GatewayControlAuthContext {
pub(crate) local_rejection: Option<GatewayLocalAuthRejection>,
#[serde(skip)]
pub(crate) allowed_models: Option<Vec<String>>,
#[serde(skip)]
pub(crate) allowed_ips: Option<Vec<String>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -207,6 +209,9 @@ fn log_local_auth_rejection(trace_id: &str, decision: &GatewayControlDecision) {
GatewayLocalAuthRejection::ModelNotAllowed { model } => {
("model_not_allowed", model.clone())
}
GatewayLocalAuthRejection::IpNotAllowed { remote_ip } => {
("ip_not_allowed", remote_ip.clone())
}
};
info!(
event_name = "local_auth_rejected",
@@ -581,6 +586,7 @@ pub(super) async fn resolve_data_backed_auth_context(
admin_bypass_limits: false,
local_rejection: Some(GatewayLocalAuthRejection::InvalidApiKey),
allowed_models: None,
allowed_ips: None,
}));
};
@@ -638,6 +644,7 @@ async fn resolve_trusted_auth_context(
admin_bypass_limits: false,
local_rejection: Some(GatewayLocalAuthRejection::InvalidApiKey),
allowed_models: None,
allowed_ips: None,
}));
};
@@ -728,6 +735,7 @@ async fn build_data_backed_auth_context(
&& !snapshot.api_key_is_standalone,
local_rejection,
allowed_models,
allowed_ips: snapshot.api_key_allowed_ips,
}
}