src/server/rest.rs: log failed requests

This commit is contained in:
Dietmar Maurer 2019-02-14 13:07:34 +01:00
parent 2cc90617dd
commit 78a1fa6722
1 changed files with 20 additions and 5 deletions

View File

@ -57,6 +57,18 @@ pub struct ApiService {
pub api_config: Arc<ApiConfig>, pub api_config: Arc<ApiConfig>,
} }
impl ApiService {
fn log_response(path: &str, resp: &Response<Body>) {
let status = resp.status();
if !status.is_success() {
let reason = status.canonical_reason().unwrap_or("unknown reason");
let client = "unknown"; // fixme: howto get peer_addr ?
let message = "request failed";
log::error!("{}: {} {}: [client {}] {}", path, status.as_str(), reason, client, message);
}
}
}
impl Service for ApiService { impl Service for ApiService {
type ReqBody = Body; type ReqBody = Body;
@ -65,18 +77,23 @@ impl Service for ApiService {
type Future = Box<Future<Item = Response<Body>, Error = Self::Error> + Send>; type Future = Box<Future<Item = Response<Body>, Error = Self::Error> + Send>;
fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future { fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future {
let path = req.uri().path().to_owned();
Box::new(handle_request(self.api_config.clone(), req).then(|result| { Box::new(handle_request(self.api_config.clone(), req).then(move |result| {
match result { match result {
Ok(res) => Ok::<_, hyper::Error>(res), Ok(res) => {
Self::log_response(&path, &res);
Ok::<_, hyper::Error>(res)
}
Err(err) => { Err(err) => {
if let Some(apierr) = err.downcast_ref::<HttpError>() { if let Some(apierr) = err.downcast_ref::<HttpError>() {
let mut resp = Response::new(Body::from(apierr.message.clone())); let mut resp = Response::new(Body::from(apierr.message.clone()));
*resp.status_mut() = apierr.code; *resp.status_mut() = apierr.code;
Self::log_response(&path, &resp);
Ok(resp) Ok(resp)
} else { } else {
let mut resp = Response::new(Body::from(err.to_string())); let mut resp = Response::new(Body::from(err.to_string()));
*resp.status_mut() = StatusCode::BAD_REQUEST; *resp.status_mut() = StatusCode::BAD_REQUEST;
Self::log_response(&path, &resp);
Ok(resp) Ok(resp)
} }
} }
@ -458,8 +475,6 @@ pub fn handle_request(api: Arc<ApiConfig>, req: Request<Body>) -> BoxFut {
if let Some(_username) = rpcenv.get_user() { if let Some(_username) = rpcenv.get_user() {
// fixme: check permissions // fixme: check permissions
} else { } else {
println!("Abort UNAUTHORIZED API REQUEST");
// always delay unauthorized calls by 3 seconds (from start of request) // always delay unauthorized calls by 3 seconds (from start of request)
let resp = (formatter.format_error)(http_err!(UNAUTHORIZED, "permission check failed.".into())); let resp = (formatter.format_error)(http_err!(UNAUTHORIZED, "permission check failed.".into()));
let delayed_response = tokio::timer::Delay::new(delay_unauth_time) let delayed_response = tokio::timer::Delay::new(delay_unauth_time)