middleware.go 2.6 KB

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