Skip to content

Commit

Permalink
Merge pull request moby#4297 from dotcloud/add-simple-checksum
Browse files Browse the repository at this point in the history
registry: Added simple checksums (sha256) for layers
  • Loading branch information
vieux committed Feb 27, 2014
2 parents 0e356d4 + a025122 commit 389eba4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
29 changes: 18 additions & 11 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package registry

import (
"bytes"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -388,6 +389,7 @@ func (r *Registry) PushImageChecksumRegistry(imgData *ImgData, registry string,
}
setTokenAuth(req, token)
req.Header.Set("X-Docker-Checksum", imgData.Checksum)
req.Header.Set("X-Docker-Checksum-Payload", imgData.ChecksumPayload)

res, err := doWithCookies(r.client, req)
if err != nil {
Expand Down Expand Up @@ -446,38 +448,42 @@ func (r *Registry) PushImageJSONRegistry(imgData *ImgData, jsonRaw []byte, regis
return nil
}

func (r *Registry) PushImageLayerRegistry(imgID string, layer io.Reader, registry string, token []string, jsonRaw []byte) (checksum string, err error) {
func (r *Registry) PushImageLayerRegistry(imgID string, layer io.Reader, registry string, token []string, jsonRaw []byte) (checksum string, checksumPayload string, err error) {

utils.Debugf("[registry] Calling PUT %s", registry+"images/"+imgID+"/layer")

tarsumLayer := &utils.TarSum{Reader: layer}
h := sha256.New()
checksumLayer := &utils.CheckSum{Reader: layer, Hash: h}
tarsumLayer := &utils.TarSum{Reader: checksumLayer}

req, err := r.reqFactory.NewRequest("PUT", registry+"images/"+imgID+"/layer", tarsumLayer)
if err != nil {
return "", err
return "", "", err
}
req.ContentLength = -1
req.TransferEncoding = []string{"chunked"}
setTokenAuth(req, token)
res, err := doWithCookies(r.client, req)
if err != nil {
return "", fmt.Errorf("Failed to upload layer: %s", err)
return "", "", fmt.Errorf("Failed to upload layer: %s", err)
}
if rc, ok := layer.(io.Closer); ok {
if err := rc.Close(); err != nil {
return "", err
return "", "", err
}
}
defer res.Body.Close()

if res.StatusCode != 200 {
errBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", utils.NewHTTPRequestError(fmt.Sprintf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err), res)
return "", "", utils.NewHTTPRequestError(fmt.Sprintf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err), res)
}
return "", utils.NewHTTPRequestError(fmt.Sprintf("Received HTTP code %d while uploading layer: %s", res.StatusCode, errBody), res)
return "", "", utils.NewHTTPRequestError(fmt.Sprintf("Received HTTP code %d while uploading layer: %s", res.StatusCode, errBody), res)
}
return tarsumLayer.Sum(jsonRaw), nil

checksumPayload = "sha256:" + checksumLayer.Sum()
return tarsumLayer.Sum(jsonRaw), checksumPayload, nil
}

// push a tag on the registry.
Expand Down Expand Up @@ -671,9 +677,10 @@ type RepositoryData struct {
}

type ImgData struct {
ID string `json:"id"`
Checksum string `json:"checksum,omitempty"`
Tag string `json:",omitempty"`
ID string `json:"id"`
Checksum string `json:"checksum,omitempty"`
ChecksumPayload string `json:"-"`
Tag string `json:",omitempty"`
}

type Registry struct {
Expand Down
2 changes: 1 addition & 1 deletion registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestPushImageJSONRegistry(t *testing.T) {
func TestPushImageLayerRegistry(t *testing.T) {
r := spawnTestRegistry(t)
layer := strings.NewReader("")
_, err := r.PushImageLayerRegistry(IMAGE_ID, layer, makeURL("/v1/"), TOKEN, []byte{})
_, _, err := r.PushImageLayerRegistry(IMAGE_ID, layer, makeURL("/v1/"), TOKEN, []byte{})
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1499,11 +1499,12 @@ func (srv *Server) pushImage(r *registry.Registry, out io.Writer, remote, imgID,
defer os.RemoveAll(layerData.Name())

// Send the layer
checksum, err = r.PushImageLayerRegistry(imgData.ID, utils.ProgressReader(layerData, int(layerData.Size), out, sf, false, utils.TruncateID(imgData.ID), "Pushing"), ep, token, jsonRaw)
checksum, checksumPayload, err := r.PushImageLayerRegistry(imgData.ID, utils.ProgressReader(layerData, int(layerData.Size), out, sf, false, utils.TruncateID(imgData.ID), "Pushing"), ep, token, jsonRaw)
if err != nil {
return "", err
}
imgData.Checksum = checksum
imgData.ChecksumPayload = checksumPayload
// Send the checksum
if err := r.PushImageChecksumRegistry(imgData, ep, token); err != nil {
return "", err
Expand Down
24 changes: 24 additions & 0 deletions utils/checksum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import (
"encoding/hex"
"hash"
"io"
)

type CheckSum struct {
io.Reader
Hash hash.Hash
}

func (cs *CheckSum) Read(buf []byte) (int, error) {
n, err := cs.Reader.Read(buf)
if err == nil {
cs.Hash.Write(buf[:n])
}
return n, err
}

func (cs *CheckSum) Sum() string {
return hex.EncodeToString(cs.Hash.Sum(nil))
}

0 comments on commit 389eba4

Please sign in to comment.