2015-07-24 21:59:36 +03:00
|
|
|
// Package registry contains client primitives to interact with a remote Docker registry.
|
2013-05-15 05:41:39 +04:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2013-12-04 18:03:51 +04:00
|
|
|
"crypto/tls"
|
2013-05-16 00:22:57 +04:00
|
|
|
"errors"
|
2015-07-15 23:42:45 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2013-08-05 04:42:24 +04:00
|
|
|
"net"
|
2013-05-15 05:41:39 +04:00
|
|
|
"net/http"
|
2013-12-04 18:03:51 +04:00
|
|
|
"os"
|
2015-07-15 23:42:45 +03:00
|
|
|
"path/filepath"
|
2013-05-15 05:41:39 +04:00
|
|
|
"strings"
|
2013-08-05 04:42:24 +04:00
|
|
|
"time"
|
2014-04-29 13:01:07 +04:00
|
|
|
|
2015-03-27 01:22:04 +03:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-17 12:07:48 +03:00
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2016-04-25 14:54:48 +03:00
|
|
|
"github.com/docker/go-connections/sockets"
|
2015-12-30 03:27:12 +03:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2013-05-15 05:41:39 +04:00
|
|
|
)
|
|
|
|
|
2013-07-23 01:50:32 +04:00
|
|
|
var (
|
2015-07-21 22:40:36 +03:00
|
|
|
// ErrAlreadyExists is an error returned if an image being pushed
|
|
|
|
// already exists on the remote side
|
2015-01-08 02:42:01 +03:00
|
|
|
ErrAlreadyExists = errors.New("Image already exists")
|
2013-07-23 01:50:32 +04:00
|
|
|
)
|
2013-05-16 00:22:57 +04:00
|
|
|
|
2015-07-28 20:36:57 +03:00
|
|
|
func newTLSConfig(hostname string, isSecure bool) (*tls.Config, error) {
|
|
|
|
// PreferredServerCipherSuites should have no effect
|
2016-09-03 04:27:20 +03:00
|
|
|
tlsConfig := tlsconfig.ServerDefault()
|
2015-07-28 20:36:57 +03:00
|
|
|
|
|
|
|
tlsConfig.InsecureSkipVerify = !isSecure
|
|
|
|
|
2015-11-20 01:30:29 +03:00
|
|
|
if isSecure && CertsDir != "" {
|
2015-08-05 02:30:00 +03:00
|
|
|
hostDir := filepath.Join(CertsDir, cleanPath(hostname))
|
2015-07-28 20:36:57 +03:00
|
|
|
logrus.Debugf("hostDir: %s", hostDir)
|
2016-09-03 04:27:20 +03:00
|
|
|
if err := ReadCertsDirectory(tlsConfig, hostDir); err != nil {
|
2015-07-28 20:36:57 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-03 04:27:20 +03:00
|
|
|
return tlsConfig, nil
|
2015-07-28 20:36:57 +03:00
|
|
|
}
|
|
|
|
|
2015-02-12 21:23:22 +03:00
|
|
|
func hasFile(files []os.FileInfo, name string) bool {
|
|
|
|
for _, f := range files {
|
|
|
|
if f.Name() == name {
|
|
|
|
return true
|
2013-12-04 18:03:51 +04:00
|
|
|
}
|
2014-08-25 20:50:18 +04:00
|
|
|
}
|
2015-02-12 21:23:22 +03:00
|
|
|
return false
|
2015-05-14 17:12:54 +03:00
|
|
|
}
|
|
|
|
|
2015-07-15 23:42:45 +03: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 {
|
2016-11-01 00:52:07 +03:00
|
|
|
systemPool, err := tlsconfig.SystemCertPool()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get system cert pool: %v", err)
|
|
|
|
}
|
|
|
|
tlsConfig.RootCAs = systemPool
|
2015-07-15 23:42:45 +03:00
|
|
|
}
|
|
|
|
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) {
|
2016-01-20 21:53:41 +03:00
|
|
|
return fmt.Errorf("Missing key %s for client certificate %s. Note that CA certificates should use the extension .crt.", keyName, certName)
|
2015-07-15 23:42:45 +03:00
|
|
|
}
|
|
|
|
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) {
|
2016-01-20 21:53:41 +03:00
|
|
|
return fmt.Errorf("Missing client certificate %s for key %s", certName, keyName)
|
2015-07-15 23:42:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-04 21:36:01 +03:00
|
|
|
// DockerHeaders returns request modifiers with a User-Agent and metaHeaders
|
|
|
|
func DockerHeaders(userAgent string, metaHeaders http.Header) []transport.RequestModifier {
|
|
|
|
modifiers := []transport.RequestModifier{}
|
|
|
|
if userAgent != "" {
|
|
|
|
modifiers = append(modifiers, transport.NewHeaderRequestModifier(http.Header{
|
|
|
|
"User-Agent": []string{userAgent},
|
|
|
|
}))
|
2015-05-14 17:12:54 +03:00
|
|
|
}
|
2015-05-16 04:35:04 +03:00
|
|
|
if metaHeaders != nil {
|
|
|
|
modifiers = append(modifiers, transport.NewHeaderRequestModifier(metaHeaders))
|
2015-05-14 17:12:54 +03:00
|
|
|
}
|
2015-05-16 04:35:04 +03:00
|
|
|
return modifiers
|
2015-05-14 17:12:54 +03:00
|
|
|
}
|
|
|
|
|
2016-05-08 04:36:10 +03:00
|
|
|
// HTTPClient returns an HTTP client structure which uses the given transport
|
2015-07-21 22:40:36 +03:00
|
|
|
// and contains the necessary headers for redirected requests
|
2015-05-14 17:12:54 +03:00
|
|
|
func HTTPClient(transport http.RoundTripper) *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Transport: transport,
|
2015-07-21 22:40:36 +03:00
|
|
|
CheckRedirect: addRequiredHeadersToRedirectedRequests,
|
2015-05-14 17:12:54 +03:00
|
|
|
}
|
2013-12-04 18:03:51 +04:00
|
|
|
}
|
|
|
|
|
2014-06-05 22:37:37 +04: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-08 01:17:56 +04:00
|
|
|
if hostname == trusted || strings.HasSuffix(hostname, "."+trusted) {
|
2014-06-05 22:37:37 +04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-21 22:40:36 +03:00
|
|
|
// addRequiredHeadersToRedirectedRequests adds the necessary redirection headers
|
|
|
|
// for redirected requests
|
|
|
|
func addRequiredHeadersToRedirectedRequests(req *http.Request, via []*http.Request) error {
|
2014-03-26 04:33:17 +04:00
|
|
|
if via != nil && via[0] != nil {
|
2014-06-05 22:37:37 +04:00
|
|
|
if trustedLocation(req) && trustedLocation(via[0]) {
|
|
|
|
req.Header = via[0].Header
|
2014-08-25 20:50:18 +04: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 22:37:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-26 04:33:17 +04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-12 21:23:22 +03:00
|
|
|
|
2015-07-21 22:40:36 +03:00
|
|
|
// NewTransport returns a new HTTP transport. If tlsConfig is nil, it uses the
|
|
|
|
// default TLS configuration.
|
2015-02-12 21:23:22 +03:00
|
|
|
func NewTransport(tlsConfig *tls.Config) *http.Transport {
|
|
|
|
if tlsConfig == nil {
|
2016-09-03 04:27:20 +03:00
|
|
|
tlsConfig = tlsconfig.ServerDefault()
|
2015-02-12 21:23:22 +03:00
|
|
|
}
|
2016-04-25 14:54:48 +03:00
|
|
|
|
|
|
|
direct := &net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
base := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: direct.Dial,
|
2015-02-12 21:23:22 +03:00
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
// TODO(dmcgowan): Call close idle connections when complete and use keep alive
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
}
|
2016-04-25 14:54:48 +03:00
|
|
|
|
|
|
|
proxyDialer, err := sockets.DialerFromEnvironment(direct)
|
|
|
|
if err == nil {
|
|
|
|
base.Dial = proxyDialer.Dial
|
|
|
|
}
|
|
|
|
return base
|
2015-02-12 21:23:22 +03:00
|
|
|
}
|