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 A38BA8A461 for ; Tue, 2 Aug 2022 14:48:35 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 89D4115B0 for ; Tue, 2 Aug 2022 14:48:05 +0200 (CEST) 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 ; Tue, 2 Aug 2022 14:48:03 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 7171A42DA2 for ; Tue, 2 Aug 2022 14:48:03 +0200 (CEST) Date: Tue, 02 Aug 2022 14:47:55 +0200 From: Fabian =?iso-8859-1?q?Gr=FCnbichler?= To: Proxmox VE development discussion References: <20220727145612.451691-1-s.sterz@proxmox.com> <20220727145612.451691-2-s.sterz@proxmox.com> In-Reply-To: <20220727145612.451691-2-s.sterz@proxmox.com> MIME-Version: 1.0 User-Agent: astroid/0.15.0 (https://github.com/astroidmail/astroid) Message-Id: <1659444406.q9d7xyq3x3.astroid@nora.none> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-LEVEL: Spam detection results: 0 AWL 0.158 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [nodes.pm, proxmox.com] Subject: [pve-devel] applied+follow-up: [PATCH manager 2/2] api2: use JSONSchema to validate commands for "nodes/{node}/execute" X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 02 Aug 2022 12:48:35 -0000 by extracting the actual schema used in the validator into a varaible,=20 and dumping the contained item properties into the verbose_description=20 of the string API type we at least get a full description in the api=20 viewer. On July 27, 2022 4:56 pm, Stefan Sterz wrote: > this also makes it more explicit what the different values should be >=20 > Signed-off-by: Stefan Sterz > --- > not sure how sensible this is because most of the information here > won't show up in the api viewer. i couldn't figure out how to make it > show up and not make breaking changes to the endpoint or change how > the api definition hash is handled. >=20 > fabian gruenbichler and i also discussed off list that this is kind of > redudant functionality as you can likely just call multiple api > endpoints from whatever script/program/client you are using anyway > instead of using this limited and flawed endpoint. perhaps the was a > reason for this before tokens existed? >=20 > so maybe we should either drop it in pve 8 or let it accept proper > json not just "a string that when parsed as json needs to be of this > format". i.e. accept an array of objects here instead of a string. >=20 > it has also confused users in the past already, e.g. see: > https://forum.proxmox.com/threads/execute-command-in-node-with-api.112290= / >=20 > PVE/API2/Nodes.pm | 51 +++++++++++++++++++++++++++++++++++++++++------ > 1 file changed, 45 insertions(+), 6 deletions(-) >=20 > diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm > index ef946301..5cc9111d 100644 > --- a/PVE/API2/Nodes.pm > +++ b/PVE/API2/Nodes.pm > @@ -66,6 +66,49 @@ eval { > =20 > use base qw(PVE::RESTHandler); > =20 > +PVE::JSONSchema::register_format('pve-command-batch', \&verify_command_b= atch); > +sub verify_command_batch { > + my ($value, $noerr) =3D @_; > + my $commands =3D eval { decode_json($value); }; > + > + return undef if $noerr && $@; > + die "commands param did not contain valid JSON: $@" if $@; > + > + eval { > + PVE::JSONSchema::validate($commands, { > + description =3D> "An array of objects describing endpoints, methods= and arguments.", > + type =3D> "array", > + items =3D> { > + type =3D> "object", > + properties =3D> { > + path =3D> { > + description =3D> "A relative path to an API endpoint on this node.", > + type =3D> "string", > + optional =3D> 0, > + }, > + method =3D> { > + description =3D> "A method related to the API endpoint (GET, POST etc= .).", > + type =3D> "string", > + pattern =3D> "(GET|POST|PUT|DELETE)", > + optional =3D> 0, > + }, > + args =3D> { > + description =3D> "A set of parameter names and their values.", > + type =3D> "object", > + optional =3D> 1, > + }, > + }, > + } > + }); > + }; > + > + return $commands if !$@; > + > + return undef if $noerr; > + > + die "commands is not a valid array of commands: $@"; > +} > + > __PACKAGE__->register_method ({ > subclass =3D> "PVE::API2::Qemu", > path =3D> 'qemu', > @@ -433,6 +476,7 @@ __PACKAGE__->register_method({ > commands =3D> { > description =3D> "JSON encoded array of commands.", > type =3D> "string", > + format =3D> "pve-command-batch", > } > }, > }, > @@ -449,16 +493,11 @@ __PACKAGE__->register_method({ > =20 > my $rpcenv =3D PVE::RPCEnvironment::get(); > my $user =3D $rpcenv->get_user(); > - > + # just parse the json again, it should already be validated > my $commands =3D eval { decode_json($param->{commands}); }; > =20 > - die "commands param did not contain valid JSON: $@" if $@; > - die "commands is not an array" if ref($commands) ne "ARRAY"; > - > foreach my $cmd (@$commands) { > eval { > - die "$cmd is not a valid command" if (ref($cmd) ne "HASH" || !$cmd->{p= ath} || !$cmd->{method}); > - > $cmd->{args} //=3D {}; > =20 > my $path =3D "nodes/$param->{node}/$cmd->{path}"; > --=20 > 2.30.2 >=20 >=20 >=20 > _______________________________________________ > pve-devel mailing list > pve-devel@lists.proxmox.com > https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel >=20 >=20 >=20