Skip to content

Commit 85fbdea

Browse files
add details to invalid api key error in middleware (#1762)
* add details to invalid api key error in middleware add user agent, src ip and dataset name to the error * fix coderabbit comments * add endpoint * use peer_addr instead of remote_addr * update test
1 parent f881f31 commit 85fbdea

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

‎src/handlers/http/middleware.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,34 @@ where
198198
Some(session_id)
199199
}
200200
None => {
201+
let source_ip = req
202+
.peer_addr()
203+
.map(|address| address.ip().to_string())
204+
.unwrap_or_else(|| "unknown".to_string());
205+
let endpoint = req.path();
206+
let user_agent = req
207+
.headers()
208+
.get(header::USER_AGENT)
209+
.and_then(|value| value.to_str().ok())
210+
.unwrap_or_default();
211+
let dataset = req
212+
.match_info()
213+
.get("logstream")
214+
.or_else(|| {
215+
req.headers()
216+
.get(STREAM_NAME_HEADER_KEY)
217+
.and_then(|value| value.to_str().ok())
218+
})
219+
.unwrap_or("unknown");
220+
221+
tracing::warn!(
222+
source_ip,
223+
endpoint,
224+
user_agent,
225+
dataset,
226+
requested_tenant = tenant_id.as_deref().unwrap_or("unknown"),
227+
"Invalid API key"
228+
);
201229
return Box::pin(async { Err(ErrorUnauthorized("Invalid API key")) });
202230
}
203231
}

‎src/handlers/http/modal/utils/ingest_utils.rs‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,13 @@ pub fn get_custom_fields_from_header(req: &HttpRequest) -> HashMap<String, Strin
412412
.and_then(|a| a.to_str().ok())
413413
.unwrap_or_default();
414414

415-
let conn = req.connection_info().clone();
416-
417-
let source_ip = conn.realip_remote_addr().unwrap_or_default();
415+
let source_ip = req
416+
.peer_addr()
417+
.map(|address| address.ip().to_string())
418+
.unwrap_or_else(|| "unknown".to_string());
418419
let mut p_custom_fields = HashMap::new();
419420
p_custom_fields.insert(USER_AGENT_KEY.to_string(), user_agent.to_string());
420-
p_custom_fields.insert(SOURCE_IP_KEY.to_string(), source_ip.to_string());
421+
p_custom_fields.insert(SOURCE_IP_KEY.to_string(), source_ip);
421422

422423
// Iterate through headers and add custom fields
423424
for (header_name, header_value) in req.headers().iter() {
@@ -592,6 +593,17 @@ mod tests {
592593

593594
assert_eq!(custom_fields.len(), 2);
594595
assert_eq!(custom_fields.get(USER_AGENT_KEY).unwrap(), "");
595-
assert_eq!(custom_fields.get(SOURCE_IP_KEY).unwrap(), "");
596+
assert_eq!(custom_fields.get(SOURCE_IP_KEY).unwrap(), "unknown");
597+
}
598+
599+
#[test]
600+
fn test_get_custom_fields_from_header_with_peer_address() {
601+
let req = TestRequest::default()
602+
.peer_addr("192.0.2.1:8080".parse().unwrap())
603+
.to_http_request();
604+
605+
let custom_fields = get_custom_fields_from_header(&req);
606+
607+
assert_eq!(custom_fields.get(SOURCE_IP_KEY).unwrap(), "192.0.2.1");
596608
}
597609
}

0 commit comments

Comments
 (0)