middleware.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package authorization
  2. import (
  3. "net/http"
  4. "sync"
  5. "github.com/Sirupsen/logrus"
  6. "golang.org/x/net/context"
  7. )
  8. // Middleware uses a list of plugins to
  9. // handle authorization in the API requests.
  10. type Middleware struct {
  11. mu sync.Mutex
  12. plugins []Plugin
  13. }
  14. // NewMiddleware creates a new Middleware
  15. // with a slice of plugins names.
  16. func NewMiddleware(names []string) *Middleware {
  17. return &Middleware{
  18. plugins: newPlugins(names),
  19. }
  20. }
  21. // SetPlugins sets the plugin used for authorization
  22. func (m *Middleware) SetPlugins(names []string) {
  23. m.mu.Lock()
  24. m.plugins = newPlugins(names)
  25. m.mu.Unlock()
  26. }
  27. // WrapHandler returns a new handler function wrapping the previous one in the request chain.
  28. 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 {
  29. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  30. m.mu.Lock()
  31. plugins := m.plugins
  32. m.mu.Unlock()
  33. if len(plugins) == 0 {
  34. return handler(ctx, w, r, vars)
  35. }
  36. user := ""
  37. userAuthNMethod := ""
  38. // Default authorization using existing TLS connection credentials
  39. // FIXME: Non trivial authorization mechanisms (such as advanced certificate validations, kerberos support
  40. // and ldap) will be extracted using AuthN feature, which is tracked under:
  41. // https://github.com/docker/docker/pull/20883
  42. if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
  43. user = r.TLS.PeerCertificates[0].Subject.CommonName
  44. userAuthNMethod = "TLS"
  45. }
  46. authCtx := NewCtx(plugins, user, userAuthNMethod, r.Method, r.RequestURI)
  47. if err := authCtx.AuthZRequest(w, r); err != nil {
  48. logrus.Errorf("AuthZRequest for %s %s returned error: %s", r.Method, r.RequestURI, err)
  49. return err
  50. }
  51. rw := NewResponseModifier(w)
  52. if err := handler(ctx, rw, r, vars); err != nil {
  53. logrus.Errorf("Handler for %s %s returned error: %s", r.Method, r.RequestURI, err)
  54. return err
  55. }
  56. if err := authCtx.AuthZResponse(rw, r); err != nil {
  57. logrus.Errorf("AuthZResponse for %s %s returned error: %s", r.Method, r.RequestURI, err)
  58. return err
  59. }
  60. return nil
  61. }
  62. }