registry/endpoint_v1.go
61c6f206
 package registry
 
 import (
19515a7a
 	"crypto/tls"
61c6f206
 	"encoding/json"
 	"fmt"
 	"io/ioutil"
 	"net/http"
 	"net/url"
 	"strings"
 
6f4d8470
 	"github.com/Sirupsen/logrus"
276c640b
 	"github.com/docker/distribution/registry/client/transport"
907407d0
 	registrytypes "github.com/docker/engine-api/types/registry"
61c6f206
 )
 
f2d481a2
 // V1Endpoint stores basic information about a V1 registry endpoint.
 type V1Endpoint struct {
 	client   *http.Client
 	URL      *url.URL
 	IsSecure bool
61c6f206
 }
 
137c8601
 // NewV1Endpoint parses the given address to return a registry endpoint.
f2d481a2
 func NewV1Endpoint(index *registrytypes.IndexInfo, userAgent string, metaHeaders http.Header) (*V1Endpoint, error) {
e863a07b
 	tlsConfig, err := newTLSConfig(index.Name, index.Secure)
 	if err != nil {
 		return nil, err
 	}
79db131a
 
f2d481a2
 	endpoint, err := newV1EndpointFromStr(GetAuthConfigKey(index), tlsConfig, userAgent, metaHeaders)
61c6f206
 	if err != nil {
 		return nil, err
 	}
79db131a
 
213e3d11
 	if err := validateEndpoint(endpoint); err != nil {
 		return nil, err
 	}
 
 	return endpoint, nil
 }
61c6f206
 
f2d481a2
 func validateEndpoint(endpoint *V1Endpoint) error {
6f4d8470
 	logrus.Debugf("pinging registry endpoint %s", endpoint)
41e20cec
 
6a1ff022
 	// Try HTTPS ping to registry
61c6f206
 	endpoint.URL.Scheme = "https"
 	if _, err := endpoint.Ping(); err != nil {
213e3d11
 		if endpoint.IsSecure {
6a1ff022
 			// If registry is secure and HTTPS failed, show user the error and tell them about `--insecure-registry`
 			// in case that's what they need. DO NOT accept unknown CA certificates, and DO NOT fallback to HTTP.
213e3d11
 			return fmt.Errorf("invalid registry endpoint %s: %v. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry %s` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/%s/ca.crt", endpoint, err, endpoint.URL.Host, endpoint.URL.Host)
6a1ff022
 		}
 
 		// If registry is insecure and HTTPS failed, fallback to HTTP.
6f4d8470
 		logrus.Debugf("Error from registry %q marked as insecure: %v. Insecurely falling back to HTTP", endpoint, err)
61c6f206
 		endpoint.URL.Scheme = "http"
41e20cec
 
 		var err2 error
 		if _, err2 = endpoint.Ping(); err2 == nil {
213e3d11
 			return nil
61c6f206
 		}
6a1ff022
 
213e3d11
 		return fmt.Errorf("invalid registry endpoint %q. HTTPS attempt: %v. HTTP attempt: %v", endpoint, err, err2)
61c6f206
 	}
 
213e3d11
 	return nil
3eba7194
 }
41e20cec
 
f2d481a2
 func newV1Endpoint(address url.URL, tlsConfig *tls.Config, userAgent string, metaHeaders http.Header) (*V1Endpoint, error) {
 	endpoint := &V1Endpoint{
79db131a
 		IsSecure: (tlsConfig == nil || !tlsConfig.InsecureSkipVerify),
 		URL:      new(url.URL),
 	}
41e20cec
 
79db131a
 	*endpoint.URL = address
 
 	// TODO(tiborvass): make sure a ConnectTimeout transport is used
 	tr := NewTransport(tlsConfig)
 	endpoint.client = HTTPClient(transport.NewTransport(tr, DockerHeaders(userAgent, metaHeaders)...))
 	return endpoint, nil
 }
 
f2d481a2
 // trimV1Address trims the version off the address and returns the
 // trimmed address or an error if there is a non-V1 version.
 func trimV1Address(address string) (string, error) {
 	var (
 		chunks        []string
 		apiVersionStr string
 	)
 
 	if strings.HasSuffix(address, "/") {
 		address = address[:len(address)-1]
 	}
 
 	chunks = strings.Split(address, "/")
 	apiVersionStr = chunks[len(chunks)-1]
 	if apiVersionStr == "v1" {
 		return strings.Join(chunks[:len(chunks)-1], "/"), nil
 	}
 
 	for k, v := range apiVersions {
 		if k != APIVersion1 && apiVersionStr == v {
 			return "", fmt.Errorf("unsupported V1 version path %s", apiVersionStr)
 		}
 	}
 
 	return address, nil
 }
 
 func newV1EndpointFromStr(address string, tlsConfig *tls.Config, userAgent string, metaHeaders http.Header) (*V1Endpoint, error) {
79db131a
 	if !strings.HasPrefix(address, "http://") && !strings.HasPrefix(address, "https://") {
41e20cec
 		address = "https://" + address
3eba7194
 	}
41e20cec
 
f2d481a2
 	address, err := trimV1Address(address)
 	if err != nil {
 		return nil, err
 	}
19515a7a
 
f2d481a2
 	uri, err := url.Parse(address)
79db131a
 	if err != nil {
 		return nil, err
 	}
41e20cec
 
f2d481a2
 	endpoint, err := newV1Endpoint(*uri, tlsConfig, userAgent, metaHeaders)
79db131a
 	if err != nil {
3eba7194
 		return nil, err
 	}
19515a7a
 
41e20cec
 	return endpoint, nil
61c6f206
 }
 
927b334e
 // Get the formatted URL for the root of this registry Endpoint
f2d481a2
 func (e *V1Endpoint) String() string {
 	return e.URL.String() + "/v1/"
61c6f206
 }
 
41e20cec
 // Path returns a formatted string for the URL
 // of this endpoint with the given path appended.
f2d481a2
 func (e *V1Endpoint) Path(path string) string {
 	return e.URL.String() + "/v1/" + path
41e20cec
 }
 
f2d481a2
 // Ping returns a PingResult which indicates whether the registry is standalone or not.
 func (e *V1Endpoint) Ping() (PingResult, error) {
6f4d8470
 	logrus.Debugf("attempting v1 ping for registry endpoint %s", e)
41e20cec
 
4fcb9ac4
 	if e.String() == IndexServer {
41e20cec
 		// Skip the check, we know this one is valid
61c6f206
 		// (and we never want to fallback to http in case of error)
4fcb9ac4
 		return PingResult{Standalone: false}, nil
61c6f206
 	}
 
a01cc3ca
 	req, err := http.NewRequest("GET", e.Path("_ping"), nil)
61c6f206
 	if err != nil {
4fcb9ac4
 		return PingResult{Standalone: false}, err
61c6f206
 	}
 
73823e5e
 	resp, err := e.client.Do(req)
61c6f206
 	if err != nil {
4fcb9ac4
 		return PingResult{Standalone: false}, err
61c6f206
 	}
 
 	defer resp.Body.Close()
 
 	jsonString, err := ioutil.ReadAll(resp.Body)
 	if err != nil {
4fcb9ac4
 		return PingResult{Standalone: false}, fmt.Errorf("error while reading the http response: %s", err)
61c6f206
 	}
 
 	// If the header is absent, we assume true for compatibility with earlier
 	// versions of the registry. default to true
4fcb9ac4
 	info := PingResult{
61c6f206
 		Standalone: true,
 	}
 	if err := json.Unmarshal(jsonString, &info); err != nil {
4fcb9ac4
 		logrus.Debugf("Error unmarshalling the _ping PingResult: %s", err)
61c6f206
 		// don't stop here. Just assume sane defaults
 	}
 	if hdr := resp.Header.Get("X-Docker-Registry-Version"); hdr != "" {
6f4d8470
 		logrus.Debugf("Registry version header: '%s'", hdr)
61c6f206
 		info.Version = hdr
 	}
4fcb9ac4
 	logrus.Debugf("PingResult.Version: %q", info.Version)
61c6f206
 
 	standalone := resp.Header.Get("X-Docker-Registry-Standalone")
6f4d8470
 	logrus.Debugf("Registry standalone header: '%s'", standalone)
61c6f206
 	// Accepted values are "true" (case-insensitive) and "1".
 	if strings.EqualFold(standalone, "true") || standalone == "1" {
 		info.Standalone = true
 	} else if len(standalone) > 0 {
 		// there is a header set, and it is not "true" or "1", so assume fails
 		info.Standalone = false
 	}
4fcb9ac4
 	logrus.Debugf("PingResult.Standalone: %t", info.Standalone)
61c6f206
 	return info, nil
 }