2014-10-02 03:26:06 +02:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"strconv"
|
|
|
|
|
2014-10-24 19:12:35 +02:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-12-17 01:57:37 +01:00
|
|
|
"github.com/docker/docker/registry/v2"
|
2014-10-02 03:26:06 +02:00
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
2014-12-17 01:57:37 +01:00
|
|
|
func getV2Builder(e *Endpoint) *v2.URLBuilder {
|
2014-12-19 23:44:18 +01:00
|
|
|
if e.URLBuilder == nil {
|
|
|
|
e.URLBuilder = v2.NewURLBuilder(e.URL)
|
|
|
|
}
|
|
|
|
return e.URLBuilder
|
2014-12-17 01:57:37 +01:00
|
|
|
}
|
2014-10-02 03:26:06 +02:00
|
|
|
|
2015-01-15 01:46:31 +01:00
|
|
|
func (r *Session) V2RegistryEndpoint(index *IndexInfo) (ep *Endpoint, err error) {
|
|
|
|
// TODO check if should use Mirror
|
|
|
|
if index.Official {
|
|
|
|
ep, err = newEndpoint(REGISTRYSERVER, true)
|
2014-12-23 22:40:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-01-15 01:46:31 +01:00
|
|
|
err = validateEndpoint(ep)
|
2014-12-19 23:44:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-01-15 01:46:31 +01:00
|
|
|
} else if r.indexEndpoint.String() == index.GetAuthConfigKey() {
|
|
|
|
ep = r.indexEndpoint
|
2014-12-19 23:44:18 +01:00
|
|
|
} else {
|
2015-01-15 01:46:31 +01:00
|
|
|
ep, err = NewEndpoint(index)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ep.URLBuilder = v2.NewURLBuilder(ep.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetV2Authorization gets the authorization needed to the given image
|
|
|
|
// If readonly access is requested, then only the authorization may
|
|
|
|
// only be used for Get operations.
|
|
|
|
func (r *Session) GetV2Authorization(ep *Endpoint, imageName string, readOnly bool) (auth *RequestAuthorization, err error) {
|
|
|
|
scopes := []string{"pull"}
|
|
|
|
if !readOnly {
|
|
|
|
scopes = append(scopes, "push")
|
2014-12-19 23:44:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Getting authorization for %s %s", imageName, scopes)
|
2015-01-15 01:46:31 +01:00
|
|
|
return NewRequestAuthorization(r.GetAuthConfig(true), ep, "repository", imageName, scopes), nil
|
2014-10-02 03:26:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 1) Check if TarSum of each layer exists /v2/
|
|
|
|
// 1.a) if 200, continue
|
|
|
|
// 1.b) if 300, then push the
|
|
|
|
// 1.c) if anything else, err
|
|
|
|
// 2) PUT the created/signed manifest
|
|
|
|
//
|
2015-01-15 01:46:31 +01:00
|
|
|
func (r *Session) GetV2ImageManifest(ep *Endpoint, imageName, tagName string, auth *RequestAuthorization) ([]byte, error) {
|
|
|
|
routeURL, err := getV2Builder(ep).BuildManifestURL(imageName, tagName)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "GET"
|
2014-12-17 01:57:37 +01:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
2014-10-02 03:26:06 +02:00
|
|
|
|
2014-12-17 01:57:37 +01:00
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, nil)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-22 23:58:08 +01:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
2014-12-20 01:14:04 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-02 03:26:06 +02:00
|
|
|
res, _, err := r.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
if res.StatusCode == 401 {
|
|
|
|
return nil, errLoginRequired
|
|
|
|
} else if res.StatusCode == 404 {
|
|
|
|
return nil, ErrDoesNotExist
|
|
|
|
}
|
|
|
|
return nil, utils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to fetch for %s:%s", res.StatusCode, imageName, tagName), res)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error while reading the http response: %s", err)
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2015-01-15 01:46:31 +01:00
|
|
|
// - Succeeded to head image blob (already exists)
|
|
|
|
// - Failed with no error (continue to Push the Blob)
|
2014-10-02 03:26:06 +02:00
|
|
|
// - Failed with error
|
2015-01-15 01:46:31 +01:00
|
|
|
func (r *Session) HeadV2ImageBlob(ep *Endpoint, imageName, sumType, sum string, auth *RequestAuthorization) (bool, error) {
|
|
|
|
routeURL, err := getV2Builder(ep).BuildBlobURL(imageName, sumType+":"+sum)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2014-12-17 01:57:37 +01:00
|
|
|
method := "HEAD"
|
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
2014-10-02 03:26:06 +02:00
|
|
|
|
2014-12-17 01:57:37 +01:00
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, nil)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2014-12-22 23:58:08 +01:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return false, err
|
2014-12-20 01:14:04 +01:00
|
|
|
}
|
2014-10-02 03:26:06 +02:00
|
|
|
res, _, err := r.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
res.Body.Close() // close early, since we're not needing a body on this call .. yet?
|
|
|
|
switch res.StatusCode {
|
|
|
|
case 200:
|
|
|
|
// return something indicating no push needed
|
|
|
|
return true, nil
|
2014-11-15 01:22:06 +01:00
|
|
|
case 404:
|
2014-10-02 03:26:06 +02:00
|
|
|
// return something indicating blob push needed
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("Failed to mount %q - %s:%s : %d", imageName, sumType, sum, res.StatusCode)
|
|
|
|
}
|
|
|
|
|
2015-01-15 01:46:31 +01:00
|
|
|
func (r *Session) GetV2ImageBlob(ep *Endpoint, imageName, sumType, sum string, blobWrtr io.Writer, auth *RequestAuthorization) error {
|
|
|
|
routeURL, err := getV2Builder(ep).BuildBlobURL(imageName, sumType+":"+sum)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "GET"
|
2014-12-17 01:57:37 +01:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, nil)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-22 23:58:08 +01:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return err
|
2014-12-20 01:14:04 +01:00
|
|
|
}
|
2014-10-02 03:26:06 +02:00
|
|
|
res, _, err := r.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
if res.StatusCode == 401 {
|
|
|
|
return errLoginRequired
|
|
|
|
}
|
|
|
|
return utils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to pull %s blob", res.StatusCode, imageName), res)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(blobWrtr, res.Body)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-15 01:46:31 +01:00
|
|
|
func (r *Session) GetV2ImageBlobReader(ep *Endpoint, imageName, sumType, sum string, auth *RequestAuthorization) (io.ReadCloser, int64, error) {
|
|
|
|
routeURL, err := getV2Builder(ep).BuildBlobURL(imageName, sumType+":"+sum)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "GET"
|
2014-12-17 01:57:37 +01:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, nil)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2014-12-22 23:58:08 +01:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return nil, 0, err
|
2014-12-20 01:14:04 +01:00
|
|
|
}
|
2014-10-02 03:26:06 +02:00
|
|
|
res, _, err := r.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
if res.StatusCode == 401 {
|
|
|
|
return nil, 0, errLoginRequired
|
|
|
|
}
|
|
|
|
return nil, 0, utils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to pull %s blob", res.StatusCode, imageName), res)
|
|
|
|
}
|
|
|
|
lenStr := res.Header.Get("Content-Length")
|
|
|
|
l, err := strconv.ParseInt(lenStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Body, l, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the image to the server for storage.
|
|
|
|
// 'layer' is an uncompressed reader of the blob to be pushed.
|
|
|
|
// The server will generate it's own checksum calculation.
|
2015-01-15 01:46:31 +01:00
|
|
|
func (r *Session) PutV2ImageBlob(ep *Endpoint, imageName, sumType, sumStr string, blobRdr io.Reader, auth *RequestAuthorization) error {
|
|
|
|
routeURL, err := getV2Builder(ep).BuildBlobUploadURL(imageName)
|
2014-12-17 01:57:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-02 03:26:06 +02:00
|
|
|
}
|
|
|
|
|
2014-12-17 01:57:37 +01:00
|
|
|
log.Debugf("[registry] Calling %q %s", "POST", routeURL)
|
|
|
|
req, err := r.reqFactory.NewRequest("POST", routeURL, nil)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
2014-12-17 01:57:37 +01:00
|
|
|
return err
|
2014-10-02 03:26:06 +02:00
|
|
|
}
|
|
|
|
|
2014-12-22 23:58:08 +01:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return err
|
2014-12-20 01:14:04 +01:00
|
|
|
}
|
2014-12-17 01:57:37 +01:00
|
|
|
res, _, err := r.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
location := res.Header.Get("Location")
|
|
|
|
|
2014-10-02 03:26:06 +02:00
|
|
|
method := "PUT"
|
2014-12-17 01:57:37 +01:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, location)
|
|
|
|
req, err = r.reqFactory.NewRequest(method, location, blobRdr)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
2014-12-17 01:57:37 +01:00
|
|
|
return err
|
2014-10-02 03:26:06 +02:00
|
|
|
}
|
2015-01-02 20:13:11 +01:00
|
|
|
queryParams := req.URL.Query()
|
2014-12-17 01:57:37 +01:00
|
|
|
queryParams.Add("digest", sumType+":"+sumStr)
|
|
|
|
req.URL.RawQuery = queryParams.Encode()
|
2014-12-22 23:58:08 +01:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return err
|
2014-12-20 01:14:04 +01:00
|
|
|
}
|
2014-12-17 01:57:37 +01:00
|
|
|
res, _, err = r.doRequest(req)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
2014-12-17 01:57:37 +01:00
|
|
|
return err
|
2014-10-02 03:26:06 +02:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
2014-12-17 01:57:37 +01:00
|
|
|
|
2014-10-02 03:26:06 +02:00
|
|
|
if res.StatusCode != 201 {
|
|
|
|
if res.StatusCode == 401 {
|
2014-12-17 01:57:37 +01:00
|
|
|
return errLoginRequired
|
2014-10-02 03:26:06 +02:00
|
|
|
}
|
2014-12-17 01:57:37 +01:00
|
|
|
return utils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to push %s blob", res.StatusCode, imageName), res)
|
2014-10-02 03:26:06 +02:00
|
|
|
}
|
|
|
|
|
2014-12-17 01:57:37 +01:00
|
|
|
return nil
|
2014-10-02 03:26:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally Push the (signed) manifest of the blobs we've just pushed
|
2015-01-15 01:46:31 +01:00
|
|
|
func (r *Session) PutV2ImageManifest(ep *Endpoint, imageName, tagName string, manifestRdr io.Reader, auth *RequestAuthorization) error {
|
|
|
|
routeURL, err := getV2Builder(ep).BuildManifestURL(imageName, tagName)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "PUT"
|
2014-12-17 01:57:37 +01:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, manifestRdr)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-22 23:58:08 +01:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return err
|
2014-12-20 01:14:04 +01:00
|
|
|
}
|
2014-10-02 03:26:06 +02:00
|
|
|
res, _, err := r.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-17 01:57:37 +01:00
|
|
|
b, _ := ioutil.ReadAll(res.Body)
|
2014-10-02 03:26:06 +02:00
|
|
|
res.Body.Close()
|
2014-12-17 01:57:37 +01:00
|
|
|
if res.StatusCode != 200 {
|
2014-10-02 03:26:06 +02:00
|
|
|
if res.StatusCode == 401 {
|
|
|
|
return errLoginRequired
|
|
|
|
}
|
2014-12-17 01:57:37 +01:00
|
|
|
log.Debugf("Unexpected response from server: %q %#v", b, res.Header)
|
2014-10-02 03:26:06 +02:00
|
|
|
return utils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to push %s:%s manifest", res.StatusCode, imageName, tagName), res)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-08 00:55:29 +01:00
|
|
|
type remoteTags struct {
|
|
|
|
name string
|
|
|
|
tags []string
|
|
|
|
}
|
|
|
|
|
2014-10-02 03:26:06 +02:00
|
|
|
// Given a repository name, returns a json array of string tags
|
2015-01-15 01:46:31 +01:00
|
|
|
func (r *Session) GetV2RemoteTags(ep *Endpoint, imageName string, auth *RequestAuthorization) ([]string, error) {
|
|
|
|
routeURL, err := getV2Builder(ep).BuildTagsURL(imageName)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "GET"
|
2014-12-17 01:57:37 +01:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
2014-10-02 03:26:06 +02:00
|
|
|
|
2014-12-17 01:57:37 +01:00
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, nil)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-22 23:58:08 +01:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
2014-12-20 01:14:04 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-02 03:26:06 +02:00
|
|
|
res, _, err := r.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
if res.StatusCode == 401 {
|
|
|
|
return nil, errLoginRequired
|
|
|
|
} else if res.StatusCode == 404 {
|
|
|
|
return nil, ErrDoesNotExist
|
|
|
|
}
|
|
|
|
return nil, utils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to fetch for %s", res.StatusCode, imageName), res)
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(res.Body)
|
2015-01-08 00:55:29 +01:00
|
|
|
var remote remoteTags
|
|
|
|
err = decoder.Decode(&remote)
|
2014-10-02 03:26:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error while decoding the http response: %s", err)
|
|
|
|
}
|
2015-01-08 00:55:29 +01:00
|
|
|
return remote.tags, nil
|
2014-10-02 03:26:06 +02:00
|
|
|
}
|