From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id E216D6506D for ; Mon, 2 Nov 2020 11:48:25 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E0287220E3 for ; Mon, 2 Nov 2020 11:48:25 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 895D2220AD for ; Mon, 2 Nov 2020 11:48:21 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 56A8345F2D for ; Mon, 2 Nov 2020 11:48:21 +0100 (CET) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pbs-devel@lists.proxmox.com Date: Mon, 2 Nov 2020 11:48:10 +0100 Message-Id: <20201102104811.1315280-2-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201102104811.1315280-1-f.gruenbichler@proxmox.com> References: <20201102104811.1315280-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.025 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup 2/3] sync: add access check tests X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 02 Nov 2020 10:48:25 -0000 should cover all the current scenarios. remote server-side checks can't be meaningfully unit-tested, but they are simple enough so should hopefully never break. Signed-off-by: Fabian Grünbichler --- src/api2/config/sync.rs | 114 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/src/api2/config/sync.rs b/src/api2/config/sync.rs index 0d67b033..f9810369 100644 --- a/src/api2/config/sync.rs +++ b/src/api2/config/sync.rs @@ -32,6 +32,7 @@ pub fn check_sync_job_read_access( let remote_privs = user_info.lookup_privs(&auth_id, &["remote", &job.remote]); remote_privs & PRIV_REMOTE_AUDIT != 0 } + // user can run the corresponding pull job pub fn check_sync_job_modify_access( user_info: &CachedUserInfo, @@ -410,3 +411,116 @@ pub const ROUTER: Router = Router::new() .get(&API_METHOD_LIST_SYNC_JOBS) .post(&API_METHOD_CREATE_SYNC_JOB) .match_all("id", &ITEM_ROUTER); + + +#[test] +fn sync_job_access_test() -> Result<(), Error> { + let (user_cfg, _) = crate::config::user::test_cfg_from_str(r###" +user: noperm@pbs + +user: read@pbs + +user: write@pbs + +"###).expect("test user.cfg is not parsable"); + let acl_tree = crate::config::acl::AclTree::from_raw(r###" +acl:1:/datastore/localstore1:read@pbs,write@pbs:DatastoreAudit +acl:1:/datastore/localstore1:write@pbs:DatastoreBackup +acl:1:/datastore/localstore2:write@pbs:DatastorePowerUser +acl:1:/datastore/localstore3:write@pbs:DatastoreAdmin +acl:1:/remote/remote1:read@pbs,write@pbs:RemoteAudit +acl:1:/remote/remote1/remotestore1:write@pbs:RemoteSyncOperator +"###).expect("test acl.cfg is not parsable"); + + let user_info = CachedUserInfo::test_new(user_cfg, acl_tree); + + let root_auth_id = Authid::root_auth_id(); + + let no_perm_auth_id: Authid = "noperm@pbs".parse()?; + let read_auth_id: Authid = "read@pbs".parse()?; + let write_auth_id: Authid = "write@pbs".parse()?; + + let mut job = SyncJobConfig { + id: "regular".to_string(), + remote: "remote0".to_string(), + remote_store: "remotestore1".to_string(), + store: "localstore0".to_string(), + owner: Some(write_auth_id.clone()), + comment: None, + remove_vanished: None, + schedule: None, + }; + + // should work without ACLs + assert_eq!(check_sync_job_read_access(&user_info, &root_auth_id, &job), true); + assert_eq!(check_sync_job_modify_access(&user_info, &root_auth_id, &job), true); + + // user without permissions must fail + assert_eq!(check_sync_job_read_access(&user_info, &no_perm_auth_id, &job), false); + assert_eq!(check_sync_job_modify_access(&user_info, &no_perm_auth_id, &job), false); + + // reading without proper read permissions on either remote or local must fail + assert_eq!(check_sync_job_read_access(&user_info, &read_auth_id, &job), false); + + // reading without proper read permissions on local end must fail + job.remote = "remote1".to_string(); + assert_eq!(check_sync_job_read_access(&user_info, &read_auth_id, &job), false); + + // reading without proper read permissions on remote end must fail + job.remote = "remote0".to_string(); + job.store = "localstore1".to_string(); + assert_eq!(check_sync_job_read_access(&user_info, &read_auth_id, &job), false); + + // writing without proper write permissions on either end must fail + job.store = "localstore0".to_string(); + assert_eq!(check_sync_job_modify_access(&user_info, &write_auth_id, &job), false); + + // writing without proper write permissions on local end must fail + job.remote = "remote1".to_string(); + + // writing without proper write permissions on remote end must fail + job.remote = "remote0".to_string(); + job.store = "localstore1".to_string(); + assert_eq!(check_sync_job_modify_access(&user_info, &write_auth_id, &job), false); + + // reset remote to one where users have access + job.remote = "remote1".to_string(); + + // user with read permission can only read, but not modify/run + assert_eq!(check_sync_job_read_access(&user_info, &read_auth_id, &job), true); + job.owner = Some(read_auth_id.clone()); + assert_eq!(check_sync_job_modify_access(&user_info, &read_auth_id, &job), false); + job.owner = None; + assert_eq!(check_sync_job_modify_access(&user_info, &read_auth_id, &job), false); + job.owner = Some(write_auth_id.clone()); + assert_eq!(check_sync_job_modify_access(&user_info, &read_auth_id, &job), false); + + // user with simple write permission can modify/run + assert_eq!(check_sync_job_read_access(&user_info, &write_auth_id, &job), true); + assert_eq!(check_sync_job_modify_access(&user_info, &write_auth_id, &job), true); + + // but can't modify/run with deletion + job.remove_vanished = Some(true); + assert_eq!(check_sync_job_modify_access(&user_info, &write_auth_id, &job), false); + + // unless they have Datastore.Prune as well + job.store = "localstore2".to_string(); + assert_eq!(check_sync_job_modify_access(&user_info, &write_auth_id, &job), true); + + // changing owner is not possible + job.owner = Some(read_auth_id.clone()); + assert_eq!(check_sync_job_modify_access(&user_info, &write_auth_id, &job), false); + + // also not to the default 'backup@pam' + job.owner = None; + assert_eq!(check_sync_job_modify_access(&user_info, &write_auth_id, &job), false); + + // unless they have Datastore.Modify as well + job.store = "localstore3".to_string(); + job.owner = Some(read_auth_id.clone()); + assert_eq!(check_sync_job_modify_access(&user_info, &write_auth_id, &job), true); + job.owner = None; + assert_eq!(check_sync_job_modify_access(&user_info, &write_auth_id, &job), true); + + Ok(()) +} -- 2.20.1