2014-04-27 18:06:09 -04:00
|
|
|
package registry
|
|
|
|
|
2015-05-15 21:35:04 -04:00
|
|
|
import (
|
2015-02-12 13:23:22 -05:00
|
|
|
"crypto/tls"
|
2016-02-23 18:18:04 -05:00
|
|
|
"fmt"
|
2015-05-15 21:35:04 -04:00
|
|
|
"net/http"
|
2015-07-21 17:10:34 -04:00
|
|
|
"net/url"
|
2015-11-18 17:20:54 -05:00
|
|
|
"strings"
|
2015-05-15 21:35:04 -04:00
|
|
|
|
2016-03-01 02:07:41 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-12-04 16:55:15 -05:00
|
|
|
"github.com/docker/docker/reference"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
registrytypes "github.com/docker/engine-api/types/registry"
|
2015-05-15 21:35:04 -04:00
|
|
|
)
|
2015-04-22 08:06:58 -04:00
|
|
|
|
2015-07-21 15:40:36 -04:00
|
|
|
// Service is a registry service. It tracks configuration data such as a list
|
|
|
|
// of mirrors.
|
2014-04-27 18:06:09 -04:00
|
|
|
type Service struct {
|
2016-03-08 16:03:37 -05:00
|
|
|
config *serviceConfig
|
2014-04-27 18:06:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns a new instance of Service ready to be
|
2015-07-21 15:40:36 -04:00
|
|
|
// installed into an engine.
|
2016-03-08 16:03:37 -05:00
|
|
|
func NewService(options ServiceOptions) *Service {
|
2014-08-20 11:31:24 -04:00
|
|
|
return &Service{
|
2016-03-08 16:03:37 -05:00
|
|
|
config: newServiceConfig(options),
|
2014-08-20 11:31:24 -04:00
|
|
|
}
|
2014-04-27 18:06:09 -04:00
|
|
|
}
|
|
|
|
|
2016-03-08 16:03:37 -05:00
|
|
|
// ServiceConfig returns the public registry service configuration.
|
|
|
|
func (s *Service) ServiceConfig() *registrytypes.ServiceConfig {
|
|
|
|
return &s.config.ServiceConfig
|
|
|
|
}
|
|
|
|
|
2014-04-27 18:06:09 -04:00
|
|
|
// Auth contacts the public registry with the provided credentials,
|
2015-08-07 18:24:18 -04:00
|
|
|
// and returns OK if authentication was successful.
|
2014-04-27 18:06:09 -04:00
|
|
|
// It can be used to verify the validity of a client's credentials.
|
2016-02-23 18:18:04 -05:00
|
|
|
func (s *Service) Auth(authConfig *types.AuthConfig, userAgent string) (status, token string, err error) {
|
|
|
|
serverAddress := authConfig.ServerAddress
|
2016-04-22 23:00:47 -04:00
|
|
|
if serverAddress == "" {
|
|
|
|
serverAddress = IndexServer
|
|
|
|
}
|
2016-02-23 18:18:04 -05:00
|
|
|
if !strings.HasPrefix(serverAddress, "https://") && !strings.HasPrefix(serverAddress, "http://") {
|
|
|
|
serverAddress = "https://" + serverAddress
|
|
|
|
}
|
|
|
|
u, err := url.Parse(serverAddress)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("unable to parse server address: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints, err := s.LookupPushEndpoints(u.Host)
|
2015-03-31 19:21:37 -04:00
|
|
|
if err != nil {
|
2016-02-23 18:18:04 -05:00
|
|
|
return "", "", err
|
2014-04-27 18:06:09 -04:00
|
|
|
}
|
2015-09-16 13:42:17 -04:00
|
|
|
|
2016-03-01 02:07:41 -05:00
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
login := loginV2
|
|
|
|
if endpoint.Version == APIVersion1 {
|
|
|
|
login = loginV1
|
|
|
|
}
|
2015-09-16 13:42:17 -04:00
|
|
|
|
2016-02-23 18:18:04 -05:00
|
|
|
status, token, err = login(authConfig, endpoint, userAgent)
|
2016-03-01 02:07:41 -05:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fErr, ok := err.(fallbackError); ok {
|
|
|
|
err = fErr.err
|
|
|
|
logrus.Infof("Error logging in to %s endpoint, trying next endpoint: %v", endpoint.Version, err)
|
|
|
|
continue
|
|
|
|
}
|
2016-02-23 18:18:04 -05:00
|
|
|
return "", "", err
|
2014-12-11 20:55:15 -05:00
|
|
|
}
|
2016-03-01 02:07:41 -05:00
|
|
|
|
2016-02-23 18:18:04 -05:00
|
|
|
return "", "", err
|
2014-04-27 18:06:09 -04:00
|
|
|
}
|
2014-04-27 18:21:42 -04:00
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
// splitReposSearchTerm breaks a search term into an index name and remote name
|
|
|
|
func splitReposSearchTerm(reposName string) (string, string) {
|
|
|
|
nameParts := strings.SplitN(reposName, "/", 2)
|
|
|
|
var indexName, remoteName string
|
|
|
|
if len(nameParts) == 1 || (!strings.Contains(nameParts[0], ".") &&
|
|
|
|
!strings.Contains(nameParts[0], ":") && nameParts[0] != "localhost") {
|
|
|
|
// This is a Docker Index repos (ex: samalba/hipache or ubuntu)
|
|
|
|
// 'docker.io'
|
|
|
|
indexName = IndexName
|
|
|
|
remoteName = reposName
|
|
|
|
} else {
|
|
|
|
indexName = nameParts[0]
|
|
|
|
remoteName = nameParts[1]
|
|
|
|
}
|
|
|
|
return indexName, remoteName
|
|
|
|
}
|
|
|
|
|
2014-04-27 18:21:42 -04:00
|
|
|
// Search queries the public registry for images matching the specified
|
|
|
|
// search terms, and returns the results.
|
2016-01-04 13:36:01 -05:00
|
|
|
func (s *Service) Search(term string, authConfig *types.AuthConfig, userAgent string, headers map[string][]string) (*registrytypes.SearchResults, error) {
|
2016-03-29 02:36:38 -04:00
|
|
|
if err := validateNoScheme(term); err != nil {
|
2015-11-18 17:20:54 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
indexName, remoteName := splitReposSearchTerm(term)
|
2015-09-22 07:44:40 -04:00
|
|
|
|
2016-03-08 16:03:37 -05:00
|
|
|
index, err := newIndexInfo(s.config, indexName)
|
2014-08-09 03:16:54 -04:00
|
|
|
if err != nil {
|
2015-03-31 19:21:37 -04:00
|
|
|
return nil, err
|
2014-08-09 03:16:54 -04:00
|
|
|
}
|
2015-05-14 10:12:54 -04:00
|
|
|
|
2014-10-06 21:54:52 -04:00
|
|
|
// *TODO: Search multiple indexes.
|
2016-03-01 02:07:41 -05:00
|
|
|
endpoint, err := NewV1Endpoint(index, userAgent, http.Header(headers))
|
2014-08-09 03:16:54 -04:00
|
|
|
if err != nil {
|
2015-03-31 19:21:37 -04:00
|
|
|
return nil, err
|
2014-04-27 18:21:42 -04:00
|
|
|
}
|
2015-09-16 13:42:17 -04:00
|
|
|
|
2015-05-15 21:35:04 -04:00
|
|
|
r, err := NewSession(endpoint.client, authConfig, endpoint)
|
2014-04-27 18:21:42 -04:00
|
|
|
if err != nil {
|
2015-03-31 19:21:37 -04:00
|
|
|
return nil, err
|
2014-04-27 18:21:42 -04:00
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
if index.Official {
|
|
|
|
localName := remoteName
|
|
|
|
if strings.HasPrefix(localName, "library/") {
|
|
|
|
// If pull "library/foo", it's stored locally under "foo"
|
|
|
|
localName = strings.SplitN(localName, "/", 2)[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.SearchRepositories(localName)
|
|
|
|
}
|
|
|
|
return r.SearchRepositories(remoteName)
|
2015-09-22 07:44:40 -04:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
// ResolveRepository splits a repository name into its components
|
2015-09-22 07:44:40 -04:00
|
|
|
// and configuration of the associated registry.
|
2015-11-18 17:20:54 -05:00
|
|
|
func (s *Service) ResolveRepository(name reference.Named) (*RepositoryInfo, error) {
|
2016-03-08 16:03:37 -05:00
|
|
|
return newRepositoryInfo(s.config, name)
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveIndex takes indexName and returns index info
|
2015-12-11 21:14:52 -05:00
|
|
|
func (s *Service) ResolveIndex(name string) (*registrytypes.IndexInfo, error) {
|
2016-03-08 16:03:37 -05:00
|
|
|
return newIndexInfo(s.config, name)
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
2015-02-12 13:23:22 -05:00
|
|
|
|
2015-07-21 15:40:36 -04:00
|
|
|
// APIEndpoint represents a remote API endpoint
|
2015-02-12 13:23:22 -05:00
|
|
|
type APIEndpoint struct {
|
2015-12-04 16:42:33 -05:00
|
|
|
Mirror bool
|
2016-02-17 19:53:25 -05:00
|
|
|
URL *url.URL
|
2015-12-04 16:42:33 -05:00
|
|
|
Version APIVersion
|
|
|
|
Official bool
|
|
|
|
TrimHostname bool
|
|
|
|
TLSConfig *tls.Config
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:40:36 -04:00
|
|
|
// ToV1Endpoint returns a V1 API endpoint based on the APIEndpoint
|
2016-03-01 02:07:41 -05:00
|
|
|
func (e APIEndpoint) ToV1Endpoint(userAgent string, metaHeaders http.Header) (*V1Endpoint, error) {
|
|
|
|
return newV1Endpoint(*e.URL, e.TLSConfig, userAgent, metaHeaders)
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:40:36 -04:00
|
|
|
// TLSConfig constructs a client TLS configuration based on server defaults
|
|
|
|
func (s *Service) TLSConfig(hostname string) (*tls.Config, error) {
|
2016-03-08 16:03:37 -05:00
|
|
|
return newTLSConfig(hostname, isSecureIndex(s.config, hostname))
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:53:25 -05:00
|
|
|
func (s *Service) tlsConfigForMirror(mirrorURL *url.URL) (*tls.Config, error) {
|
2015-07-21 15:40:36 -04:00
|
|
|
return s.TLSConfig(mirrorURL.Host)
|
2015-07-21 17:10:34 -04:00
|
|
|
}
|
|
|
|
|
2016-03-09 11:17:57 -05:00
|
|
|
// LookupPullEndpoints creates a list of endpoints to try to pull from, in order of preference.
|
2015-07-21 15:40:36 -04:00
|
|
|
// It gives preference to v2 endpoints over v1, mirrors over the actual
|
|
|
|
// registry, and HTTPS over plain HTTP.
|
2016-03-01 02:07:41 -05:00
|
|
|
func (s *Service) LookupPullEndpoints(hostname string) (endpoints []APIEndpoint, err error) {
|
|
|
|
return s.lookupEndpoints(hostname)
|
2015-08-06 17:41:59 -04:00
|
|
|
}
|
|
|
|
|
2016-03-09 11:17:57 -05:00
|
|
|
// LookupPushEndpoints creates a list of endpoints to try to push to, in order of preference.
|
2015-08-06 17:41:59 -04:00
|
|
|
// It gives preference to v2 endpoints over v1, and HTTPS over plain HTTP.
|
|
|
|
// Mirrors are not included.
|
2016-03-01 02:07:41 -05:00
|
|
|
func (s *Service) LookupPushEndpoints(hostname string) (endpoints []APIEndpoint, err error) {
|
|
|
|
allEndpoints, err := s.lookupEndpoints(hostname)
|
2015-08-06 21:21:02 -04:00
|
|
|
if err == nil {
|
|
|
|
for _, endpoint := range allEndpoints {
|
|
|
|
if !endpoint.Mirror {
|
|
|
|
endpoints = append(endpoints, endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return endpoints, err
|
2015-08-06 17:41:59 -04:00
|
|
|
}
|
|
|
|
|
2016-03-01 02:07:41 -05:00
|
|
|
func (s *Service) lookupEndpoints(hostname string) (endpoints []APIEndpoint, err error) {
|
|
|
|
endpoints, err = s.lookupV2Endpoints(hostname)
|
2015-02-12 13:23:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:03:37 -05:00
|
|
|
if s.config.V2Only {
|
2015-09-16 13:42:17 -04:00
|
|
|
return endpoints, nil
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
|
|
|
|
2016-03-01 02:07:41 -05:00
|
|
|
legacyEndpoints, err := s.lookupV1Endpoints(hostname)
|
2015-09-16 13:42:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-02-12 13:23:22 -05:00
|
|
|
}
|
2015-09-16 13:42:17 -04:00
|
|
|
endpoints = append(endpoints, legacyEndpoints...)
|
2015-02-12 13:23:22 -05:00
|
|
|
|
|
|
|
return endpoints, nil
|
|
|
|
}
|