public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH manager 6/6] api: backup/vzdump: add get_storage_param helper
Date: Wed, 16 Nov 2022 15:04:35 +0100	[thread overview]
Message-ID: <20221116140435.93067-7-f.ebner@proxmox.com> (raw)
In-Reply-To: <20221116140435.93067-1-f.ebner@proxmox.com>

to capture the logic in a single place.

Suggested-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 PVE/API2/Backup.pm | 10 +++++-----
 PVE/API2/VZDump.pm | 15 ++++++++-------
 PVE/VZDump.pm      | 11 +++++++++--
 3 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/PVE/API2/Backup.pm b/PVE/API2/Backup.pm
index 74bf95ca..25e615d1 100644
--- a/PVE/API2/Backup.pm
+++ b/PVE/API2/Backup.pm
@@ -60,10 +60,9 @@ my sub assert_param_permission_create {
 
     assert_param_permission_common($rpcenv, $user, $param);
 
-    if (!$param->{dumpdir}) {
-	my $storeid = $param->{storage} || 'local';
+    if (my $storeid = PVE::VZDump::get_storage_param($param)) {
 	$rpcenv->check($user, "/storage/$storeid", [ 'Datastore.Allocate' ]);
-    } # no else branch, because dumpdir is root-only
+    }
 }
 
 my sub assert_param_permission_update {
@@ -84,8 +83,9 @@ my sub assert_param_permission_update {
     if ($current->{dumpdir}) {
 	die "only root\@pam may edit jobs with a 'dumpdir' option.";
     } else {
-	my $storeid = $current->{storage} || 'local';
-	$rpcenv->check($user, "/storage/$storeid", [ 'Datastore.Allocate' ]);
+	if (my $storeid = PVE::VZDump::get_storage_param($current)) {
+	    $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.Allocate' ]);
+	}
     }
 }
 
diff --git a/PVE/API2/VZDump.pm b/PVE/API2/VZDump.pm
index 8e873c05..e3dcd0bd 100644
--- a/PVE/API2/VZDump.pm
+++ b/PVE/API2/VZDump.pm
@@ -27,10 +27,11 @@ my sub assert_param_permission_vzdump {
 
     PVE::API2::Backup::assert_param_permission_common($rpcenv, $user, $param);
 
-    if (!$param->{dumpdir} && (defined($param->{maxfiles}) || defined($param->{'prune-backups'}))) {
-	my $storeid = $param->{storage} || 'local';
-	$rpcenv->check($user, "/storage/$storeid", [ 'Datastore.Allocate' ]);
-    } # no else branch, because dumpdir is root-only
+    if (defined($param->{maxfiles}) || defined($param->{'prune-backups'})) {
+	if (my $storeid = PVE::VZDump::get_storage_param($param)) {
+	    $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.Allocate' ]);
+	}
+    }
 }
 
 __PACKAGE__->register_method ({
@@ -108,9 +109,9 @@ __PACKAGE__->register_method ({
 	die "you can only backup a single VM with option --stdout\n"
 	    if $param->{stdout} && scalar(@{$local_vmids}) != 1;
 
-	# If the root-only dumpdir is used rather than a storage, the check will succeed anyways.
-	my $storeid = $param->{storage} || 'local';
-	$rpcenv->check($user, "/storage/$storeid", [ 'Datastore.AllocateSpace' ]);
+	if (my $storeid = PVE::VZDump::get_storage_param($param)) {
+	    $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.AllocateSpace' ]);
+	}
 
 	my $worker = sub {
 	    my $upid = shift;
diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index a04837e7..bd17c986 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -55,6 +55,13 @@ foreach my $plug (@pve_vzdump_classes) {
     }
 }
 
+sub get_storage_param {
+    my ($param) = @_;
+
+    return if $param->{dumpdir};
+    return $param->{storage} || 'local';
+}
+
 # helper functions
 
 sub debugmsg {
@@ -557,8 +564,8 @@ sub new {
 	die "cannot use options 'storage' and 'dumpdir' at the same time\n";
     }
 
-    if (!$opts->{dumpdir} && !$opts->{storage}) {
-	$opts->{storage} = 'local';
+    if (my $storage = get_storage_param($opts)) {
+	$opts->{storage} = $storage;
     }
 
     # Enforced by the API too, but these options might come in via defaults. Drop them if necessary.
-- 
2.30.2





  parent reply	other threads:[~2022-11-16 14:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-16 14:04 [pve-devel] [PATCH-SERIES manager] backup permission improvements Fiona Ebner
2022-11-16 14:04 ` [pve-devel] [PATCH manager 1/6] api: vzdump: soften parameter permission checks Fiona Ebner
2022-11-21 14:58   ` [pve-devel] applied: " Thomas Lamprecht
2022-11-16 14:04 ` [pve-devel] [PATCH manager 2/6] api: backup: update: turn delete into a hash Fiona Ebner
2022-11-16 14:04 ` [pve-devel] [PATCH manager 3/6] api: backup: update: allow only deleting Fiona Ebner
2022-11-16 14:04 ` [pve-devel] [PATCH manager 4/6] api: backup: update: check permissions of delete params too Fiona Ebner
2022-11-16 14:04 ` [pve-devel] [PATCH manager 5/6] api: backup: require Datastore.Allocate on storage Fiona Ebner
2022-11-16 14:04 ` Fiona Ebner [this message]
2023-04-05  7:43 ` [pve-devel] [PATCH-SERIES manager] backup permission improvements Fiona Ebner
2023-06-06  6:33   ` Fiona Ebner
2023-06-07 14:58 ` [pve-devel] applied: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221116140435.93067-7-f.ebner@proxmox.com \
    --to=f.ebner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal