2015-11-19 01:18:44 +03:00
|
|
|
package distribution
|
|
|
|
|
|
|
|
import (
|
2015-11-04 08:03:12 +03:00
|
|
|
"fmt"
|
2015-11-19 01:18:44 +03:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
2016-01-26 22:20:14 +03:00
|
|
|
distreference "github.com/docker/distribution/reference"
|
2015-11-19 01:18:44 +03:00
|
|
|
"github.com/docker/distribution/registry/client"
|
|
|
|
"github.com/docker/distribution/registry/client/auth"
|
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2016-01-04 21:36:01 +03:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2015-11-19 01:18:44 +03:00
|
|
|
"github.com/docker/docker/registry"
|
2016-01-05 03:05:26 +03:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-11-19 01:18:44 +03:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dumbCredentialStore struct {
|
2015-12-12 07:11:42 +03:00
|
|
|
auth *types.AuthConfig
|
2015-11-19 01:18:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dcs dumbCredentialStore) Basic(*url.URL) (string, string) {
|
|
|
|
return dcs.auth.Username, dcs.auth.Password
|
|
|
|
}
|
|
|
|
|
2016-02-24 02:18:04 +03:00
|
|
|
func (dcs dumbCredentialStore) RefreshToken(*url.URL, string) string {
|
|
|
|
return dcs.auth.IdentityToken
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dcs dumbCredentialStore) SetRefreshToken(*url.URL, string, string) {
|
|
|
|
}
|
|
|
|
|
2015-11-19 01:18:44 +03:00
|
|
|
// NewV2Repository returns a repository (v2 only). It creates a HTTP transport
|
|
|
|
// providing timeout settings and authentication support, and also verifies the
|
|
|
|
// remote API version.
|
2015-12-05 00:42:33 +03:00
|
|
|
func NewV2Repository(ctx context.Context, repoInfo *registry.RepositoryInfo, endpoint registry.APIEndpoint, metaHeaders http.Header, authConfig *types.AuthConfig, actions ...string) (repo distribution.Repository, foundVersion bool, err error) {
|
2016-03-09 05:18:53 +03:00
|
|
|
upstreamUA := dockerversion.GetUserAgentFromContext(ctx)
|
|
|
|
|
2015-12-11 22:00:13 +03:00
|
|
|
repoName := repoInfo.FullName()
|
2015-11-19 01:18:44 +03:00
|
|
|
// If endpoint does not support CanonicalName, use the RemoteName instead
|
|
|
|
if endpoint.TrimHostname {
|
2015-12-11 22:00:13 +03:00
|
|
|
repoName = repoInfo.RemoteName()
|
2015-11-19 01:18:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(dmcgowan): Call close idle connections when complete, use keep alive
|
|
|
|
base := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2016-02-03 20:55:16 +03:00
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}).Dial,
|
2015-11-19 01:18:44 +03:00
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
TLSClientConfig: endpoint.TLSConfig,
|
|
|
|
// TODO(dmcgowan): Call close idle connections when complete and use keep alive
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
}
|
|
|
|
|
2016-03-09 05:18:53 +03:00
|
|
|
modifiers := registry.DockerHeaders(dockerversion.DockerUserAgent(upstreamUA), metaHeaders)
|
2015-11-19 01:18:44 +03:00
|
|
|
authTransport := transport.NewTransport(base, modifiers...)
|
2016-02-12 02:45:29 +03:00
|
|
|
|
2016-03-01 10:07:41 +03:00
|
|
|
challengeManager, foundVersion, err := registry.PingV2Registry(endpoint, authTransport)
|
|
|
|
if err != nil {
|
|
|
|
transportOK := false
|
|
|
|
if responseErr, ok := err.(registry.PingResponseError); ok {
|
|
|
|
transportOK = true
|
|
|
|
err = responseErr.Err
|
2015-11-19 01:18:44 +03:00
|
|
|
}
|
2016-02-12 02:45:29 +03:00
|
|
|
return nil, foundVersion, fallbackError{
|
|
|
|
err: err,
|
|
|
|
confirmedV2: foundVersion,
|
2016-03-01 10:07:41 +03:00
|
|
|
transportOK: transportOK,
|
2016-02-12 02:45:29 +03:00
|
|
|
}
|
2015-11-19 01:18:44 +03:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:03:12 +03:00
|
|
|
if authConfig.RegistryToken != "" {
|
|
|
|
passThruTokenHandler := &existingTokenHandler{token: authConfig.RegistryToken}
|
|
|
|
modifiers = append(modifiers, auth.NewAuthorizer(challengeManager, passThruTokenHandler))
|
|
|
|
} else {
|
|
|
|
creds := dumbCredentialStore{auth: authConfig}
|
2016-02-24 02:18:04 +03:00
|
|
|
tokenHandlerOptions := auth.TokenHandlerOptions{
|
|
|
|
Transport: authTransport,
|
|
|
|
Credentials: creds,
|
|
|
|
Scopes: []auth.Scope{
|
|
|
|
auth.RepositoryScope{
|
|
|
|
Repository: repoName,
|
|
|
|
Actions: actions,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ClientID: registry.AuthClientID,
|
|
|
|
}
|
|
|
|
tokenHandler := auth.NewTokenHandlerWithOptions(tokenHandlerOptions)
|
2015-11-04 08:03:12 +03:00
|
|
|
basicHandler := auth.NewBasicHandler(creds)
|
|
|
|
modifiers = append(modifiers, auth.NewAuthorizer(challengeManager, tokenHandler, basicHandler))
|
|
|
|
}
|
2015-11-19 01:18:44 +03:00
|
|
|
tr := transport.NewTransport(base, modifiers...)
|
|
|
|
|
2016-01-26 22:20:14 +03:00
|
|
|
repoNameRef, err := distreference.ParseNamed(repoName)
|
|
|
|
if err != nil {
|
2016-02-12 02:45:29 +03:00
|
|
|
return nil, foundVersion, fallbackError{
|
|
|
|
err: err,
|
|
|
|
confirmedV2: foundVersion,
|
|
|
|
transportOK: true,
|
|
|
|
}
|
2016-01-26 22:20:14 +03:00
|
|
|
}
|
|
|
|
|
2016-02-18 03:53:25 +03:00
|
|
|
repo, err = client.NewRepository(ctx, repoNameRef, endpoint.URL.String(), tr)
|
2016-02-12 02:45:29 +03:00
|
|
|
if err != nil {
|
|
|
|
err = fallbackError{
|
|
|
|
err: err,
|
|
|
|
confirmedV2: foundVersion,
|
|
|
|
transportOK: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2015-11-19 01:18:44 +03:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:03:12 +03:00
|
|
|
type existingTokenHandler struct {
|
|
|
|
token string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *existingTokenHandler) Scheme() string {
|
|
|
|
return "bearer"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *existingTokenHandler) AuthorizeRequest(req *http.Request, params map[string]string) error {
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", th.token))
|
|
|
|
return nil
|
|
|
|
}
|