middleware.go 2.4 KB

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