2014-04-28 00:06:09 +02:00
|
|
|
package registry
|
|
|
|
|
2015-05-16 03:35:04 +02:00
|
|
|
import (
|
2015-02-12 19:23:22 +01:00
|
|
|
"crypto/tls"
|
2015-05-16 03:35:04 +02:00
|
|
|
"net/http"
|
2015-07-21 23:10:34 +02:00
|
|
|
"net/url"
|
2015-11-18 23:20:54 +01:00
|
|
|
"strings"
|
2015-05-16 03:35:04 +02:00
|
|
|
|
2015-11-18 23:20:54 +01:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-02-12 19:23:22 +01:00
|
|
|
"github.com/docker/distribution/registry/client/auth"
|
2015-05-16 03:35:04 +02:00
|
|
|
"github.com/docker/docker/cliconfig"
|
|
|
|
)
|
2015-04-22 14:06:58 +02:00
|
|
|
|
2015-07-21 21:40:36 +02:00
|
|
|
// Service is a registry service. It tracks configuration data such as a list
|
|
|
|
// of mirrors.
|
2014-04-28 00:06:09 +02:00
|
|
|
type Service struct {
|
2014-10-07 03:54:52 +02:00
|
|
|
Config *ServiceConfig
|
2014-04-28 00:06:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns a new instance of Service ready to be
|
2015-07-21 21:40:36 +02:00
|
|
|
// installed into an engine.
|
2014-10-07 03:54:52 +02:00
|
|
|
func NewService(options *Options) *Service {
|
2014-08-20 17:31:24 +02:00
|
|
|
return &Service{
|
2014-10-07 03:54:52 +02:00
|
|
|
Config: NewServiceConfig(options),
|
2014-08-20 17:31:24 +02:00
|
|
|
}
|
2014-04-28 00:06:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Auth contacts the public registry with the provided credentials,
|
2015-08-08 00:24:18 +02:00
|
|
|
// and returns OK if authentication was successful.
|
2014-04-28 00:06:09 +02:00
|
|
|
// It can be used to verify the validity of a client's credentials.
|
2015-04-22 14:06:58 +02:00
|
|
|
func (s *Service) Auth(authConfig *cliconfig.AuthConfig) (string, error) {
|
2014-12-12 02:55:15 +01:00
|
|
|
addr := authConfig.ServerAddress
|
|
|
|
if addr == "" {
|
|
|
|
// Use the official registry address if not specified.
|
2015-07-21 21:40:36 +02:00
|
|
|
addr = IndexServer
|
2014-04-28 00:06:09 +02:00
|
|
|
}
|
2015-04-01 01:21:37 +02:00
|
|
|
index, err := s.ResolveIndex(addr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2014-04-28 00:06:09 +02:00
|
|
|
}
|
2015-09-16 19:42:17 +02:00
|
|
|
|
|
|
|
endpointVersion := APIVersion(APIVersionUnknown)
|
|
|
|
if V2Only {
|
|
|
|
// Override the endpoint to only attempt a v2 ping
|
|
|
|
endpointVersion = APIVersion2
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint, err := NewEndpoint(index, nil, endpointVersion)
|
2015-04-01 01:21:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2014-12-12 02:55:15 +01:00
|
|
|
}
|
|
|
|
authConfig.ServerAddress = endpoint.String()
|
2015-05-14 16:12:54 +02:00
|
|
|
return Login(authConfig, endpoint)
|
2014-04-28 00:06:09 +02:00
|
|
|
}
|
2014-04-28 00:21:42 +02:00
|
|
|
|
2015-11-18 23:20:54 +01: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-28 00:21:42 +02:00
|
|
|
// Search queries the public registry for images matching the specified
|
|
|
|
// search terms, and returns the results.
|
2015-04-22 14:06:58 +02:00
|
|
|
func (s *Service) Search(term string, authConfig *cliconfig.AuthConfig, headers map[string][]string) (*SearchResults, error) {
|
2015-11-18 23:20:54 +01:00
|
|
|
if err := validateNoSchema(term); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
indexName, remoteName := splitReposSearchTerm(term)
|
2015-09-22 13:44:40 +02:00
|
|
|
|
2015-11-18 23:20:54 +01:00
|
|
|
index, err := s.Config.NewIndexInfo(indexName)
|
2014-08-09 09:16:54 +02:00
|
|
|
if err != nil {
|
2015-04-01 01:21:37 +02:00
|
|
|
return nil, err
|
2014-08-09 09:16:54 +02:00
|
|
|
}
|
2015-05-14 16:12:54 +02:00
|
|
|
|
2014-10-07 03:54:52 +02:00
|
|
|
// *TODO: Search multiple indexes.
|
2015-11-18 23:20:54 +01:00
|
|
|
endpoint, err := NewEndpoint(index, http.Header(headers), APIVersionUnknown)
|
2014-08-09 09:16:54 +02:00
|
|
|
if err != nil {
|
2015-04-01 01:21:37 +02:00
|
|
|
return nil, err
|
2014-04-28 00:21:42 +02:00
|
|
|
}
|
2015-09-16 19:42:17 +02:00
|
|
|
|
2015-05-16 03:35:04 +02:00
|
|
|
r, err := NewSession(endpoint.client, authConfig, endpoint)
|
2014-04-28 00:21:42 +02:00
|
|
|
if err != nil {
|
2015-04-01 01:21:37 +02:00
|
|
|
return nil, err
|
2014-04-28 00:21:42 +02:00
|
|
|
}
|
2014-10-07 03:54:52 +02:00
|
|
|
|
2015-11-18 23:20:54 +01: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 13:44:40 +02:00
|
|
|
}
|
|
|
|
|
2015-11-18 23:20:54 +01:00
|
|
|
// ResolveRepository splits a repository name into its components
|
2015-09-22 13:44:40 +02:00
|
|
|
// and configuration of the associated registry.
|
2015-11-18 23:20:54 +01:00
|
|
|
func (s *Service) ResolveRepository(name reference.Named) (*RepositoryInfo, error) {
|
|
|
|
return s.Config.NewRepositoryInfo(name)
|
2014-10-07 03:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveIndex takes indexName and returns index info
|
2015-04-01 01:21:37 +02:00
|
|
|
func (s *Service) ResolveIndex(name string) (*IndexInfo, error) {
|
|
|
|
return s.Config.NewIndexInfo(name)
|
2014-10-07 03:54:52 +02:00
|
|
|
}
|
2015-02-12 19:23:22 +01:00
|
|
|
|
2015-07-21 21:40:36 +02:00
|
|
|
// APIEndpoint represents a remote API endpoint
|
2015-02-12 19:23:22 +01:00
|
|
|
type APIEndpoint struct {
|
|
|
|
Mirror bool
|
|
|
|
URL string
|
|
|
|
Version APIVersion
|
|
|
|
Official bool
|
|
|
|
TrimHostname bool
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
VersionHeader string
|
|
|
|
Versions []auth.APIVersion
|
|
|
|
}
|
|
|
|
|
2015-07-21 21:40:36 +02:00
|
|
|
// ToV1Endpoint returns a V1 API endpoint based on the APIEndpoint
|
2015-02-12 19:23:22 +01:00
|
|
|
func (e APIEndpoint) ToV1Endpoint(metaHeaders http.Header) (*Endpoint, error) {
|
|
|
|
return newEndpoint(e.URL, e.TLSConfig, metaHeaders)
|
|
|
|
}
|
|
|
|
|
2015-07-21 21:40:36 +02:00
|
|
|
// TLSConfig constructs a client TLS configuration based on server defaults
|
|
|
|
func (s *Service) TLSConfig(hostname string) (*tls.Config, error) {
|
2015-07-28 19:36:57 +02:00
|
|
|
return newTLSConfig(hostname, s.Config.isSecureIndex(hostname))
|
2015-02-12 19:23:22 +01:00
|
|
|
}
|
|
|
|
|
2015-07-21 23:10:34 +02:00
|
|
|
func (s *Service) tlsConfigForMirror(mirror string) (*tls.Config, error) {
|
2015-07-21 21:40:36 +02:00
|
|
|
mirrorURL, err := url.Parse(mirror)
|
2015-07-21 23:10:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-21 21:40:36 +02:00
|
|
|
return s.TLSConfig(mirrorURL.Host)
|
2015-07-21 23:10:34 +02:00
|
|
|
}
|
|
|
|
|
2015-08-06 23:41:59 +02:00
|
|
|
// LookupPullEndpoints creates an list of endpoints to try to pull from, in order of preference.
|
2015-07-21 21:40:36 +02:00
|
|
|
// It gives preference to v2 endpoints over v1, mirrors over the actual
|
|
|
|
// registry, and HTTPS over plain HTTP.
|
2015-11-18 23:20:54 +01:00
|
|
|
func (s *Service) LookupPullEndpoints(repoName reference.Named) (endpoints []APIEndpoint, err error) {
|
2015-08-07 03:21:02 +02:00
|
|
|
return s.lookupEndpoints(repoName)
|
2015-08-06 23:41:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LookupPushEndpoints creates an list of endpoints to try to push to, in order of preference.
|
|
|
|
// It gives preference to v2 endpoints over v1, and HTTPS over plain HTTP.
|
|
|
|
// Mirrors are not included.
|
2015-11-18 23:20:54 +01:00
|
|
|
func (s *Service) LookupPushEndpoints(repoName reference.Named) (endpoints []APIEndpoint, err error) {
|
2015-08-07 03:21:02 +02:00
|
|
|
allEndpoints, err := s.lookupEndpoints(repoName)
|
|
|
|
if err == nil {
|
|
|
|
for _, endpoint := range allEndpoints {
|
|
|
|
if !endpoint.Mirror {
|
|
|
|
endpoints = append(endpoints, endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return endpoints, err
|
2015-08-06 23:41:59 +02:00
|
|
|
}
|
|
|
|
|
2015-11-18 23:20:54 +01:00
|
|
|
func (s *Service) lookupEndpoints(repoName reference.Named) (endpoints []APIEndpoint, err error) {
|
2015-09-16 19:42:17 +02:00
|
|
|
endpoints, err = s.lookupV2Endpoints(repoName)
|
2015-02-12 19:23:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-16 19:42:17 +02:00
|
|
|
if V2Only {
|
|
|
|
return endpoints, nil
|
2015-02-12 19:23:22 +01:00
|
|
|
}
|
|
|
|
|
2015-09-16 19:42:17 +02:00
|
|
|
legacyEndpoints, err := s.lookupV1Endpoints(repoName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-02-12 19:23:22 +01:00
|
|
|
}
|
2015-09-16 19:42:17 +02:00
|
|
|
endpoints = append(endpoints, legacyEndpoints...)
|
2015-02-12 19:23:22 +01:00
|
|
|
|
|
|
|
return endpoints, nil
|
|
|
|
}
|