]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/admin/verify.rs
move acl to pbs_config workspaces, pbs_api_types cleanups
[proxmox-backup.git] / src / api2 / admin / verify.rs
1 //! Datastore Verify Job Management
2
3 use anyhow::{format_err, Error};
4 use serde_json::Value;
5
6 use proxmox::api::router::SubdirMap;
7 use proxmox::{list_subdirs_api_method, sortable};
8 use proxmox::api::{api, ApiMethod, Permission, Router, RpcEnvironment};
9
10 use pbs_api_types::{
11 VerificationJobConfig, VerificationJobStatus, JOB_ID_SCHEMA, Authid,
12 PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_VERIFY,
13 };
14 use pbs_config::verify;
15
16 use crate::{
17 api2::types::{
18 DATASTORE_SCHEMA,
19 },
20 server::{
21 do_verification_job,
22 jobstate::{
23 Job,
24 JobState,
25 compute_schedule_status,
26 },
27 },
28 config::cached_user_info::CachedUserInfo,
29 };
30
31 #[api(
32 input: {
33 properties: {
34 store: {
35 schema: DATASTORE_SCHEMA,
36 optional: true,
37 },
38 },
39 },
40 returns: {
41 description: "List configured jobs and their status (filtered by access)",
42 type: Array,
43 items: { type: VerificationJobStatus },
44 },
45 access: {
46 permission: &Permission::Anybody,
47 description: "Requires Datastore.Audit or Datastore.Verify on datastore.",
48 },
49 )]
50 /// List all verification jobs
51 pub fn list_verification_jobs(
52 store: Option<String>,
53 _param: Value,
54 mut rpcenv: &mut dyn RpcEnvironment,
55 ) -> Result<Vec<VerificationJobStatus>, Error> {
56 let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
57 let user_info = CachedUserInfo::new()?;
58
59 let required_privs = PRIV_DATASTORE_AUDIT | PRIV_DATASTORE_VERIFY;
60
61 let (config, digest) = verify::config()?;
62
63 let job_config_iter = config
64 .convert_to_typed_array("verification")?
65 .into_iter()
66 .filter(|job: &VerificationJobConfig| {
67 let privs = user_info.lookup_privs(&auth_id, &["datastore", &job.store]);
68 if privs & required_privs == 0 {
69 return false;
70 }
71
72 if let Some(store) = &store {
73 &job.store == store
74 } else {
75 true
76 }
77 });
78
79 let mut list = Vec::new();
80
81 for job in job_config_iter {
82 let last_state = JobState::load("verificationjob", &job.id)
83 .map_err(|err| format_err!("could not open statefile for {}: {}", &job.id, err))?;
84
85 let status = compute_schedule_status(&last_state, job.schedule.as_deref())?;
86
87 list.push(VerificationJobStatus { config: job, status });
88 }
89
90 rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
91
92 Ok(list)
93 }
94
95 #[api(
96 input: {
97 properties: {
98 id: {
99 schema: JOB_ID_SCHEMA,
100 }
101 }
102 },
103 access: {
104 permission: &Permission::Anybody,
105 description: "Requires Datastore.Verify on job's datastore.",
106 },
107 )]
108 /// Runs a verification job manually.
109 pub fn run_verification_job(
110 id: String,
111 _info: &ApiMethod,
112 rpcenv: &mut dyn RpcEnvironment,
113 ) -> Result<String, Error> {
114 let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
115 let user_info = CachedUserInfo::new()?;
116
117 let (config, _digest) = verify::config()?;
118 let verification_job: VerificationJobConfig = config.lookup("verification", &id)?;
119
120 user_info.check_privs(&auth_id, &["datastore", &verification_job.store], PRIV_DATASTORE_VERIFY, true)?;
121
122 let job = Job::new("verificationjob", &id)?;
123
124 let upid_str = do_verification_job(job, verification_job, &auth_id, None)?;
125
126 Ok(upid_str)
127 }
128
129 #[sortable]
130 const VERIFICATION_INFO_SUBDIRS: SubdirMap = &[("run", &Router::new().post(&API_METHOD_RUN_VERIFICATION_JOB))];
131
132 const VERIFICATION_INFO_ROUTER: Router = Router::new()
133 .get(&list_subdirs_api_method!(VERIFICATION_INFO_SUBDIRS))
134 .subdirs(VERIFICATION_INFO_SUBDIRS);
135
136 pub const ROUTER: Router = Router::new()
137 .get(&API_METHOD_LIST_VERIFICATION_JOBS)
138 .match_all("id", &VERIFICATION_INFO_ROUTER);