2014-12-17 06:58:39 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2015-07-24 02:48:47 +00:00
|
|
|
"github.com/docker/distribution/context"
|
2015-02-11 02:18:45 +00:00
|
|
|
"github.com/docker/distribution/registry/auth"
|
2015-02-04 01:59:24 +00:00
|
|
|
"github.com/docker/libtrust"
|
2014-12-17 06:58:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// accessSet maps a typed, named resource to
|
|
|
|
// a set of actions requested or authorized.
|
|
|
|
type accessSet map[auth.Resource]actionSet
|
|
|
|
|
|
|
|
// newAccessSet constructs an accessSet from
|
|
|
|
// a variable number of auth.Access items.
|
|
|
|
func newAccessSet(accessItems ...auth.Access) accessSet {
|
|
|
|
accessSet := make(accessSet, len(accessItems))
|
|
|
|
|
|
|
|
for _, access := range accessItems {
|
|
|
|
resource := auth.Resource{
|
|
|
|
Type: access.Type,
|
|
|
|
Name: access.Name,
|
|
|
|
}
|
|
|
|
|
2014-12-17 18:57:05 +00:00
|
|
|
set, exists := accessSet[resource]
|
|
|
|
if !exists {
|
|
|
|
set = newActionSet()
|
2014-12-17 06:58:39 +00:00
|
|
|
accessSet[resource] = set
|
|
|
|
}
|
|
|
|
|
2015-01-06 02:21:03 +00:00
|
|
|
set.add(access.Action)
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return accessSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// contains returns whether or not the given access is in this accessSet.
|
|
|
|
func (s accessSet) contains(access auth.Access) bool {
|
|
|
|
actionSet, ok := s[access.Resource]
|
|
|
|
if ok {
|
2015-01-06 02:21:03 +00:00
|
|
|
return actionSet.contains(access.Action)
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// scopeParam returns a collection of scopes which can
|
|
|
|
// be used for a WWW-Authenticate challenge parameter.
|
|
|
|
// See https://tools.ietf.org/html/rfc6750#section-3
|
|
|
|
func (s accessSet) scopeParam() string {
|
|
|
|
scopes := make([]string, 0, len(s))
|
|
|
|
|
|
|
|
for resource, actionSet := range s {
|
2015-01-06 02:21:03 +00:00
|
|
|
actions := strings.Join(actionSet.keys(), ",")
|
2014-12-17 06:58:39 +00:00
|
|
|
scopes = append(scopes, fmt.Sprintf("%s:%s:%s", resource.Type, resource.Name, actions))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(scopes, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errors used and exported by this package.
|
|
|
|
var (
|
|
|
|
ErrInsufficientScope = errors.New("insufficient scope")
|
|
|
|
ErrTokenRequired = errors.New("authorization token required")
|
|
|
|
)
|
|
|
|
|
|
|
|
// authChallenge implements the auth.Challenge interface.
|
|
|
|
type authChallenge struct {
|
|
|
|
err error
|
|
|
|
realm string
|
|
|
|
service string
|
|
|
|
accessSet accessSet
|
|
|
|
}
|
|
|
|
|
2015-07-24 02:39:56 +00:00
|
|
|
var _ auth.Challenge = authChallenge{}
|
|
|
|
|
2014-12-17 06:58:39 +00:00
|
|
|
// Error returns the internal error string for this authChallenge.
|
2015-07-24 02:39:56 +00:00
|
|
|
func (ac authChallenge) Error() string {
|
2014-12-17 06:58:39 +00:00
|
|
|
return ac.err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status returns the HTTP Response Status Code for this authChallenge.
|
2015-07-24 02:39:56 +00:00
|
|
|
func (ac authChallenge) Status() int {
|
2014-12-17 06:58:39 +00:00
|
|
|
return http.StatusUnauthorized
|
|
|
|
}
|
|
|
|
|
|
|
|
// challengeParams constructs the value to be used in
|
|
|
|
// the WWW-Authenticate response challenge header.
|
|
|
|
// See https://tools.ietf.org/html/rfc6750#section-3
|
2015-07-24 02:39:56 +00:00
|
|
|
func (ac authChallenge) challengeParams() string {
|
2014-12-18 00:16:02 +00:00
|
|
|
str := fmt.Sprintf("Bearer realm=%q,service=%q", ac.realm, ac.service)
|
2014-12-17 06:58:39 +00:00
|
|
|
|
|
|
|
if scope := ac.accessSet.scopeParam(); scope != "" {
|
2014-12-18 00:16:02 +00:00
|
|
|
str = fmt.Sprintf("%s,scope=%q", str, scope)
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ac.err == ErrInvalidToken || ac.err == ErrMalformedToken {
|
2014-12-18 00:16:02 +00:00
|
|
|
str = fmt.Sprintf("%s,error=%q", str, "invalid_token")
|
2014-12-17 06:58:39 +00:00
|
|
|
} else if ac.err == ErrInsufficientScope {
|
2014-12-18 00:16:02 +00:00
|
|
|
str = fmt.Sprintf("%s,error=%q", str, "insufficient_scope")
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2015-07-24 02:39:56 +00:00
|
|
|
// SetChallenge sets the WWW-Authenticate value for the response.
|
|
|
|
func (ac authChallenge) SetHeaders(w http.ResponseWriter) {
|
|
|
|
w.Header().Add("WWW-Authenticate", ac.challengeParams())
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// accessController implements the auth.AccessController interface.
|
|
|
|
type accessController struct {
|
|
|
|
realm string
|
|
|
|
issuer string
|
|
|
|
service string
|
|
|
|
rootCerts *x509.CertPool
|
|
|
|
trustedKeys map[string]libtrust.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// tokenAccessOptions is a convenience type for handling
|
|
|
|
// options to the contstructor of an accessController.
|
|
|
|
type tokenAccessOptions struct {
|
|
|
|
realm string
|
|
|
|
issuer string
|
|
|
|
service string
|
|
|
|
rootCertBundle string
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkOptions gathers the necessary options
|
|
|
|
// for an accessController from the given map.
|
|
|
|
func checkOptions(options map[string]interface{}) (tokenAccessOptions, error) {
|
|
|
|
var opts tokenAccessOptions
|
|
|
|
|
2015-02-20 00:46:24 +00:00
|
|
|
keys := []string{"realm", "issuer", "service", "rootcertbundle"}
|
2014-12-17 06:58:39 +00:00
|
|
|
vals := make([]string, 0, len(keys))
|
|
|
|
for _, key := range keys {
|
|
|
|
val, ok := options[key].(string)
|
|
|
|
if !ok {
|
|
|
|
return opts, fmt.Errorf("token auth requires a valid option string: %q", key)
|
|
|
|
}
|
|
|
|
vals = append(vals, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.realm, opts.issuer, opts.service, opts.rootCertBundle = vals[0], vals[1], vals[2], vals[3]
|
|
|
|
|
|
|
|
return opts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newAccessController creates an accessController using the given options.
|
|
|
|
func newAccessController(options map[string]interface{}) (auth.AccessController, error) {
|
|
|
|
config, err := checkOptions(options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fp, err := os.Open(config.rootCertBundle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to open token auth root certificate bundle file %q: %s", config.rootCertBundle, err)
|
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
|
|
|
|
rawCertBundle, err := ioutil.ReadAll(fp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read token auth root certificate bundle file %q: %s", config.rootCertBundle, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootCerts []*x509.Certificate
|
|
|
|
pemBlock, rawCertBundle := pem.Decode(rawCertBundle)
|
|
|
|
for pemBlock != nil {
|
2016-08-31 17:00:12 +00:00
|
|
|
if pemBlock.Type == "CERTIFICATE" {
|
|
|
|
cert, err := x509.ParseCertificate(pemBlock.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to parse token auth root certificate: %s", err)
|
|
|
|
}
|
2014-12-17 06:58:39 +00:00
|
|
|
|
2016-08-31 17:00:12 +00:00
|
|
|
rootCerts = append(rootCerts, cert)
|
|
|
|
}
|
2014-12-17 06:58:39 +00:00
|
|
|
|
|
|
|
pemBlock, rawCertBundle = pem.Decode(rawCertBundle)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rootCerts) == 0 {
|
|
|
|
return nil, errors.New("token auth requires at least one token signing root certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
rootPool := x509.NewCertPool()
|
|
|
|
trustedKeys := make(map[string]libtrust.PublicKey, len(rootCerts))
|
|
|
|
for _, rootCert := range rootCerts {
|
|
|
|
rootPool.AddCert(rootCert)
|
|
|
|
pubKey, err := libtrust.FromCryptoPublicKey(crypto.PublicKey(rootCert.PublicKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to get public key from token auth root certificate: %s", err)
|
|
|
|
}
|
|
|
|
trustedKeys[pubKey.KeyID()] = pubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return &accessController{
|
|
|
|
realm: config.realm,
|
|
|
|
issuer: config.issuer,
|
|
|
|
service: config.service,
|
|
|
|
rootCerts: rootPool,
|
|
|
|
trustedKeys: trustedKeys,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authorized handles checking whether the given request is authorized
|
|
|
|
// for actions on resources described by the given access items.
|
2015-02-04 01:59:24 +00:00
|
|
|
func (ac *accessController) Authorized(ctx context.Context, accessItems ...auth.Access) (context.Context, error) {
|
2014-12-17 06:58:39 +00:00
|
|
|
challenge := &authChallenge{
|
|
|
|
realm: ac.realm,
|
|
|
|
service: ac.service,
|
|
|
|
accessSet: newAccessSet(accessItems...),
|
|
|
|
}
|
|
|
|
|
2015-07-24 02:48:47 +00:00
|
|
|
req, err := context.GetRequest(ctx)
|
2015-02-04 01:59:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-17 06:58:39 +00:00
|
|
|
parts := strings.Split(req.Header.Get("Authorization"), " ")
|
|
|
|
|
|
|
|
if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
|
|
|
|
challenge.err = ErrTokenRequired
|
2015-02-04 01:59:24 +00:00
|
|
|
return nil, challenge
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rawToken := parts[1]
|
|
|
|
|
|
|
|
token, err := NewToken(rawToken)
|
|
|
|
if err != nil {
|
|
|
|
challenge.err = err
|
2015-02-04 01:59:24 +00:00
|
|
|
return nil, challenge
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
verifyOpts := VerifyOptions{
|
2015-01-06 02:21:03 +00:00
|
|
|
TrustedIssuers: []string{ac.issuer},
|
|
|
|
AcceptedAudiences: []string{ac.service},
|
2014-12-17 06:58:39 +00:00
|
|
|
Roots: ac.rootCerts,
|
|
|
|
TrustedKeys: ac.trustedKeys,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = token.Verify(verifyOpts); err != nil {
|
|
|
|
challenge.err = err
|
2015-02-04 01:59:24 +00:00
|
|
|
return nil, challenge
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
accessSet := token.accessSet()
|
|
|
|
for _, access := range accessItems {
|
|
|
|
if !accessSet.contains(access) {
|
|
|
|
challenge.err = ErrInsufficientScope
|
2015-02-04 01:59:24 +00:00
|
|
|
return nil, challenge
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 03:12:32 +00:00
|
|
|
return auth.WithUser(ctx, auth.UserInfo{Name: token.Claims.Subject}), nil
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// init handles registering the token auth backend.
|
|
|
|
func init() {
|
|
|
|
auth.Register("token", auth.InitFunc(newAccessController))
|
|
|
|
}
|