1 use anyhow
::{bail, Error}
;
3 use ::serde
::{Deserialize, Serialize}
;
5 use proxmox
::api
::{api, Permission, Router, RpcEnvironment}
;
8 Authid
, VerificationJobConfig
, VerificationJobConfigUpdater
, JOB_ID_SCHEMA
,
9 PROXMOX_CONFIG_DIGEST_SCHEMA
, PRIV_DATASTORE_AUDIT
, PRIV_DATASTORE_VERIFY
,
11 use pbs_config
::verify
;
13 use pbs_config
::CachedUserInfo
;
20 description
: "List configured jobs.",
22 items
: { type: VerificationJobConfig }
,
25 permission
: &Permission
::Anybody
,
26 description
: "Requires Datastore.Audit or Datastore.Verify on datastore.",
29 /// List all verification jobs
30 pub fn list_verification_jobs(
32 mut rpcenv
: &mut dyn RpcEnvironment
,
33 ) -> Result
<Vec
<VerificationJobConfig
>, Error
> {
34 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
35 let user_info
= CachedUserInfo
::new()?
;
37 let required_privs
= PRIV_DATASTORE_AUDIT
| PRIV_DATASTORE_VERIFY
;
39 let (config
, digest
) = verify
::config()?
;
41 let list
= config
.convert_to_typed_array("verification")?
;
43 let list
= list
.into_iter()
44 .filter(|job
: &VerificationJobConfig
| {
45 let privs
= user_info
.lookup_privs(&auth_id
, &["datastore", &job
.store
]);
47 privs
& required_privs
!= 00
50 rpcenv
["digest"] = proxmox
::tools
::digest_to_hex(&digest
).into();
61 type: VerificationJobConfig
,
67 permission
: &Permission
::Anybody
,
68 description
: "Requires Datastore.Verify on job's datastore.",
71 /// Create a new verification job.
72 pub fn create_verification_job(
73 config
: VerificationJobConfig
,
74 rpcenv
: &mut dyn RpcEnvironment
75 ) -> Result
<(), Error
> {
76 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
77 let user_info
= CachedUserInfo
::new()?
;
79 user_info
.check_privs(&auth_id
, &["datastore", &config
.store
], PRIV_DATASTORE_VERIFY
, false)?
;
81 let _lock
= verify
::lock_config()?
;
83 let (mut section_config
, _digest
) = verify
::config()?
;
85 if section_config
.sections
.get(&config
.id
).is_some() {
86 bail
!("job '{}' already exists.", config
.id
);
89 section_config
.set_data(&config
.id
, "verification", &config
)?
;
91 verify
::save_config(§ion_config
)?
;
93 crate::server
::jobstate
::create_state_file("verificationjob", &config
.id
)?
;
102 schema
: JOB_ID_SCHEMA
,
106 returns
: { type: VerificationJobConfig }
,
108 permission
: &Permission
::Anybody
,
109 description
: "Requires Datastore.Audit or Datastore.Verify on job's datastore.",
112 /// Read a verification job configuration.
113 pub fn read_verification_job(
115 mut rpcenv
: &mut dyn RpcEnvironment
,
116 ) -> Result
<VerificationJobConfig
, Error
> {
117 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
118 let user_info
= CachedUserInfo
::new()?
;
120 let (config
, digest
) = verify
::config()?
;
122 let verification_job
: VerificationJobConfig
= config
.lookup("verification", &id
)?
;
124 let required_privs
= PRIV_DATASTORE_AUDIT
| PRIV_DATASTORE_VERIFY
;
125 user_info
.check_privs(&auth_id
, &["datastore", &verification_job
.store
], required_privs
, true)?
;
127 rpcenv
["digest"] = proxmox
::tools
::digest_to_hex(&digest
).into();
133 #[derive(Serialize, Deserialize)]
134 #[serde(rename_all="kebab-case")]
135 /// Deletable property name
136 pub enum DeletableProperty
{
137 /// Delete the ignore verified property.
139 /// Delete the comment property.
141 /// Delete the job schedule.
143 /// Delete outdated after property.
152 schema
: JOB_ID_SCHEMA
,
155 type: VerificationJobConfigUpdater
,
159 description
: "List of properties to delete.",
163 type: DeletableProperty
,
168 schema
: PROXMOX_CONFIG_DIGEST_SCHEMA
,
173 permission
: &Permission
::Anybody
,
174 description
: "Requires Datastore.Verify on job's datastore.",
177 /// Update verification job config.
178 #[allow(clippy::too_many_arguments)]
179 pub fn update_verification_job(
181 update
: VerificationJobConfigUpdater
,
182 delete
: Option
<Vec
<DeletableProperty
>>,
183 digest
: Option
<String
>,
184 rpcenv
: &mut dyn RpcEnvironment
,
185 ) -> Result
<(), Error
> {
186 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
187 let user_info
= CachedUserInfo
::new()?
;
189 let _lock
= verify
::lock_config()?
;
191 // pass/compare digest
192 let (mut config
, expected_digest
) = verify
::config()?
;
194 if let Some(ref digest
) = digest
{
195 let digest
= proxmox
::tools
::hex_to_digest(digest
)?
;
196 crate::tools
::detect_modified_configuration_file(&digest
, &expected_digest
)?
;
199 let mut data
: VerificationJobConfig
= config
.lookup("verification", &id
)?
;
201 // check existing store
202 user_info
.check_privs(&auth_id
, &["datastore", &data
.store
], PRIV_DATASTORE_VERIFY
, true)?
;
204 if let Some(delete
) = delete
{
205 for delete_prop
in delete
{
207 DeletableProperty
::IgnoreVerified
=> { data.ignore_verified = None; }
,
208 DeletableProperty
::OutdatedAfter
=> { data.outdated_after = None; }
,
209 DeletableProperty
::Comment
=> { data.comment = None; }
,
210 DeletableProperty
::Schedule
=> { data.schedule = None; }
,
215 if let Some(comment
) = update
.comment
{
216 let comment
= comment
.trim().to_string();
217 if comment
.is_empty() {
220 data
.comment
= Some(comment
);
224 if let Some(store
) = update
.store
{
226 user_info
.check_privs(&auth_id
, &["datastore", &store
], PRIV_DATASTORE_VERIFY
, true)?
;
231 if update
.ignore_verified
.is_some() { data.ignore_verified = update.ignore_verified; }
232 if update
.outdated_after
.is_some() { data.outdated_after = update.outdated_after; }
233 let schedule_changed
= data
.schedule
!= update
.schedule
;
234 if update
.schedule
.is_some() { data.schedule = update.schedule; }
236 config
.set_data(&id
, "verification", &data
)?
;
238 verify
::save_config(&config
)?
;
240 if schedule_changed
{
241 crate::server
::jobstate
::update_job_last_run_time("verificationjob", &id
)?
;
252 schema
: JOB_ID_SCHEMA
,
256 schema
: PROXMOX_CONFIG_DIGEST_SCHEMA
,
261 permission
: &Permission
::Anybody
,
262 description
: "Requires Datastore.Verify on job's datastore.",
265 /// Remove a verification job configuration
266 pub fn delete_verification_job(
268 digest
: Option
<String
>,
269 rpcenv
: &mut dyn RpcEnvironment
,
270 ) -> Result
<(), Error
> {
271 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
272 let user_info
= CachedUserInfo
::new()?
;
274 let _lock
= verify
::lock_config()?
;
276 let (mut config
, expected_digest
) = verify
::config()?
;
278 let job
: VerificationJobConfig
= config
.lookup("verification", &id
)?
;
279 user_info
.check_privs(&auth_id
, &["datastore", &job
.store
], PRIV_DATASTORE_VERIFY
, true)?
;
281 if let Some(ref digest
) = digest
{
282 let digest
= proxmox
::tools
::hex_to_digest(digest
)?
;
283 crate::tools
::detect_modified_configuration_file(&digest
, &expected_digest
)?
;
286 match config
.sections
.get(&id
) {
287 Some(_
) => { config.sections.remove(&id); }
,
288 None
=> bail
!("job '{}' does not exist.", id
),
291 verify
::save_config(&config
)?
;
293 crate::server
::jobstate
::remove_state_file("verificationjob", &id
)?
;
298 const ITEM_ROUTER
: Router
= Router
::new()
299 .get(&API_METHOD_READ_VERIFICATION_JOB
)
300 .put(&API_METHOD_UPDATE_VERIFICATION_JOB
)
301 .delete(&API_METHOD_DELETE_VERIFICATION_JOB
);
303 pub const ROUTER
: Router
= Router
::new()
304 .get(&API_METHOD_LIST_VERIFICATION_JOBS
)
305 .post(&API_METHOD_CREATE_VERIFICATION_JOB
)
306 .match_all("id", &ITEM_ROUTER
);