Browse code

Merge pull request #10305 from jlhawn/use_tarsum_v1

Always store images with tarsum.v1 checksum added

Arnaud Porterie authored on 2015/01/27 07:42:07
Showing 3 changed files
... ...
@@ -80,8 +80,8 @@ func LoadImage(root string) (*Image, error) {
80 80
 
81 81
 // StoreImage stores file system layer data for the given image to the
82 82
 // image's registered storage driver. Image metadata is stored in a file
83
-// at the specified root directory. This function also computes the TarSum
84
-// of `layerData` (currently using tarsum.dev).
83
+// at the specified root directory. This function also computes a checksum
84
+// of `layerData` if the image does not have one already.
85 85
 func StoreImage(img *Image, layerData archive.ArchiveReader, root string) error {
86 86
 	// Store the layer
87 87
 	var (
... ...
@@ -95,15 +95,18 @@ func StoreImage(img *Image, layerData archive.ArchiveReader, root string) error
95 95
 	if layerData != nil {
96 96
 		// If the image doesn't have a checksum, we should add it. The layer
97 97
 		// checksums are verified when they are pulled from a remote, but when
98
-		// a container is committed it should be added here.
99
-		if img.Checksum == "" {
98
+		// a container is committed it should be added here. Also ensure that
99
+		// the stored checksum has the latest version of tarsum (assuming we
100
+		// are using tarsum).
101
+		if tarsum.VersionLabelForChecksum(img.Checksum) != tarsum.Version1.String() {
102
+			// Either there was no checksum or it's not a tarsum.v1
100 103
 			layerDataDecompressed, err := archive.DecompressStream(layerData)
101 104
 			if err != nil {
102 105
 				return err
103 106
 			}
104 107
 			defer layerDataDecompressed.Close()
105 108
 
106
-			if layerTarSum, err = tarsum.NewTarSum(layerDataDecompressed, true, tarsum.VersionDev); err != nil {
109
+			if layerTarSum, err = tarsum.NewTarSum(layerDataDecompressed, true, tarsum.Version1); err != nil {
107 110
 				return err
108 111
 			}
109 112
 
... ...
@@ -122,6 +122,7 @@ type tHashConfig struct {
122 122
 }
123 123
 
124 124
 var (
125
+	// NOTE: DO NOT include MD5 or SHA1, which are considered insecure.
125 126
 	standardHashConfigs = map[string]tHashConfig{
126 127
 		"sha256": {name: "sha256", hash: crypto.SHA256},
127 128
 		"sha512": {name: "sha512", hash: crypto.SHA512},
... ...
@@ -22,6 +22,18 @@ const (
22 22
 	VersionDev
23 23
 )
24 24
 
25
+// VersionLabelForChecksum returns the label for the given tarsum
26
+// checksum, i.e., everything before the first `+` character in
27
+// the string or an empty string if no label separator is found.
28
+func VersionLabelForChecksum(checksum string) string {
29
+	// Checksums are in the form: {versionLabel}+{hashID}:{hex}
30
+	sepIndex := strings.Index(checksum, "+")
31
+	if sepIndex < 0 {
32
+		return ""
33
+	}
34
+	return checksum[:sepIndex]
35
+}
36
+
25 37
 // Get a list of all known tarsum Version
26 38
 func GetVersions() []Version {
27 39
 	v := []Version{}