From bac83c76084d2b8667b023b4506f385db993deb9 Mon Sep 17 00:00:00 2001 From: Jake Moshenko Date: Thu, 20 Feb 2014 17:57:58 -0500 Subject: [PATCH 1/5] Fix registry auth by storing the string passed on the command line, and allowing for credential selection by normalizing on hostname. Also, remove remote ping calls from CmdPush and CmdPull. Docker-DCO-1.1-Signed-off-by: Jake Moshenko (github: jakedt) --- docs/registry.go | 9 +++------ docs/registry_test.go | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/docs/registry.go b/docs/registry.go index df943023..37e107fa 100644 --- a/docs/registry.go +++ b/docs/registry.go @@ -91,7 +91,7 @@ func validateRepositoryName(repositoryName string) error { return nil } -// Resolves a repository name to a endpoint + name +// Resolves a repository name to a hostname + name func ResolveRepositoryName(reposName string) (string, string, error) { if strings.Contains(reposName, "://") { // It cannot contain a scheme! @@ -117,11 +117,8 @@ func ResolveRepositoryName(reposName string) (string, string, error) { if err := validateRepositoryName(reposName); err != nil { return "", "", err } - endpoint, err := ExpandAndVerifyRegistryUrl(hostname) - if err != nil { - return "", "", err - } - return endpoint, reposName, err + + return hostname, reposName, nil } // this method expands the registry name as used in the prefix of a repo diff --git a/docs/registry_test.go b/docs/registry_test.go index 16bc431e..5e398f99 100644 --- a/docs/registry_test.go +++ b/docs/registry_test.go @@ -145,7 +145,7 @@ func TestResolveRepositoryName(t *testing.T) { if err != nil { t.Fatal(err) } - assertEqual(t, ep, "http://"+u+"/v1/", "Expected endpoint to be "+u) + assertEqual(t, ep, u, "Expected endpoint to be "+u) assertEqual(t, repo, "private/moonbase", "Expected endpoint to be private/moonbase") } From 3bf0ee5e52b608b8b6d9bafa8a7fac6fb0fdf6f5 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Sun, 23 Feb 2014 18:33:46 -0800 Subject: [PATCH 2/5] registry: Added simple checksums (sha256) for layers Docker-DCO-1.1-Signed-off-by: Sam Alba (github: samalba) --- docs/registry.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/docs/registry.go b/docs/registry.go index df943023..7bdf12c8 100644 --- a/docs/registry.go +++ b/docs/registry.go @@ -2,6 +2,7 @@ package registry import ( "bytes" + "crypto/sha256" "encoding/json" "errors" "fmt" @@ -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 { @@ -446,26 +448,28 @@ 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() @@ -473,11 +477,13 @@ func (r *Registry) PushImageLayerRegistry(imgID string, layer io.Reader, registr 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. @@ -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:"checksum,omitempty"` + Tag string `json:",omitempty"` } type Registry struct { From bae6dc35bc18d86b0a62a48e5f8e1c0e6bea7e31 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 24 Feb 2014 09:04:27 -0800 Subject: [PATCH 3/5] registry: Fixed tests Docker-DCO-1.1-Signed-off-by: Sam Alba (github: samalba) --- docs/registry_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/registry_test.go b/docs/registry_test.go index 16bc431e..5b33485d 100644 --- a/docs/registry_test.go +++ b/docs/registry_test.go @@ -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) } From ba8dbe4b9b6d280a2aeaeefbcd238c598bdb1a73 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 24 Feb 2014 12:40:33 -0800 Subject: [PATCH 4/5] registry: Removed checksumPayload from exported fields Docker-DCO-1.1-Signed-off-by: Sam Alba (github: samalba) --- docs/registry.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/registry.go b/docs/registry.go index 7bdf12c8..d0ddedb7 100644 --- a/docs/registry.go +++ b/docs/registry.go @@ -389,7 +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) + req.Header.Set("X-Docker-Checksum-Payload", imgData.checksumPayload) res, err := doWithCookies(r.client, req) if err != nil { @@ -679,8 +679,8 @@ type RepositoryData struct { type ImgData struct { ID string `json:"id"` Checksum string `json:"checksum,omitempty"` - ChecksumPayload string `json:"checksum,omitempty"` Tag string `json:",omitempty"` + checksumPayload string } type Registry struct { From f29683f794763a1aacd96a4b5ac9a49db830a4b1 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Tue, 25 Feb 2014 16:06:04 -0800 Subject: [PATCH 5/5] registry: Fixed unexported field Docker-DCO-1.1-Signed-off-by: Sam Alba (github: samalba) --- docs/registry.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/registry.go b/docs/registry.go index d0ddedb7..c570e7b7 100644 --- a/docs/registry.go +++ b/docs/registry.go @@ -389,7 +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) + req.Header.Set("X-Docker-Checksum-Payload", imgData.ChecksumPayload) res, err := doWithCookies(r.client, req) if err != nil { @@ -679,8 +679,8 @@ type RepositoryData struct { type ImgData struct { ID string `json:"id"` Checksum string `json:"checksum,omitempty"` + ChecksumPayload string `json:"-"` Tag string `json:",omitempty"` - checksumPayload string } type Registry struct {