2014-10-02 01:26:06 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
2014-10-24 17:12:35 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-12-17 00:57:37 +00:00
|
|
|
"github.com/docker/docker/registry/v2"
|
2014-10-02 01:26:06 +00:00
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
2014-12-17 00:57:37 +00:00
|
|
|
func getV2Builder(e *Endpoint) *v2.URLBuilder {
|
2014-12-19 22:44:18 +00:00
|
|
|
if e.URLBuilder == nil {
|
|
|
|
e.URLBuilder = v2.NewURLBuilder(e.URL)
|
|
|
|
}
|
|
|
|
return e.URLBuilder
|
2014-12-17 00:57:37 +00:00
|
|
|
}
|
2014-10-02 01:26:06 +00:00
|
|
|
|
2014-12-17 00:57:37 +00:00
|
|
|
// 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.
|
2014-12-19 22:44:18 +00:00
|
|
|
func (r *Session) GetV2Authorization(imageName string, readOnly bool) (auth *RequestAuthorization, err error) {
|
2014-12-17 00:57:37 +00:00
|
|
|
scopes := []string{"pull"}
|
|
|
|
if !readOnly {
|
|
|
|
scopes = append(scopes, "push")
|
2014-10-02 01:26:06 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 22:44:18 +00:00
|
|
|
var registry *Endpoint
|
|
|
|
if r.indexEndpoint.URL.Host == IndexServerURL.Host {
|
|
|
|
registry, err = NewEndpoint(REGISTRYSERVER, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
registry = r.indexEndpoint
|
|
|
|
}
|
|
|
|
registry.URLBuilder = v2.NewURLBuilder(registry.URL)
|
|
|
|
r.indexEndpoint = registry
|
|
|
|
|
|
|
|
log.Debugf("Getting authorization for %s %s", imageName, scopes)
|
2014-12-20 00:14:04 +00:00
|
|
|
return NewRequestAuthorization(r.GetAuthConfig(true), registry, "repository", imageName, scopes), nil
|
2014-10-02 01:26:06 +00: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
|
|
|
|
//
|
2014-12-17 00:57:37 +00:00
|
|
|
func (r *Session) GetV2ImageManifest(imageName, tagName string, auth *RequestAuthorization) ([]byte, error) {
|
|
|
|
routeURL, err := getV2Builder(r.indexEndpoint).BuildManifestURL(imageName, tagName)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "GET"
|
2014-12-17 00:57:37 +00:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
2014-10-02 01:26:06 +00:00
|
|
|
|
2014-12-17 00:57:37 +00:00
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, nil)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-22 22:58:08 +00:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
2014-12-20 00:14:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-02 01:26:06 +00: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
|
|
|
|
}
|
|
|
|
|
|
|
|
// - Succeeded to mount for this image scope
|
|
|
|
// - Failed with no error (So continue to Push the Blob)
|
|
|
|
// - Failed with error
|
2014-12-17 00:57:37 +00:00
|
|
|
func (r *Session) PostV2ImageMountBlob(imageName, sumType, sum string, auth *RequestAuthorization) (bool, error) {
|
|
|
|
routeURL, err := getV2Builder(r.indexEndpoint).BuildBlobURL(imageName, sumType+":"+sum)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2014-12-17 00:57:37 +00:00
|
|
|
method := "HEAD"
|
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
2014-10-02 01:26:06 +00:00
|
|
|
|
2014-12-17 00:57:37 +00:00
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, nil)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2014-12-22 22:58:08 +00:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return false, err
|
2014-12-20 00:14:04 +00:00
|
|
|
}
|
2014-10-02 01:26:06 +00: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 00:22:06 +00:00
|
|
|
case 404:
|
2014-10-02 01:26:06 +00: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)
|
|
|
|
}
|
|
|
|
|
2014-12-17 00:57:37 +00:00
|
|
|
func (r *Session) GetV2ImageBlob(imageName, sumType, sum string, blobWrtr io.Writer, auth *RequestAuthorization) error {
|
|
|
|
routeURL, err := getV2Builder(r.indexEndpoint).BuildBlobURL(imageName, sumType+":"+sum)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "GET"
|
2014-12-17 00:57:37 +00:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, nil)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-22 22:58:08 +00:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return err
|
2014-12-20 00:14:04 +00:00
|
|
|
}
|
2014-10-02 01:26:06 +00: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
|
|
|
|
}
|
|
|
|
|
2014-12-17 00:57:37 +00:00
|
|
|
func (r *Session) GetV2ImageBlobReader(imageName, sumType, sum string, auth *RequestAuthorization) (io.ReadCloser, int64, error) {
|
|
|
|
routeURL, err := getV2Builder(r.indexEndpoint).BuildBlobURL(imageName, sumType+":"+sum)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "GET"
|
2014-12-17 00:57:37 +00:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, nil)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2014-12-22 22:58:08 +00:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return nil, 0, err
|
2014-12-20 00:14:04 +00:00
|
|
|
}
|
2014-10-02 01:26:06 +00: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.
|
2014-12-17 00:57:37 +00:00
|
|
|
func (r *Session) PutV2ImageBlob(imageName, sumType, sumStr string, blobRdr io.Reader, auth *RequestAuthorization) error {
|
|
|
|
routeURL, err := getV2Builder(r.indexEndpoint).BuildBlobUploadURL(imageName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-02 01:26:06 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 00:57:37 +00:00
|
|
|
log.Debugf("[registry] Calling %q %s", "POST", routeURL)
|
|
|
|
req, err := r.reqFactory.NewRequest("POST", routeURL, nil)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
2014-12-17 00:57:37 +00:00
|
|
|
return err
|
2014-10-02 01:26:06 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 22:58:08 +00:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return err
|
2014-12-20 00:14:04 +00:00
|
|
|
}
|
2014-12-17 00:57:37 +00:00
|
|
|
res, _, err := r.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
location := res.Header.Get("Location")
|
|
|
|
|
2014-10-02 01:26:06 +00:00
|
|
|
method := "PUT"
|
2014-12-17 00:57:37 +00:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, location)
|
|
|
|
req, err = r.reqFactory.NewRequest(method, location, blobRdr)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
2014-12-17 00:57:37 +00:00
|
|
|
return err
|
2014-10-02 01:26:06 +00:00
|
|
|
}
|
2014-12-17 00:57:37 +00:00
|
|
|
queryParams := url.Values{}
|
|
|
|
queryParams.Add("digest", sumType+":"+sumStr)
|
|
|
|
req.URL.RawQuery = queryParams.Encode()
|
2014-12-22 22:58:08 +00:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return err
|
2014-12-20 00:14:04 +00:00
|
|
|
}
|
2014-12-17 00:57:37 +00:00
|
|
|
res, _, err = r.doRequest(req)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
2014-12-17 00:57:37 +00:00
|
|
|
return err
|
2014-10-02 01:26:06 +00:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
2014-12-17 00:57:37 +00:00
|
|
|
|
2014-10-02 01:26:06 +00:00
|
|
|
if res.StatusCode != 201 {
|
|
|
|
if res.StatusCode == 401 {
|
2014-12-17 00:57:37 +00:00
|
|
|
return errLoginRequired
|
2014-10-02 01:26:06 +00:00
|
|
|
}
|
2014-12-17 00:57:37 +00:00
|
|
|
return utils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to push %s blob", res.StatusCode, imageName), res)
|
2014-10-02 01:26:06 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 00:57:37 +00:00
|
|
|
return nil
|
2014-10-02 01:26:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally Push the (signed) manifest of the blobs we've just pushed
|
2014-12-17 00:57:37 +00:00
|
|
|
func (r *Session) PutV2ImageManifest(imageName, tagName string, manifestRdr io.Reader, auth *RequestAuthorization) error {
|
|
|
|
routeURL, err := getV2Builder(r.indexEndpoint).BuildManifestURL(imageName, tagName)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "PUT"
|
2014-12-17 00:57:37 +00:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, manifestRdr)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-22 22:58:08 +00:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
|
|
|
return err
|
2014-12-20 00:14:04 +00:00
|
|
|
}
|
2014-10-02 01:26:06 +00:00
|
|
|
res, _, err := r.doRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-17 00:57:37 +00:00
|
|
|
b, _ := ioutil.ReadAll(res.Body)
|
2014-10-02 01:26:06 +00:00
|
|
|
res.Body.Close()
|
2014-12-17 00:57:37 +00:00
|
|
|
if res.StatusCode != 200 {
|
2014-10-02 01:26:06 +00:00
|
|
|
if res.StatusCode == 401 {
|
|
|
|
return errLoginRequired
|
|
|
|
}
|
2014-12-17 00:57:37 +00:00
|
|
|
log.Debugf("Unexpected response from server: %q %#v", b, res.Header)
|
2014-10-02 01:26:06 +00:00
|
|
|
return utils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to push %s:%s manifest", res.StatusCode, imageName, tagName), res)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a repository name, returns a json array of string tags
|
2014-12-17 00:57:37 +00:00
|
|
|
func (r *Session) GetV2RemoteTags(imageName string, auth *RequestAuthorization) ([]string, error) {
|
|
|
|
routeURL, err := getV2Builder(r.indexEndpoint).BuildTagsURL(imageName)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
method := "GET"
|
2014-12-17 00:57:37 +00:00
|
|
|
log.Debugf("[registry] Calling %q %s", method, routeURL)
|
2014-10-02 01:26:06 +00:00
|
|
|
|
2014-12-17 00:57:37 +00:00
|
|
|
req, err := r.reqFactory.NewRequest(method, routeURL, nil)
|
2014-10-02 01:26:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-22 22:58:08 +00:00
|
|
|
if err := auth.Authorize(req); err != nil {
|
2014-12-20 00:14:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-02 01:26:06 +00: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)
|
|
|
|
var tags []string
|
|
|
|
err = decoder.Decode(&tags)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error while decoding the http response: %s", err)
|
|
|
|
}
|
|
|
|
return tags, nil
|
|
|
|
}
|