github.com/adxhyt/docker@v1.4.2-0.20150117221845-467b7c821390/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 log "github.com/Sirupsen/logrus" 13 ) 14 15 // for mocking in unit tests 16 var lookupIP = net.LookupIP 17 18 // scans string for api version in the URL path. returns the trimmed hostname, if version found, string and API version. 19 func scanForAPIVersion(hostname string) (string, APIVersion) { 20 var ( 21 chunks []string 22 apiVersionStr string 23 ) 24 if strings.HasSuffix(hostname, "/") { 25 chunks = strings.Split(hostname[:len(hostname)-1], "/") 26 apiVersionStr = chunks[len(chunks)-1] 27 } else { 28 chunks = strings.Split(hostname, "/") 29 apiVersionStr = chunks[len(chunks)-1] 30 } 31 for k, v := range apiVersions { 32 if apiVersionStr == v { 33 hostname = strings.Join(chunks[:len(chunks)-1], "/") 34 return hostname, k 35 } 36 } 37 return hostname, DefaultAPIVersion 38 } 39 40 func NewEndpoint(index *IndexInfo) (*Endpoint, error) { 41 // *TODO: Allow per-registry configuration of endpoints. 42 endpoint, err := newEndpoint(index.GetAuthConfigKey(), index.Secure) 43 if err != nil { 44 return nil, err 45 } 46 47 // Try HTTPS ping to registry 48 endpoint.URL.Scheme = "https" 49 if _, err := endpoint.Ping(); err != nil { 50 51 //TODO: triggering highland build can be done there without "failing" 52 53 if index.Secure { 54 // If registry is secure and HTTPS failed, show user the error and tell them about `--insecure-registry` 55 // in case that's what they need. DO NOT accept unknown CA certificates, and DO NOT fallback to HTTP. 56 return nil, 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) 57 } 58 59 // If registry is insecure and HTTPS failed, fallback to HTTP. 60 log.Debugf("Error from registry %q marked as insecure: %v. Insecurely falling back to HTTP", endpoint, err) 61 endpoint.URL.Scheme = "http" 62 _, err2 := endpoint.Ping() 63 if err2 == nil { 64 return endpoint, nil 65 } 66 67 return nil, fmt.Errorf("Invalid registry endpoint %q. HTTPS attempt: %v. HTTP attempt: %v", endpoint, err, err2) 68 } 69 70 return endpoint, nil 71 } 72 func newEndpoint(hostname string, secure bool) (*Endpoint, error) { 73 var ( 74 endpoint = Endpoint{} 75 trimmedHostname string 76 err error 77 ) 78 if !strings.HasPrefix(hostname, "http") { 79 hostname = "https://" + hostname 80 } 81 trimmedHostname, endpoint.Version = scanForAPIVersion(hostname) 82 endpoint.URL, err = url.Parse(trimmedHostname) 83 if err != nil { 84 return nil, err 85 } 86 endpoint.secure = secure 87 return &endpoint, nil 88 } 89 90 func (repoInfo *RepositoryInfo) GetEndpoint() (*Endpoint, error) { 91 return NewEndpoint(repoInfo.Index) 92 } 93 94 type Endpoint struct { 95 URL *url.URL 96 Version APIVersion 97 secure bool 98 } 99 100 // Get the formated URL for the root of this registry Endpoint 101 func (e Endpoint) String() string { 102 return fmt.Sprintf("%s/v%d/", e.URL.String(), e.Version) 103 } 104 105 func (e Endpoint) VersionString(version APIVersion) string { 106 return fmt.Sprintf("%s/v%d/", e.URL.String(), version) 107 } 108 109 func (e Endpoint) Ping() (RegistryInfo, error) { 110 if e.String() == IndexServerAddress() { 111 // Skip the check, we now this one is valid 112 // (and we never want to fallback to http in case of error) 113 return RegistryInfo{Standalone: false}, nil 114 } 115 116 req, err := http.NewRequest("GET", e.String()+"_ping", nil) 117 if err != nil { 118 return RegistryInfo{Standalone: false}, err 119 } 120 121 resp, _, err := doRequest(req, nil, ConnectTimeout, e.secure) 122 if err != nil { 123 return RegistryInfo{Standalone: false}, err 124 } 125 126 defer resp.Body.Close() 127 128 jsonString, err := ioutil.ReadAll(resp.Body) 129 if err != nil { 130 return RegistryInfo{Standalone: false}, fmt.Errorf("Error while reading the http response: %s", err) 131 } 132 133 // If the header is absent, we assume true for compatibility with earlier 134 // versions of the registry. default to true 135 info := RegistryInfo{ 136 Standalone: true, 137 } 138 if err := json.Unmarshal(jsonString, &info); err != nil { 139 log.Debugf("Error unmarshalling the _ping RegistryInfo: %s", err) 140 // don't stop here. Just assume sane defaults 141 } 142 if hdr := resp.Header.Get("X-Docker-Registry-Version"); hdr != "" { 143 log.Debugf("Registry version header: '%s'", hdr) 144 info.Version = hdr 145 } 146 log.Debugf("RegistryInfo.Version: %q", info.Version) 147 148 standalone := resp.Header.Get("X-Docker-Registry-Standalone") 149 log.Debugf("Registry standalone header: '%s'", standalone) 150 // Accepted values are "true" (case-insensitive) and "1". 151 if strings.EqualFold(standalone, "true") || standalone == "1" { 152 info.Standalone = true 153 } else if len(standalone) > 0 { 154 // there is a header set, and it is not "true" or "1", so assume fails 155 info.Standalone = false 156 } 157 log.Debugf("RegistryInfo.Standalone: %t", info.Standalone) 158 return info, nil 159 }