3d605683 |
package registry
|
73823e5e |
import ( |
19515a7a |
"crypto/tls" |
73823e5e |
"net/http" |
cb57b256 |
"net/url" |
4352da78 |
"strings" |
73823e5e |
|
2655954c |
"github.com/docker/docker/reference" |
907407d0 |
"github.com/docker/engine-api/types"
registrytypes "github.com/docker/engine-api/types/registry" |
73823e5e |
) |
bb9da6ba |
|
4fcb9ac4 |
// Service is a registry service. It tracks configuration data such as a list
// of mirrors. |
3d605683 |
type Service struct { |
96c10098 |
Config *registrytypes.ServiceConfig |
3d605683 |
}
// NewService returns a new instance of Service ready to be |
4fcb9ac4 |
// installed into an engine. |
568f86eb |
func NewService(options *Options) *Service { |
afade423 |
return &Service{ |
568f86eb |
Config: NewServiceConfig(options), |
afade423 |
} |
3d605683 |
}
// Auth contacts the public registry with the provided credentials, |
51462327 |
// and returns OK if authentication was successful. |
3d605683 |
// It can be used to verify the validity of a client's credentials. |
5b321e32 |
func (s *Service) Auth(authConfig *types.AuthConfig) (string, error) { |
41e20cec |
addr := authConfig.ServerAddress
if addr == "" {
// Use the official registry address if not specified. |
4fcb9ac4 |
addr = IndexServer |
3d605683 |
} |
03d3d79b |
index, err := s.ResolveIndex(addr)
if err != nil {
return "", err |
3d605683 |
} |
39f2f15a |
endpointVersion := APIVersion(APIVersionUnknown)
if V2Only {
// Override the endpoint to only attempt a v2 ping
endpointVersion = APIVersion2
}
endpoint, err := NewEndpoint(index, nil, endpointVersion) |
03d3d79b |
if err != nil {
return "", err |
41e20cec |
}
authConfig.ServerAddress = endpoint.String() |
a01cc3ca |
return Login(authConfig, endpoint) |
3d605683 |
} |
c4089ad8 |
|
4352da78 |
// 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
}
|
c4089ad8 |
// Search queries the public registry for images matching the specified
// search terms, and returns the results. |
c4472b38 |
func (s *Service) Search(term string, authConfig *types.AuthConfig, headers map[string][]string) (*registrytypes.SearchResults, error) { |
4352da78 |
if err := validateNoSchema(term); err != nil {
return nil, err
}
indexName, remoteName := splitReposSearchTerm(term) |
f04e8fdb |
|
96c10098 |
index, err := newIndexInfo(s.Config, indexName) |
3231033a |
if err != nil { |
03d3d79b |
return nil, err |
3231033a |
} |
a01cc3ca |
|
568f86eb |
// *TODO: Search multiple indexes. |
4352da78 |
endpoint, err := NewEndpoint(index, http.Header(headers), APIVersionUnknown) |
3231033a |
if err != nil { |
03d3d79b |
return nil, err |
c4089ad8 |
} |
39f2f15a |
|
73823e5e |
r, err := NewSession(endpoint.client, authConfig, endpoint) |
c4089ad8 |
if err != nil { |
03d3d79b |
return nil, err |
c4089ad8 |
} |
568f86eb |
|
4352da78 |
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) |
f04e8fdb |
}
|
4352da78 |
// ResolveRepository splits a repository name into its components |
f04e8fdb |
// and configuration of the associated registry. |
4352da78 |
func (s *Service) ResolveRepository(name reference.Named) (*RepositoryInfo, error) { |
96c10098 |
return newRepositoryInfo(s.Config, name) |
568f86eb |
}
// ResolveIndex takes indexName and returns index info |
96c10098 |
func (s *Service) ResolveIndex(name string) (*registrytypes.IndexInfo, error) {
return newIndexInfo(s.Config, name) |
568f86eb |
} |
19515a7a |
|
4fcb9ac4 |
// APIEndpoint represents a remote API endpoint |
19515a7a |
type APIEndpoint struct { |
a57478d6 |
Mirror bool
URL string
Version APIVersion
Official bool
TrimHostname bool
TLSConfig *tls.Config |
19515a7a |
}
|
4fcb9ac4 |
// ToV1Endpoint returns a V1 API endpoint based on the APIEndpoint |
19515a7a |
func (e APIEndpoint) ToV1Endpoint(metaHeaders http.Header) (*Endpoint, error) {
return newEndpoint(e.URL, e.TLSConfig, metaHeaders)
}
|
4fcb9ac4 |
// TLSConfig constructs a client TLS configuration based on server defaults
func (s *Service) TLSConfig(hostname string) (*tls.Config, error) { |
96c10098 |
return newTLSConfig(hostname, isSecureIndex(s.Config, hostname)) |
19515a7a |
}
|
cb57b256 |
func (s *Service) tlsConfigForMirror(mirror string) (*tls.Config, error) { |
4fcb9ac4 |
mirrorURL, err := url.Parse(mirror) |
cb57b256 |
if err != nil {
return nil, err
} |
4fcb9ac4 |
return s.TLSConfig(mirrorURL.Host) |
cb57b256 |
}
|
b899977e |
// LookupPullEndpoints creates an list of endpoints to try to pull from, in order of preference. |
4fcb9ac4 |
// It gives preference to v2 endpoints over v1, mirrors over the actual
// registry, and HTTPS over plain HTTP. |
4352da78 |
func (s *Service) LookupPullEndpoints(repoName reference.Named) (endpoints []APIEndpoint, err error) { |
c016d2de |
return s.lookupEndpoints(repoName) |
b899977e |
}
// 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. |
4352da78 |
func (s *Service) LookupPushEndpoints(repoName reference.Named) (endpoints []APIEndpoint, err error) { |
c016d2de |
allEndpoints, err := s.lookupEndpoints(repoName)
if err == nil {
for _, endpoint := range allEndpoints {
if !endpoint.Mirror {
endpoints = append(endpoints, endpoint)
}
}
}
return endpoints, err |
b899977e |
}
|
4352da78 |
func (s *Service) lookupEndpoints(repoName reference.Named) (endpoints []APIEndpoint, err error) { |
39f2f15a |
endpoints, err = s.lookupV2Endpoints(repoName) |
19515a7a |
if err != nil {
return nil, err
}
|
39f2f15a |
if V2Only {
return endpoints, nil |
19515a7a |
}
|
39f2f15a |
legacyEndpoints, err := s.lookupV1Endpoints(repoName)
if err != nil {
return nil, err |
19515a7a |
} |
39f2f15a |
endpoints = append(endpoints, legacyEndpoints...) |
19515a7a |
return endpoints, nil
} |