Skip to content

Commit 0aaf8cc

Browse files
committed
chore(anda_engine_server): support x-api-key
1 parent 0ba729a commit 0aaf8cc

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

agents/anda_assistant/src/agent.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ impl Agent<AgentCtx> for Assistant {
210210
"type": "Person",
211211
"name": caller.to_string(),
212212
"attributes": {},
213+
"metadata": {},
213214
})
214215
});
215216

anda_engine_server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "anda_engine_server"
33
description = "A http server to serve multiple Anda engines."
44
repository = "https://github.com/ldclabs/anda/tree/main/anda_engine_server"
55
publish = true
6-
version.workspace = true
6+
version = "0.9.3"
77
edition.workspace = true
88
keywords.workspace = true
99
categories.workspace = true

anda_engine_server/src/handler.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ pub struct AppState {
2626
pub(crate) engines: Arc<BTreeMap<Principal, Engine>>,
2727
pub(crate) default_engine: Principal,
2828
pub(crate) start_time_ms: u64,
29+
pub(crate) api_key: Option<String>,
30+
}
31+
32+
impl AppState {
33+
pub fn check_api_key(&self, headers: &http::HeaderMap) -> Result<(), String> {
34+
if let Some(expected_key) = &self.api_key {
35+
match headers.get("x-api-key") {
36+
Some(provided_key) if provided_key == expected_key => Ok(()),
37+
_ => Err("missing or invalid x-api-key in headers".to_string()),
38+
}
39+
} else {
40+
Ok(())
41+
}
42+
}
2943
}
3044

3145
/// GET /.well-known/information
@@ -98,6 +112,10 @@ pub async fn anda_engine(
98112
Path(id): Path<String>,
99113
ct: ContentWithSHA3<RPCRequest>,
100114
) -> impl IntoResponse {
115+
if let Err(err) = app.check_api_key(&headers) {
116+
return (StatusCode::UNAUTHORIZED, err).into_response();
117+
}
118+
101119
let id = if &id == "default" {
102120
app.default_engine
103121
} else if let Ok(id) = Principal::from_text(&id) {

anda_engine_server/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct ServerBuilder {
2222
origin: String,
2323
engines: BTreeMap<Principal, Engine>,
2424
default_engine: Option<Principal>,
25+
api_key: Option<String>,
2526
}
2627

2728
impl Default for ServerBuilder {
@@ -42,6 +43,7 @@ impl ServerBuilder {
4243
origin: "https://localhost:8443".to_string(),
4344
engines: BTreeMap::new(),
4445
default_engine: None,
46+
api_key: None,
4547
}
4648
}
4749

@@ -98,6 +100,7 @@ impl ServerBuilder {
98100
engines: Arc::new(self.engines),
99101
default_engine,
100102
start_time_ms: unix_ms(),
103+
api_key: self.api_key,
101104
};
102105
let app = Router::new()
103106
.route("/", routing::get(get_information))

0 commit comments

Comments
 (0)