Skip to content

Commit

Permalink
registry: getting Endpoint ironned out
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Batts <vbatts@redhat.com>
  • Loading branch information
vbatts authored and dmcgowan committed Oct 1, 2014
1 parent 1b6da6a commit 61c6f20
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 107 deletions.
4 changes: 2 additions & 2 deletions graph/pull.go
Expand Up @@ -52,7 +52,7 @@ func (s *TagStore) CmdPull(job *engine.Job) engine.Status {
return job.Error(err)
}

endpoint, err := registry.ExpandAndVerifyRegistryUrl(hostname)
endpoint, err := registry.NewEndpoint(hostname)
if err != nil {
return job.Error(err)
}
Expand All @@ -62,7 +62,7 @@ func (s *TagStore) CmdPull(job *engine.Job) engine.Status {
return job.Error(err)
}

if endpoint == registry.IndexServerAddress() {
if endpoint.String() == registry.IndexServerAddress() {
// If pull "index.docker.io/foo/bar", it's stored locally under "foo/bar"
localName = remoteName

Expand Down
4 changes: 2 additions & 2 deletions graph/push.go
Expand Up @@ -214,7 +214,7 @@ func (s *TagStore) CmdPush(job *engine.Job) engine.Status {
return job.Error(err)
}

endpoint, err := registry.ExpandAndVerifyRegistryUrl(hostname)
endpoint, err := registry.NewEndpoint(hostname)
if err != nil {
return job.Error(err)
}
Expand Down Expand Up @@ -243,7 +243,7 @@ func (s *TagStore) CmdPush(job *engine.Job) engine.Status {

var token []string
job.Stdout.Write(sf.FormatStatus("", "The push refers to an image: [%s]", localName))
if _, err := s.pushImage(r, job.Stdout, remoteName, img.ID, endpoint, token, sf); err != nil {
if _, err := s.pushImage(r, job.Stdout, remoteName, img.ID, endpoint.String(), token, sf); err != nil {
return job.Error(err)
}
return engine.StatusOK
Expand Down
129 changes: 129 additions & 0 deletions registry/endpoint.go
@@ -0,0 +1,129 @@
package registry

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"

"github.com/docker/docker/pkg/log"
)

// scans string for api version in the URL path. returns the trimmed hostname, if version found, string and API version.
func scanForApiVersion(hostname string) (string, APIVersion) {
var (
chunks []string
apiVersionStr string
)
if strings.HasSuffix(hostname, "/") {
chunks = strings.Split(hostname[:len(hostname)-1], "/")
apiVersionStr = chunks[len(chunks)-1]
} else {
chunks = strings.Split(hostname, "/")
apiVersionStr = chunks[len(chunks)-1]
}
for k, v := range apiVersions {
if apiVersionStr == v {
hostname = strings.Join(chunks[:len(chunks)-1], "/")
return hostname, k
}
}
return hostname, DefaultAPIVersion
}

func NewEndpoint(hostname string) (*Endpoint, error) {
var (
endpoint Endpoint
trimmedHostname string
err error
)
if !strings.HasPrefix(hostname, "http") {
hostname = "https://" + hostname
}
trimmedHostname, endpoint.Version = scanForApiVersion(hostname)
endpoint.URL, err = url.Parse(trimmedHostname)
if err != nil {
return nil, err
}

endpoint.URL.Scheme = "https"
if _, err := endpoint.Ping(); err != nil {
log.Debugf("Registry %s does not work (%s), falling back to http", endpoint, err)
// TODO: Check if http fallback is enabled
endpoint.URL.Scheme = "http"
if _, err = endpoint.Ping(); err != nil {
return nil, errors.New("Invalid Registry endpoint: " + err.Error())
}
}

return &endpoint, nil
}

type Endpoint struct {
URL *url.URL
Version APIVersion
}

// Get the formated URL for the root of this registry Endpoint
func (e Endpoint) String() string {
return fmt.Sprintf("%s/v%d/", e.URL.String(), e.Version)
}

func (e Endpoint) VersionString(version APIVersion) string {
return fmt.Sprintf("%s/v%d/", e.URL.String(), version)
}

func (e Endpoint) Ping() (RegistryInfo, error) {
if e.String() == IndexServerAddress() {
// Skip the check, we now this one is valid
// (and we never want to fallback to http in case of error)
return RegistryInfo{Standalone: false}, nil
}

req, err := http.NewRequest("GET", e.String()+"_ping", nil)
if err != nil {
return RegistryInfo{Standalone: false}, err
}

resp, _, err := doRequest(req, nil, ConnectTimeout)
if err != nil {
return RegistryInfo{Standalone: false}, err
}

defer resp.Body.Close()

jsonString, err := ioutil.ReadAll(resp.Body)
if err != nil {
return RegistryInfo{Standalone: false}, fmt.Errorf("Error while reading the http response: %s", err)
}

// If the header is absent, we assume true for compatibility with earlier
// versions of the registry. default to true
info := RegistryInfo{
Standalone: true,
}
if err := json.Unmarshal(jsonString, &info); err != nil {
log.Debugf("Error unmarshalling the _ping RegistryInfo: %s", err)
// don't stop here. Just assume sane defaults
}
if hdr := resp.Header.Get("X-Docker-Registry-Version"); hdr != "" {
log.Debugf("Registry version header: '%s'", hdr)
info.Version = hdr
}
log.Debugf("RegistryInfo.Version: %q", info.Version)

standalone := resp.Header.Get("X-Docker-Registry-Standalone")
log.Debugf("Registry standalone header: '%s'", standalone)
// 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
}
log.Debugf("RegistryInfo.Standalone: %q", info.Standalone)
return info, nil
}
78 changes: 0 additions & 78 deletions registry/registry.go
Expand Up @@ -3,7 +3,6 @@ package registry
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand All @@ -15,7 +14,6 @@ import (
"strings"
"time"

"github.com/docker/docker/pkg/log"
"github.com/docker/docker/utils"
)

Expand Down Expand Up @@ -152,55 +150,6 @@ func doRequest(req *http.Request, jar http.CookieJar, timeout TimeoutType) (*htt
return nil, nil, nil
}

func pingRegistryEndpoint(endpoint string) (RegistryInfo, error) {
if endpoint == IndexServerAddress() {
// Skip the check, we now this one is valid
// (and we never want to fallback to http in case of error)
return RegistryInfo{Standalone: false}, nil
}

req, err := http.NewRequest("GET", endpoint+"_ping", nil)
if err != nil {
return RegistryInfo{Standalone: false}, err
}

resp, _, err := doRequest(req, nil, ConnectTimeout)
if err != nil {
return RegistryInfo{Standalone: false}, err
}

defer resp.Body.Close()

jsonString, err := ioutil.ReadAll(resp.Body)
if err != nil {
return RegistryInfo{Standalone: false}, fmt.Errorf("Error while reading the http response: %s", err)
}

// If the header is absent, we assume true for compatibility with earlier
// versions of the registry. default to true
info := RegistryInfo{
Standalone: true,
}
if err := json.Unmarshal(jsonString, &info); err != nil {
log.Debugf("Error unmarshalling the _ping RegistryInfo: %s", err)
// don't stop here. Just assume sane defaults
}
if hdr := resp.Header.Get("X-Docker-Registry-Version"); hdr != "" {
log.Debugf("Registry version header: '%s'", hdr)
info.Version = hdr
}
log.Debugf("RegistryInfo.Version: %q", info.Version)

standalone := resp.Header.Get("X-Docker-Registry-Standalone")
log.Debugf("Registry standalone header: '%s'", standalone)
if !strings.EqualFold(standalone, "true") && standalone != "1" && len(standalone) > 0 {
// there is a header set, and it is not "true" or "1", so assume fails
info.Standalone = false
}
log.Debugf("RegistryInfo.Standalone: %q", info.Standalone)
return info, nil
}

func validateRepositoryName(repositoryName string) error {
var (
namespace string
Expand Down Expand Up @@ -252,33 +201,6 @@ func ResolveRepositoryName(reposName string) (string, string, error) {
return hostname, reposName, nil
}

// this method expands the registry name as used in the prefix of a repo
// to a full url. if it already is a url, there will be no change.
// The registry is pinged to test if it http or https
func ExpandAndVerifyRegistryUrl(hostname string) (string, error) {
if strings.HasPrefix(hostname, "http:") || strings.HasPrefix(hostname, "https:") {
// if there is no slash after https:// (8 characters) then we have no path in the url
if strings.LastIndex(hostname, "/") < 9 {
// there is no path given. Expand with default path
hostname = hostname + "/v1/"
}
if _, err := pingRegistryEndpoint(hostname); err != nil {
return "", errors.New("Invalid Registry endpoint: " + err.Error())
}
return hostname, nil
}
endpoint := fmt.Sprintf("https://%s/v1/", hostname)
if _, err := pingRegistryEndpoint(endpoint); err != nil {
log.Debugf("Registry %s does not work (%s), falling back to http", endpoint, err)
endpoint = fmt.Sprintf("http://%s/v1/", hostname)
if _, err = pingRegistryEndpoint(endpoint); err != nil {
//TODO: triggering highland build can be done there without "failing"
return "", errors.New("Invalid Registry endpoint: " + err.Error())
}
}
return endpoint, nil
}

func trustedLocation(req *http.Request) bool {
var (
trusteds = []string{"docker.com", "docker.io"}
Expand Down
14 changes: 11 additions & 3 deletions registry/registry_test.go
Expand Up @@ -18,15 +18,23 @@ var (

func spawnTestRegistrySession(t *testing.T) *Session {
authConfig := &AuthConfig{}
r, err := NewSession(authConfig, utils.NewHTTPRequestFactory(), makeURL("/v1/"), true)
endpoint, err := NewEndpoint(makeURL("/v1/"))
if err != nil {
t.Fatal(err)
}
r, err := NewSession(authConfig, utils.NewHTTPRequestFactory(), endpoint, true)
if err != nil {
t.Fatal(err)
}
return r
}

func TestPingRegistryEndpoint(t *testing.T) {
regInfo, err := pingRegistryEndpoint(makeURL("/v1/"))
ep, err := NewEndpoint(makeURL("/v1/"))
if err != nil {
t.Fatal(err)
}
regInfo, err := ep.Ping()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -197,7 +205,7 @@ func TestPushImageJSONIndex(t *testing.T) {
if repoData == nil {
t.Fatal("Expected RepositoryData object")
}
repoData, err = r.PushImageJSONIndex("foo42/bar", imgData, true, []string{r.indexEndpoint})
repoData, err = r.PushImageJSONIndex("foo42/bar", imgData, true, []string{r.indexEndpoint.String()})
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 7 additions & 4 deletions registry/service.go
Expand Up @@ -40,11 +40,14 @@ func (s *Service) Auth(job *engine.Job) engine.Status {
job.GetenvJson("authConfig", authConfig)
// TODO: this is only done here because auth and registry need to be merged into one pkg
if addr := authConfig.ServerAddress; addr != "" && addr != IndexServerAddress() {
addr, err = ExpandAndVerifyRegistryUrl(addr)
endpoint, err := NewEndpoint(addr)
if err != nil {
return job.Error(err)
}
authConfig.ServerAddress = addr
if _, err := endpoint.Ping(); err != nil {
return job.Error(err)
}
authConfig.ServerAddress = endpoint.String()
}
status, err := Login(authConfig, HTTPRequestFactory(nil))
if err != nil {
Expand Down Expand Up @@ -86,11 +89,11 @@ func (s *Service) Search(job *engine.Job) engine.Status {
if err != nil {
return job.Error(err)
}
hostname, err = ExpandAndVerifyRegistryUrl(hostname)
endpoint, err := NewEndpoint(hostname)
if err != nil {
return job.Error(err)
}
r, err := NewSession(authConfig, HTTPRequestFactory(metaHeaders), hostname, true)
r, err := NewSession(authConfig, HTTPRequestFactory(metaHeaders), endpoint, true)
if err != nil {
return job.Error(err)
}
Expand Down

0 comments on commit 61c6f20

Please sign in to comment.