2015-07-24 18:59:36 +00:00
|
|
|
// Package registry contains client primitives to interact with a remote Docker registry.
|
2013-05-15 01:41:39 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2013-12-04 14:03:51 +00:00
|
|
|
"crypto/tls"
|
2015-07-15 20:42:45 +00:00
|
|
|
"crypto/x509"
|
2013-05-15 20:22:57 +00:00
|
|
|
"errors"
|
2015-07-15 20:42:45 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2013-08-05 00:42:24 +00:00
|
|
|
"net"
|
2013-05-15 01:41:39 +00:00
|
|
|
"net/http"
|
2013-12-04 14:03:51 +00:00
|
|
|
"os"
|
2015-07-15 20:42:45 +00:00
|
|
|
"path/filepath"
|
2015-05-14 14:12:54 +00:00
|
|
|
"runtime"
|
2013-05-15 01:41:39 +00:00
|
|
|
"strings"
|
2015-11-02 08:08:02 +00:00
|
|
|
"syscall"
|
2013-08-05 00:42:24 +00:00
|
|
|
"time"
|
2014-04-29 09:01:07 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-02-12 18:23:22 +00:00
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
2015-07-30 23:03:38 +00:00
|
|
|
"github.com/docker/distribution/registry/client"
|
2015-05-17 09:07:48 +00:00
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2015-11-09 18:32:46 +00:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2015-05-14 14:12:54 +00:00
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
2015-06-10 17:37:31 +00:00
|
|
|
"github.com/docker/docker/pkg/tlsconfig"
|
2015-05-15 22:03:08 +00:00
|
|
|
"github.com/docker/docker/pkg/useragent"
|
2013-05-15 01:41:39 +00:00
|
|
|
)
|
|
|
|
|
2013-07-22 21:50:32 +00:00
|
|
|
var (
|
2015-07-21 19:40:36 +00:00
|
|
|
// ErrAlreadyExists is an error returned if an image being pushed
|
|
|
|
// already exists on the remote side
|
2015-01-07 23:42:01 +00:00
|
|
|
ErrAlreadyExists = errors.New("Image already exists")
|
|
|
|
errLoginRequired = errors.New("Authentication is required.")
|
2013-07-22 21:50:32 +00:00
|
|
|
)
|
2013-05-15 20:22:57 +00:00
|
|
|
|
2015-05-16 01:35:04 +00:00
|
|
|
// dockerUserAgent is the User-Agent the Docker client uses to identify itself.
|
|
|
|
// It is populated on init(), comprising version information of different components.
|
|
|
|
var dockerUserAgent string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
httpVersion := make([]useragent.VersionInfo, 0, 6)
|
2015-11-09 18:32:46 +00:00
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "docker", Version: dockerversion.Version})
|
2015-11-02 16:28:34 +00:00
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "go", Version: runtime.Version()})
|
2015-11-09 18:32:46 +00:00
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "git-commit", Version: dockerversion.GitCommit})
|
2015-05-16 01:35:04 +00:00
|
|
|
if kernelVersion, err := kernel.GetKernelVersion(); err == nil {
|
2015-11-02 16:28:34 +00:00
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "kernel", Version: kernelVersion.String()})
|
2015-05-16 01:35:04 +00:00
|
|
|
}
|
2015-11-02 16:28:34 +00:00
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "os", Version: runtime.GOOS})
|
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{Name: "arch", Version: runtime.GOARCH})
|
2015-05-16 01:35:04 +00:00
|
|
|
|
|
|
|
dockerUserAgent = useragent.AppendVersions("", httpVersion...)
|
2015-09-16 17:42:17 +00:00
|
|
|
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
V2Only = true
|
|
|
|
}
|
2013-12-04 14:03:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 17:36:57 +00:00
|
|
|
func newTLSConfig(hostname string, isSecure bool) (*tls.Config, error) {
|
|
|
|
// PreferredServerCipherSuites should have no effect
|
|
|
|
tlsConfig := tlsconfig.ServerDefault
|
|
|
|
|
|
|
|
tlsConfig.InsecureSkipVerify = !isSecure
|
|
|
|
|
|
|
|
if isSecure {
|
2015-08-04 23:30:00 +00:00
|
|
|
hostDir := filepath.Join(CertsDir, cleanPath(hostname))
|
2015-07-28 17:36:57 +00:00
|
|
|
logrus.Debugf("hostDir: %s", hostDir)
|
|
|
|
if err := ReadCertsDirectory(&tlsConfig, hostDir); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tlsConfig, nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 18:23:22 +00:00
|
|
|
func hasFile(files []os.FileInfo, name string) bool {
|
|
|
|
for _, f := range files {
|
|
|
|
if f.Name() == name {
|
|
|
|
return true
|
2013-12-04 14:03:51 +00:00
|
|
|
}
|
2014-08-25 16:50:18 +00:00
|
|
|
}
|
2015-02-12 18:23:22 +00:00
|
|
|
return false
|
2015-05-14 14:12:54 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 20:42:45 +00:00
|
|
|
// ReadCertsDirectory reads the directory for TLS certificates
|
|
|
|
// including roots and certificate pairs and updates the
|
|
|
|
// provided TLS configuration.
|
|
|
|
func ReadCertsDirectory(tlsConfig *tls.Config, directory string) error {
|
|
|
|
fs, err := ioutil.ReadDir(directory)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range fs {
|
|
|
|
if strings.HasSuffix(f.Name(), ".crt") {
|
|
|
|
if tlsConfig.RootCAs == nil {
|
|
|
|
// TODO(dmcgowan): Copy system pool
|
|
|
|
tlsConfig.RootCAs = x509.NewCertPool()
|
|
|
|
}
|
|
|
|
logrus.Debugf("crt: %s", filepath.Join(directory, f.Name()))
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(directory, f.Name()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tlsConfig.RootCAs.AppendCertsFromPEM(data)
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(f.Name(), ".cert") {
|
|
|
|
certName := f.Name()
|
|
|
|
keyName := certName[:len(certName)-5] + ".key"
|
|
|
|
logrus.Debugf("cert: %s", filepath.Join(directory, f.Name()))
|
|
|
|
if !hasFile(fs, keyName) {
|
|
|
|
return fmt.Errorf("Missing key %s for certificate %s", keyName, certName)
|
|
|
|
}
|
|
|
|
cert, err := tls.LoadX509KeyPair(filepath.Join(directory, certName), filepath.Join(directory, keyName))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tlsConfig.Certificates = append(tlsConfig.Certificates, cert)
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(f.Name(), ".key") {
|
|
|
|
keyName := f.Name()
|
|
|
|
certName := keyName[:len(keyName)-4] + ".cert"
|
|
|
|
logrus.Debugf("key: %s", filepath.Join(directory, f.Name()))
|
|
|
|
if !hasFile(fs, certName) {
|
|
|
|
return fmt.Errorf("Missing certificate %s for key %s", certName, keyName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-16 01:35:04 +00:00
|
|
|
// DockerHeaders returns request modifiers that ensure requests have
|
|
|
|
// the User-Agent header set to dockerUserAgent and that metaHeaders
|
|
|
|
// are added.
|
|
|
|
func DockerHeaders(metaHeaders http.Header) []transport.RequestModifier {
|
|
|
|
modifiers := []transport.RequestModifier{
|
|
|
|
transport.NewHeaderRequestModifier(http.Header{"User-Agent": []string{dockerUserAgent}}),
|
2015-05-14 14:12:54 +00:00
|
|
|
}
|
2015-05-16 01:35:04 +00:00
|
|
|
if metaHeaders != nil {
|
|
|
|
modifiers = append(modifiers, transport.NewHeaderRequestModifier(metaHeaders))
|
2015-05-14 14:12:54 +00:00
|
|
|
}
|
2015-05-16 01:35:04 +00:00
|
|
|
return modifiers
|
2015-05-14 14:12:54 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 19:40:36 +00:00
|
|
|
// HTTPClient returns a HTTP client structure which uses the given transport
|
|
|
|
// and contains the necessary headers for redirected requests
|
2015-05-14 14:12:54 +00:00
|
|
|
func HTTPClient(transport http.RoundTripper) *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Transport: transport,
|
2015-07-21 19:40:36 +00:00
|
|
|
CheckRedirect: addRequiredHeadersToRedirectedRequests,
|
2015-05-14 14:12:54 +00:00
|
|
|
}
|
2013-12-04 14:03:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 18:37:37 +00:00
|
|
|
func trustedLocation(req *http.Request) bool {
|
|
|
|
var (
|
|
|
|
trusteds = []string{"docker.com", "docker.io"}
|
|
|
|
hostname = strings.SplitN(req.Host, ":", 2)[0]
|
|
|
|
)
|
|
|
|
if req.URL.Scheme != "https" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, trusted := range trusteds {
|
2014-06-07 21:17:56 +00:00
|
|
|
if hostname == trusted || strings.HasSuffix(hostname, "."+trusted) {
|
2014-06-05 18:37:37 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-21 19:40:36 +00:00
|
|
|
// addRequiredHeadersToRedirectedRequests adds the necessary redirection headers
|
|
|
|
// for redirected requests
|
|
|
|
func addRequiredHeadersToRedirectedRequests(req *http.Request, via []*http.Request) error {
|
2014-03-26 00:33:17 +00:00
|
|
|
if via != nil && via[0] != nil {
|
2014-06-05 18:37:37 +00:00
|
|
|
if trustedLocation(req) && trustedLocation(via[0]) {
|
|
|
|
req.Header = via[0].Header
|
2014-08-25 16:50:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for k, v := range via[0].Header {
|
|
|
|
if k != "Authorization" {
|
|
|
|
for _, vv := range v {
|
|
|
|
req.Header.Add(k, vv)
|
2014-06-05 18:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-26 00:33:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-12 18:23:22 +00:00
|
|
|
|
|
|
|
func shouldV2Fallback(err errcode.Error) bool {
|
|
|
|
logrus.Debugf("v2 error: %T %v", err, err)
|
|
|
|
switch err.Code {
|
2015-10-31 00:46:25 +00:00
|
|
|
case errcode.ErrorCodeUnauthorized, v2.ErrorCodeManifestUnknown:
|
2015-02-12 18:23:22 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-21 19:40:36 +00:00
|
|
|
// ErrNoSupport is an error type used for errors indicating that an operation
|
|
|
|
// is not supported. It encapsulates a more specific error.
|
2015-02-12 18:23:22 +00:00
|
|
|
type ErrNoSupport struct{ Err error }
|
|
|
|
|
|
|
|
func (e ErrNoSupport) Error() string {
|
|
|
|
if e.Err == nil {
|
|
|
|
return "not supported"
|
|
|
|
}
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
2015-07-21 19:40:36 +00:00
|
|
|
// ContinueOnError returns true if we should fallback to the next endpoint
|
|
|
|
// as a result of this error.
|
2015-02-12 18:23:22 +00:00
|
|
|
func ContinueOnError(err error) bool {
|
|
|
|
switch v := err.(type) {
|
|
|
|
case errcode.Errors:
|
|
|
|
return ContinueOnError(v[0])
|
|
|
|
case ErrNoSupport:
|
|
|
|
return ContinueOnError(v.Err)
|
|
|
|
case errcode.Error:
|
|
|
|
return shouldV2Fallback(v)
|
2015-07-30 23:03:38 +00:00
|
|
|
case *client.UnexpectedHTTPResponseError:
|
|
|
|
return true
|
2015-11-02 08:08:02 +00:00
|
|
|
case error:
|
2015-11-18 00:12:11 +00:00
|
|
|
return !strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error()))
|
2015-02-12 18:23:22 +00:00
|
|
|
}
|
2015-07-30 23:03:38 +00: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 true
|
2015-02-12 18:23:22 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 19:40:36 +00:00
|
|
|
// NewTransport returns a new HTTP transport. If tlsConfig is nil, it uses the
|
|
|
|
// default TLS configuration.
|
2015-02-12 18:23:22 +00:00
|
|
|
func NewTransport(tlsConfig *tls.Config) *http.Transport {
|
|
|
|
if tlsConfig == nil {
|
|
|
|
var cfg = tlsconfig.ServerDefault
|
|
|
|
tlsConfig = &cfg
|
|
|
|
}
|
|
|
|
return &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
// TODO(dmcgowan): Call close idle connections when complete and use keep alive
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
}
|
|
|
|
}
|