2018-02-05 16:05:59 -05:00
|
|
|
package registry // import "github.com/docker/docker/registry"
|
2014-08-07 10:43:06 -04:00
|
|
|
|
|
|
|
import (
|
2014-10-06 15:34:39 -04:00
|
|
|
// this is required for some certificates
|
2014-08-07 10:43:06 -04:00
|
|
|
_ "crypto/sha512"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2018-05-19 07:38:54 -04:00
|
|
|
"sync"
|
2014-08-07 10:43:06 -04:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2022-02-26 13:13:43 -05:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2015-05-17 05:07:48 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2017-06-01 17:00:00 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2015-04-07 22:29:29 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-07-19 10:20:13 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2014-08-07 10:43:06 -04:00
|
|
|
)
|
|
|
|
|
2022-02-25 19:05:27 -05:00
|
|
|
// A session is used to communicate with a V1 registry
|
|
|
|
type session struct {
|
2022-02-25 18:02:37 -05:00
|
|
|
indexEndpoint *v1Endpoint
|
2015-05-14 10:12:54 -04:00
|
|
|
client *http.Client
|
2022-02-25 16:14:47 -05:00
|
|
|
id string
|
2014-08-07 10:43:06 -04:00
|
|
|
}
|
|
|
|
|
2015-05-15 21:35:04 -04:00
|
|
|
type authTransport struct {
|
|
|
|
http.RoundTripper
|
2015-12-11 23:11:42 -05:00
|
|
|
*types.AuthConfig
|
2015-05-15 21:35:04 -04:00
|
|
|
|
|
|
|
alwaysSetBasicAuth bool
|
|
|
|
token []string
|
|
|
|
|
|
|
|
mu sync.Mutex // guards modReq
|
|
|
|
modReq map[*http.Request]*http.Request // original -> modified
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:52:20 -04:00
|
|
|
// newAuthTransport handles the auth layer when communicating with a v1 registry (private or official)
|
2015-05-14 10:12:54 -04:00
|
|
|
//
|
|
|
|
// For private v1 registries, set alwaysSetBasicAuth to true.
|
|
|
|
//
|
|
|
|
// For the official v1 registry, if there isn't already an Authorization header in the request,
|
|
|
|
// but there is an X-Docker-Token header set to true, then Basic Auth will be used to set the Authorization header.
|
|
|
|
// After sending the request with the provided base http.RoundTripper, if an X-Docker-Token header, representing
|
|
|
|
// a token, is present in the response, then it gets cached and sent in the Authorization header of all subsequent
|
|
|
|
// requests.
|
|
|
|
//
|
|
|
|
// If the server sends a token without the client having requested it, it is ignored.
|
|
|
|
//
|
|
|
|
// This RoundTripper also has a CancelRequest method important for correct timeout handling.
|
2022-03-18 08:52:20 -04:00
|
|
|
func newAuthTransport(base http.RoundTripper, authConfig *types.AuthConfig, alwaysSetBasicAuth bool) *authTransport {
|
2015-05-15 21:35:04 -04:00
|
|
|
if base == nil {
|
|
|
|
base = http.DefaultTransport
|
|
|
|
}
|
|
|
|
return &authTransport{
|
|
|
|
RoundTripper: base,
|
|
|
|
AuthConfig: authConfig,
|
|
|
|
alwaysSetBasicAuth: alwaysSetBasicAuth,
|
|
|
|
modReq: make(map[*http.Request]*http.Request),
|
|
|
|
}
|
2015-05-14 10:12:54 -04:00
|
|
|
}
|
2014-08-07 10:43:06 -04:00
|
|
|
|
2015-05-17 05:07:48 -04:00
|
|
|
// cloneRequest returns a clone of the provided *http.Request.
|
|
|
|
// The clone is a shallow copy of the struct and its Header map.
|
|
|
|
func cloneRequest(r *http.Request) *http.Request {
|
|
|
|
// shallow copy of the struct
|
|
|
|
r2 := new(http.Request)
|
|
|
|
*r2 = *r
|
|
|
|
// deep copy of the Header
|
|
|
|
r2.Header = make(http.Header, len(r.Header))
|
|
|
|
for k, s := range r.Header {
|
|
|
|
r2.Header[k] = append([]string(nil), s...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r2
|
|
|
|
}
|
|
|
|
|
2016-05-07 21:36:10 -04:00
|
|
|
// RoundTrip changes an HTTP request's headers to add the necessary
|
2015-07-21 15:40:36 -04:00
|
|
|
// authentication-related headers
|
2015-05-15 21:35:04 -04:00
|
|
|
func (tr *authTransport) RoundTrip(orig *http.Request) (*http.Response, error) {
|
2015-06-08 19:56:37 -04:00
|
|
|
// Authorization should not be set on 302 redirect for untrusted locations.
|
2015-07-21 15:40:36 -04:00
|
|
|
// This logic mirrors the behavior in addRequiredHeadersToRedirectedRequests.
|
2015-06-08 19:56:37 -04:00
|
|
|
// As the authorization logic is currently implemented in RoundTrip,
|
2015-12-13 11:00:39 -05:00
|
|
|
// a 302 redirect is detected by looking at the Referrer header as go http package adds said header.
|
|
|
|
// This is safe as Docker doesn't set Referrer in other scenarios.
|
2015-06-08 19:56:37 -04:00
|
|
|
if orig.Header.Get("Referer") != "" && !trustedLocation(orig) {
|
|
|
|
return tr.RoundTripper.RoundTrip(orig)
|
|
|
|
}
|
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
req := cloneRequest(orig)
|
2015-05-15 21:35:04 -04:00
|
|
|
tr.mu.Lock()
|
|
|
|
tr.modReq[orig] = req
|
|
|
|
tr.mu.Unlock()
|
2015-05-14 10:12:54 -04:00
|
|
|
|
|
|
|
if tr.alwaysSetBasicAuth {
|
2015-07-16 12:38:44 -04:00
|
|
|
if tr.AuthConfig == nil {
|
|
|
|
return nil, errors.New("unexpected error: empty auth config")
|
|
|
|
}
|
2015-05-14 10:12:54 -04:00
|
|
|
req.SetBasicAuth(tr.Username, tr.Password)
|
|
|
|
return tr.RoundTripper.RoundTrip(req)
|
2014-08-07 10:43:06 -04:00
|
|
|
}
|
|
|
|
|
2015-05-14 10:12:54 -04:00
|
|
|
// Don't override
|
|
|
|
if req.Header.Get("Authorization") == "" {
|
2015-07-16 12:38:44 -04:00
|
|
|
if req.Header.Get("X-Docker-Token") == "true" && tr.AuthConfig != nil && len(tr.Username) > 0 {
|
2015-05-14 10:12:54 -04:00
|
|
|
req.SetBasicAuth(tr.Username, tr.Password)
|
2015-06-08 19:56:37 -04:00
|
|
|
} else if len(tr.token) > 0 {
|
2015-05-14 10:12:54 -04:00
|
|
|
req.Header.Set("Authorization", "Token "+strings.Join(tr.token, ","))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp, err := tr.RoundTripper.RoundTrip(req)
|
2014-08-07 10:43:06 -04:00
|
|
|
if err != nil {
|
2019-10-27 19:57:15 -04:00
|
|
|
tr.mu.Lock()
|
2015-05-15 21:35:04 -04:00
|
|
|
delete(tr.modReq, orig)
|
2019-10-27 19:57:15 -04:00
|
|
|
tr.mu.Unlock()
|
2014-08-07 10:43:06 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-21 16:53:22 -04:00
|
|
|
if len(resp.Header["X-Docker-Token"]) > 0 {
|
2015-05-14 10:12:54 -04:00
|
|
|
tr.token = resp.Header["X-Docker-Token"]
|
|
|
|
}
|
2015-05-17 05:07:48 -04:00
|
|
|
resp.Body = &ioutils.OnEOFReader{
|
2015-05-15 21:35:04 -04:00
|
|
|
Rc: resp.Body,
|
2015-06-01 16:25:18 -04:00
|
|
|
Fn: func() {
|
|
|
|
tr.mu.Lock()
|
|
|
|
delete(tr.modReq, orig)
|
|
|
|
tr.mu.Unlock()
|
|
|
|
},
|
2015-05-15 21:35:04 -04:00
|
|
|
}
|
2015-05-14 10:12:54 -04:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:35:04 -04:00
|
|
|
// CancelRequest cancels an in-flight request by closing its connection.
|
|
|
|
func (tr *authTransport) CancelRequest(req *http.Request) {
|
|
|
|
type canceler interface {
|
|
|
|
CancelRequest(*http.Request)
|
|
|
|
}
|
|
|
|
if cr, ok := tr.RoundTripper.(canceler); ok {
|
|
|
|
tr.mu.Lock()
|
|
|
|
modReq := tr.modReq[req]
|
|
|
|
delete(tr.modReq, req)
|
|
|
|
tr.mu.Unlock()
|
|
|
|
cr.CancelRequest(modReq)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:02:37 -05:00
|
|
|
func authorizeClient(client *http.Client, authConfig *types.AuthConfig, endpoint *v1Endpoint) error {
|
2015-05-14 10:12:54 -04:00
|
|
|
var alwaysSetBasicAuth bool
|
2014-08-07 10:43:06 -04:00
|
|
|
|
|
|
|
// If we're working with a standalone private registry over HTTPS, send Basic Auth headers
|
2015-05-14 10:12:54 -04:00
|
|
|
// alongside all our requests.
|
2016-03-01 02:07:41 -05:00
|
|
|
if endpoint.String() != IndexServer && endpoint.URL.Scheme == "https" {
|
2022-02-25 18:08:20 -05:00
|
|
|
info, err := endpoint.ping()
|
2014-08-07 10:43:06 -04:00
|
|
|
if err != nil {
|
2016-07-13 16:30:24 -04:00
|
|
|
return err
|
2014-08-07 10:43:06 -04:00
|
|
|
}
|
2015-05-14 10:12:54 -04:00
|
|
|
if info.Standalone && authConfig != nil {
|
|
|
|
logrus.Debugf("Endpoint %s is eligible for private registry. Enabling decorator.", endpoint.String())
|
|
|
|
alwaysSetBasicAuth = true
|
2014-08-07 10:43:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 13:12:52 -04:00
|
|
|
// Annotate the transport unconditionally so that v2 can
|
|
|
|
// properly fallback on v1 when an image is not found.
|
2022-03-18 08:52:20 -04:00
|
|
|
client.Transport = newAuthTransport(client.Transport, authConfig, alwaysSetBasicAuth)
|
2014-08-07 10:43:06 -04:00
|
|
|
|
2015-05-14 10:12:54 -04:00
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
if err != nil {
|
2022-02-26 07:45:12 -05:00
|
|
|
return errdefs.System(errors.New("cookiejar.New is not supposed to return an error"))
|
2015-05-14 10:12:54 -04:00
|
|
|
}
|
|
|
|
client.Jar = jar
|
|
|
|
|
2016-07-13 16:30:24 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:05:27 -05:00
|
|
|
func newSession(client *http.Client, endpoint *v1Endpoint) *session {
|
|
|
|
return &session{
|
2016-07-13 16:30:24 -04:00
|
|
|
client: client,
|
|
|
|
indexEndpoint: endpoint,
|
|
|
|
id: stringid.GenerateRandomID(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 07:29:47 -05:00
|
|
|
// defaultSearchLimit is the default value for maximum number of returned search results.
|
|
|
|
const defaultSearchLimit = 25
|
|
|
|
|
2022-02-25 19:05:27 -05:00
|
|
|
// searchRepositories performs a search against the remote repository
|
2022-02-26 13:13:43 -05:00
|
|
|
func (r *session) searchRepositories(term string, limit int) (*registry.SearchResults, error) {
|
2022-03-02 07:29:47 -05:00
|
|
|
if limit == 0 {
|
|
|
|
limit = defaultSearchLimit
|
|
|
|
}
|
2016-06-01 16:38:14 -04:00
|
|
|
if limit < 1 || limit > 100 {
|
2022-02-26 07:45:12 -05:00
|
|
|
return nil, invalidParamf("limit %d is outside the range of [1, 100]", limit)
|
2016-06-01 16:38:14 -04:00
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Index server: %s", r.indexEndpoint)
|
2016-06-01 16:38:14 -04:00
|
|
|
u := r.indexEndpoint.String() + "search?q=" + url.QueryEscape(term) + "&n=" + url.QueryEscape(fmt.Sprintf("%d", limit))
|
2015-07-09 23:56:23 -04:00
|
|
|
|
2019-10-12 14:42:44 -04:00
|
|
|
req, err := http.NewRequest(http.MethodGet, u, nil)
|
2015-07-09 23:56:23 -04:00
|
|
|
if err != nil {
|
2022-02-26 07:45:12 -05:00
|
|
|
return nil, invalidParamWrapf(err, "error building request")
|
2015-07-09 23:56:23 -04:00
|
|
|
}
|
|
|
|
// Have the AuthTransport send authentication, when logged in.
|
|
|
|
req.Header.Set("X-Docker-Token", "true")
|
|
|
|
res, err := r.client.Do(req)
|
2014-08-07 10:43:06 -04:00
|
|
|
if err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.System(err)
|
2014-08-07 10:43:06 -04:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
2019-10-13 11:25:25 -04:00
|
|
|
if res.StatusCode != http.StatusOK {
|
2020-10-28 08:46:00 -04:00
|
|
|
return nil, &jsonmessage.JSONError{
|
|
|
|
Message: fmt.Sprintf("Unexpected status code %d", res.StatusCode),
|
|
|
|
Code: res.StatusCode,
|
|
|
|
}
|
2014-08-07 10:43:06 -04:00
|
|
|
}
|
2022-02-26 13:13:43 -05:00
|
|
|
result := new(registry.SearchResults)
|
2017-07-19 10:20:13 -04:00
|
|
|
return result, errors.Wrap(json.NewDecoder(res.Body).Decode(result), "error decoding registry search results")
|
2014-08-07 10:43:06 -04:00
|
|
|
}
|