github.com/duglin/docker@v1.13.1/registry/session.go (about)

     1  package registry
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/sha256"
     6  	"errors"
     7  	"sync"
     8  	// this is required for some certificates
     9  	_ "crypto/sha512"
    10  	"encoding/hex"
    11  	"encoding/json"
    12  	"fmt"
    13  	"io"
    14  	"io/ioutil"
    15  	"net/http"
    16  	"net/http/cookiejar"
    17  	"net/url"
    18  	"strconv"
    19  	"strings"
    20  
    21  	"github.com/Sirupsen/logrus"
    22  	"github.com/docker/distribution/registry/api/errcode"
    23  	"github.com/docker/docker/api/types"
    24  	registrytypes "github.com/docker/docker/api/types/registry"
    25  	"github.com/docker/docker/pkg/httputils"
    26  	"github.com/docker/docker/pkg/ioutils"
    27  	"github.com/docker/docker/pkg/stringid"
    28  	"github.com/docker/docker/pkg/tarsum"
    29  	"github.com/docker/docker/reference"
    30  )
    31  
    32  var (
    33  	// ErrRepoNotFound is returned if the repository didn't exist on the
    34  	// remote side
    35  	ErrRepoNotFound = errors.New("Repository not found")
    36  )
    37  
    38  // A Session is used to communicate with a V1 registry
    39  type Session struct {
    40  	indexEndpoint *V1Endpoint
    41  	client        *http.Client
    42  	// TODO(tiborvass): remove authConfig
    43  	authConfig *types.AuthConfig
    44  	id         string
    45  }
    46  
    47  type authTransport struct {
    48  	http.RoundTripper
    49  	*types.AuthConfig
    50  
    51  	alwaysSetBasicAuth bool
    52  	token              []string
    53  
    54  	mu     sync.Mutex                      // guards modReq
    55  	modReq map[*http.Request]*http.Request // original -> modified
    56  }
    57  
    58  // AuthTransport handles the auth layer when communicating with a v1 registry (private or official)
    59  //
    60  // For private v1 registries, set alwaysSetBasicAuth to true.
    61  //
    62  // For the official v1 registry, if there isn't already an Authorization header in the request,
    63  // but there is an X-Docker-Token header set to true, then Basic Auth will be used to set the Authorization header.
    64  // After sending the request with the provided base http.RoundTripper, if an X-Docker-Token header, representing
    65  // a token, is present in the response, then it gets cached and sent in the Authorization header of all subsequent
    66  // requests.
    67  //
    68  // If the server sends a token without the client having requested it, it is ignored.
    69  //
    70  // This RoundTripper also has a CancelRequest method important for correct timeout handling.
    71  func AuthTransport(base http.RoundTripper, authConfig *types.AuthConfig, alwaysSetBasicAuth bool) http.RoundTripper {
    72  	if base == nil {
    73  		base = http.DefaultTransport
    74  	}
    75  	return &authTransport{
    76  		RoundTripper:       base,
    77  		AuthConfig:         authConfig,
    78  		alwaysSetBasicAuth: alwaysSetBasicAuth,
    79  		modReq:             make(map[*http.Request]*http.Request),
    80  	}
    81  }
    82  
    83  // cloneRequest returns a clone of the provided *http.Request.
    84  // The clone is a shallow copy of the struct and its Header map.
    85  func cloneRequest(r *http.Request) *http.Request {
    86  	// shallow copy of the struct
    87  	r2 := new(http.Request)
    88  	*r2 = *r
    89  	// deep copy of the Header
    90  	r2.Header = make(http.Header, len(r.Header))
    91  	for k, s := range r.Header {
    92  		r2.Header[k] = append([]string(nil), s...)
    93  	}
    94  
    95  	return r2
    96  }
    97  
    98  // RoundTrip changes an HTTP request's headers to add the necessary
    99  // authentication-related headers
   100  func (tr *authTransport) RoundTrip(orig *http.Request) (*http.Response, error) {
   101  	// Authorization should not be set on 302 redirect for untrusted locations.
   102  	// This logic mirrors the behavior in addRequiredHeadersToRedirectedRequests.
   103  	// As the authorization logic is currently implemented in RoundTrip,
   104  	// a 302 redirect is detected by looking at the Referrer header as go http package adds said header.
   105  	// This is safe as Docker doesn't set Referrer in other scenarios.
   106  	if orig.Header.Get("Referer") != "" && !trustedLocation(orig) {
   107  		return tr.RoundTripper.RoundTrip(orig)
   108  	}
   109  
   110  	req := cloneRequest(orig)
   111  	tr.mu.Lock()
   112  	tr.modReq[orig] = req
   113  	tr.mu.Unlock()
   114  
   115  	if tr.alwaysSetBasicAuth {
   116  		if tr.AuthConfig == nil {
   117  			return nil, errors.New("unexpected error: empty auth config")
   118  		}
   119  		req.SetBasicAuth(tr.Username, tr.Password)
   120  		return tr.RoundTripper.RoundTrip(req)
   121  	}
   122  
   123  	// Don't override
   124  	if req.Header.Get("Authorization") == "" {
   125  		if req.Header.Get("X-Docker-Token") == "true" && tr.AuthConfig != nil && len(tr.Username) > 0 {
   126  			req.SetBasicAuth(tr.Username, tr.Password)
   127  		} else if len(tr.token) > 0 {
   128  			req.Header.Set("Authorization", "Token "+strings.Join(tr.token, ","))
   129  		}
   130  	}
   131  	resp, err := tr.RoundTripper.RoundTrip(req)
   132  	if err != nil {
   133  		delete(tr.modReq, orig)
   134  		return nil, err
   135  	}
   136  	if len(resp.Header["X-Docker-Token"]) > 0 {
   137  		tr.token = resp.Header["X-Docker-Token"]
   138  	}
   139  	resp.Body = &ioutils.OnEOFReader{
   140  		Rc: resp.Body,
   141  		Fn: func() {
   142  			tr.mu.Lock()
   143  			delete(tr.modReq, orig)
   144  			tr.mu.Unlock()
   145  		},
   146  	}
   147  	return resp, nil
   148  }
   149  
   150  // CancelRequest cancels an in-flight request by closing its connection.
   151  func (tr *authTransport) CancelRequest(req *http.Request) {
   152  	type canceler interface {
   153  		CancelRequest(*http.Request)
   154  	}
   155  	if cr, ok := tr.RoundTripper.(canceler); ok {
   156  		tr.mu.Lock()
   157  		modReq := tr.modReq[req]
   158  		delete(tr.modReq, req)
   159  		tr.mu.Unlock()
   160  		cr.CancelRequest(modReq)
   161  	}
   162  }
   163  
   164  func authorizeClient(client *http.Client, authConfig *types.AuthConfig, endpoint *V1Endpoint) error {
   165  	var alwaysSetBasicAuth bool
   166  
   167  	// If we're working with a standalone private registry over HTTPS, send Basic Auth headers
   168  	// alongside all our requests.
   169  	if endpoint.String() != IndexServer && endpoint.URL.Scheme == "https" {
   170  		info, err := endpoint.Ping()
   171  		if err != nil {
   172  			return err
   173  		}
   174  		if info.Standalone && authConfig != nil {
   175  			logrus.Debugf("Endpoint %s is eligible for private registry. Enabling decorator.", endpoint.String())
   176  			alwaysSetBasicAuth = true
   177  		}
   178  	}
   179  
   180  	// Annotate the transport unconditionally so that v2 can
   181  	// properly fallback on v1 when an image is not found.
   182  	client.Transport = AuthTransport(client.Transport, authConfig, alwaysSetBasicAuth)
   183  
   184  	jar, err := cookiejar.New(nil)
   185  	if err != nil {
   186  		return errors.New("cookiejar.New is not supposed to return an error")
   187  	}
   188  	client.Jar = jar
   189  
   190  	return nil
   191  }
   192  
   193  func newSession(client *http.Client, authConfig *types.AuthConfig, endpoint *V1Endpoint) *Session {
   194  	return &Session{
   195  		authConfig:    authConfig,
   196  		client:        client,
   197  		indexEndpoint: endpoint,
   198  		id:            stringid.GenerateRandomID(),
   199  	}
   200  }
   201  
   202  // NewSession creates a new session
   203  // TODO(tiborvass): remove authConfig param once registry client v2 is vendored
   204  func NewSession(client *http.Client, authConfig *types.AuthConfig, endpoint *V1Endpoint) (*Session, error) {
   205  	if err := authorizeClient(client, authConfig, endpoint); err != nil {
   206  		return nil, err
   207  	}
   208  
   209  	return newSession(client, authConfig, endpoint), nil
   210  }
   211  
   212  // ID returns this registry session's ID.
   213  func (r *Session) ID() string {
   214  	return r.id
   215  }
   216  
   217  // GetRemoteHistory retrieves the history of a given image from the registry.
   218  // It returns a list of the parent's JSON files (including the requested image).
   219  func (r *Session) GetRemoteHistory(imgID, registry string) ([]string, error) {
   220  	res, err := r.client.Get(registry + "images/" + imgID + "/ancestry")
   221  	if err != nil {
   222  		return nil, err
   223  	}
   224  	defer res.Body.Close()
   225  	if res.StatusCode != 200 {
   226  		if res.StatusCode == 401 {
   227  			return nil, errcode.ErrorCodeUnauthorized.WithArgs()
   228  		}
   229  		return nil, httputils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to fetch remote history for %s", res.StatusCode, imgID), res)
   230  	}
   231  
   232  	var history []string
   233  	if err := json.NewDecoder(res.Body).Decode(&history); err != nil {
   234  		return nil, fmt.Errorf("Error while reading the http response: %v", err)
   235  	}
   236  
   237  	logrus.Debugf("Ancestry: %v", history)
   238  	return history, nil
   239  }
   240  
   241  // LookupRemoteImage checks if an image exists in the registry
   242  func (r *Session) LookupRemoteImage(imgID, registry string) error {
   243  	res, err := r.client.Get(registry + "images/" + imgID + "/json")
   244  	if err != nil {
   245  		return err
   246  	}
   247  	res.Body.Close()
   248  	if res.StatusCode != 200 {
   249  		return httputils.NewHTTPRequestError(fmt.Sprintf("HTTP code %d", res.StatusCode), res)
   250  	}
   251  	return nil
   252  }
   253  
   254  // GetRemoteImageJSON retrieves an image's JSON metadata from the registry.
   255  func (r *Session) GetRemoteImageJSON(imgID, registry string) ([]byte, int64, error) {
   256  	res, err := r.client.Get(registry + "images/" + imgID + "/json")
   257  	if err != nil {
   258  		return nil, -1, fmt.Errorf("Failed to download json: %s", err)
   259  	}
   260  	defer res.Body.Close()
   261  	if res.StatusCode != 200 {
   262  		return nil, -1, httputils.NewHTTPRequestError(fmt.Sprintf("HTTP code %d", res.StatusCode), res)
   263  	}
   264  	// if the size header is not present, then set it to '-1'
   265  	imageSize := int64(-1)
   266  	if hdr := res.Header.Get("X-Docker-Size"); hdr != "" {
   267  		imageSize, err = strconv.ParseInt(hdr, 10, 64)
   268  		if err != nil {
   269  			return nil, -1, err
   270  		}
   271  	}
   272  
   273  	jsonString, err := ioutil.ReadAll(res.Body)
   274  	if err != nil {
   275  		return nil, -1, fmt.Errorf("Failed to parse downloaded json: %v (%s)", err, jsonString)
   276  	}
   277  	return jsonString, imageSize, nil
   278  }
   279  
   280  // GetRemoteImageLayer retrieves an image layer from the registry
   281  func (r *Session) GetRemoteImageLayer(imgID, registry string, imgSize int64) (io.ReadCloser, error) {
   282  	var (
   283  		statusCode = 0
   284  		res        *http.Response
   285  		err        error
   286  		imageURL   = fmt.Sprintf("%simages/%s/layer", registry, imgID)
   287  	)
   288  
   289  	req, err := http.NewRequest("GET", imageURL, nil)
   290  	if err != nil {
   291  		return nil, fmt.Errorf("Error while getting from the server: %v", err)
   292  	}
   293  	statusCode = 0
   294  	res, err = r.client.Do(req)
   295  	if err != nil {
   296  		logrus.Debugf("Error contacting registry %s: %v", registry, err)
   297  		// the only case err != nil && res != nil is https://golang.org/src/net/http/client.go#L515
   298  		if res != nil {
   299  			if res.Body != nil {
   300  				res.Body.Close()
   301  			}
   302  			statusCode = res.StatusCode
   303  		}
   304  		return nil, fmt.Errorf("Server error: Status %d while fetching image layer (%s)",
   305  			statusCode, imgID)
   306  	}
   307  
   308  	if res.StatusCode != 200 {
   309  		res.Body.Close()
   310  		return nil, fmt.Errorf("Server error: Status %d while fetching image layer (%s)",
   311  			res.StatusCode, imgID)
   312  	}
   313  
   314  	if res.Header.Get("Accept-Ranges") == "bytes" && imgSize > 0 {
   315  		logrus.Debug("server supports resume")
   316  		return httputils.ResumableRequestReaderWithInitialResponse(r.client, req, 5, imgSize, res), nil
   317  	}
   318  	logrus.Debug("server doesn't support resume")
   319  	return res.Body, nil
   320  }
   321  
   322  // GetRemoteTag retrieves the tag named in the askedTag argument from the given
   323  // repository. It queries each of the registries supplied in the registries
   324  // argument, and returns data from the first one that answers the query
   325  // successfully.
   326  func (r *Session) GetRemoteTag(registries []string, repositoryRef reference.Named, askedTag string) (string, error) {
   327  	repository := repositoryRef.RemoteName()
   328  
   329  	if strings.Count(repository, "/") == 0 {
   330  		// This will be removed once the registry supports auto-resolution on
   331  		// the "library" namespace
   332  		repository = "library/" + repository
   333  	}
   334  	for _, host := range registries {
   335  		endpoint := fmt.Sprintf("%srepositories/%s/tags/%s", host, repository, askedTag)
   336  		res, err := r.client.Get(endpoint)
   337  		if err != nil {
   338  			return "", err
   339  		}
   340  
   341  		logrus.Debugf("Got status code %d from %s", res.StatusCode, endpoint)
   342  		defer res.Body.Close()
   343  
   344  		if res.StatusCode == 404 {
   345  			return "", ErrRepoNotFound
   346  		}
   347  		if res.StatusCode != 200 {
   348  			continue
   349  		}
   350  
   351  		var tagID string
   352  		if err := json.NewDecoder(res.Body).Decode(&tagID); err != nil {
   353  			return "", err
   354  		}
   355  		return tagID, nil
   356  	}
   357  	return "", fmt.Errorf("Could not reach any registry endpoint")
   358  }
   359  
   360  // GetRemoteTags retrieves all tags from the given repository. It queries each
   361  // of the registries supplied in the registries argument, and returns data from
   362  // the first one that answers the query successfully. It returns a map with
   363  // tag names as the keys and image IDs as the values.
   364  func (r *Session) GetRemoteTags(registries []string, repositoryRef reference.Named) (map[string]string, error) {
   365  	repository := repositoryRef.RemoteName()
   366  
   367  	if strings.Count(repository, "/") == 0 {
   368  		// This will be removed once the registry supports auto-resolution on
   369  		// the "library" namespace
   370  		repository = "library/" + repository
   371  	}
   372  	for _, host := range registries {
   373  		endpoint := fmt.Sprintf("%srepositories/%s/tags", host, repository)
   374  		res, err := r.client.Get(endpoint)
   375  		if err != nil {
   376  			return nil, err
   377  		}
   378  
   379  		logrus.Debugf("Got status code %d from %s", res.StatusCode, endpoint)
   380  		defer res.Body.Close()
   381  
   382  		if res.StatusCode == 404 {
   383  			return nil, ErrRepoNotFound
   384  		}
   385  		if res.StatusCode != 200 {
   386  			continue
   387  		}
   388  
   389  		result := make(map[string]string)
   390  		if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
   391  			return nil, err
   392  		}
   393  		return result, nil
   394  	}
   395  	return nil, fmt.Errorf("Could not reach any registry endpoint")
   396  }
   397  
   398  func buildEndpointsList(headers []string, indexEp string) ([]string, error) {
   399  	var endpoints []string
   400  	parsedURL, err := url.Parse(indexEp)
   401  	if err != nil {
   402  		return nil, err
   403  	}
   404  	var urlScheme = parsedURL.Scheme
   405  	// The registry's URL scheme has to match the Index'
   406  	for _, ep := range headers {
   407  		epList := strings.Split(ep, ",")
   408  		for _, epListElement := range epList {
   409  			endpoints = append(
   410  				endpoints,
   411  				fmt.Sprintf("%s://%s/v1/", urlScheme, strings.TrimSpace(epListElement)))
   412  		}
   413  	}
   414  	return endpoints, nil
   415  }
   416  
   417  // GetRepositoryData returns lists of images and endpoints for the repository
   418  func (r *Session) GetRepositoryData(name reference.Named) (*RepositoryData, error) {
   419  	repositoryTarget := fmt.Sprintf("%srepositories/%s/images", r.indexEndpoint.String(), name.RemoteName())
   420  
   421  	logrus.Debugf("[registry] Calling GET %s", repositoryTarget)
   422  
   423  	req, err := http.NewRequest("GET", repositoryTarget, nil)
   424  	if err != nil {
   425  		return nil, err
   426  	}
   427  	// this will set basic auth in r.client.Transport and send cached X-Docker-Token headers for all subsequent requests
   428  	req.Header.Set("X-Docker-Token", "true")
   429  	res, err := r.client.Do(req)
   430  	if err != nil {
   431  		// check if the error is because of i/o timeout
   432  		// and return a non-obtuse error message for users
   433  		// "Get https://index.docker.io/v1/repositories/library/busybox/images: i/o timeout"
   434  		// was a top search on the docker user forum
   435  		if isTimeout(err) {
   436  			return nil, fmt.Errorf("Network timed out while trying to connect to %s. You may want to check your internet connection or if you are behind a proxy.", repositoryTarget)
   437  		}
   438  		return nil, fmt.Errorf("Error while pulling image: %v", err)
   439  	}
   440  	defer res.Body.Close()
   441  	if res.StatusCode == 401 {
   442  		return nil, errcode.ErrorCodeUnauthorized.WithArgs()
   443  	}
   444  	// TODO: Right now we're ignoring checksums in the response body.
   445  	// In the future, we need to use them to check image validity.
   446  	if res.StatusCode == 404 {
   447  		return nil, httputils.NewHTTPRequestError(fmt.Sprintf("HTTP code: %d", res.StatusCode), res)
   448  	} else if res.StatusCode != 200 {
   449  		errBody, err := ioutil.ReadAll(res.Body)
   450  		if err != nil {
   451  			logrus.Debugf("Error reading response body: %s", err)
   452  		}
   453  		return nil, httputils.NewHTTPRequestError(fmt.Sprintf("Error: Status %d trying to pull repository %s: %q", res.StatusCode, name.RemoteName(), errBody), res)
   454  	}
   455  
   456  	var endpoints []string
   457  	if res.Header.Get("X-Docker-Endpoints") != "" {
   458  		endpoints, err = buildEndpointsList(res.Header["X-Docker-Endpoints"], r.indexEndpoint.String())
   459  		if err != nil {
   460  			return nil, err
   461  		}
   462  	} else {
   463  		// Assume the endpoint is on the same host
   464  		endpoints = append(endpoints, fmt.Sprintf("%s://%s/v1/", r.indexEndpoint.URL.Scheme, req.URL.Host))
   465  	}
   466  
   467  	remoteChecksums := []*ImgData{}
   468  	if err := json.NewDecoder(res.Body).Decode(&remoteChecksums); err != nil {
   469  		return nil, err
   470  	}
   471  
   472  	// Forge a better object from the retrieved data
   473  	imgsData := make(map[string]*ImgData, len(remoteChecksums))
   474  	for _, elem := range remoteChecksums {
   475  		imgsData[elem.ID] = elem
   476  	}
   477  
   478  	return &RepositoryData{
   479  		ImgList:   imgsData,
   480  		Endpoints: endpoints,
   481  	}, nil
   482  }
   483  
   484  // PushImageChecksumRegistry uploads checksums for an image
   485  func (r *Session) PushImageChecksumRegistry(imgData *ImgData, registry string) error {
   486  	u := registry + "images/" + imgData.ID + "/checksum"
   487  
   488  	logrus.Debugf("[registry] Calling PUT %s", u)
   489  
   490  	req, err := http.NewRequest("PUT", u, nil)
   491  	if err != nil {
   492  		return err
   493  	}
   494  	req.Header.Set("X-Docker-Checksum", imgData.Checksum)
   495  	req.Header.Set("X-Docker-Checksum-Payload", imgData.ChecksumPayload)
   496  
   497  	res, err := r.client.Do(req)
   498  	if err != nil {
   499  		return fmt.Errorf("Failed to upload metadata: %v", err)
   500  	}
   501  	defer res.Body.Close()
   502  	if len(res.Cookies()) > 0 {
   503  		r.client.Jar.SetCookies(req.URL, res.Cookies())
   504  	}
   505  	if res.StatusCode != 200 {
   506  		errBody, err := ioutil.ReadAll(res.Body)
   507  		if err != nil {
   508  			return fmt.Errorf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err)
   509  		}
   510  		var jsonBody map[string]string
   511  		if err := json.Unmarshal(errBody, &jsonBody); err != nil {
   512  			errBody = []byte(err.Error())
   513  		} else if jsonBody["error"] == "Image already exists" {
   514  			return ErrAlreadyExists
   515  		}
   516  		return fmt.Errorf("HTTP code %d while uploading metadata: %q", res.StatusCode, errBody)
   517  	}
   518  	return nil
   519  }
   520  
   521  // PushImageJSONRegistry pushes JSON metadata for a local image to the registry
   522  func (r *Session) PushImageJSONRegistry(imgData *ImgData, jsonRaw []byte, registry string) error {
   523  
   524  	u := registry + "images/" + imgData.ID + "/json"
   525  
   526  	logrus.Debugf("[registry] Calling PUT %s", u)
   527  
   528  	req, err := http.NewRequest("PUT", u, bytes.NewReader(jsonRaw))
   529  	if err != nil {
   530  		return err
   531  	}
   532  	req.Header.Add("Content-type", "application/json")
   533  
   534  	res, err := r.client.Do(req)
   535  	if err != nil {
   536  		return fmt.Errorf("Failed to upload metadata: %s", err)
   537  	}
   538  	defer res.Body.Close()
   539  	if res.StatusCode == 401 && strings.HasPrefix(registry, "http://") {
   540  		return httputils.NewHTTPRequestError("HTTP code 401, Docker will not send auth headers over HTTP.", res)
   541  	}
   542  	if res.StatusCode != 200 {
   543  		errBody, err := ioutil.ReadAll(res.Body)
   544  		if err != nil {
   545  			return httputils.NewHTTPRequestError(fmt.Sprintf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err), res)
   546  		}
   547  		var jsonBody map[string]string
   548  		if err := json.Unmarshal(errBody, &jsonBody); err != nil {
   549  			errBody = []byte(err.Error())
   550  		} else if jsonBody["error"] == "Image already exists" {
   551  			return ErrAlreadyExists
   552  		}
   553  		return httputils.NewHTTPRequestError(fmt.Sprintf("HTTP code %d while uploading metadata: %q", res.StatusCode, errBody), res)
   554  	}
   555  	return nil
   556  }
   557  
   558  // PushImageLayerRegistry sends the checksum of an image layer to the registry
   559  func (r *Session) PushImageLayerRegistry(imgID string, layer io.Reader, registry string, jsonRaw []byte) (checksum string, checksumPayload string, err error) {
   560  	u := registry + "images/" + imgID + "/layer"
   561  
   562  	logrus.Debugf("[registry] Calling PUT %s", u)
   563  
   564  	tarsumLayer, err := tarsum.NewTarSum(layer, false, tarsum.Version0)
   565  	if err != nil {
   566  		return "", "", err
   567  	}
   568  	h := sha256.New()
   569  	h.Write(jsonRaw)
   570  	h.Write([]byte{'\n'})
   571  	checksumLayer := io.TeeReader(tarsumLayer, h)
   572  
   573  	req, err := http.NewRequest("PUT", u, checksumLayer)
   574  	if err != nil {
   575  		return "", "", err
   576  	}
   577  	req.Header.Add("Content-Type", "application/octet-stream")
   578  	req.ContentLength = -1
   579  	req.TransferEncoding = []string{"chunked"}
   580  	res, err := r.client.Do(req)
   581  	if err != nil {
   582  		return "", "", fmt.Errorf("Failed to upload layer: %v", err)
   583  	}
   584  	if rc, ok := layer.(io.Closer); ok {
   585  		if err := rc.Close(); err != nil {
   586  			return "", "", err
   587  		}
   588  	}
   589  	defer res.Body.Close()
   590  
   591  	if res.StatusCode != 200 {
   592  		errBody, err := ioutil.ReadAll(res.Body)
   593  		if err != nil {
   594  			return "", "", httputils.NewHTTPRequestError(fmt.Sprintf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err), res)
   595  		}
   596  		return "", "", httputils.NewHTTPRequestError(fmt.Sprintf("Received HTTP code %d while uploading layer: %q", res.StatusCode, errBody), res)
   597  	}
   598  
   599  	checksumPayload = "sha256:" + hex.EncodeToString(h.Sum(nil))
   600  	return tarsumLayer.Sum(jsonRaw), checksumPayload, nil
   601  }
   602  
   603  // PushRegistryTag pushes a tag on the registry.
   604  // Remote has the format '<user>/<repo>
   605  func (r *Session) PushRegistryTag(remote reference.Named, revision, tag, registry string) error {
   606  	// "jsonify" the string
   607  	revision = "\"" + revision + "\""
   608  	path := fmt.Sprintf("repositories/%s/tags/%s", remote.RemoteName(), tag)
   609  
   610  	req, err := http.NewRequest("PUT", registry+path, strings.NewReader(revision))
   611  	if err != nil {
   612  		return err
   613  	}
   614  	req.Header.Add("Content-type", "application/json")
   615  	req.ContentLength = int64(len(revision))
   616  	res, err := r.client.Do(req)
   617  	if err != nil {
   618  		return err
   619  	}
   620  	res.Body.Close()
   621  	if res.StatusCode != 200 && res.StatusCode != 201 {
   622  		return httputils.NewHTTPRequestError(fmt.Sprintf("Internal server error: %d trying to push tag %s on %s", res.StatusCode, tag, remote.RemoteName()), res)
   623  	}
   624  	return nil
   625  }
   626  
   627  // PushImageJSONIndex uploads an image list to the repository
   628  func (r *Session) PushImageJSONIndex(remote reference.Named, imgList []*ImgData, validate bool, regs []string) (*RepositoryData, error) {
   629  	cleanImgList := []*ImgData{}
   630  	if validate {
   631  		for _, elem := range imgList {
   632  			if elem.Checksum != "" {
   633  				cleanImgList = append(cleanImgList, elem)
   634  			}
   635  		}
   636  	} else {
   637  		cleanImgList = imgList
   638  	}
   639  
   640  	imgListJSON, err := json.Marshal(cleanImgList)
   641  	if err != nil {
   642  		return nil, err
   643  	}
   644  	var suffix string
   645  	if validate {
   646  		suffix = "images"
   647  	}
   648  	u := fmt.Sprintf("%srepositories/%s/%s", r.indexEndpoint.String(), remote.RemoteName(), suffix)
   649  	logrus.Debugf("[registry] PUT %s", u)
   650  	logrus.Debugf("Image list pushed to index:\n%s", imgListJSON)
   651  	headers := map[string][]string{
   652  		"Content-type": {"application/json"},
   653  		// this will set basic auth in r.client.Transport and send cached X-Docker-Token headers for all subsequent requests
   654  		"X-Docker-Token": {"true"},
   655  	}
   656  	if validate {
   657  		headers["X-Docker-Endpoints"] = regs
   658  	}
   659  
   660  	// Redirect if necessary
   661  	var res *http.Response
   662  	for {
   663  		if res, err = r.putImageRequest(u, headers, imgListJSON); err != nil {
   664  			return nil, err
   665  		}
   666  		if !shouldRedirect(res) {
   667  			break
   668  		}
   669  		res.Body.Close()
   670  		u = res.Header.Get("Location")
   671  		logrus.Debugf("Redirected to %s", u)
   672  	}
   673  	defer res.Body.Close()
   674  
   675  	if res.StatusCode == 401 {
   676  		return nil, errcode.ErrorCodeUnauthorized.WithArgs()
   677  	}
   678  
   679  	var tokens, endpoints []string
   680  	if !validate {
   681  		if res.StatusCode != 200 && res.StatusCode != 201 {
   682  			errBody, err := ioutil.ReadAll(res.Body)
   683  			if err != nil {
   684  				logrus.Debugf("Error reading response body: %s", err)
   685  			}
   686  			return nil, httputils.NewHTTPRequestError(fmt.Sprintf("Error: Status %d trying to push repository %s: %q", res.StatusCode, remote.RemoteName(), errBody), res)
   687  		}
   688  		tokens = res.Header["X-Docker-Token"]
   689  		logrus.Debugf("Auth token: %v", tokens)
   690  
   691  		if res.Header.Get("X-Docker-Endpoints") == "" {
   692  			return nil, fmt.Errorf("Index response didn't contain any endpoints")
   693  		}
   694  		endpoints, err = buildEndpointsList(res.Header["X-Docker-Endpoints"], r.indexEndpoint.String())
   695  		if err != nil {
   696  			return nil, err
   697  		}
   698  	} else {
   699  		if res.StatusCode != 204 {
   700  			errBody, err := ioutil.ReadAll(res.Body)
   701  			if err != nil {
   702  				logrus.Debugf("Error reading response body: %s", err)
   703  			}
   704  			return nil, httputils.NewHTTPRequestError(fmt.Sprintf("Error: Status %d trying to push checksums %s: %q", res.StatusCode, remote.RemoteName(), errBody), res)
   705  		}
   706  	}
   707  
   708  	return &RepositoryData{
   709  		Endpoints: endpoints,
   710  	}, nil
   711  }
   712  
   713  func (r *Session) putImageRequest(u string, headers map[string][]string, body []byte) (*http.Response, error) {
   714  	req, err := http.NewRequest("PUT", u, bytes.NewReader(body))
   715  	if err != nil {
   716  		return nil, err
   717  	}
   718  	req.ContentLength = int64(len(body))
   719  	for k, v := range headers {
   720  		req.Header[k] = v
   721  	}
   722  	response, err := r.client.Do(req)
   723  	if err != nil {
   724  		return nil, err
   725  	}
   726  	return response, nil
   727  }
   728  
   729  func shouldRedirect(response *http.Response) bool {
   730  	return response.StatusCode >= 300 && response.StatusCode < 400
   731  }
   732  
   733  // SearchRepositories performs a search against the remote repository
   734  func (r *Session) SearchRepositories(term string, limit int) (*registrytypes.SearchResults, error) {
   735  	if limit < 1 || limit > 100 {
   736  		return nil, fmt.Errorf("Limit %d is outside the range of [1, 100]", limit)
   737  	}
   738  	logrus.Debugf("Index server: %s", r.indexEndpoint)
   739  	u := r.indexEndpoint.String() + "search?q=" + url.QueryEscape(term) + "&n=" + url.QueryEscape(fmt.Sprintf("%d", limit))
   740  
   741  	req, err := http.NewRequest("GET", u, nil)
   742  	if err != nil {
   743  		return nil, fmt.Errorf("Error while getting from the server: %v", err)
   744  	}
   745  	// Have the AuthTransport send authentication, when logged in.
   746  	req.Header.Set("X-Docker-Token", "true")
   747  	res, err := r.client.Do(req)
   748  	if err != nil {
   749  		return nil, err
   750  	}
   751  	defer res.Body.Close()
   752  	if res.StatusCode != 200 {
   753  		return nil, httputils.NewHTTPRequestError(fmt.Sprintf("Unexpected status code %d", res.StatusCode), res)
   754  	}
   755  	result := new(registrytypes.SearchResults)
   756  	return result, json.NewDecoder(res.Body).Decode(result)
   757  }
   758  
   759  // GetAuthConfig returns the authentication settings for a session
   760  // TODO(tiborvass): remove this once registry client v2 is vendored
   761  func (r *Session) GetAuthConfig(withPasswd bool) *types.AuthConfig {
   762  	password := ""
   763  	if withPasswd {
   764  		password = r.authConfig.Password
   765  	}
   766  	return &types.AuthConfig{
   767  		Username: r.authConfig.Username,
   768  		Password: password,
   769  	}
   770  }
   771  
   772  func isTimeout(err error) bool {
   773  	type timeout interface {
   774  		Timeout() bool
   775  	}
   776  	e := err
   777  	switch urlErr := err.(type) {
   778  	case *url.Error:
   779  		e = urlErr.Err
   780  	}
   781  	t, ok := e.(timeout)
   782  	return ok && t.Timeout()
   783  }