From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id B9C4C1FF0E1 for ; Mon, 16 Nov 2026 01:51:05 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 479052199D; Mon, 16 Nov 2026 01:48:41 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH manager 26/34] api: cluster resources: add guest uid and registration time Date: Mon, 16 Nov 2026 01:46:38 +0100 Message-ID: <20261116004703.2908294-27-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261116004703.2908294-1-t.lamprecht@proxmox.com> References: <20261116004703.2908294-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794790030319 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.788 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) KAM_MAILER 2 Automated Mailer Tag Left in Email 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 Message-ID-Hash: YO253F2HLE6PRRZXH4HTUYEYRYVOESAY X-Message-ID-Hash: YO253F2HLE6PRRZXH4HTUYEYRYVOESAY X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Tools that follow guests across restores and migrations, for example backup products, need the guest identity. Report the 'uid' and 'rtime' of guests that have them on their rows, so such tools get both for all guests with one call instead of reading each guest config. Like lock and tags, they are only reported to users with VM.Audit on the guest. Both come from the 'meta' line, which the existing request for lock and tags now also returns, so there is no extra round trip to pmxcfs. The line is parsed per guest, and a guest with a missing or malformed identity only lacks the fields. Neither the raw line nor its other members are exposed. Signed-off-by: Thomas Lamprecht --- PVE/API2/Cluster.pm | 25 +++- test/ClusterResources_test.pl | 249 ++++++++++++++++++++++++++++++++++ test/Makefile | 6 +- 3 files changed, 278 insertions(+), 2 deletions(-) create mode 100755 test/ClusterResources_test.pl diff --git a/PVE/API2/Cluster.pm b/PVE/API2/Cluster.pm index 4e5efbfd..346ecdb2 100644 --- a/PVE/API2/Cluster.pm +++ b/PVE/API2/Cluster.pm @@ -11,6 +11,7 @@ use PVE::DataCenterConfig; use PVE::Exception qw(raise_param_exc); use PVE::Firewall; use PVE::GuestHelpers; +use PVE::GuestIdentity; use PVE::HA::Config; use PVE::HA::Env::PVE2; use PVE::INotify; @@ -474,6 +475,22 @@ __PACKAGE__->register_method({ optional => 1, default => 0, }, + uid => { + description => + "Guest lineage identifier, normally kept across restores and migrations" + . " from other clusters (for types 'qemu' and 'lxc').", + type => 'string', + optional => 1, + }, + rtime => { + description => + "Time the guest was registered in this cluster, for example by" + . " creation, restore, or migration from another cluster, in milliseconds" + . " since the UNIX epoch (for types 'qemu' and 'lxc').", + type => 'integer', + minimum => 0, + optional => 1, + }, network => { description => "The name of a Network entity (for type 'network').", type => "string", @@ -552,7 +569,7 @@ __PACKAGE__->register_method({ # we try to generate 'numbers' by using "$X + 0" if (!$param->{type} || $param->{type} eq 'vm') { my $prop_list = [qw(lock tags)]; - my $props = PVE::Cluster::get_guest_config_properties($prop_list); + my $props = PVE::Cluster::get_guest_config_properties([@$prop_list, 'meta']); for my $vmid (sort keys %$idlist) { @@ -593,6 +610,12 @@ __PACKAGE__->register_method({ } } + my $identity = PVE::GuestIdentity::parse_identity($props->{$vmid}->{meta}); + if ($identity) { + $entry->{uid} = $identity->{uid}; + $entry->{rtime} = int($identity->{rtime}) if defined($identity->{rtime}); + } + if ( defined($entry->{pool}) && !$rpcenv->check($authuser, "/pool/$entry->{pool}", ['Pool.Audit'], 1) diff --git a/test/ClusterResources_test.pl b/test/ClusterResources_test.pl new file mode 100755 index 00000000..4b981dc7 --- /dev/null +++ b/test/ClusterResources_test.pl @@ -0,0 +1,249 @@ +#!/usr/bin/perl + +use v5.36; + +use lib ('.', '..'); + +use JSON; +use Test::MockModule; +use Test::More; + +use PVE::JSONSchema; + +use PVE::API2::Cluster; + +my $uid_100 = 'k3d7q2mxa5vb4n6pw2fz'; +my $uid_101 = 'r6t2w4y7z3c5e2g4h7jm'; +my $uid_104 = 'q2w3e4r5t6y7u2i3o4pa'; + +# top-level config values per guest as decoded from the pmxcfs reply, plus whether the user has +# VM.Audit on it; node2 is offline +my $guests; +my $default_guests = { + 100 => { + type => 'qemu', + node => 'node1', + audit => 1, + lock => 'backup', + tags => 'prod;web', + meta => "creation-qemu=9.2.0,ctime=1700000000,rtime=1731600000123,uid=$uid_100", + }, + 101 => { type => 'lxc', node => 'node2', audit => 1, meta => "uid=$uid_101" }, + 102 => { + type => 'qemu', + node => 'node1', + audit => 1, + meta => 'rtime=1731600000123,uid=NOT-A-UID', + }, + 103 => { type => 'qemu', node => 'node1', audit => 1, lock => 'migrate' }, + 104 => { type => 'qemu', node => 'node2', audit => 1, meta => "rtime=12x,uid=$uid_104" }, + 105 => { + type => 'lxc', + node => 'node1', + audit => 0, + meta => "rtime=1731600000123,uid=$uid_100", + }, +}; + +my $default_identities = { + 100 => { uid => $uid_100, rtime => 1731600000123 }, + 101 => { uid => $uid_101 }, + 102 => {}, + 103 => {}, + 104 => { uid => $uid_104 }, +}; + +my $property_requests; +my $ipc_failures = 0; + +my $cluster_mock = Test::MockModule->new('PVE::Cluster'); +$cluster_mock->redefine( + get_nodelist => sub { return ['node1', 'node2'] }, + get_members => sub { + return { node1 => { id => 1, online => 1 }, node2 => { id => 2, online => 0 } }; + }, + rrd_dump => sub { return {} }, + get_node_kv => sub { return {} }, + get_vmlist => sub { + my $ids = {}; + for my $vmid (keys %$guests) { + my $guest = $guests->{$vmid}; + $ids->{$vmid} = { node => $guest->{node}, type => $guest->{type}, version => 1 }; + } + return { ids => $ids }; + }, + get_guest_config_properties => sub($properties, $vmid = undef) { + die "only the request for all guests is expected\n" if defined($vmid); + push @$property_requests, [@$properties]; + die "ipcc_send_rec[11] failed: Connection refused\n" if $ipc_failures-- > 0; + + my $res = {}; + for my $id (keys %$guests) { + for my $prop (@$properties) { + my $value = $guests->{$id}->{$prop} // next; + $res->{$id}->{$prop} = $value; + } + } + return $res; + }, +); + +my $ha_config_mock = Test::MockModule->new('PVE::HA::Config'); +$ha_config_mock->redefine( + read_manager_status => sub { return {} }, + read_resources_config => sub { return { ids => {} } }, +); + +my $storage_mock = Test::MockModule->new('PVE::Storage'); +$storage_mock->redefine( + config => sub { return { ids => {} } }, + storage_ids => sub { return () }, +); + +my $rpcenv = bless { user_cfg => { pools => { p1 => {} }, vms => { 100 => 'p1' } } }, + 'PVE::RPCEnvironment::Test'; +sub PVE::RPCEnvironment::Test::get_user { return 'auditor@pve'; } + +sub PVE::RPCEnvironment::Test::check($self, $user, $path, $privs, $noerr) { + return 1 if $path eq '/pool/p1' && grep { $_ eq 'Pool.Audit' } @$privs; + my ($vmid) = $path =~ m!^/vms/(\d+)$! or return 0; + return $guests->{$vmid}->{audit} && grep { $_ eq 'VM.Audit' } @$privs; +} +my $rpc_mock = Test::MockModule->new('PVE::RPCEnvironment'); +$rpc_mock->redefine(get => sub { return $rpcenv }); + +my $method = PVE::API2::Cluster->map_method_by_name('resources'); + +# returns the rows and their JSON encoding, which is what API clients see +sub get_rows($param) { + $property_requests = []; + + my $res = PVE::API2::Cluster->handle($method, $param); + my $json = JSON->new->utf8->canonical->encode($res); + + PVE::JSONSchema::validate($res, $method->{returns}, "return schema violated\n"); + + return ($res, $json); +} + +sub guest_rows($res) { + return { map { $_->{vmid} => $_ } grep { $_->{type} =~ m/^(?:qemu|lxc)$/ } @$res }; +} + +sub identity_view($res) { + my $rows = guest_rows($res); + + my $view = {}; + for my $vmid (keys %$rows) { + my $row = $rows->{$vmid}; + $view->{$vmid} = { map { $_ => $row->{$_} } grep { exists($row->{$_}) } qw(uid rtime) }; + } + return $view; +} + +subtest 'identity of guests with valid, partial, invalid and missing meta' => sub { + $guests = {%$default_guests}; + + my ($res, $json) = get_rows({ type => 'vm' }); + my $rows = guest_rows($res); + + is_deeply( + $property_requests, + [[qw(lock tags meta)]], + 'meta is fetched together with lock and tags in a single request', + ); + + is_deeply( + identity_view($res), + $default_identities, + 'uid and rtime only where valid, guests without VM.Audit are not listed', + ); + + like($json, qr/"rtime":1731600000123[,}]/, 'rtime is encoded as JSON number'); + like($json, qr/"uid":"$uid_100"/, 'uid is encoded as JSON string'); + + is($rows->{100}->{lock}, 'backup', 'lock is still reported'); + is($rows->{100}->{tags}, 'prod;web', 'tags are still reported'); + is($rows->{103}->{lock}, 'migrate', 'lock of a guest without meta is still reported'); + + for my $key (qw(meta ctime creation-qemu)) { + ok(!grep({ exists($_->{$key}) } @$res), "no '$key' in any row"); + } +}; + +subtest 'listing of all resource types' => sub { + $guests = {%$default_guests}; + + my ($res) = get_rows({}); + + is(scalar(@$property_requests), 1, 'guest config properties are requested once'); + is_deeply(identity_view($res), $default_identities, 'same identities as for type vm'); + is(guest_rows($res)->{101}->{status}, 'unknown', 'guest on offline node is listed'); + + my @others = grep { $_->{type} !~ m/^(?:qemu|lxc)$/ } @$res; + is_deeply( + [sort map { $_->{id} } @others], + ['/pool/p1', 'node/node1', 'node/node2'], + 'pool and node rows are listed', + ); + ok(!grep({ exists($_->{uid}) || exists($_->{rtime}) } @others), + 'no identity on other rows'); +}; + +subtest 'control character or replacement character in meta' => sub { + # pmxcfs escapes control characters and replaces bytes that are not valid UTF-8 with U+FFFD + $guests = { + %$default_guests, + 106 => { + type => 'qemu', + node => 'node1', + audit => 1, + lock => 'backup', + meta => "rtime=1731600000123,uid=k3d7q2mx\ta5vb4n6p", + }, + 107 => { + type => 'qemu', + node => 'node1', + audit => 1, + meta => "rtime=1731600000123,uid=k3d7q2mx\x{FFFD}5vb4n6p", + }, + 108 => { + type => 'lxc', + node => 'node1', + audit => 1, + meta => "ctime=17000\x{1}00000,rtime=1731600000456,uid=z2y3x4w5v6u7t2s3r4qn", + }, + }; + + my ($res) = eval { get_rows({ type => 'vm' }) }; + is($@, '', 'listing does not fail'); + + is_deeply( + identity_view($res), + { + %$default_identities, + 106 => {}, + 107 => {}, + 108 => { uid => 'z2y3x4w5v6u7t2s3r4qn', rtime => 1731600000456 }, + }, + 'only the guests with a malformed uid lack the identity', + ); + is(guest_rows($res)->{106}->{lock}, 'backup', 'lock of such a guest is still reported'); +}; + +subtest 'transient IPC failure fails the listing' => sub { + $guests = {%$default_guests}; + $ipc_failures = 1; + + eval { get_rows({ type => 'vm' }) }; + like( + $@, + qr/Connection refused/, + 'error is raised instead of listing guests without identity', + ); + is(scalar(@$property_requests), 1, 'request is not repeated'); + + $ipc_failures = 0; +}; + +done_testing(); diff --git a/test/Makefile b/test/Makefile index 2dfd20a4..7eaa46a2 100644 --- a/test/Makefile +++ b/test/Makefile @@ -5,7 +5,7 @@ all: export PERLLIB=.. .PHONY: check -check: test-replication test-balloon test-vzdump test-osd test-ceph-health test-ceph-services test-ceph-auth test-ceph-key-migration test-ceph-lockbox-migration test-custom-cpu-models test-pvesh +check: test-replication test-balloon test-vzdump test-osd test-ceph-health test-ceph-services test-ceph-auth test-ceph-key-migration test-ceph-lockbox-migration test-custom-cpu-models test-pvesh test-cluster-resources .PHONY: test-balloon test-balloon: @@ -61,6 +61,10 @@ test-custom-cpu-models: test-pvesh: ./pvesh_test.pl +.PHONY: test-cluster-resources +test-cluster-resources: + ./ClusterResources_test.pl + .PHONY: install install: -- 2.47.3