From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <m.carrara@proxmox.com>
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 7D258B968F
 for <pve-devel@lists.proxmox.com>; Thu, 14 Mar 2024 10:34:53 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 589819C5C
 for <pve-devel@lists.proxmox.com>; Thu, 14 Mar 2024 10:34:23 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (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
 for <pve-devel@lists.proxmox.com>; Thu, 14 Mar 2024 10:34:19 +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 AAD1C489D3
 for <pve-devel@lists.proxmox.com>; Thu, 14 Mar 2024 10:34:19 +0100 (CET)
Mime-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8
Date: Thu, 14 Mar 2024 10:34:18 +0100
Message-Id: <CZTDC6AUSKDJ.2PTUR40AJO0TI@proxmox.com>
To: "Fiona Ebner" <f.ebner@proxmox.com>, "Proxmox VE development discussion"
 <pve-devel@lists.proxmox.com>, "Stefan Sterz" <s.sterz@proxmox.com>
From: "Max Carrara" <m.carrara@proxmox.com>
X-Mailer: aerc 0.17.0-72-g6a84f1331f1c
References: <20240313145345.484627-1-m.carrara@proxmox.com>
 <CZSQJKAR83OT.GQ4AT0S8C94O@proxmox.com>
 <3877e8ba-5aec-46fa-922a-458a08243203@proxmox.com>
In-Reply-To: <3877e8ba-5aec-46fa-922a-458a08243203@proxmox.com>
X-SPAM-LEVEL: Spam detection results:  0
 AWL -1.498 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
 URIBL_BLACK 3 Contains an URL listed in the URIBL blacklist [mon.pm]
Subject: Re: [pve-devel] [PATCH pve-manager 1/2] fix #5198: ceph: mon: fix
 mon existence check in mon removal assertion
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 14 Mar 2024 09:34:53 -0000

On Thu Mar 14, 2024 at 8:56 AM CET, Fiona Ebner wrote:
> Am 13.03.24 um 16:42 schrieb Stefan Sterz:
> > On Wed Mar 13, 2024 at 3:53 PM CET, Max Carrara wrote:
> >> The Ceph monitor removal assertion contains a condition that checks
> >> whether the given mon ID actually exists and thus may be removed.
> >>
> >> The first part of the condition checks whether the hash returned by
> >> `get_services_info` [0] contains the key "mon.$monid". However, the
> >> hash's keys are never prefixed with "mon.", which makes this check
> >> incorrect.
> >>
> >> This is fixed by just using "$monid" directly.
> >>
> >> The second part checks whether the mon hashes returned by
> >> Ceph contain the "name" key before comparing the key with the given
> >> mon ID. This key existence check is also incorrect; in particular:
> >>   * If the lookup `$_->{name}` evaluates to e.g. "foo", the check
> >>     passes, because "foo" is truthy. [1]
> >>   * If the lookup `$_->{name}` evaluates to "0", the check fails,
> >>     because "0" is falsy (due to it being equivalent to the number 0,
> >>     according to Perl [1]).
> >>
> >> This is solved by using the inbuilt `exists()` instead of relying on
> >> Perl's definition of truthiness.
> >>
>
> Technically, it's two changes, so could be two patches, but can be fine
> like this too, since both touch the same check.
>
> >> [0]: https://git.proxmox.com/?p=3Dpve-manager.git;a=3Dblob;f=3DPVE/Cep=
h/Services.pm;h=3De0f31e8eb6bc9b3777b3d0d548497276efaa5c41;hb=3DHEAD#l112
> >> [1]: https://perldoc.perl.org/perldata#Scalar-values
> >>
> >> Fixes: https://bugzilla.proxmox.com/show_bug.cgi?id=3D5198
> >> Signed-off-by: Max Carrara <m.carrara@proxmox.com>
> >> ---
> >>  PVE/API2/Ceph/MON.pm | 4 ++--
> >>  1 file changed, 2 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/PVE/API2/Ceph/MON.pm b/PVE/API2/Ceph/MON.pm
> >> index 1e959ef3..1737c294 100644
> >> --- a/PVE/API2/Ceph/MON.pm
> >> +++ b/PVE/API2/Ceph/MON.pm
> >> @@ -147,8 +147,8 @@ my $assert_mon_prerequisites =3D sub {
> >>  my $assert_mon_can_remove =3D sub {
> >>      my ($monhash, $monlist, $monid, $mondir) =3D @_;
> >>
> >> -    if (!(defined($monhash->{"mon.$monid"}) ||
> >> -	  grep { $_->{name} && $_->{name} eq $monid } @$monlist))
> >=20
> > not sure if splitting the fix and the code style clean up makes sense
> > here but otherwise this works as advertised. So:
> >=20
>
> If you already touch the same line, you can also adapt style in one go.
> But since the style change is also done in a second place, which is not
> touched by this patch, it's fine like Max did it too.
>
> > Tested-by: Stefan Sterz <s.sterz@proxmox.com>
> >=20
> >> +    if (!(defined($monhash->{$monid}) ||
> >> +	  grep { exists($_->{name}) && $_->{name} eq $monid } @$monlist))
>
> While I suppose we do not expect an entry with undef value here, you do
> not want to compare against undef (just leads to an ugly Perl warning),
> so this should be a definedness check, not an existence check.

Good catch, thanks! Will send in a v2.

>
> >>      {
> >>  	die "no such monitor id '$monid'\n"
> >>      }
> >=20
> >=20
> >=20
> > _______________________________________________
> > pve-devel mailing list
> > pve-devel@lists.proxmox.com
> > https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
> >=20
> >=20