server/rest: implement request access log
reuse the FileLogger module in append mode. As it implements write, which is not thread safe (mutable self) and we use it in a async context we need to serialize access using a mutex. Try to use the same format we do in pveproxy, namely the one which is also used in apache or nginx by default. Use the response extensions to pass up the userid, if we extract it from a ticket. The privileged and unprivileged dameons log both to the same file, to have a unified view, and avoiding the need to handle more log files. We avoid extra intra-process locking by reusing the fact that a write smaller than PIPE_BUF (4k on linux) is atomic for files opened with the 'O_APPEND' flag. For now the logged request path is not yet guaranteed to be smaller than that, this will be improved in a future patch. Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
committed by
Dietmar Maurer
parent
081c37cccf
commit
8e7e2223d8
@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::time::SystemTime;
|
||||
use std::fs::metadata;
|
||||
use std::sync::RwLock;
|
||||
use std::sync::{Mutex, RwLock};
|
||||
|
||||
use anyhow::{bail, Error, format_err};
|
||||
use hyper::Method;
|
||||
@ -10,6 +10,9 @@ use handlebars::Handlebars;
|
||||
use serde::Serialize;
|
||||
|
||||
use proxmox::api::{ApiMethod, Router, RpcEnvironmentType};
|
||||
use proxmox::tools::fs::{create_path, CreateOptions};
|
||||
|
||||
use crate::tools::{FileLogger, FileLogOptions};
|
||||
|
||||
pub struct ApiConfig {
|
||||
basedir: PathBuf,
|
||||
@ -18,6 +21,7 @@ pub struct ApiConfig {
|
||||
env_type: RpcEnvironmentType,
|
||||
templates: RwLock<Handlebars<'static>>,
|
||||
template_files: RwLock<HashMap<String, (SystemTime, PathBuf)>>,
|
||||
request_log: Option<Mutex<FileLogger>>,
|
||||
}
|
||||
|
||||
impl ApiConfig {
|
||||
@ -30,6 +34,7 @@ impl ApiConfig {
|
||||
env_type,
|
||||
templates: RwLock::new(Handlebars::new()),
|
||||
template_files: RwLock::new(HashMap::new()),
|
||||
request_log: None,
|
||||
})
|
||||
}
|
||||
|
||||
@ -118,4 +123,29 @@ impl ApiConfig {
|
||||
templates.render(name, data).map_err(|err| format_err!("{}", err))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn enable_file_log<P>(&mut self, path: P) -> Result<(), Error>
|
||||
where
|
||||
P: Into<PathBuf>
|
||||
{
|
||||
let path: PathBuf = path.into();
|
||||
if let Some(base) = path.parent() {
|
||||
if !base.exists() {
|
||||
let backup_user = crate::backup::backup_user()?;
|
||||
let opts = CreateOptions::new().owner(backup_user.uid).group(backup_user.gid);
|
||||
create_path(base, None, Some(opts)).map_err(|err| format_err!("{}", err))?;
|
||||
}
|
||||
}
|
||||
|
||||
let logger_options = FileLogOptions {
|
||||
append: true,
|
||||
..Default::default()
|
||||
};
|
||||
self.request_log = Some(Mutex::new(FileLogger::new(&path, logger_options)?));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
pub fn get_file_log(&self) -> Option<&Mutex<FileLogger>> {
|
||||
self.request_log.as_ref()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user