2018-02-05 16:05:59 -05:00
|
|
|
package registry // import "github.com/docker/docker/registry"
|
2013-03-14 20:43:59 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-03-01 02:07:41 -05:00
|
|
|
"net/url"
|
2013-03-14 20:43:59 -04:00
|
|
|
"strings"
|
2016-03-01 02:07:41 -05:00
|
|
|
"time"
|
2014-06-02 20:46:06 -04:00
|
|
|
|
2016-03-01 02:07:41 -05:00
|
|
|
"github.com/docker/distribution/registry/client/auth"
|
2016-11-10 18:59:02 -05:00
|
|
|
"github.com/docker/distribution/registry/client/auth/challenge"
|
2016-03-01 02:07:41 -05:00
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2022-02-26 13:13:43 -05:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2017-07-19 10:20:13 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2013-03-14 20:43:59 -04:00
|
|
|
)
|
|
|
|
|
2022-02-26 08:32:13 -05:00
|
|
|
// AuthClientID is used the ClientID used for the token server
|
|
|
|
const AuthClientID = "docker"
|
2016-02-23 18:18:04 -05:00
|
|
|
|
2016-03-01 02:07:41 -05:00
|
|
|
type loginCredentialStore struct {
|
2022-03-03 04:32:29 -05:00
|
|
|
authConfig *registry.AuthConfig
|
2016-03-01 02:07:41 -05:00
|
|
|
}
|
2014-12-11 20:55:15 -05:00
|
|
|
|
2016-03-01 02:07:41 -05:00
|
|
|
func (lcs loginCredentialStore) Basic(*url.URL) (string, string) {
|
|
|
|
return lcs.authConfig.Username, lcs.authConfig.Password
|
|
|
|
}
|
2014-12-11 20:55:15 -05:00
|
|
|
|
2016-02-23 18:18:04 -05:00
|
|
|
func (lcs loginCredentialStore) RefreshToken(*url.URL, string) string {
|
|
|
|
return lcs.authConfig.IdentityToken
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lcs loginCredentialStore) SetRefreshToken(u *url.URL, service, token string) {
|
|
|
|
lcs.authConfig.IdentityToken = token
|
|
|
|
}
|
|
|
|
|
2016-07-13 16:30:24 -04:00
|
|
|
type staticCredentialStore struct {
|
2022-03-03 04:32:29 -05:00
|
|
|
auth *registry.AuthConfig
|
2016-07-13 16:30:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStaticCredentialStore returns a credential store
|
|
|
|
// which always returns the same credential values.
|
2022-03-03 04:32:29 -05:00
|
|
|
func NewStaticCredentialStore(auth *registry.AuthConfig) auth.CredentialStore {
|
2016-07-13 16:30:24 -04:00
|
|
|
return staticCredentialStore{
|
|
|
|
auth: auth,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scs staticCredentialStore) Basic(*url.URL) (string, string) {
|
|
|
|
if scs.auth == nil {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
return scs.auth.Username, scs.auth.Password
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scs staticCredentialStore) RefreshToken(*url.URL, string) string {
|
|
|
|
if scs.auth == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return scs.auth.IdentityToken
|
|
|
|
}
|
|
|
|
|
|
|
|
func (scs staticCredentialStore) SetRefreshToken(*url.URL, string, string) {
|
|
|
|
}
|
|
|
|
|
2016-03-01 02:07:41 -05:00
|
|
|
// loginV2 tries to login to the v2 registry server. The given registry
|
|
|
|
// endpoint will be pinged to get authorization challenges. These challenges
|
|
|
|
// will be used to authenticate against the registry to validate credentials.
|
2022-03-03 04:32:29 -05:00
|
|
|
func loginV2(authConfig *registry.AuthConfig, endpoint APIEndpoint, userAgent string) (string, string, error) {
|
2020-10-29 17:26:57 -04:00
|
|
|
var (
|
|
|
|
endpointStr = strings.TrimRight(endpoint.URL.String(), "/") + "/v2/"
|
|
|
|
modifiers = Headers(userAgent, nil)
|
2022-02-25 17:58:25 -05:00
|
|
|
authTransport = transport.NewTransport(newTransport(endpoint.TLSConfig), modifiers...)
|
2020-10-29 17:26:57 -04:00
|
|
|
credentialAuthConfig = *authConfig
|
|
|
|
creds = loginCredentialStore{authConfig: &credentialAuthConfig}
|
|
|
|
)
|
2014-12-11 20:55:15 -05:00
|
|
|
|
2020-10-29 17:26:57 -04:00
|
|
|
logrus.Debugf("attempting v2 login to registry endpoint %s", endpointStr)
|
2014-12-11 20:55:15 -05:00
|
|
|
|
2019-06-17 21:42:24 -04:00
|
|
|
loginClient, err := v2AuthHTTPClient(endpoint.URL, authTransport, modifiers, creds, nil)
|
2016-07-13 16:30:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
2014-12-11 20:55:15 -05:00
|
|
|
}
|
|
|
|
|
2019-10-12 14:42:44 -04:00
|
|
|
req, err := http.NewRequest(http.MethodGet, endpointStr, nil)
|
2014-12-11 20:55:15 -05:00
|
|
|
if err != nil {
|
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
|
|
|
resp, err := loginClient.Do(req)
|
2014-12-11 20:55:15 -05:00
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
err = translateV2AuthError(err)
|
2016-02-23 18:18:04 -05:00
|
|
|
return "", "", err
|
2014-12-11 20:55:15 -05:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
return "Login Succeeded", credentialAuthConfig.IdentityToken, nil
|
2014-12-11 20:55:15 -05:00
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
// TODO(dmcgowan): Attempt to further interpret result, status code and error code string
|
2022-02-26 08:32:13 -05:00
|
|
|
return "", "", errors.Errorf("login attempt to %s failed with status: %d %s", endpointStr, resp.StatusCode, http.StatusText(resp.StatusCode))
|
2014-12-11 20:55:15 -05:00
|
|
|
}
|
|
|
|
|
2019-06-17 21:42:24 -04:00
|
|
|
func v2AuthHTTPClient(endpoint *url.URL, authTransport http.RoundTripper, modifiers []transport.RequestModifier, creds auth.CredentialStore, scopes []auth.Scope) (*http.Client, error) {
|
|
|
|
challengeManager, err := PingV2Registry(endpoint, authTransport)
|
2016-07-13 16:30:24 -04:00
|
|
|
if err != nil {
|
2019-06-17 21:42:24 -04:00
|
|
|
return nil, err
|
2016-07-13 16:30:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tokenHandlerOptions := auth.TokenHandlerOptions{
|
|
|
|
Transport: authTransport,
|
|
|
|
Credentials: creds,
|
|
|
|
OfflineAccess: true,
|
|
|
|
ClientID: AuthClientID,
|
|
|
|
Scopes: scopes,
|
|
|
|
}
|
|
|
|
tokenHandler := auth.NewTokenHandlerWithOptions(tokenHandlerOptions)
|
|
|
|
basicHandler := auth.NewBasicHandler(creds)
|
|
|
|
modifiers = append(modifiers, auth.NewAuthorizer(challengeManager, tokenHandler, basicHandler))
|
|
|
|
|
|
|
|
return &http.Client{
|
2022-02-26 08:32:13 -05:00
|
|
|
Transport: transport.NewTransport(authTransport, modifiers...),
|
2016-07-13 16:30:24 -04:00
|
|
|
Timeout: 15 * time.Second,
|
2019-06-17 21:42:24 -04:00
|
|
|
}, nil
|
2016-07-13 16:30:24 -04:00
|
|
|
}
|
|
|
|
|
2016-05-30 06:47:49 -04:00
|
|
|
// ConvertToHostname converts a registry url which has http|https prepended
|
|
|
|
// to just an hostname.
|
|
|
|
func ConvertToHostname(url string) string {
|
|
|
|
stripped := url
|
|
|
|
if strings.HasPrefix(url, "http://") {
|
|
|
|
stripped = strings.TrimPrefix(url, "http://")
|
|
|
|
} else if strings.HasPrefix(url, "https://") {
|
|
|
|
stripped = strings.TrimPrefix(url, "https://")
|
|
|
|
}
|
2022-02-26 08:32:13 -05:00
|
|
|
return strings.SplitN(stripped, "/", 2)[0]
|
2016-05-30 06:47:49 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:40:36 -04:00
|
|
|
// ResolveAuthConfig matches an auth configuration to a server address or a URL
|
2022-03-03 04:32:29 -05:00
|
|
|
func ResolveAuthConfig(authConfigs map[string]registry.AuthConfig, index *registry.IndexInfo) registry.AuthConfig {
|
2015-12-11 21:14:52 -05:00
|
|
|
configKey := GetAuthConfigKey(index)
|
2014-02-20 17:57:58 -05:00
|
|
|
// First try the happy case
|
2015-12-11 22:11:20 -05:00
|
|
|
if c, found := authConfigs[configKey]; found || index.Official {
|
2014-02-20 17:57:58 -05:00
|
|
|
return c
|
2013-09-03 14:45:49 -04:00
|
|
|
}
|
|
|
|
|
2014-02-20 17:57:58 -05:00
|
|
|
// Maybe they have a legacy config file, we will iterate the keys converting
|
|
|
|
// them to the new format and testing
|
2015-12-11 22:11:20 -05:00
|
|
|
for registry, ac := range authConfigs {
|
2016-05-30 06:47:49 -04:00
|
|
|
if configKey == ConvertToHostname(registry) {
|
2015-04-22 08:06:58 -04:00
|
|
|
return ac
|
2014-02-20 17:57:58 -05:00
|
|
|
}
|
2013-09-03 14:45:49 -04:00
|
|
|
}
|
2014-02-20 17:57:58 -05:00
|
|
|
|
|
|
|
// When all else fails, return an empty auth config
|
2022-03-03 04:32:29 -05:00
|
|
|
return registry.AuthConfig{}
|
2015-04-01 18:39:37 -04:00
|
|
|
}
|
2016-03-01 02:07:41 -05:00
|
|
|
|
|
|
|
// PingResponseError is used when the response from a ping
|
|
|
|
// was received but invalid.
|
|
|
|
type PingResponseError struct {
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err PingResponseError) Error() string {
|
2016-11-18 07:57:50 -05:00
|
|
|
return err.Err.Error()
|
2016-03-01 02:07:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PingV2Registry attempts to ping a v2 registry and on success return a
|
2019-06-17 21:42:24 -04:00
|
|
|
// challenge manager for the supported authentication types.
|
|
|
|
// If a response is received but cannot be interpreted, a PingResponseError will be returned.
|
|
|
|
func PingV2Registry(endpoint *url.URL, transport http.RoundTripper) (challenge.Manager, error) {
|
2016-03-01 02:07:41 -05:00
|
|
|
pingClient := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
Timeout: 15 * time.Second,
|
|
|
|
}
|
2016-07-13 16:30:24 -04:00
|
|
|
endpointStr := strings.TrimRight(endpoint.String(), "/") + "/v2/"
|
2019-10-12 14:42:44 -04:00
|
|
|
req, err := http.NewRequest(http.MethodGet, endpointStr, nil)
|
2016-03-01 02:07:41 -05:00
|
|
|
if err != nil {
|
2019-06-17 21:42:24 -04:00
|
|
|
return nil, err
|
2016-03-01 02:07:41 -05:00
|
|
|
}
|
|
|
|
resp, err := pingClient.Do(req)
|
|
|
|
if err != nil {
|
2019-06-17 21:42:24 -04:00
|
|
|
return nil, err
|
2016-03-01 02:07:41 -05:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-11-10 18:59:02 -05:00
|
|
|
challengeManager := challenge.NewSimpleManager()
|
2016-03-01 02:07:41 -05:00
|
|
|
if err := challengeManager.AddResponse(resp); err != nil {
|
2019-06-17 21:42:24 -04:00
|
|
|
return nil, PingResponseError{
|
2016-03-01 02:07:41 -05:00
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:42:24 -04:00
|
|
|
return challengeManager, nil
|
2016-03-01 02:07:41 -05:00
|
|
|
}
|