From: Friedrich Weber <f.weber@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH guest-common v3 1/5] guest helpers: add helper to abort active guest tasks of a certain type
Date: Fri, 12 Apr 2024 16:15:49 +0200 [thread overview]
Message-ID: <20240412141553.430554-2-f.weber@proxmox.com> (raw)
In-Reply-To: <20240412141553.430554-1-f.weber@proxmox.com>
Given a `(type, user, vmid)` tuple, the helper aborts all tasks of the
given `type` for guest `vmid` that `user` is allowed to abort:
- If `user` has `Sys.Modify` on the node, they can abort any task
- If `user` is an API token, it can abort any task it started itself
- If `user` is a user, they can abort any task started by themselves
or one of their API tokens.
The helper is used to overrule any active qmshutdown/vzshutdown tasks
when attempting to stop a VM/CT (if requested).
Signed-off-by: Friedrich Weber <f.weber@proxmox.com>
---
Notes:
As the computation of `$can_abort_task` essentially duplicates logic
from PVE/API2/Tasks.pm, I considered reusing that, but this would have
required moving it to one of the dependencies of pve-guest-common
(Thomas suggested pve-access-control off-list). Seeing that the logic
boils down to 4 lines in `abort_guest_tasks`, I didn't consider it
worth the trouble in the end. Happy to reconsider, though.
changes v2 -> v3:
- improved readability: renamed subroutine to describe what it does,
renamed return value, added comment, clarified commit message (thx
Thomas)
- better align logic with current permission model for stopping tasks:
- allow users with Sys.Modify to abort *any* task (thx Thomas)
- allow users to abort tasks of their tokens
no changes v1 -> v2
src/PVE/GuestHelpers.pm | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/src/PVE/GuestHelpers.pm b/src/PVE/GuestHelpers.pm
index 961a7b8..c9fe147 100644
--- a/src/PVE/GuestHelpers.pm
+++ b/src/PVE/GuestHelpers.pm
@@ -416,4 +416,39 @@ sub check_vnet_access {
if !($tag || $trunks);
}
+sub abort_guest_tasks {
+ my ($rpcenv, $type, $vmid) = @_;
+
+ my $authuser = $rpcenv->get_user();
+ my $node = PVE::INotify::nodename();
+ my $can_abort_all = $rpcenv->check($authuser, "/nodes/$node", [ 'Sys.Modify' ], 1);
+
+ my $active_tasks = PVE::INotify::read_file('active');
+ my $aborted_tasks = [];
+ for my $task (@$active_tasks) {
+ if (!$task->{saved}
+ && $task->{type} eq $type
+ && $task->{id} eq $vmid
+ ) {
+ my $can_abort_task;
+ # tasks started by a token can be aborted by the token or token owner,
+ # tasks started by a user can be aborted by the user
+ if (PVE::AccessControl::pve_verify_tokenid($task->{user}, 1)) {
+ my $full_tokenid = $task->{user};
+ my ($task_username, undef) = PVE::AccessControl::split_tokenid($full_tokenid);
+ $can_abort_task = $authuser eq $task_username || $authuser eq $full_tokenid;
+ } else {
+ $can_abort_task = $authuser eq $task->{user};
+ }
+
+ if ($can_abort_all || $can_abort_task) {
+ # passing `1` for parameter $killit aborts the task
+ PVE::RPCEnvironment->check_worker($task->{upid}, 1);
+ push @$aborted_tasks, $task->{upid};
+ }
+ }
+ }
+ return $aborted_tasks;
+}
+
1;
--
2.39.2
next prev parent reply other threads:[~2024-04-12 14:16 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-12 14:15 [pve-devel] [PATCH guest-common/container/qemu-server/manager v3 0/5] fix #4474: stop tasks may overrule shutdown tasks Friedrich Weber
2024-04-12 14:15 ` Friedrich Weber [this message]
2024-04-17 18:44 ` [pve-devel] applied: [PATCH guest-common v3 1/5] guest helpers: add helper to abort active guest tasks of a certain type Thomas Lamprecht
2024-04-12 14:15 ` [pve-devel] [PATCH container v3 2/5] fix #4474: lxc api: add overrule-shutdown parameter to stop endpoint Friedrich Weber
2024-04-17 18:44 ` [pve-devel] applied: " Thomas Lamprecht
2024-04-12 14:15 ` [pve-devel] [PATCH qemu-server v3 3/5] fix #4474: qemu " Friedrich Weber
2024-04-17 18:44 ` [pve-devel] applied: " Thomas Lamprecht
2024-04-12 14:15 ` [pve-devel] [PATCH manager v3 4/5] ui: fix typo to make pve-cluster-tasks store globally available Friedrich Weber
2024-04-17 18:45 ` [pve-devel] applied: " Thomas Lamprecht
2024-04-12 14:15 ` [pve-devel] [PATCH manager v3 5/5] fix #4474: ui: guest stop: offer to overrule active shutdown tasks Friedrich Weber
2024-04-19 10:17 ` Dominik Csapak
2024-04-21 8:28 ` Thomas Lamprecht
2024-04-20 18:34 ` [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=20240412141553.430554-2-f.weber@proxmox.com \
--to=f.weber@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