[lxc-devel] [lxd/master] Fix partial image fingerprint matches

stgraber on Github lxc-bot at linuxcontainers.org
Fri Jan 27 21:49:00 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 411 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170127/6eaf3110/attachment.bin>
-------------- next part --------------
From f4d1da798f74ea03f7e32138a427b84ee0d33696 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 27 Jan 2017 16:46:55 -0500
Subject: [PATCH] Fix partial image fingerprint matches
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

We should always ensure that we have a single result.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/db_images.go | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/lxd/db_images.go b/lxd/db_images.go
index c3aef67..c8bc4fa 100644
--- a/lxd/db_images.go
+++ b/lxd/db_images.go
@@ -152,11 +152,26 @@ func dbImageGet(db *sql.DB, fingerprint string, public bool, strictMatching bool
 	}
 
 	err = dbQueryRowScan(db, query, inargs, outfmt)
-
 	if err != nil {
 		return -1, nil, err // Likely: there are no rows for this fingerprint
 	}
 
+	// Validate we only have a single match
+	if !strictMatching {
+		query = "SELECT COUNT(id) FROM images WHERE fingerprint LIKE ?"
+		count := 0
+		outfmt := []interface{}{&count}
+
+		err = dbQueryRowScan(db, query, inargs, outfmt)
+		if err != nil {
+			return -1, nil, err
+		}
+
+		if count > 1 {
+			return -1, nil, fmt.Errorf("Partial fingerprint matches more than one image")
+		}
+	}
+
 	// Some of the dates can be nil in the DB, let's process them.
 	if create != nil {
 		image.CreatedAt = *create


More information about the lxc-devel mailing list