2013-05-14 21:41:39 -04:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2013-12-04 09:03:51 -05:00
|
|
|
"crypto/tls"
|
2013-05-15 16:22:57 -04:00
|
|
|
"errors"
|
2013-08-04 20:42:24 -04:00
|
|
|
"net"
|
2013-05-14 21:41:39 -04:00
|
|
|
"net/http"
|
2013-12-04 09:03:51 -05:00
|
|
|
"os"
|
2015-05-14 10:12:54 -04:00
|
|
|
"runtime"
|
2013-05-14 21:41:39 -04:00
|
|
|
"strings"
|
2013-08-04 20:42:24 -04:00
|
|
|
"time"
|
2014-04-29 05:01:07 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-02-12 13:23:22 -05:00
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
2015-05-17 05:07:48 -04:00
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2015-05-14 10:12:54 -04:00
|
|
|
"github.com/docker/docker/autogen/dockerversion"
|
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
2015-06-10 13:37:31 -04:00
|
|
|
"github.com/docker/docker/pkg/tlsconfig"
|
2015-05-15 18:03:08 -04:00
|
|
|
"github.com/docker/docker/pkg/useragent"
|
2013-05-14 21:41:39 -04:00
|
|
|
)
|
|
|
|
|
2013-07-22 17:50:32 -04:00
|
|
|
var (
|
2015-01-07 18:42:01 -05:00
|
|
|
ErrAlreadyExists = errors.New("Image already exists")
|
|
|
|
ErrDoesNotExist = errors.New("Image does not exist")
|
|
|
|
errLoginRequired = errors.New("Authentication is required.")
|
2013-07-22 17:50:32 -04:00
|
|
|
)
|
2013-05-15 16:22:57 -04:00
|
|
|
|
2013-12-04 09:03:51 -05:00
|
|
|
type TimeoutType uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
NoTimeout TimeoutType = iota
|
|
|
|
ReceiveTimeout
|
|
|
|
ConnectTimeout
|
|
|
|
)
|
|
|
|
|
2015-05-15 21:35:04 -04: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)
|
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{"docker", dockerversion.VERSION})
|
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{"go", runtime.Version()})
|
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{"git-commit", dockerversion.GITCOMMIT})
|
|
|
|
if kernelVersion, err := kernel.GetKernelVersion(); err == nil {
|
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{"kernel", kernelVersion.String()})
|
|
|
|
}
|
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{"os", runtime.GOOS})
|
|
|
|
httpVersion = append(httpVersion, useragent.VersionInfo{"arch", runtime.GOARCH})
|
|
|
|
|
|
|
|
dockerUserAgent = useragent.AppendVersions("", httpVersion...)
|
2013-12-04 09:03:51 -05:00
|
|
|
}
|
|
|
|
|
2015-02-12 13:23:22 -05:00
|
|
|
func hasFile(files []os.FileInfo, name string) bool {
|
|
|
|
for _, f := range files {
|
|
|
|
if f.Name() == name {
|
|
|
|
return true
|
2013-12-04 09:03:51 -05:00
|
|
|
}
|
2014-08-25 12:50:18 -04:00
|
|
|
}
|
2015-02-12 13:23:22 -05:00
|
|
|
return false
|
2015-05-14 10:12:54 -04:00
|
|
|
}
|
|
|
|
|
2015-05-15 21:35:04 -04: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 10:12:54 -04:00
|
|
|
}
|
2015-05-15 21:35:04 -04:00
|
|
|
if metaHeaders != nil {
|
|
|
|
modifiers = append(modifiers, transport.NewHeaderRequestModifier(metaHeaders))
|
2015-05-14 10:12:54 -04:00
|
|
|
}
|
2015-05-15 21:35:04 -04:00
|
|
|
return modifiers
|
2015-05-14 10:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func HTTPClient(transport http.RoundTripper) *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
CheckRedirect: AddRequiredHeadersToRedirectedRequests,
|
|
|
|
}
|
2013-12-04 09:03:51 -05:00
|
|
|
}
|
|
|
|
|
2014-06-05 14: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-07 17:17:56 -04:00
|
|
|
if hostname == trusted || strings.HasSuffix(hostname, "."+trusted) {
|
2014-06-05 14:37:37 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-03-25 20:33:17 -04:00
|
|
|
func AddRequiredHeadersToRedirectedRequests(req *http.Request, via []*http.Request) error {
|
|
|
|
if via != nil && via[0] != nil {
|
2014-06-05 14:37:37 -04:00
|
|
|
if trustedLocation(req) && trustedLocation(via[0]) {
|
|
|
|
req.Header = via[0].Header
|
2014-08-25 12: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 14:37:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-25 20:33:17 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-12 13:23:22 -05:00
|
|
|
|
|
|
|
func shouldV2Fallback(err errcode.Error) bool {
|
|
|
|
logrus.Debugf("v2 error: %T %v", err, err)
|
|
|
|
switch err.Code {
|
|
|
|
case v2.ErrorCodeUnauthorized, v2.ErrorCodeManifestUnknown:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrNoSupport struct{ Err error }
|
|
|
|
|
|
|
|
func (e ErrNoSupport) Error() string {
|
|
|
|
if e.Err == nil {
|
|
|
|
return "not supported"
|
|
|
|
}
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|