endpoint_v1.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package registry
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "github.com/Sirupsen/logrus"
  11. "github.com/docker/distribution/registry/client/transport"
  12. registrytypes "github.com/docker/engine-api/types/registry"
  13. )
  14. // V1Endpoint stores basic information about a V1 registry endpoint.
  15. type V1Endpoint struct {
  16. client *http.Client
  17. URL *url.URL
  18. IsSecure bool
  19. }
  20. // NewV1Endpoint parses the given address to return a registry endpoint.
  21. func NewV1Endpoint(index *registrytypes.IndexInfo, userAgent string, metaHeaders http.Header) (*V1Endpoint, error) {
  22. tlsConfig, err := newTLSConfig(index.Name, index.Secure)
  23. if err != nil {
  24. return nil, err
  25. }
  26. endpoint, err := newV1EndpointFromStr(GetAuthConfigKey(index), tlsConfig, userAgent, metaHeaders)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if err := validateEndpoint(endpoint); err != nil {
  31. return nil, err
  32. }
  33. return endpoint, nil
  34. }
  35. func validateEndpoint(endpoint *V1Endpoint) error {
  36. logrus.Debugf("pinging registry endpoint %s", endpoint)
  37. // Try HTTPS ping to registry
  38. endpoint.URL.Scheme = "https"
  39. if _, err := endpoint.Ping(); err != nil {
  40. if endpoint.IsSecure {
  41. // If registry is secure and HTTPS failed, show user the error and tell them about `--insecure-registry`
  42. // in case that's what they need. DO NOT accept unknown CA certificates, and DO NOT fallback to HTTP.
  43. 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)
  44. }
  45. // If registry is insecure and HTTPS failed, fallback to HTTP.
  46. logrus.Debugf("Error from registry %q marked as insecure: %v. Insecurely falling back to HTTP", endpoint, err)
  47. endpoint.URL.Scheme = "http"
  48. var err2 error
  49. if _, err2 = endpoint.Ping(); err2 == nil {
  50. return nil
  51. }
  52. return fmt.Errorf("invalid registry endpoint %q. HTTPS attempt: %v. HTTP attempt: %v", endpoint, err, err2)
  53. }
  54. return nil
  55. }
  56. func newV1Endpoint(address url.URL, tlsConfig *tls.Config, userAgent string, metaHeaders http.Header) (*V1Endpoint, error) {
  57. endpoint := &V1Endpoint{
  58. IsSecure: (tlsConfig == nil || !tlsConfig.InsecureSkipVerify),
  59. URL: new(url.URL),
  60. }
  61. *endpoint.URL = address
  62. // TODO(tiborvass): make sure a ConnectTimeout transport is used
  63. tr := NewTransport(tlsConfig)
  64. endpoint.client = HTTPClient(transport.NewTransport(tr, DockerHeaders(userAgent, metaHeaders)...))
  65. return endpoint, nil
  66. }
  67. // trimV1Address trims the version off the address and returns the
  68. // trimmed address or an error if there is a non-V1 version.
  69. func trimV1Address(address string) (string, error) {
  70. var (
  71. chunks []string
  72. apiVersionStr string
  73. )
  74. if strings.HasSuffix(address, "/") {
  75. address = address[:len(address)-1]
  76. }
  77. chunks = strings.Split(address, "/")
  78. apiVersionStr = chunks[len(chunks)-1]
  79. if apiVersionStr == "v1" {
  80. return strings.Join(chunks[:len(chunks)-1], "/"), nil
  81. }
  82. for k, v := range apiVersions {
  83. if k != APIVersion1 && apiVersionStr == v {
  84. return "", fmt.Errorf("unsupported V1 version path %s", apiVersionStr)
  85. }
  86. }
  87. return address, nil
  88. }
  89. func newV1EndpointFromStr(address string, tlsConfig *tls.Config, userAgent string, metaHeaders http.Header) (*V1Endpoint, error) {
  90. if !strings.HasPrefix(address, "http://") && !strings.HasPrefix(address, "https://") {
  91. address = "https://" + address
  92. }
  93. address, err := trimV1Address(address)
  94. if err != nil {
  95. return nil, err
  96. }
  97. uri, err := url.Parse(address)
  98. if err != nil {
  99. return nil, err
  100. }
  101. endpoint, err := newV1Endpoint(*uri, tlsConfig, userAgent, metaHeaders)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return endpoint, nil
  106. }
  107. // Get the formatted URL for the root of this registry Endpoint
  108. func (e *V1Endpoint) String() string {
  109. return e.URL.String() + "/v1/"
  110. }
  111. // Path returns a formatted string for the URL
  112. // of this endpoint with the given path appended.
  113. func (e *V1Endpoint) Path(path string) string {
  114. return e.URL.String() + "/v1/" + path
  115. }
  116. // Ping returns a PingResult which indicates whether the registry is standalone or not.
  117. func (e *V1Endpoint) Ping() (PingResult, error) {
  118. logrus.Debugf("attempting v1 ping for registry endpoint %s", e)
  119. if e.String() == IndexServer {
  120. // Skip the check, we know this one is valid
  121. // (and we never want to fallback to http in case of error)
  122. return PingResult{Standalone: false}, nil
  123. }
  124. req, err := http.NewRequest("GET", e.Path("_ping"), nil)
  125. if err != nil {
  126. return PingResult{Standalone: false}, err
  127. }
  128. resp, err := e.client.Do(req)
  129. if err != nil {
  130. return PingResult{Standalone: false}, err
  131. }
  132. defer resp.Body.Close()
  133. jsonString, err := ioutil.ReadAll(resp.Body)
  134. if err != nil {
  135. return PingResult{Standalone: false}, fmt.Errorf("error while reading the http response: %s", err)
  136. }
  137. // If the header is absent, we assume true for compatibility with earlier
  138. // versions of the registry. default to true
  139. info := PingResult{
  140. Standalone: true,
  141. }
  142. if err := json.Unmarshal(jsonString, &info); err != nil {
  143. logrus.Debugf("Error unmarshalling the _ping PingResult: %s", err)
  144. // don't stop here. Just assume sane defaults
  145. }
  146. if hdr := resp.Header.Get("X-Docker-Registry-Version"); hdr != "" {
  147. logrus.Debugf("Registry version header: '%s'", hdr)
  148. info.Version = hdr
  149. }
  150. logrus.Debugf("PingResult.Version: %q", info.Version)
  151. standalone := resp.Header.Get("X-Docker-Registry-Standalone")
  152. logrus.Debugf("Registry standalone header: '%s'", standalone)
  153. // Accepted values are "true" (case-insensitive) and "1".
  154. if strings.EqualFold(standalone, "true") || standalone == "1" {
  155. info.Standalone = true
  156. } else if len(standalone) > 0 {
  157. // there is a header set, and it is not "true" or "1", so assume fails
  158. info.Standalone = false
  159. }
  160. logrus.Debugf("PingResult.Standalone: %t", info.Standalone)
  161. return info, nil
  162. }