1 use ::serde
::{Deserialize, Serialize}
;
6 use proxmox_router
::{http_bail, Permission, Router, RpcEnvironment}
;
7 use proxmox_schema
::{api, param_bail}
;
10 Authid
, VerificationJobConfig
, VerificationJobConfigUpdater
, JOB_ID_SCHEMA
,
11 PRIV_DATASTORE_AUDIT
, PRIV_DATASTORE_VERIFY
, PROXMOX_CONFIG_DIGEST_SCHEMA
,
13 use pbs_config
::verify
;
15 use pbs_config
::CachedUserInfo
;
22 description
: "List configured jobs.",
24 items
: { type: VerificationJobConfig }
,
27 permission
: &Permission
::Anybody
,
28 description
: "Requires Datastore.Audit or Datastore.Verify on datastore.",
31 /// List all verification jobs
32 pub fn list_verification_jobs(
34 rpcenv
: &mut dyn RpcEnvironment
,
35 ) -> Result
<Vec
<VerificationJobConfig
>, Error
> {
36 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
37 let user_info
= CachedUserInfo
::new()?
;
39 let required_privs
= PRIV_DATASTORE_AUDIT
| PRIV_DATASTORE_VERIFY
;
41 let (config
, digest
) = verify
::config()?
;
43 let list
= config
.convert_to_typed_array("verification")?
;
47 .filter(|job
: &VerificationJobConfig
| {
48 let privs
= user_info
.lookup_privs(&auth_id
, &job
.acl_path());
50 privs
& required_privs
!= 00
54 rpcenv
["digest"] = hex
::encode(digest
).into();
64 type: VerificationJobConfig
,
70 permission
: &Permission
::Anybody
,
71 description
: "Requires Datastore.Verify on job's datastore.",
74 /// Create a new verification job.
75 pub fn create_verification_job(
76 config
: VerificationJobConfig
,
77 rpcenv
: &mut dyn RpcEnvironment
,
78 ) -> Result
<(), Error
> {
79 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
80 let user_info
= CachedUserInfo
::new()?
;
82 user_info
.check_privs(&auth_id
, &config
.acl_path(), PRIV_DATASTORE_VERIFY
, false)?
;
84 let _lock
= verify
::lock_config()?
;
86 let (mut section_config
, _digest
) = verify
::config()?
;
88 if section_config
.sections
.get(&config
.id
).is_some() {
89 param_bail
!("id", "job '{}' already exists.", config
.id
);
92 section_config
.set_data(&config
.id
, "verification", &config
)?
;
94 verify
::save_config(§ion_config
)?
;
96 crate::server
::jobstate
::create_state_file("verificationjob", &config
.id
)?
;
105 schema
: JOB_ID_SCHEMA
,
109 returns
: { type: VerificationJobConfig }
,
111 permission
: &Permission
::Anybody
,
112 description
: "Requires Datastore.Audit or Datastore.Verify on job's datastore.",
115 /// Read a verification job configuration.
116 pub fn read_verification_job(
118 rpcenv
: &mut dyn RpcEnvironment
,
119 ) -> Result
<VerificationJobConfig
, Error
> {
120 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
121 let user_info
= CachedUserInfo
::new()?
;
123 let (config
, digest
) = verify
::config()?
;
125 let verification_job
: VerificationJobConfig
= config
.lookup("verification", &id
)?
;
127 let required_privs
= PRIV_DATASTORE_AUDIT
| PRIV_DATASTORE_VERIFY
;
128 user_info
.check_privs(&auth_id
, &verification_job
.acl_path(), required_privs
, true)?
;
130 rpcenv
["digest"] = hex
::encode(digest
).into();
136 #[derive(Serialize, Deserialize)]
137 #[serde(rename_all = "kebab-case")]
138 /// Deletable property name
139 pub enum DeletableProperty
{
140 /// Delete the ignore verified property.
142 /// Delete the comment property.
144 /// Delete the job schedule.
146 /// Delete outdated after property.
148 /// Delete namespace property, defaulting to root namespace then.
150 /// Delete max-depth property, defaulting to full recursion again
159 schema
: JOB_ID_SCHEMA
,
162 type: VerificationJobConfigUpdater
,
166 description
: "List of properties to delete.",
170 type: DeletableProperty
,
175 schema
: PROXMOX_CONFIG_DIGEST_SCHEMA
,
180 permission
: &Permission
::Anybody
,
181 description
: "Requires Datastore.Verify on job's datastore.",
184 /// Update verification job config.
185 #[allow(clippy::too_many_arguments)]
186 pub fn update_verification_job(
188 update
: VerificationJobConfigUpdater
,
189 delete
: Option
<Vec
<DeletableProperty
>>,
190 digest
: Option
<String
>,
191 rpcenv
: &mut dyn RpcEnvironment
,
192 ) -> Result
<(), Error
> {
193 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
194 let user_info
= CachedUserInfo
::new()?
;
196 let _lock
= verify
::lock_config()?
;
198 // pass/compare digest
199 let (mut config
, expected_digest
) = verify
::config()?
;
201 if let Some(ref digest
) = digest
{
202 let digest
= <[u8; 32]>::from_hex(digest
)?
;
203 crate::tools
::detect_modified_configuration_file(&digest
, &expected_digest
)?
;
206 let mut data
: VerificationJobConfig
= config
.lookup("verification", &id
)?
;
208 // check existing store and NS
209 user_info
.check_privs(&auth_id
, &data
.acl_path(), PRIV_DATASTORE_VERIFY
, true)?
;
211 if let Some(delete
) = delete
{
212 for delete_prop
in delete
{
214 DeletableProperty
::IgnoreVerified
=> {
215 data
.ignore_verified
= None
;
217 DeletableProperty
::OutdatedAfter
=> {
218 data
.outdated_after
= None
;
220 DeletableProperty
::Comment
=> {
223 DeletableProperty
::Schedule
=> {
224 data
.schedule
= None
;
226 DeletableProperty
::Ns
=> {
229 DeletableProperty
::MaxDepth
=> {
230 data
.max_depth
= None
;
236 if let Some(comment
) = update
.comment
{
237 let comment
= comment
.trim().to_string();
238 if comment
.is_empty() {
241 data
.comment
= Some(comment
);
245 if let Some(store
) = update
.store
{
249 if update
.ignore_verified
.is_some() {
250 data
.ignore_verified
= update
.ignore_verified
;
252 if update
.outdated_after
.is_some() {
253 data
.outdated_after
= update
.outdated_after
;
255 let schedule_changed
= data
.schedule
!= update
.schedule
;
256 if update
.schedule
.is_some() {
257 data
.schedule
= update
.schedule
;
259 if let Some(ns
) = update
.ns
{
264 if let Some(max_depth
) = update
.max_depth
{
265 if max_depth
<= pbs_api_types
::MAX_NAMESPACE_DEPTH
{
266 data
.max_depth
= Some(max_depth
);
270 // check new store and NS
271 user_info
.check_privs(&auth_id
, &data
.acl_path(), PRIV_DATASTORE_VERIFY
, true)?
;
273 config
.set_data(&id
, "verification", &data
)?
;
275 verify
::save_config(&config
)?
;
277 if schedule_changed
{
278 crate::server
::jobstate
::update_job_last_run_time("verificationjob", &id
)?
;
289 schema
: JOB_ID_SCHEMA
,
293 schema
: PROXMOX_CONFIG_DIGEST_SCHEMA
,
298 permission
: &Permission
::Anybody
,
299 description
: "Requires Datastore.Verify on job's datastore.",
302 /// Remove a verification job configuration
303 pub fn delete_verification_job(
305 digest
: Option
<String
>,
306 rpcenv
: &mut dyn RpcEnvironment
,
307 ) -> Result
<(), Error
> {
308 let auth_id
: Authid
= rpcenv
.get_auth_id().unwrap().parse()?
;
309 let user_info
= CachedUserInfo
::new()?
;
311 let _lock
= verify
::lock_config()?
;
313 let (mut config
, expected_digest
) = verify
::config()?
;
315 let job
: VerificationJobConfig
= config
.lookup("verification", &id
)?
;
316 user_info
.check_privs(&auth_id
, &job
.acl_path(), PRIV_DATASTORE_VERIFY
, true)?
;
318 if let Some(ref digest
) = digest
{
319 let digest
= <[u8; 32]>::from_hex(digest
)?
;
320 crate::tools
::detect_modified_configuration_file(&digest
, &expected_digest
)?
;
323 match config
.sections
.get(&id
) {
325 config
.sections
.remove(&id
);
327 None
=> http_bail
!(NOT_FOUND
, "job '{}' does not exist.", id
),
330 verify
::save_config(&config
)?
;
332 crate::server
::jobstate
::remove_state_file("verificationjob", &id
)?
;
337 const ITEM_ROUTER
: Router
= Router
::new()
338 .get(&API_METHOD_READ_VERIFICATION_JOB
)
339 .put(&API_METHOD_UPDATE_VERIFICATION_JOB
)
340 .delete(&API_METHOD_DELETE_VERIFICATION_JOB
);
342 pub const ROUTER
: Router
= Router
::new()
343 .get(&API_METHOD_LIST_VERIFICATION_JOBS
)
344 .post(&API_METHOD_CREATE_VERIFICATION_JOB
)
345 .match_all("id", &ITEM_ROUTER
);