2016-04-08 19:22:39 -04:00
|
|
|
package authorization
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Middleware uses a list of plugins to
|
|
|
|
// handle authorization in the API requests.
|
|
|
|
type Middleware struct {
|
|
|
|
plugins []Plugin
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMiddleware creates a new Middleware
|
2016-05-16 14:12:48 -04:00
|
|
|
// with a slice of plugins names.
|
|
|
|
func NewMiddleware(names []string) *Middleware {
|
|
|
|
return &Middleware{
|
|
|
|
plugins: newPlugins(names),
|
2016-04-08 19:22:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:12:48 -04:00
|
|
|
// SetPlugins sets the plugin used for authorization
|
|
|
|
func (m *Middleware) SetPlugins(names []string) {
|
|
|
|
m.plugins = newPlugins(names)
|
|
|
|
}
|
|
|
|
|
2016-04-08 19:22:39 -04:00
|
|
|
// WrapHandler returns a new handler function wrapping the previous one in the request chain.
|
2016-05-16 14:12:48 -04:00
|
|
|
func (m *Middleware) WrapHandler(handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error) func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2016-04-08 19:22:39 -04:00
|
|
|
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
|
2016-05-16 14:12:48 -04:00
|
|
|
if len(m.plugins) == 0 {
|
|
|
|
return handler(ctx, w, r, vars)
|
|
|
|
}
|
|
|
|
|
2016-04-08 19:22:39 -04:00
|
|
|
user := ""
|
|
|
|
userAuthNMethod := ""
|
|
|
|
|
|
|
|
// Default authorization using existing TLS connection credentials
|
|
|
|
// FIXME: Non trivial authorization mechanisms (such as advanced certificate validations, kerberos support
|
|
|
|
// and ldap) will be extracted using AuthN feature, which is tracked under:
|
|
|
|
// https://github.com/docker/docker/pull/20883
|
|
|
|
if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
|
|
|
|
user = r.TLS.PeerCertificates[0].Subject.CommonName
|
|
|
|
userAuthNMethod = "TLS"
|
|
|
|
}
|
|
|
|
|
|
|
|
authCtx := NewCtx(m.plugins, user, userAuthNMethod, r.Method, r.RequestURI)
|
|
|
|
|
|
|
|
if err := authCtx.AuthZRequest(w, r); err != nil {
|
|
|
|
logrus.Errorf("AuthZRequest for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rw := NewResponseModifier(w)
|
|
|
|
|
|
|
|
if err := handler(ctx, rw, r, vars); err != nil {
|
|
|
|
logrus.Errorf("Handler for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := authCtx.AuthZResponse(rw, r); err != nil {
|
|
|
|
logrus.Errorf("AuthZResponse for %s %s returned error: %s", r.Method, r.RequestURI, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|