]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/reader/environment.rs
reader: track index chunks and limit access
[proxmox-backup.git] / src / api2 / reader / environment.rs
1 use std::sync::{Arc,RwLock};
2 use std::collections::HashSet;
3
4 use serde_json::{json, Value};
5
6 use proxmox::api::{RpcEnvironment, RpcEnvironmentType};
7
8 use crate::api2::types::Userid;
9 use crate::backup::*;
10 use crate::server::formatter::*;
11 use crate::server::WorkerTask;
12
13 //use proxmox::tools;
14
15 /// `RpcEnvironmet` implementation for backup reader service
16 #[derive(Clone)]
17 pub struct ReaderEnvironment {
18 env_type: RpcEnvironmentType,
19 result_attributes: Value,
20 user: Userid,
21 pub debug: bool,
22 pub formatter: &'static OutputFormatter,
23 pub worker: Arc<WorkerTask>,
24 pub datastore: Arc<DataStore>,
25 pub backup_dir: BackupDir,
26 allowed_chunks: Arc<RwLock<HashSet<[u8;32]>>>,
27 }
28
29 impl ReaderEnvironment {
30 pub fn new(
31 env_type: RpcEnvironmentType,
32 user: Userid,
33 worker: Arc<WorkerTask>,
34 datastore: Arc<DataStore>,
35 backup_dir: BackupDir,
36 ) -> Self {
37
38
39 Self {
40 result_attributes: json!({}),
41 env_type,
42 user,
43 worker,
44 datastore,
45 debug: false,
46 formatter: &JSON_FORMATTER,
47 backup_dir,
48 allowed_chunks: Arc::new(RwLock::new(HashSet::new())),
49 }
50 }
51
52 pub fn log<S: AsRef<str>>(&self, msg: S) {
53 self.worker.log(msg);
54 }
55
56 pub fn debug<S: AsRef<str>>(&self, msg: S) {
57 if self.debug { self.worker.log(msg); }
58 }
59
60
61 pub fn register_chunk(&self, digest: [u8;32]) {
62 let mut allowed_chunks = self.allowed_chunks.write().unwrap();
63 allowed_chunks.insert(digest);
64 }
65
66 pub fn check_chunk_access(&self, digest: [u8;32]) -> bool {
67 self.allowed_chunks.read().unwrap().contains(&digest)
68 }
69 }
70
71 impl RpcEnvironment for ReaderEnvironment {
72
73 fn result_attrib_mut(&mut self) -> &mut Value {
74 &mut self.result_attributes
75 }
76
77 fn result_attrib(&self) -> &Value {
78 &self.result_attributes
79 }
80
81 fn env_type(&self) -> RpcEnvironmentType {
82 self.env_type
83 }
84
85 fn set_user(&mut self, _user: Option<String>) {
86 panic!("unable to change user");
87 }
88
89 fn get_user(&self) -> Option<String> {
90 Some(self.user.to_string())
91 }
92 }
93
94 impl AsRef<ReaderEnvironment> for dyn RpcEnvironment {
95 fn as_ref(&self) -> &ReaderEnvironment {
96 self.as_any().downcast_ref::<ReaderEnvironment>().unwrap()
97 }
98 }
99
100 impl AsRef<ReaderEnvironment> for Box<dyn RpcEnvironment> {
101 fn as_ref(&self) -> &ReaderEnvironment {
102 self.as_any().downcast_ref::<ReaderEnvironment>().unwrap()
103 }
104 }