web_server.rs: Log X-Forwarded-For IP #504

Merged
lx merged 3 commits from jpds/garage:web_server-log-x-forwarded-for into main 2023-03-06 12:33:07 +00:00
Contributor
No description provided.
jpds force-pushed web_server-log-x-forwarded-for from b8be8b6af4 to e840c9b5b8 2023-02-11 15:18:05 +00:00 Compare
Owner

Do you confirm this work is the continuation of #500 but for the web endpoint?

Do you confirm this work is the continuation of #500 but for the web endpoint?
Author
Contributor

@quentin Indeed, it is.

@quentin Indeed, it is.
lx requested changes 2023-03-01 19:05:01 +00:00
lx left a comment
Owner

Proposing some changes to make code more consise and better exploit Rust's type system

Proposing some changes to make code more consise and better exploit Rust's type system
@ -135,2 +132,2 @@
.to_str()
.unwrap_or_default();
let forwarded_for_ip_addr =
forwarded_headers::handle_forwarded_for_headers(&req.headers());
Owner

With handle_forwarded_for_headers returning a Result (see below), lines 130-133 should be rewritten as follows:

if let Ok(forwarded_for_ip_addr) = forwarded_headers::handle_forwarded_for_headers(&req.headers()) {

  info!(...)
With `handle_forwarded_for_headers` returning a `Result` (see below), lines 130-133 should be rewritten as follows: ```rust if let Ok(forwarded_for_ip_addr) = forwarded_headers::handle_forwarded_for_headers(&req.headers()) { info!(...) ```
jpds marked this conversation as resolved
@ -0,0 +1,42 @@
use http::{HeaderMap, HeaderValue};
pub fn handle_forwarded_for_headers(headers: &HeaderMap<HeaderValue>) -> String {
Owner

This function should return a Result<String, Error>, which is Ok if the value exists and passes all the checks, and Err otherwise.

This function should return a `Result<String, Error>`, which is `Ok` if the value exists and passes all the checks, and `Err` otherwise.
jpds marked this conversation as resolved
@ -108,0 +108,4 @@
let has_forwarded_for_header = req.headers().contains_key("x-forwarded-for");
if has_forwarded_for_header {
let forwarded_for_ip_addr =
forwarded_headers::handle_forwarded_for_headers(&req.headers());
Owner

Same as above, lines 108-111 should be rewritten as:

if let Ok(forwarded_for_ip_addr) = forwarded_headers::handle_forwarded_for_headers(&req.headers()) {

  info!(...)
Same as above, lines 108-111 should be rewritten as: ```rust if let Ok(forwarded_for_ip_addr) = forwarded_headers::handle_forwarded_for_headers(&req.headers()) { info!(...) ```
jpds marked this conversation as resolved
jpds force-pushed web_server-log-x-forwarded-for from e840c9b5b8 to b65af6df86 2023-03-03 14:33:54 +00:00 Compare
jpds force-pushed web_server-log-x-forwarded-for from b65af6df86 to f65beea554 2023-03-03 15:38:07 +00:00 Compare
jpds force-pushed web_server-log-x-forwarded-for from f65beea554 to 401a737acf 2023-03-03 15:40:55 +00:00 Compare
jpds force-pushed web_server-log-x-forwarded-for from 401a737acf to 9f026396f6 2023-03-03 17:24:45 +00:00 Compare
jpds force-pushed web_server-log-x-forwarded-for from 9f026396f6 to 872ffe68bd 2023-03-03 19:18:11 +00:00 Compare
lx requested changes 2023-03-06 10:19:33 +00:00
@ -128,20 +129,17 @@ impl<A: ApiHandler> ApiServer<A> {
let has_forwarded_for_header = req.headers().contains_key("x-forwarded-for");
if has_forwarded_for_header {
Owner

You don't need lines 130-131, just use the second if (the if let statement) with the else branch of the first one.

You don't need lines 130-131, just use the second if (the `if let` statement) with the else branch of the first one.
@ -106,2 +107,3 @@
) -> Result<Response<Body>, Infallible> {
info!("{} {} {}", addr, req.method(), req.uri());
let has_forwarded_for_header = req.headers().contains_key("x-forwarded-for");
if has_forwarded_for_header {
Owner

Same here

Same here
jpds force-pushed web_server-log-x-forwarded-for from 872ffe68bd to 4e0fc3d6c9 2023-03-06 11:44:13 +00:00 Compare
Owner

Thanks!

Thanks!
lx merged commit 00dcfc97a5 into main 2023-03-06 12:33:07 +00:00
jpds deleted branch web_server-log-x-forwarded-for 2023-03-06 12:36:17 +00:00
Sign in to join this conversation.
No description provided.