2015-05-21 18:14:46 +00:00
|
|
|
package auth
|
2015-05-08 23:29:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2015-05-21 18:14:46 +00:00
|
|
|
|
2015-09-30 15:47:01 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-07-24 23:14:04 +00:00
|
|
|
"github.com/docker/distribution/registry/client"
|
2015-05-21 18:14:46 +00:00
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2015-05-08 23:29:23 +00:00
|
|
|
)
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
// AuthenticationHandler is an interface for authorizing a request from
|
|
|
|
// params from a "WWW-Authenicate" header for a single scheme.
|
|
|
|
type AuthenticationHandler interface {
|
2015-05-20 17:09:37 +00:00
|
|
|
// Scheme returns the scheme as expected from the "WWW-Authenicate" header.
|
2015-05-11 18:31:22 +00:00
|
|
|
Scheme() string
|
2015-05-20 17:09:37 +00:00
|
|
|
|
|
|
|
// AuthorizeRequest adds the authorization header to a request (if needed)
|
|
|
|
// using the parameters from "WWW-Authenticate" method. The parameters
|
|
|
|
// values depend on the scheme.
|
2015-05-11 18:31:22 +00:00
|
|
|
AuthorizeRequest(req *http.Request, params map[string]string) error
|
|
|
|
}
|
|
|
|
|
2015-05-08 23:29:23 +00:00
|
|
|
// CredentialStore is an interface for getting credentials for
|
|
|
|
// a given URL
|
|
|
|
type CredentialStore interface {
|
|
|
|
// Basic returns basic auth for the given URL
|
|
|
|
Basic(*url.URL) (string, string)
|
|
|
|
}
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
// NewAuthorizer creates an authorizer which can handle multiple authentication
|
|
|
|
// schemes. The handlers are tried in order, the higher priority authentication
|
2015-05-21 18:14:46 +00:00
|
|
|
// methods should be first. The challengeMap holds a list of challenges for
|
|
|
|
// a given root API endpoint (for example "https://registry-1.docker.io/v2/").
|
2015-06-30 17:56:29 +00:00
|
|
|
func NewAuthorizer(manager ChallengeManager, handlers ...AuthenticationHandler) transport.RequestModifier {
|
2015-05-21 18:14:46 +00:00
|
|
|
return &endpointAuthorizer{
|
2015-06-30 17:56:29 +00:00
|
|
|
challenges: manager,
|
2015-05-11 18:31:22 +00:00
|
|
|
handlers: handlers,
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 18:14:46 +00:00
|
|
|
type endpointAuthorizer struct {
|
2015-06-30 17:56:29 +00:00
|
|
|
challenges ChallengeManager
|
2015-05-11 18:31:22 +00:00
|
|
|
handlers []AuthenticationHandler
|
2015-05-12 19:04:18 +00:00
|
|
|
transport http.RoundTripper
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 18:14:46 +00:00
|
|
|
func (ea *endpointAuthorizer) ModifyRequest(req *http.Request) error {
|
2015-05-08 23:29:23 +00:00
|
|
|
v2Root := strings.Index(req.URL.Path, "/v2/")
|
2015-05-21 18:14:46 +00:00
|
|
|
if v2Root == -1 {
|
2015-05-08 23:29:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ping := url.URL{
|
|
|
|
Host: req.URL.Host,
|
|
|
|
Scheme: req.URL.Scheme,
|
|
|
|
Path: req.URL.Path[:v2Root+4],
|
|
|
|
}
|
|
|
|
|
|
|
|
pingEndpoint := ping.String()
|
|
|
|
|
2015-06-30 17:56:29 +00:00
|
|
|
challenges, err := ea.challenges.GetChallenges(pingEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 17:56:29 +00:00
|
|
|
if len(challenges) > 0 {
|
|
|
|
for _, handler := range ea.handlers {
|
|
|
|
for _, challenge := range challenges {
|
|
|
|
if challenge.Scheme != handler.Scheme() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := handler.AuthorizeRequest(req, challenge.Parameters); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-11 18:31:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 23:29:23 +00:00
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
return nil
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:47:01 +00:00
|
|
|
// This is the minimum duration a token can last (in seconds).
|
|
|
|
// A token must not live less than 60 seconds because older versions
|
|
|
|
// of the Docker client didn't read their expiration from the token
|
|
|
|
// response and assumed 60 seconds. So to remain compatible with
|
|
|
|
// those implementations, a token must live at least this long.
|
|
|
|
const minimumTokenLifetimeSeconds = 60
|
|
|
|
|
|
|
|
// Private interface for time used by this package to enable tests to provide their own implementation.
|
|
|
|
type clock interface {
|
|
|
|
Now() time.Time
|
|
|
|
}
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
type tokenHandler struct {
|
2015-05-12 19:04:18 +00:00
|
|
|
header http.Header
|
|
|
|
creds CredentialStore
|
2015-05-21 18:14:46 +00:00
|
|
|
scope tokenScope
|
2015-05-12 19:04:18 +00:00
|
|
|
transport http.RoundTripper
|
2015-09-30 15:47:01 +00:00
|
|
|
clock clock
|
2015-05-08 23:29:23 +00:00
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
tokenLock sync.Mutex
|
|
|
|
tokenCache string
|
|
|
|
tokenExpiration time.Time
|
|
|
|
}
|
2015-05-08 23:29:23 +00:00
|
|
|
|
2015-05-21 18:14:46 +00:00
|
|
|
// tokenScope represents the scope at which a token will be requested.
|
2015-05-11 18:31:22 +00:00
|
|
|
// This represents a specific action on a registry resource.
|
2015-05-21 18:14:46 +00:00
|
|
|
type tokenScope struct {
|
2015-05-11 18:31:22 +00:00
|
|
|
Resource string
|
|
|
|
Scope string
|
|
|
|
Actions []string
|
|
|
|
}
|
|
|
|
|
2015-05-21 18:14:46 +00:00
|
|
|
func (ts tokenScope) String() string {
|
2015-05-14 16:54:23 +00:00
|
|
|
return fmt.Sprintf("%s:%s:%s", ts.Resource, ts.Scope, strings.Join(ts.Actions, ","))
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:47:01 +00:00
|
|
|
// An implementation of clock for providing real time data.
|
|
|
|
type realClock struct{}
|
|
|
|
|
|
|
|
// Now implements clock
|
|
|
|
func (realClock) Now() time.Time { return time.Now() }
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
// NewTokenHandler creates a new AuthenicationHandler which supports
|
|
|
|
// fetching tokens from a remote token server.
|
2015-05-21 18:14:46 +00:00
|
|
|
func NewTokenHandler(transport http.RoundTripper, creds CredentialStore, scope string, actions ...string) AuthenticationHandler {
|
2015-09-30 15:47:01 +00:00
|
|
|
return newTokenHandler(transport, creds, realClock{}, scope, actions...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTokenHandler exposes the option to provide a clock to manipulate time in unit testing.
|
|
|
|
func newTokenHandler(transport http.RoundTripper, creds CredentialStore, c clock, scope string, actions ...string) AuthenticationHandler {
|
2015-05-11 18:31:22 +00:00
|
|
|
return &tokenHandler{
|
2015-05-14 16:54:23 +00:00
|
|
|
transport: transport,
|
|
|
|
creds: creds,
|
2015-09-30 15:47:01 +00:00
|
|
|
clock: c,
|
2015-05-21 18:14:46 +00:00
|
|
|
scope: tokenScope{
|
|
|
|
Resource: "repository",
|
|
|
|
Scope: scope,
|
|
|
|
Actions: actions,
|
|
|
|
},
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
2015-05-11 18:31:22 +00:00
|
|
|
}
|
2015-05-08 23:29:23 +00:00
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
func (th *tokenHandler) client() *http.Client {
|
2015-05-12 19:04:18 +00:00
|
|
|
return &http.Client{
|
2015-05-14 16:54:23 +00:00
|
|
|
Transport: th.transport,
|
|
|
|
Timeout: 15 * time.Second,
|
2015-05-12 19:04:18 +00:00
|
|
|
}
|
2015-05-11 18:31:22 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
func (th *tokenHandler) Scheme() string {
|
2015-05-11 18:31:22 +00:00
|
|
|
return "bearer"
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
func (th *tokenHandler) AuthorizeRequest(req *http.Request, params map[string]string) error {
|
|
|
|
if err := th.refreshToken(params); err != nil {
|
2015-05-11 18:31:22 +00:00
|
|
|
return err
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", th.tokenCache))
|
2015-05-11 18:31:22 +00:00
|
|
|
|
2015-05-08 23:29:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
func (th *tokenHandler) refreshToken(params map[string]string) error {
|
|
|
|
th.tokenLock.Lock()
|
|
|
|
defer th.tokenLock.Unlock()
|
2015-09-30 15:47:01 +00:00
|
|
|
now := th.clock.Now()
|
2015-05-14 16:54:23 +00:00
|
|
|
if now.After(th.tokenExpiration) {
|
2015-09-30 15:47:01 +00:00
|
|
|
tr, err := th.fetchToken(params)
|
2015-05-08 23:29:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-30 15:47:01 +00:00
|
|
|
th.tokenCache = tr.Token
|
|
|
|
th.tokenExpiration = tr.IssuedAt.Add(time.Duration(tr.ExpiresIn) * time.Second)
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type tokenResponse struct {
|
2015-09-30 15:47:01 +00:00
|
|
|
Token string `json:"token"`
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
IssuedAt time.Time `json:"issued_at"`
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:47:01 +00:00
|
|
|
func (th *tokenHandler) fetchToken(params map[string]string) (token *tokenResponse, err error) {
|
2015-05-08 23:29:23 +00:00
|
|
|
//log.Debugf("Getting bearer token with %s for %s", challenge.Parameters, ta.auth.Username)
|
|
|
|
realm, ok := params["realm"]
|
|
|
|
if !ok {
|
2015-09-30 15:47:01 +00:00
|
|
|
return nil, errors.New("no realm specified for token auth challenge")
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 18:31:22 +00:00
|
|
|
// TODO(dmcgowan): Handle empty scheme
|
|
|
|
|
2015-05-08 23:29:23 +00:00
|
|
|
realmURL, err := url.Parse(realm)
|
|
|
|
if err != nil {
|
2015-09-30 15:47:01 +00:00
|
|
|
return nil, fmt.Errorf("invalid token auth challenge realm: %s", err)
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", realmURL.String(), nil)
|
|
|
|
if err != nil {
|
2015-09-30 15:47:01 +00:00
|
|
|
return nil, err
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reqParams := req.URL.Query()
|
|
|
|
service := params["service"]
|
2015-05-14 16:54:23 +00:00
|
|
|
scope := th.scope.String()
|
2015-05-08 23:29:23 +00:00
|
|
|
|
|
|
|
if service != "" {
|
|
|
|
reqParams.Add("service", service)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, scopeField := range strings.Fields(scope) {
|
|
|
|
reqParams.Add("scope", scopeField)
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
if th.creds != nil {
|
|
|
|
username, password := th.creds.Basic(realmURL)
|
2015-05-08 23:29:23 +00:00
|
|
|
if username != "" && password != "" {
|
|
|
|
reqParams.Add("account", username)
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req.URL.RawQuery = reqParams.Encode()
|
|
|
|
|
2015-05-14 16:54:23 +00:00
|
|
|
resp, err := th.client().Do(req)
|
2015-05-08 23:29:23 +00:00
|
|
|
if err != nil {
|
2015-09-30 15:47:01 +00:00
|
|
|
return nil, err
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-07-24 23:14:04 +00:00
|
|
|
if !client.SuccessStatus(resp.StatusCode) {
|
2015-12-08 22:24:03 +00:00
|
|
|
err := client.HandleErrorResponse(resp)
|
|
|
|
return nil, fmt.Errorf("token auth attempt for registry: %s request failed with status: %d %s: %q", req.URL, resp.StatusCode, http.StatusText(resp.StatusCode), err)
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
|
|
|
|
tr := new(tokenResponse)
|
|
|
|
if err = decoder.Decode(tr); err != nil {
|
2015-09-30 15:47:01 +00:00
|
|
|
return nil, fmt.Errorf("unable to decode token response: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// `access_token` is equivalent to `token` and if both are specified
|
|
|
|
// the choice is undefined. Canonicalize `access_token` by sticking
|
|
|
|
// things in `token`.
|
|
|
|
if tr.AccessToken != "" {
|
|
|
|
tr.Token = tr.AccessToken
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tr.Token == "" {
|
2015-09-30 15:47:01 +00:00
|
|
|
return nil, errors.New("authorization server did not include a token in the response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.ExpiresIn < minimumTokenLifetimeSeconds {
|
|
|
|
logrus.Debugf("Increasing token expiration to: %d seconds", tr.ExpiresIn)
|
|
|
|
// The default/minimum lifetime.
|
|
|
|
tr.ExpiresIn = minimumTokenLifetimeSeconds
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.IssuedAt.IsZero() {
|
|
|
|
// issued_at is optional in the token response.
|
|
|
|
tr.IssuedAt = th.clock.Now()
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:47:01 +00:00
|
|
|
return tr, nil
|
2015-05-08 23:29:23 +00:00
|
|
|
}
|
2015-05-11 18:31:22 +00:00
|
|
|
|
|
|
|
type basicHandler struct {
|
|
|
|
creds CredentialStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBasicHandler creaters a new authentiation handler which adds
|
|
|
|
// basic authentication credentials to a request.
|
|
|
|
func NewBasicHandler(creds CredentialStore) AuthenticationHandler {
|
|
|
|
return &basicHandler{
|
|
|
|
creds: creds,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*basicHandler) Scheme() string {
|
|
|
|
return "basic"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bh *basicHandler) AuthorizeRequest(req *http.Request, params map[string]string) error {
|
|
|
|
if bh.creds != nil {
|
|
|
|
username, password := bh.creds.Basic(req.URL)
|
|
|
|
if username != "" && password != "" {
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("no basic auth credentials")
|
|
|
|
}
|