Add optional API key auth and allowed_hosts enforcement
- AuthConfig (api_key, allowed_hosts) read from the shared config; router_with_auth applies an axum middleware before any handler. - Host not in allowlist -> 403 host_not_allowed; missing/wrong key -> 401 invalid_api_key (accepts Authorization: Bearer <key> or x-api-key); /health open. - router() stays the open default: backward compatible (no key, wildcard hosts). - The non-loopback bind warning now fires only when no api_key is configured. - README updated (auth is a feature, no longer a gap). 4 new tests; 80 green + 2 real-file ignored; clippy clean; cargo fmt applied.
This commit is contained in:
1 parent
5bafd000c8
commit
b6619d30de
6 files changed
+227
-20
No files matched your search
+9
-5
@@ -8,7 +8,7 @@ use std::path::Path;
|
||||
use std::process::ExitCode;
|
||||
use std::sync::Arc;
|
||||
|
||||
use strata_ctl::api::{router, ChatService};
|
||||
use strata_ctl::api::{router_with_auth, AuthConfig, ChatService};
|
||||
use strata_ctl::config::{Config, ConfigError};
|
||||
use strata_ctl::engine::Engine;
|
||||
use strata_ctl::inkling::{ChatTemplate, InklingFormatter, InklingTokenizer};
|
||||
@@ -171,14 +171,18 @@ async fn serve(cfg: &Config, cli_host: Option<&str>, cli_port: Option<u16>) -> E
|
||||
);
|
||||
|
||||
let svc = ChatService::new(engine.clone(), fmt);
|
||||
let app = router(svc);
|
||||
let auth = AuthConfig {
|
||||
api_key: cfg.api_key.clone(),
|
||||
allowed_hosts: cfg.allowed_hosts.clone().unwrap_or_default(),
|
||||
};
|
||||
let app = router_with_auth(svc, auth);
|
||||
let host = cfg.bind_host(cli_host);
|
||||
let port = cfg.bind_port(cli_port);
|
||||
if !is_loopback_host(&host) {
|
||||
if !is_loopback_host(&host) && cfg.api_key.is_none() {
|
||||
tracing::warn!(
|
||||
host = %host,
|
||||
"binding a non-loopback address: the API has NO authentication and \
|
||||
`allowed_hosts` is not enforced — anyone on the network can use it"
|
||||
"binding a non-loopback address with NO API key: anyone on the network \
|
||||
can use the API (set `api_key` in the config to require authentication)"
|
||||
);
|
||||
}
|
||||
let addr = format!("{host}:{port}");
|
||||
|
||||
Reference in new issue
Block a user