[Rust Frontend] Support API key authentication (#44321)

Signed-off-by: RickyChen / 陳昭儒 <ricky.chen@infinirc.com>
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
Co-authored-by: Bugen Zhao <i@bugenzhao.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Chao-Ju Chen
2026-06-09 10:15:20 +00:00
committed by GitHub
co-authored by Bugen Zhao mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent fff9210b2a
commit d841386d27
14 changed files with 384 additions and 15 deletions
+25 -1
View File
@@ -16,6 +16,7 @@ use educe::Educe;
use serde::Deserialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
use serde_with::{DefaultOnNull, OneOrMany, serde_as};
use thiserror_ext::AsReport as _;
use uuid::Uuid;
use vllm_engine_core_client::TransportMode;
@@ -84,6 +85,7 @@ pub enum Command {
}
/// Runtime arguments shared by the external-engine and managed-engine paths.
#[serde_as]
#[derive(Educe, Clone, Args, PartialEq, Eq, Deserialize)]
#[educe(Debug)]
pub struct SharedRuntimeArgs {
@@ -178,6 +180,14 @@ pub struct SharedRuntimeArgs {
#[serde(default)]
pub enable_request_id_headers: bool,
/// If provided, the server will require one of these keys to be presented
/// in the Authorization header.
#[educe(Debug(ignore))]
#[arg(long, env = "VLLM_API_KEY", value_delimiter = ' ')]
#[serde_as(as = "DefaultOnNull<OneOrMany<_>>")]
#[serde(default)]
pub api_key: Vec<String>,
/// Disable periodic logging of engine statistics (throughput, queue depth,
/// cache usage).
#[arg(long)]
@@ -215,6 +225,15 @@ impl SharedRuntimeArgs {
Duration::from_secs(self.shutdown_timeout)
}
/// Apply fallback logic for API key configuration from env variables.
fn apply_env_api_key_fallback(&mut self) {
if self.api_key.is_empty()
&& let Ok(api_key) = std::env::var("VLLM_API_KEY")
{
self.api_key.push(api_key);
}
}
/// Build the OpenAI-server config for the Python-bootstrap worker contract.
///
/// The resulting config binds the Python-supplied transport addresses and
@@ -253,6 +272,7 @@ impl SharedRuntimeArgs {
chat_template_content_format: self.chat_template_content_format,
enable_log_requests: self.enable_log_requests,
enable_request_id_headers: self.enable_request_id_headers,
api_keys: self.api_key,
disable_log_stats: self.disable_log_stats,
grpc_port: self.grpc_port,
shutdown_timeout,
@@ -295,6 +315,7 @@ impl SharedRuntimeArgs {
chat_template_content_format: self.chat_template_content_format,
enable_log_requests: self.enable_log_requests,
enable_request_id_headers: self.enable_request_id_headers,
api_keys: self.api_key,
disable_log_stats: self.disable_log_stats,
grpc_port: self.grpc_port,
shutdown_timeout,
@@ -311,8 +332,11 @@ fn parse_json<T: DeserializeOwned>(value: &str) -> Result<T, String> {
}
fn parse_runtime_args_json(value: &str) -> Result<SharedRuntimeArgs, String> {
let args: SharedRuntimeArgs = serde_json::from_str(value)
let mut args: SharedRuntimeArgs = serde_json::from_str(value)
.map_err(|e| format!("invalid JSON arguments: {}", e.as_report()))?;
// --args-json is parsed with serde, so clap's env support does not run for
// the Python-supervised frontend path.
args.apply_env_api_key_fallback();
args.unsupported.check()?;
Ok(args)
}
+78 -4
View File
@@ -169,6 +169,76 @@ fn frontend_args_json_passes_enable_request_id_headers_into_config() {
assert!(config.enable_request_id_headers);
}
#[test]
fn serve_passes_api_keys_into_config() {
let cli = Cli::try_parse_from([
"vllm-rs",
"serve",
"Qwen/Qwen3-0.6B",
"--api-key",
"secret-a",
"--api-key",
"secret-b",
])
.unwrap();
let Command::Serve(args) = cli.command else {
panic!("expected serve args");
};
let config = args.to_frontend_config("tcp://127.0.0.1:62100".to_string());
assert_eq!(config.api_keys, vec!["secret-a", "secret-b"]);
let debug = format!("{config:#?}");
assert!(debug.contains("api_keys: [<redacted>; 2]"));
assert!(!debug.contains("secret-a"));
assert!(!debug.contains("secret-b"));
}
#[test]
fn frontend_args_json_accepts_api_key_string() {
let cli = Cli::try_parse_from([
"vllm-rs",
"frontend",
"--listen-fd",
"3",
"--input-address",
"ipc:///tmp/input.sock",
"--output-address",
"ipc:///tmp/output.sock",
"--args-json",
r#"{"model_tag":"Qwen/Qwen3-0.6B","api_key":"secret"}"#,
])
.unwrap();
let Command::Frontend(args) = cli.command else {
panic!("expected frontend args");
};
let config = args.into_config();
assert_eq!(config.api_keys, vec!["secret"]);
}
#[test]
fn frontend_args_json_accepts_api_key_list() {
let cli = Cli::try_parse_from([
"vllm-rs",
"frontend",
"--listen-fd",
"3",
"--input-address",
"ipc:///tmp/input.sock",
"--output-address",
"ipc:///tmp/output.sock",
"--args-json",
r#"{"model_tag":"Qwen/Qwen3-0.6B","api_key":["secret-a","secret-b"]}"#,
])
.unwrap();
let Command::Frontend(args) = cli.command else {
panic!("expected frontend args");
};
let config = args.into_config();
assert_eq!(config.api_keys, vec!["secret-a", "secret-b"]);
}
#[test]
fn serve_args_reject_unknown_renderer_value() {
let error = Cli::try_parse_from([
@@ -446,20 +516,21 @@ fn frontend_args_json_aggregates_multiple_unsupported_fields() {
"--output-address",
"ipc:///tmp/output.sock",
"--args-json",
r#"{"model_tag":"Qwen/Qwen3-0.6B","allow_credentials":true,"api_key":"secret"}"#,
r#"{"model_tag":"Qwen/Qwen3-0.6B","allow_credentials":true,"ssl_keyfile":"/tmp/key.pem"}"#,
])
.unwrap_err();
let actual = error.to_string().replace(": \n", ":\n");
expect![[r#"
error: invalid value '{"model_tag":"Qwen/Qwen3-0.6B","allow_credentials":true,"api_key":"secret"}' for '--args-json <JSON>':
error: invalid value '{"model_tag":"Qwen/Qwen3-0.6B","allow_credentials":true,"ssl_keyfile":"/tmp/key.pem"}' for '--args-json <JSON>':
The following arguments are not implemented in Rust frontend yet:
- allow_credentials
- api_key
- ssl_keyfile
Remove these arguments to continue.
For more information, try '--help'.
"#]].assert_eq(&error.to_string());
"#]].assert_eq(&actual);
}
#[test]
@@ -793,6 +864,7 @@ fn serve_frontend_config_uses_dp_address_as_advertised_host() {
chat_template_content_format: Auto,
enable_log_requests: false,
enable_request_id_headers: false,
api_keys: [],
disable_log_stats: false,
grpc_port: None,
shutdown_timeout: 0ns,
@@ -857,6 +929,7 @@ fn serve_frontend_config_keeps_tcp_transport_for_non_local_only_topology() {
chat_template_content_format: Auto,
enable_log_requests: false,
enable_request_id_headers: false,
api_keys: [],
disable_log_stats: false,
grpc_port: None,
shutdown_timeout: 0ns,
@@ -936,6 +1009,7 @@ fn frontend_config_uses_external_coordinator_when_coordinator_address_is_present
chat_template_content_format: Auto,
enable_log_requests: false,
enable_request_id_headers: false,
api_keys: [],
disable_log_stats: false,
grpc_port: None,
shutdown_timeout: 0ns,
-5
View File
@@ -564,11 +564,6 @@ pub struct ServerUnsupportedArgs {
#[arg(long)]
pub allowed_headers: Option<Unsupported>,
/// If provided, the server will require one of these keys to be presented
/// in the header.
#[arg(long)]
pub api_key: Option<Unsupported>,
/// The file path to the SSL key file.
#[arg(long)]
pub ssl_keyfile: Option<Unsupported>,