search_endpoint_v1.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package registry // import "github.com/docker/docker/registry"
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "encoding/json"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "github.com/containerd/log"
  10. "github.com/docker/distribution/registry/client/transport"
  11. "github.com/docker/docker/api/types/registry"
  12. )
  13. // v1PingResult contains the information returned when pinging a registry. It
  14. // indicates whether the registry claims to be a standalone registry.
  15. type v1PingResult struct {
  16. // Standalone is set to true if the registry indicates it is a
  17. // standalone registry in the X-Docker-Registry-Standalone
  18. // header
  19. Standalone bool `json:"standalone"`
  20. }
  21. // v1Endpoint stores basic information about a V1 registry endpoint.
  22. type v1Endpoint struct {
  23. client *http.Client
  24. URL *url.URL
  25. IsSecure bool
  26. }
  27. // newV1Endpoint parses the given address to return a registry endpoint.
  28. // TODO: remove. This is only used by search.
  29. func newV1Endpoint(index *registry.IndexInfo, headers http.Header) (*v1Endpoint, error) {
  30. tlsConfig, err := newTLSConfig(index.Name, index.Secure)
  31. if err != nil {
  32. return nil, err
  33. }
  34. endpoint, err := newV1EndpointFromStr(GetAuthConfigKey(index), tlsConfig, headers)
  35. if err != nil {
  36. return nil, err
  37. }
  38. if endpoint.String() == IndexServer {
  39. // Skip the check, we know this one is valid
  40. // (and we never want to fall back to http in case of error)
  41. return endpoint, nil
  42. }
  43. // Try HTTPS ping to registry
  44. endpoint.URL.Scheme = "https"
  45. if _, err := endpoint.ping(); err != nil {
  46. if endpoint.IsSecure {
  47. // If registry is secure and HTTPS failed, show user the error and tell them about `--insecure-registry`
  48. // in case that's what they need. DO NOT accept unknown CA certificates, and DO NOT fall back to HTTP.
  49. return nil, invalidParamf("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)
  50. }
  51. // registry is insecure and HTTPS failed, fallback to HTTP.
  52. log.G(context.TODO()).WithError(err).Debugf("error from registry %q marked as insecure - insecurely falling back to HTTP", endpoint)
  53. endpoint.URL.Scheme = "http"
  54. if _, err2 := endpoint.ping(); err2 != nil {
  55. return nil, invalidParamf("invalid registry endpoint %q. HTTPS attempt: %v. HTTP attempt: %v", endpoint, err, err2)
  56. }
  57. }
  58. return endpoint, nil
  59. }
  60. // trimV1Address trims the "v1" version suffix off the address and returns
  61. // the trimmed address. It returns an error on "v2" endpoints.
  62. func trimV1Address(address string) (string, error) {
  63. trimmed := strings.TrimSuffix(address, "/")
  64. if strings.HasSuffix(trimmed, "/v2") {
  65. return "", invalidParamf("search is not supported on v2 endpoints: %s", address)
  66. }
  67. return strings.TrimSuffix(trimmed, "/v1"), nil
  68. }
  69. func newV1EndpointFromStr(address string, tlsConfig *tls.Config, headers http.Header) (*v1Endpoint, error) {
  70. if !strings.HasPrefix(address, "http://") && !strings.HasPrefix(address, "https://") {
  71. address = "https://" + address
  72. }
  73. address, err := trimV1Address(address)
  74. if err != nil {
  75. return nil, err
  76. }
  77. uri, err := url.Parse(address)
  78. if err != nil {
  79. return nil, invalidParam(err)
  80. }
  81. // TODO(tiborvass): make sure a ConnectTimeout transport is used
  82. tr := newTransport(tlsConfig)
  83. return &v1Endpoint{
  84. IsSecure: tlsConfig == nil || !tlsConfig.InsecureSkipVerify,
  85. URL: uri,
  86. client: httpClient(transport.NewTransport(tr, Headers("", headers)...)),
  87. }, nil
  88. }
  89. // Get the formatted URL for the root of this registry Endpoint
  90. func (e *v1Endpoint) String() string {
  91. return e.URL.String() + "/v1/"
  92. }
  93. // ping returns a v1PingResult which indicates whether the registry is standalone or not.
  94. func (e *v1Endpoint) ping() (v1PingResult, error) {
  95. if e.String() == IndexServer {
  96. // Skip the check, we know this one is valid
  97. // (and we never want to fallback to http in case of error)
  98. return v1PingResult{}, nil
  99. }
  100. pingURL := e.String() + "_ping"
  101. log.G(context.TODO()).WithField("url", pingURL).Debug("attempting v1 ping for registry endpoint")
  102. req, err := http.NewRequest(http.MethodGet, pingURL, nil)
  103. if err != nil {
  104. return v1PingResult{}, invalidParam(err)
  105. }
  106. resp, err := e.client.Do(req)
  107. if err != nil {
  108. return v1PingResult{}, invalidParam(err)
  109. }
  110. defer resp.Body.Close()
  111. if v := resp.Header.Get("X-Docker-Registry-Standalone"); v != "" {
  112. info := v1PingResult{}
  113. // Accepted values are "1", and "true" (case-insensitive).
  114. if v == "1" || strings.EqualFold(v, "true") {
  115. info.Standalone = true
  116. }
  117. log.G(context.TODO()).Debugf("v1PingResult.Standalone (from X-Docker-Registry-Standalone header): %t", info.Standalone)
  118. return info, nil
  119. }
  120. // If the header is absent, we assume true for compatibility with earlier
  121. // versions of the registry. default to true
  122. info := v1PingResult{
  123. Standalone: true,
  124. }
  125. if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
  126. log.G(context.TODO()).WithError(err).Debug("error unmarshaling _ping response")
  127. // don't stop here. Just assume sane defaults
  128. }
  129. log.G(context.TODO()).Debugf("v1PingResult.Standalone: %t", info.Standalone)
  130. return info, nil
  131. }
  132. // httpClient returns an HTTP client structure which uses the given transport
  133. // and contains the necessary headers for redirected requests
  134. func httpClient(transport http.RoundTripper) *http.Client {
  135. return &http.Client{
  136. Transport: transport,
  137. CheckRedirect: addRequiredHeadersToRedirectedRequests,
  138. }
  139. }
  140. func trustedLocation(req *http.Request) bool {
  141. var (
  142. trusteds = []string{"docker.com", "docker.io"}
  143. hostname = strings.SplitN(req.Host, ":", 2)[0]
  144. )
  145. if req.URL.Scheme != "https" {
  146. return false
  147. }
  148. for _, trusted := range trusteds {
  149. if hostname == trusted || strings.HasSuffix(hostname, "."+trusted) {
  150. return true
  151. }
  152. }
  153. return false
  154. }
  155. // addRequiredHeadersToRedirectedRequests adds the necessary redirection headers
  156. // for redirected requests
  157. func addRequiredHeadersToRedirectedRequests(req *http.Request, via []*http.Request) error {
  158. if len(via) != 0 && via[0] != nil {
  159. if trustedLocation(req) && trustedLocation(via[0]) {
  160. req.Header = via[0].Header
  161. return nil
  162. }
  163. for k, v := range via[0].Header {
  164. if k != "Authorization" {
  165. for _, vv := range v {
  166. req.Header.Add(k, vv)
  167. }
  168. }
  169. }
  170. }
  171. return nil
  172. }