public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lorenz Stechauner <l.stechauner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v3 storage 3/3] fix #3505: status: add checksum and algorithm to file upload
Date: Tue,  3 Aug 2021 14:16:35 +0200	[thread overview]
Message-ID: <20210803121640.2530993-5-l.stechauner@proxmox.com> (raw)
In-Reply-To: <20210803121640.2530993-1-l.stechauner@proxmox.com>

Signed-off-by: Lorenz Stechauner <l.stechauner@proxmox.com>
---
 PVE/API2/Storage/Status.pm | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/PVE/API2/Storage/Status.pm b/PVE/API2/Storage/Status.pm
index db5a76e..07366cc 100644
--- a/PVE/API2/Storage/Status.pm
+++ b/PVE/API2/Storage/Status.pm
@@ -388,6 +388,19 @@ __PACKAGE__->register_method ({
 		maxLength => 255,
 		type => 'string',
 	    },
+	    checksum => {
+		description => "The expected checksum of the file.",
+		type => 'string',
+		requires => 'checksum-algorithm',
+		optional => 1,
+	    },
+	    'checksum-algorithm' => {
+		description => "The algorithm to calculate the checksum of the file.",
+		type => 'string',
+		enum => ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'],
+		requires => 'checksum',
+		optional => 1,
+	    },
 	    tmpfilename => {
 		description => "The source file name. This parameter is usually set by the REST handler. You can only overwrite it when connecting to the trusted port on localhost.",
 		type => 'string',
@@ -480,6 +493,27 @@ __PACKAGE__->register_method ({
 	    my $upid = shift;
 
 	    print "starting file import from: $tmpfilename\n";
+
+	    eval {
+		my ($checksum, $checksum_algorithm) = $param->@{'checksum', 'checksum-algorithm'};
+		if ($checksum_algorithm) {
+		    print "calculating checksum...";
+
+		    my $checksum_got = PVE::Tools::get_file_hash($checksum_algorithm, $tmpfilename);
+
+		    if (lc($checksum_got) eq lc($checksum)) {
+			print "OK, checksum verified\n";
+		    } else {
+			print "\n";  # the front end expects the error to reside at the last line without any noise
+			die "checksum mismatch: got '$checksum_got' != expect '$checksum'\n";
+		    }
+		}
+	    };
+	    if (my $err = $@) {
+		unlink $tmpfilename or warn "unable to clean up temporory file '$tmpfilename' - $!";
+		die $err;
+	    }
+
 	    print "target node: $node\n";
 	    print "target file: $dest\n";
 	    print "file size is: $size\n";
-- 
2.30.2





  parent reply	other threads:[~2021-08-03 12:17 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-03 12:16 [pve-devel] [PATCH-SERIES v3 http-server/storage/manager] fix #3505: add checksum and algorithm to iso upload Lorenz Stechauner
2021-08-03 12:16 ` [pve-devel] [PATCH v3 http-server 1/1] anyevent: move unlink from http-server to endpoint Lorenz Stechauner
2021-08-03 12:16 ` [pve-devel] [PATCH v3 storage 1/3] status: move unlink from http-server to enpoint Lorenz Stechauner
2021-08-23 16:14   ` Thomas Lamprecht
2021-08-26 16:30   ` Thomas Lamprecht
2021-08-30 11:54     ` Lorenz Stechauner
2021-08-31  7:51       ` Thomas Lamprecht
2021-08-03 12:16 ` [pve-devel] [PATCH v3 storage 2/3] status: remove sleep(1) in file upload Lorenz Stechauner
2021-08-03 12:16 ` Lorenz Stechauner [this message]
2021-08-03 12:16 ` [pve-devel] [PATCH v3 manager 1/5] ui: move upload window into UploadToStorage.js Lorenz Stechauner
2021-08-03 12:16 ` [pve-devel] [PATCH v3 manager 2/5] ui: refactor UploadToStorage.js Lorenz Stechauner
2021-08-03 12:16 ` [pve-devel] [PATCH v3 manager 3/5] fix #3505: ui/UploadToStorage: add checksum and algorithm Lorenz Stechauner
2021-08-03 12:16 ` [pve-devel] [PATCH v3 manager 4/5] ui/UploadToStorage: add TaskViewer Lorenz Stechauner
2021-08-03 12:16 ` [pve-devel] [PATCH v3 manager 5/5] ui/UplaodToStorage: check file extension Lorenz Stechauner

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=20210803121640.2530993-5-l.stechauner@proxmox.com \
    --to=l.stechauner@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