github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/registry/session.go (about)

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