[lxc-devel] [lxd/master] simplestreams: give better error on invalid source stream

tych0 on Github lxc-bot at linuxcontainers.org
Thu Apr 21 23:05:10 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 591 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160421/ede93ac8/attachment.bin>
-------------- next part --------------
From 35d21ede265b65d2f4e9e0047b9c716a968b9799 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Thu, 21 Apr 2016 17:02:22 -0600
Subject: [PATCH] simplestreams: give better error on invalid source stream

Before, we always tried to hash the body regardless of the status code,
which meant that we would hash the contents of e.g. a 404 message if the
thing wasn't found, and issue a "Hash mismatch" error, masking the real
error.

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 shared/simplestreams.go | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/shared/simplestreams.go b/shared/simplestreams.go
index ab2d540..7842e75 100644
--- a/shared/simplestreams.go
+++ b/shared/simplestreams.go
@@ -482,6 +482,10 @@ func (s *SimpleStreams) downloadFile(path string, hash string, target string, pr
 		}
 		defer resp.Body.Close()
 
+		if resp.StatusCode != http.StatusOK {
+			return fmt.Errorf("invalid simplestreams source: got %d looking for %s", resp.StatusCode, path)
+		}
+
 		body := &TransferProgress{Reader: resp.Body, Length: resp.ContentLength, Handler: progress}
 
 		sha256 := sha256.New()
@@ -490,9 +494,10 @@ func (s *SimpleStreams) downloadFile(path string, hash string, target string, pr
 			return err
 		}
 
-		if fmt.Sprintf("%x", sha256.Sum(nil)) != hash {
+		result := fmt.Sprintf("%x", sha256.Sum(nil))
+		if result != hash {
 			os.Remove(target)
-			return fmt.Errorf("Hash mismatch")
+			return fmt.Errorf("Hash mismatch for %s: %s != %s", path, result, hash)
 		}
 
 		return nil


More information about the lxc-devel mailing list