2015-11-18 17:18:44 -05:00
|
|
|
package distribution
|
|
|
|
|
|
|
|
import (
|
2015-11-04 00:03:12 -05:00
|
|
|
"fmt"
|
2015-11-18 17:18:44 -05:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2016-01-21 13:01:51 -05:00
|
|
|
"syscall"
|
2015-11-18 17:18:44 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
2015-11-13 19:59:01 -05:00
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
2015-11-18 17:18:44 -05:00
|
|
|
"github.com/docker/distribution/registry/client"
|
|
|
|
"github.com/docker/distribution/registry/client/auth"
|
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2015-11-13 19:59:01 -05:00
|
|
|
"github.com/docker/docker/distribution/xfer"
|
2016-01-04 13:36:01 -05:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2015-11-18 17:18:44 -05:00
|
|
|
"github.com/docker/docker/registry"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-11-18 17:18:44 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2015-12-04 16:42:33 -05:00
|
|
|
// fallbackError wraps an error that can possibly allow fallback to a different
|
|
|
|
// endpoint.
|
|
|
|
type fallbackError struct {
|
|
|
|
// err is the error being wrapped.
|
|
|
|
err error
|
|
|
|
// confirmedV2 is set to true if it was confirmed that the registry
|
|
|
|
// supports the v2 protocol. This is used to limit fallbacks to the v1
|
|
|
|
// protocol.
|
|
|
|
confirmedV2 bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error renders the FallbackError as a string.
|
|
|
|
func (f fallbackError) Error() string {
|
|
|
|
return f.err.Error()
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:18:44 -05:00
|
|
|
type dumbCredentialStore struct {
|
2015-12-11 23:11:42 -05:00
|
|
|
auth *types.AuthConfig
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dcs dumbCredentialStore) Basic(*url.URL) (string, string) {
|
|
|
|
return dcs.auth.Username, dcs.auth.Password
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:33:10 -05:00
|
|
|
// conn wraps a net.Conn, and sets a deadline for every read
|
|
|
|
// and write operation.
|
|
|
|
type conn struct {
|
|
|
|
net.Conn
|
|
|
|
readTimeout time.Duration
|
|
|
|
writeTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) Read(b []byte) (int, error) {
|
|
|
|
err := c.Conn.SetReadDeadline(time.Now().Add(c.readTimeout))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return c.Conn.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) Write(b []byte) (int, error) {
|
|
|
|
err := c.Conn.SetWriteDeadline(time.Now().Add(c.writeTimeout))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return c.Conn.Write(b)
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:18:44 -05:00
|
|
|
// NewV2Repository returns a repository (v2 only). It creates a HTTP transport
|
|
|
|
// providing timeout settings and authentication support, and also verifies the
|
|
|
|
// remote API version.
|
2015-12-04 16:42:33 -05:00
|
|
|
func NewV2Repository(ctx context.Context, repoInfo *registry.RepositoryInfo, endpoint registry.APIEndpoint, metaHeaders http.Header, authConfig *types.AuthConfig, actions ...string) (repo distribution.Repository, foundVersion bool, err error) {
|
2015-12-11 14:00:13 -05:00
|
|
|
repoName := repoInfo.FullName()
|
2015-11-18 17:18:44 -05:00
|
|
|
// If endpoint does not support CanonicalName, use the RemoteName instead
|
|
|
|
if endpoint.TrimHostname {
|
2015-12-11 14:00:13 -05:00
|
|
|
repoName = repoInfo.RemoteName()
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(dmcgowan): Call close idle connections when complete, use keep alive
|
|
|
|
base := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2016-01-26 15:33:10 -05:00
|
|
|
Dial: func(network, address string) (net.Conn, error) {
|
|
|
|
dialer := &net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}
|
|
|
|
netConn, err := dialer.Dial(network, address)
|
|
|
|
if err != nil {
|
|
|
|
return netConn, err
|
|
|
|
}
|
|
|
|
return &conn{
|
|
|
|
Conn: netConn,
|
|
|
|
readTimeout: time.Minute,
|
|
|
|
writeTimeout: time.Minute,
|
|
|
|
}, nil
|
|
|
|
},
|
2015-11-18 17:18:44 -05:00
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
TLSClientConfig: endpoint.TLSConfig,
|
|
|
|
// TODO(dmcgowan): Call close idle connections when complete and use keep alive
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
}
|
|
|
|
|
2016-01-04 13:36:01 -05:00
|
|
|
modifiers := registry.DockerHeaders(dockerversion.DockerUserAgent(), metaHeaders)
|
2015-11-18 17:18:44 -05:00
|
|
|
authTransport := transport.NewTransport(base, modifiers...)
|
|
|
|
pingClient := &http.Client{
|
|
|
|
Transport: authTransport,
|
2015-11-13 19:59:01 -05:00
|
|
|
Timeout: 15 * time.Second,
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
endpointStr := strings.TrimRight(endpoint.URL, "/") + "/v2/"
|
|
|
|
req, err := http.NewRequest("GET", endpointStr, nil)
|
|
|
|
if err != nil {
|
2015-12-04 16:42:33 -05:00
|
|
|
return nil, false, err
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
resp, err := pingClient.Do(req)
|
|
|
|
if err != nil {
|
2015-12-04 16:42:33 -05:00
|
|
|
return nil, false, err
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-12-04 16:42:33 -05:00
|
|
|
v2Version := auth.APIVersion{
|
|
|
|
Type: "registry",
|
|
|
|
Version: "2.0",
|
|
|
|
}
|
|
|
|
|
|
|
|
versions := auth.APIVersions(resp, registry.DefaultRegistryVersionHeader)
|
|
|
|
for _, pingVersion := range versions {
|
|
|
|
if pingVersion == v2Version {
|
|
|
|
// The version header indicates we're definitely
|
|
|
|
// talking to a v2 registry. So don't allow future
|
|
|
|
// fallbacks to the v1 protocol.
|
|
|
|
|
|
|
|
foundVersion = true
|
|
|
|
break
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
challengeManager := auth.NewSimpleChallengeManager()
|
|
|
|
if err := challengeManager.AddResponse(resp); err != nil {
|
2015-12-04 16:42:33 -05:00
|
|
|
return nil, foundVersion, err
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
2015-11-04 00:03:12 -05:00
|
|
|
if authConfig.RegistryToken != "" {
|
|
|
|
passThruTokenHandler := &existingTokenHandler{token: authConfig.RegistryToken}
|
|
|
|
modifiers = append(modifiers, auth.NewAuthorizer(challengeManager, passThruTokenHandler))
|
|
|
|
} else {
|
|
|
|
creds := dumbCredentialStore{auth: authConfig}
|
2015-12-11 14:00:13 -05:00
|
|
|
tokenHandler := auth.NewTokenHandler(authTransport, creds, repoName, actions...)
|
2015-11-04 00:03:12 -05:00
|
|
|
basicHandler := auth.NewBasicHandler(creds)
|
|
|
|
modifiers = append(modifiers, auth.NewAuthorizer(challengeManager, tokenHandler, basicHandler))
|
|
|
|
}
|
2015-11-18 17:18:44 -05:00
|
|
|
tr := transport.NewTransport(base, modifiers...)
|
|
|
|
|
2015-12-04 16:42:33 -05:00
|
|
|
repo, err = client.NewRepository(ctx, repoName, endpoint.URL, tr)
|
|
|
|
return repo, foundVersion, err
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
2015-11-04 00:03:12 -05:00
|
|
|
type existingTokenHandler struct {
|
|
|
|
token string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *existingTokenHandler) Scheme() string {
|
|
|
|
return "bearer"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *existingTokenHandler) AuthorizeRequest(req *http.Request, params map[string]string) error {
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", th.token))
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
// retryOnError wraps the error in xfer.DoNotRetry if we should not retry the
|
|
|
|
// operation after this error.
|
|
|
|
func retryOnError(err error) error {
|
|
|
|
switch v := err.(type) {
|
|
|
|
case errcode.Errors:
|
|
|
|
return retryOnError(v[0])
|
|
|
|
case errcode.Error:
|
|
|
|
switch v.Code {
|
|
|
|
case errcode.ErrorCodeUnauthorized, errcode.ErrorCodeUnsupported, errcode.ErrorCodeDenied:
|
|
|
|
return xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
2016-01-20 17:29:38 -05:00
|
|
|
case *url.Error:
|
|
|
|
return retryOnError(v.Err)
|
2015-12-04 16:42:33 -05:00
|
|
|
case *client.UnexpectedHTTPResponseError:
|
|
|
|
return xfer.DoNotRetry{Err: err}
|
2016-01-21 13:01:51 -05:00
|
|
|
case error:
|
|
|
|
if strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error())) {
|
|
|
|
return xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
// let's be nice and fallback if the error is a completely
|
|
|
|
// unexpected one.
|
|
|
|
// If new errors have to be handled in some way, please
|
|
|
|
// add them to the switch above.
|
|
|
|
return err
|
|
|
|
}
|