]>
Commit | Line | Data |
---|---|---|
16b48b81 | 1 | use std::collections::HashMap; |
2ab5acac DC |
2 | use std::path::PathBuf; |
3 | use std::time::SystemTime; | |
4 | use std::fs::metadata; | |
fe4cc5b1 | 5 | use std::sync::{Arc, Mutex, RwLock}; |
16b48b81 | 6 | |
2ab5acac | 7 | use anyhow::{bail, Error, format_err}; |
7fa9a37c DM |
8 | use hyper::{Method, Body, Response}; |
9 | use hyper::http::request::Parts; | |
10 | ||
f9e3b110 | 11 | use handlebars::Handlebars; |
2ab5acac | 12 | use serde::Serialize; |
16b48b81 | 13 | |
a2479cfa | 14 | use proxmox::api::{ApiMethod, Router, RpcEnvironmentType}; |
8e7e2223 TL |
15 | use proxmox::tools::fs::{create_path, CreateOptions}; |
16 | ||
fd6d2438 | 17 | use crate::{ApiAuth, FileLogger, FileLogOptions, CommandoSocket}; |
a2479cfa | 18 | |
7fa9a37c DM |
19 | pub type GetIndexFn = fn(Option<String>, Option<String>, &ApiConfig, Parts) -> Response<Body>; |
20 | ||
16b48b81 DM |
21 | pub struct ApiConfig { |
22 | basedir: PathBuf, | |
e63e99d6 | 23 | router: &'static Router, |
16b48b81 | 24 | aliases: HashMap<String, PathBuf>, |
02c7a755 | 25 | env_type: RpcEnvironmentType, |
2ab5acac DC |
26 | templates: RwLock<Handlebars<'static>>, |
27 | template_files: RwLock<HashMap<String, (SystemTime, PathBuf)>>, | |
fe4cc5b1 | 28 | request_log: Option<Arc<Mutex<FileLogger>>>, |
36b7085e | 29 | auth_log: Option<Arc<Mutex<FileLogger>>>, |
26858dba | 30 | pub api_auth: Arc<dyn ApiAuth + Send + Sync>, |
7fa9a37c | 31 | get_index_fn: GetIndexFn, |
16b48b81 DM |
32 | } |
33 | ||
34 | impl ApiConfig { | |
26858dba SR |
35 | pub fn new<B: Into<PathBuf>>( |
36 | basedir: B, | |
37 | router: &'static Router, | |
38 | env_type: RpcEnvironmentType, | |
39 | api_auth: Arc<dyn ApiAuth + Send + Sync>, | |
7fa9a37c | 40 | get_index_fn: GetIndexFn, |
26858dba | 41 | ) -> Result<Self, Error> { |
f9e3b110 | 42 | Ok(Self { |
2ab5acac | 43 | basedir: basedir.into(), |
653b1ca1 | 44 | router, |
16b48b81 | 45 | aliases: HashMap::new(), |
02c7a755 | 46 | env_type, |
2ab5acac DC |
47 | templates: RwLock::new(Handlebars::new()), |
48 | template_files: RwLock::new(HashMap::new()), | |
8e7e2223 | 49 | request_log: None, |
36b7085e | 50 | auth_log: None, |
26858dba | 51 | api_auth, |
7fa9a37c | 52 | get_index_fn, |
26858dba | 53 | }) |
16b48b81 DM |
54 | } |
55 | ||
7fa9a37c DM |
56 | pub fn get_index( |
57 | &self, | |
58 | auth_id: Option<String>, | |
59 | language: Option<String>, | |
60 | parts: Parts, | |
61 | ) -> Response<Body> { | |
62 | (self.get_index_fn)(auth_id, language, self, parts) | |
63 | } | |
64 | ||
255f378a DM |
65 | pub fn find_method( |
66 | &self, | |
67 | components: &[&str], | |
68 | method: Method, | |
69 | uri_param: &mut HashMap<String, String>, | |
70 | ) -> Option<&'static ApiMethod> { | |
16b48b81 | 71 | |
01bf3b7b | 72 | self.router.find_method(components, method, uri_param) |
16b48b81 DM |
73 | } |
74 | ||
75 | pub fn find_alias(&self, components: &[&str]) -> PathBuf { | |
76 | ||
77 | let mut prefix = String::new(); | |
78 | let mut filename = self.basedir.clone(); | |
79 | let comp_len = components.len(); | |
80 | if comp_len >= 1 { | |
81 | prefix.push_str(components[0]); | |
82 | if let Some(subdir) = self.aliases.get(&prefix) { | |
83 | filename.push(subdir); | |
382f10a0 | 84 | components.iter().skip(1).for_each(|comp| filename.push(comp)); |
8adbdb0a | 85 | } else { |
382f10a0 | 86 | components.iter().for_each(|comp| filename.push(comp)); |
16b48b81 DM |
87 | } |
88 | } | |
89 | filename | |
90 | } | |
91 | ||
92 | pub fn add_alias<S, P>(&mut self, alias: S, path: P) | |
93 | where S: Into<String>, | |
94 | P: Into<PathBuf>, | |
95 | { | |
96 | self.aliases.insert(alias.into(), path.into()); | |
97 | } | |
02c7a755 DM |
98 | |
99 | pub fn env_type(&self) -> RpcEnvironmentType { | |
100 | self.env_type | |
101 | } | |
2ab5acac DC |
102 | |
103 | pub fn register_template<P>(&self, name: &str, path: P) -> Result<(), Error> | |
104 | where | |
105 | P: Into<PathBuf> | |
106 | { | |
107 | if self.template_files.read().unwrap().contains_key(name) { | |
108 | bail!("template already registered"); | |
109 | } | |
110 | ||
111 | let path: PathBuf = path.into(); | |
112 | let metadata = metadata(&path)?; | |
113 | let mtime = metadata.modified()?; | |
114 | ||
115 | self.templates.write().unwrap().register_template_file(name, &path)?; | |
116 | self.template_files.write().unwrap().insert(name.to_string(), (mtime, path)); | |
117 | ||
118 | Ok(()) | |
119 | } | |
120 | ||
121 | /// Checks if the template was modified since the last rendering | |
122 | /// if yes, it loads a the new version of the template | |
123 | pub fn render_template<T>(&self, name: &str, data: &T) -> Result<String, Error> | |
124 | where | |
125 | T: Serialize, | |
126 | { | |
127 | let path; | |
128 | let mtime; | |
129 | { | |
130 | let template_files = self.template_files.read().unwrap(); | |
131 | let (old_mtime, old_path) = template_files.get(name).ok_or_else(|| format_err!("template not found"))?; | |
132 | ||
133 | mtime = metadata(old_path)?.modified()?; | |
134 | if mtime <= *old_mtime { | |
135 | return self.templates.read().unwrap().render(name, data).map_err(|err| format_err!("{}", err)); | |
136 | } | |
137 | path = old_path.to_path_buf(); | |
138 | } | |
139 | ||
140 | { | |
141 | let mut template_files = self.template_files.write().unwrap(); | |
142 | let mut templates = self.templates.write().unwrap(); | |
143 | ||
144 | templates.register_template_file(name, &path)?; | |
145 | template_files.insert(name.to_string(), (mtime, path)); | |
146 | ||
147 | templates.render(name, data).map_err(|err| format_err!("{}", err)) | |
148 | } | |
149 | } | |
8e7e2223 | 150 | |
fe4cc5b1 TL |
151 | pub fn enable_file_log<P>( |
152 | &mut self, | |
153 | path: P, | |
fd6d2438 DM |
154 | dir_opts: Option<CreateOptions>, |
155 | file_opts: Option<CreateOptions>, | |
156 | commando_sock: &mut CommandoSocket, | |
fe4cc5b1 | 157 | ) -> Result<(), Error> |
8e7e2223 TL |
158 | where |
159 | P: Into<PathBuf> | |
160 | { | |
161 | let path: PathBuf = path.into(); | |
162 | if let Some(base) = path.parent() { | |
163 | if !base.exists() { | |
fd6d2438 | 164 | create_path(base, None, dir_opts).map_err(|err| format_err!("{}", err))?; |
8e7e2223 TL |
165 | } |
166 | } | |
167 | ||
168 | let logger_options = FileLogOptions { | |
169 | append: true, | |
fd6d2438 | 170 | file_opts: file_opts.unwrap_or(CreateOptions::default()), |
8e7e2223 TL |
171 | ..Default::default() |
172 | }; | |
fe4cc5b1 TL |
173 | let request_log = Arc::new(Mutex::new(FileLogger::new(&path, logger_options)?)); |
174 | self.request_log = Some(Arc::clone(&request_log)); | |
175 | ||
176 | commando_sock.register_command("api-access-log-reopen".into(), move |_args| { | |
36b7085e | 177 | println!("re-opening access-log file"); |
fe4cc5b1 TL |
178 | request_log.lock().unwrap().reopen()?; |
179 | Ok(serde_json::Value::Null) | |
180 | })?; | |
8e7e2223 TL |
181 | |
182 | Ok(()) | |
183 | } | |
fe4cc5b1 | 184 | |
36b7085e DM |
185 | pub fn enable_auth_log<P>( |
186 | &mut self, | |
187 | path: P, | |
188 | dir_opts: Option<CreateOptions>, | |
189 | file_opts: Option<CreateOptions>, | |
190 | commando_sock: &mut CommandoSocket, | |
191 | ) -> Result<(), Error> | |
192 | where | |
193 | P: Into<PathBuf> | |
194 | { | |
195 | let path: PathBuf = path.into(); | |
196 | if let Some(base) = path.parent() { | |
197 | if !base.exists() { | |
198 | create_path(base, None, dir_opts).map_err(|err| format_err!("{}", err))?; | |
199 | } | |
200 | } | |
201 | ||
202 | let logger_options = FileLogOptions { | |
203 | append: true, | |
204 | prefix_time: true, | |
205 | file_opts: file_opts.unwrap_or(CreateOptions::default()), | |
206 | ..Default::default() | |
207 | }; | |
208 | let auth_log = Arc::new(Mutex::new(FileLogger::new(&path, logger_options)?)); | |
209 | self.auth_log = Some(Arc::clone(&auth_log)); | |
210 | ||
211 | commando_sock.register_command("api-auth-log-reopen".into(), move |_args| { | |
212 | println!("re-opening auth-log file"); | |
213 | auth_log.lock().unwrap().reopen()?; | |
214 | Ok(serde_json::Value::Null) | |
215 | })?; | |
216 | ||
217 | Ok(()) | |
218 | } | |
219 | ||
220 | pub fn get_access_log(&self) -> Option<&Arc<Mutex<FileLogger>>> { | |
8e7e2223 TL |
221 | self.request_log.as_ref() |
222 | } | |
36b7085e DM |
223 | |
224 | pub fn get_auth_log(&self) -> Option<&Arc<Mutex<FileLogger>>> { | |
225 | self.auth_log.as_ref() | |
226 | } | |
16b48b81 | 227 | } |