]> git.proxmox.com Git - proxmox-backup.git/blame - src/server/config.rs
server/rest: add ApiAuth trait to make user auth generic
[proxmox-backup.git] / src / server / config.rs
CommitLineData
16b48b81 1use std::collections::HashMap;
2ab5acac
DC
2use std::path::PathBuf;
3use std::time::SystemTime;
4use std::fs::metadata;
fe4cc5b1 5use std::sync::{Arc, Mutex, RwLock};
16b48b81 6
2ab5acac 7use anyhow::{bail, Error, format_err};
16b48b81 8use hyper::Method;
f9e3b110 9use handlebars::Handlebars;
2ab5acac 10use serde::Serialize;
16b48b81 11
a2479cfa 12use proxmox::api::{ApiMethod, Router, RpcEnvironmentType};
8e7e2223
TL
13use proxmox::tools::fs::{create_path, CreateOptions};
14
15use crate::tools::{FileLogger, FileLogOptions};
26858dba 16use super::auth::ApiAuth;
a2479cfa 17
16b48b81
DM
18pub struct ApiConfig {
19 basedir: PathBuf,
e63e99d6 20 router: &'static Router,
16b48b81 21 aliases: HashMap<String, PathBuf>,
02c7a755 22 env_type: RpcEnvironmentType,
2ab5acac
DC
23 templates: RwLock<Handlebars<'static>>,
24 template_files: RwLock<HashMap<String, (SystemTime, PathBuf)>>,
fe4cc5b1 25 request_log: Option<Arc<Mutex<FileLogger>>>,
40bf636b 26 pub enable_tape_ui: bool,
26858dba 27 pub api_auth: Arc<dyn ApiAuth + Send + Sync>,
16b48b81
DM
28}
29
30impl ApiConfig {
26858dba
SR
31 pub fn new<B: Into<PathBuf>>(
32 basedir: B,
33 router: &'static Router,
34 env_type: RpcEnvironmentType,
35 api_auth: Arc<dyn ApiAuth + Send + Sync>,
36 ) -> Result<Self, Error> {
f9e3b110 37 Ok(Self {
2ab5acac 38 basedir: basedir.into(),
653b1ca1 39 router,
16b48b81 40 aliases: HashMap::new(),
02c7a755 41 env_type,
2ab5acac
DC
42 templates: RwLock::new(Handlebars::new()),
43 template_files: RwLock::new(HashMap::new()),
8e7e2223 44 request_log: None,
40bf636b 45 enable_tape_ui: false,
26858dba
SR
46 api_auth,
47 })
16b48b81
DM
48 }
49
255f378a
DM
50 pub fn find_method(
51 &self,
52 components: &[&str],
53 method: Method,
54 uri_param: &mut HashMap<String, String>,
55 ) -> Option<&'static ApiMethod> {
16b48b81 56
01bf3b7b 57 self.router.find_method(components, method, uri_param)
16b48b81
DM
58 }
59
60 pub fn find_alias(&self, components: &[&str]) -> PathBuf {
61
62 let mut prefix = String::new();
63 let mut filename = self.basedir.clone();
64 let comp_len = components.len();
65 if comp_len >= 1 {
66 prefix.push_str(components[0]);
67 if let Some(subdir) = self.aliases.get(&prefix) {
68 filename.push(subdir);
382f10a0 69 components.iter().skip(1).for_each(|comp| filename.push(comp));
8adbdb0a 70 } else {
382f10a0 71 components.iter().for_each(|comp| filename.push(comp));
16b48b81
DM
72 }
73 }
74 filename
75 }
76
77 pub fn add_alias<S, P>(&mut self, alias: S, path: P)
78 where S: Into<String>,
79 P: Into<PathBuf>,
80 {
81 self.aliases.insert(alias.into(), path.into());
82 }
02c7a755
DM
83
84 pub fn env_type(&self) -> RpcEnvironmentType {
85 self.env_type
86 }
2ab5acac
DC
87
88 pub fn register_template<P>(&self, name: &str, path: P) -> Result<(), Error>
89 where
90 P: Into<PathBuf>
91 {
92 if self.template_files.read().unwrap().contains_key(name) {
93 bail!("template already registered");
94 }
95
96 let path: PathBuf = path.into();
97 let metadata = metadata(&path)?;
98 let mtime = metadata.modified()?;
99
100 self.templates.write().unwrap().register_template_file(name, &path)?;
101 self.template_files.write().unwrap().insert(name.to_string(), (mtime, path));
102
103 Ok(())
104 }
105
106 /// Checks if the template was modified since the last rendering
107 /// if yes, it loads a the new version of the template
108 pub fn render_template<T>(&self, name: &str, data: &T) -> Result<String, Error>
109 where
110 T: Serialize,
111 {
112 let path;
113 let mtime;
114 {
115 let template_files = self.template_files.read().unwrap();
116 let (old_mtime, old_path) = template_files.get(name).ok_or_else(|| format_err!("template not found"))?;
117
118 mtime = metadata(old_path)?.modified()?;
119 if mtime <= *old_mtime {
120 return self.templates.read().unwrap().render(name, data).map_err(|err| format_err!("{}", err));
121 }
122 path = old_path.to_path_buf();
123 }
124
125 {
126 let mut template_files = self.template_files.write().unwrap();
127 let mut templates = self.templates.write().unwrap();
128
129 templates.register_template_file(name, &path)?;
130 template_files.insert(name.to_string(), (mtime, path));
131
132 templates.render(name, data).map_err(|err| format_err!("{}", err))
133 }
134 }
8e7e2223 135
fe4cc5b1
TL
136 pub fn enable_file_log<P>(
137 &mut self,
138 path: P,
139 commando_sock: &mut super::CommandoSocket,
140 ) -> Result<(), Error>
8e7e2223
TL
141 where
142 P: Into<PathBuf>
143 {
144 let path: PathBuf = path.into();
145 if let Some(base) = path.parent() {
146 if !base.exists() {
147 let backup_user = crate::backup::backup_user()?;
148 let opts = CreateOptions::new().owner(backup_user.uid).group(backup_user.gid);
149 create_path(base, None, Some(opts)).map_err(|err| format_err!("{}", err))?;
150 }
151 }
152
153 let logger_options = FileLogOptions {
154 append: true,
c7e18ba0 155 owned_by_backup: true,
8e7e2223
TL
156 ..Default::default()
157 };
fe4cc5b1
TL
158 let request_log = Arc::new(Mutex::new(FileLogger::new(&path, logger_options)?));
159 self.request_log = Some(Arc::clone(&request_log));
160
161 commando_sock.register_command("api-access-log-reopen".into(), move |_args| {
162 println!("re-opening log file");
163 request_log.lock().unwrap().reopen()?;
164 Ok(serde_json::Value::Null)
165 })?;
8e7e2223
TL
166
167 Ok(())
168 }
fe4cc5b1
TL
169
170 pub fn get_file_log(&self) -> Option<&Arc<Mutex<FileLogger>>> {
8e7e2223
TL
171 self.request_log.as_ref()
172 }
16b48b81 173}