github.com/slowteetoe/docker@v1.7.1-rc3/registry/endpoint.go (about)

     1  package registry
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net"
     8  	"net/http"
     9  	"net/url"
    10  	"strings"
    11  
    12  	"github.com/Sirupsen/logrus"
    13  	"github.com/docker/distribution/registry/api/v2"
    14  	"github.com/docker/docker/pkg/transport"
    15  )
    16  
    17  // for mocking in unit tests
    18  var lookupIP = net.LookupIP
    19  
    20  // scans string for api version in the URL path. returns the trimmed address, if version found, string and API version.
    21  func scanForAPIVersion(address string) (string, APIVersion) {
    22  	var (
    23  		chunks        []string
    24  		apiVersionStr string
    25  	)
    26  
    27  	if strings.HasSuffix(address, "/") {
    28  		address = address[:len(address)-1]
    29  	}
    30  
    31  	chunks = strings.Split(address, "/")
    32  	apiVersionStr = chunks[len(chunks)-1]
    33  
    34  	for k, v := range apiVersions {
    35  		if apiVersionStr == v {
    36  			address = strings.Join(chunks[:len(chunks)-1], "/")
    37  			return address, k
    38  		}
    39  	}
    40  
    41  	return address, APIVersionUnknown
    42  }
    43  
    44  // NewEndpoint parses the given address to return a registry endpoint.
    45  func NewEndpoint(index *IndexInfo, metaHeaders http.Header) (*Endpoint, error) {
    46  	// *TODO: Allow per-registry configuration of endpoints.
    47  	endpoint, err := newEndpoint(index.GetAuthConfigKey(), index.Secure, metaHeaders)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	if err := validateEndpoint(endpoint); err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	return endpoint, nil
    56  }
    57  
    58  func validateEndpoint(endpoint *Endpoint) error {
    59  	logrus.Debugf("pinging registry endpoint %s", endpoint)
    60  
    61  	// Try HTTPS ping to registry
    62  	endpoint.URL.Scheme = "https"
    63  	if _, err := endpoint.Ping(); err != nil {
    64  		if endpoint.IsSecure {
    65  			// If registry is secure and HTTPS failed, show user the error and tell them about `--insecure-registry`
    66  			// in case that's what they need. DO NOT accept unknown CA certificates, and DO NOT fallback to HTTP.
    67  			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)
    68  		}
    69  
    70  		// If registry is insecure and HTTPS failed, fallback to HTTP.
    71  		logrus.Debugf("Error from registry %q marked as insecure: %v. Insecurely falling back to HTTP", endpoint, err)
    72  		endpoint.URL.Scheme = "http"
    73  
    74  		var err2 error
    75  		if _, err2 = endpoint.Ping(); err2 == nil {
    76  			return nil
    77  		}
    78  
    79  		return fmt.Errorf("invalid registry endpoint %q. HTTPS attempt: %v. HTTP attempt: %v", endpoint, err, err2)
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  func newEndpoint(address string, secure bool, metaHeaders http.Header) (*Endpoint, error) {
    86  	var (
    87  		endpoint       = new(Endpoint)
    88  		trimmedAddress string
    89  		err            error
    90  	)
    91  
    92  	if !strings.HasPrefix(address, "http") {
    93  		address = "https://" + address
    94  	}
    95  
    96  	trimmedAddress, endpoint.Version = scanForAPIVersion(address)
    97  
    98  	if endpoint.URL, err = url.Parse(trimmedAddress); err != nil {
    99  		return nil, err
   100  	}
   101  	endpoint.IsSecure = secure
   102  	tr := NewTransport(ConnectTimeout, endpoint.IsSecure)
   103  	endpoint.client = HTTPClient(transport.NewTransport(tr, DockerHeaders(metaHeaders)...))
   104  	return endpoint, nil
   105  }
   106  
   107  func (repoInfo *RepositoryInfo) GetEndpoint(metaHeaders http.Header) (*Endpoint, error) {
   108  	return NewEndpoint(repoInfo.Index, metaHeaders)
   109  }
   110  
   111  // Endpoint stores basic information about a registry endpoint.
   112  type Endpoint struct {
   113  	client         *http.Client
   114  	URL            *url.URL
   115  	Version        APIVersion
   116  	IsSecure       bool
   117  	AuthChallenges []*AuthorizationChallenge
   118  	URLBuilder     *v2.URLBuilder
   119  }
   120  
   121  // Get the formated URL for the root of this registry Endpoint
   122  func (e *Endpoint) String() string {
   123  	return fmt.Sprintf("%s/v%d/", e.URL, e.Version)
   124  }
   125  
   126  // VersionString returns a formatted string of this
   127  // endpoint address using the given API Version.
   128  func (e *Endpoint) VersionString(version APIVersion) string {
   129  	return fmt.Sprintf("%s/v%d/", e.URL, version)
   130  }
   131  
   132  // Path returns a formatted string for the URL
   133  // of this endpoint with the given path appended.
   134  func (e *Endpoint) Path(path string) string {
   135  	return fmt.Sprintf("%s/v%d/%s", e.URL, e.Version, path)
   136  }
   137  
   138  func (e *Endpoint) Ping() (RegistryInfo, error) {
   139  	// The ping logic to use is determined by the registry endpoint version.
   140  	switch e.Version {
   141  	case APIVersion1:
   142  		return e.pingV1()
   143  	case APIVersion2:
   144  		return e.pingV2()
   145  	}
   146  
   147  	// APIVersionUnknown
   148  	// We should try v2 first...
   149  	e.Version = APIVersion2
   150  	regInfo, errV2 := e.pingV2()
   151  	if errV2 == nil {
   152  		return regInfo, nil
   153  	}
   154  
   155  	// ... then fallback to v1.
   156  	e.Version = APIVersion1
   157  	regInfo, errV1 := e.pingV1()
   158  	if errV1 == nil {
   159  		return regInfo, nil
   160  	}
   161  
   162  	e.Version = APIVersionUnknown
   163  	return RegistryInfo{}, fmt.Errorf("unable to ping registry endpoint %s\nv2 ping attempt failed with error: %s\n v1 ping attempt failed with error: %s", e, errV2, errV1)
   164  }
   165  
   166  func (e *Endpoint) pingV1() (RegistryInfo, error) {
   167  	logrus.Debugf("attempting v1 ping for registry endpoint %s", e)
   168  
   169  	if e.String() == IndexServerAddress() {
   170  		// Skip the check, we know this one is valid
   171  		// (and we never want to fallback to http in case of error)
   172  		return RegistryInfo{Standalone: false}, nil
   173  	}
   174  
   175  	req, err := http.NewRequest("GET", e.Path("_ping"), nil)
   176  	if err != nil {
   177  		return RegistryInfo{Standalone: false}, err
   178  	}
   179  
   180  	resp, err := e.client.Do(req)
   181  	if err != nil {
   182  		return RegistryInfo{Standalone: false}, err
   183  	}
   184  
   185  	defer resp.Body.Close()
   186  
   187  	jsonString, err := ioutil.ReadAll(resp.Body)
   188  	if err != nil {
   189  		return RegistryInfo{Standalone: false}, fmt.Errorf("error while reading the http response: %s", err)
   190  	}
   191  
   192  	// If the header is absent, we assume true for compatibility with earlier
   193  	// versions of the registry. default to true
   194  	info := RegistryInfo{
   195  		Standalone: true,
   196  	}
   197  	if err := json.Unmarshal(jsonString, &info); err != nil {
   198  		logrus.Debugf("Error unmarshalling the _ping RegistryInfo: %s", err)
   199  		// don't stop here. Just assume sane defaults
   200  	}
   201  	if hdr := resp.Header.Get("X-Docker-Registry-Version"); hdr != "" {
   202  		logrus.Debugf("Registry version header: '%s'", hdr)
   203  		info.Version = hdr
   204  	}
   205  	logrus.Debugf("RegistryInfo.Version: %q", info.Version)
   206  
   207  	standalone := resp.Header.Get("X-Docker-Registry-Standalone")
   208  	logrus.Debugf("Registry standalone header: '%s'", standalone)
   209  	// Accepted values are "true" (case-insensitive) and "1".
   210  	if strings.EqualFold(standalone, "true") || standalone == "1" {
   211  		info.Standalone = true
   212  	} else if len(standalone) > 0 {
   213  		// there is a header set, and it is not "true" or "1", so assume fails
   214  		info.Standalone = false
   215  	}
   216  	logrus.Debugf("RegistryInfo.Standalone: %t", info.Standalone)
   217  	return info, nil
   218  }
   219  
   220  func (e *Endpoint) pingV2() (RegistryInfo, error) {
   221  	logrus.Debugf("attempting v2 ping for registry endpoint %s", e)
   222  
   223  	req, err := http.NewRequest("GET", e.Path(""), nil)
   224  	if err != nil {
   225  		return RegistryInfo{}, err
   226  	}
   227  
   228  	resp, err := e.client.Do(req)
   229  	if err != nil {
   230  		return RegistryInfo{}, err
   231  	}
   232  	defer resp.Body.Close()
   233  
   234  	// The endpoint may have multiple supported versions.
   235  	// Ensure it supports the v2 Registry API.
   236  	var supportsV2 bool
   237  
   238  HeaderLoop:
   239  	for _, supportedVersions := range resp.Header[http.CanonicalHeaderKey("Docker-Distribution-API-Version")] {
   240  		for _, versionName := range strings.Fields(supportedVersions) {
   241  			if versionName == "registry/2.0" {
   242  				supportsV2 = true
   243  				break HeaderLoop
   244  			}
   245  		}
   246  	}
   247  
   248  	if !supportsV2 {
   249  		return RegistryInfo{}, fmt.Errorf("%s does not appear to be a v2 registry endpoint", e)
   250  	}
   251  
   252  	if resp.StatusCode == http.StatusOK {
   253  		// It would seem that no authentication/authorization is required.
   254  		// So we don't need to parse/add any authorization schemes.
   255  		return RegistryInfo{Standalone: true}, nil
   256  	}
   257  
   258  	if resp.StatusCode == http.StatusUnauthorized {
   259  		// Parse the WWW-Authenticate Header and store the challenges
   260  		// on this endpoint object.
   261  		e.AuthChallenges = parseAuthHeader(resp.Header)
   262  		return RegistryInfo{}, nil
   263  	}
   264  
   265  	return RegistryInfo{}, fmt.Errorf("v2 registry endpoint returned status %d: %q", resp.StatusCode, http.StatusText(resp.StatusCode))
   266  }