middleware.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // GetAuthzPlugins gets authorization plugins
  24. func (m *Middleware) GetAuthzPlugins() []Plugin {
  25. m.mu.Lock()
  26. defer m.mu.Unlock()
  27. return m.plugins
  28. }
  29. // SetAuthzPlugins sets authorization plugins
  30. func (m *Middleware) SetAuthzPlugins(plugins []Plugin) {
  31. m.mu.Lock()
  32. m.plugins = plugins
  33. m.mu.Unlock()
  34. }
  35. // SetPlugins sets the plugin used for authorization
  36. func (m *Middleware) SetPlugins(names []string) {
  37. m.mu.Lock()
  38. m.plugins = newPlugins(names)
  39. m.mu.Unlock()
  40. }
  41. // WrapHandler returns a new handler function wrapping the previous one in the request chain.
  42. 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 {
  43. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  44. plugins := m.GetAuthzPlugins()
  45. if len(plugins) == 0 {
  46. return handler(ctx, w, r, vars)
  47. }
  48. user := ""
  49. userAuthNMethod := ""
  50. // Default authorization using existing TLS connection credentials
  51. // FIXME: Non trivial authorization mechanisms (such as advanced certificate validations, kerberos support
  52. // and ldap) will be extracted using AuthN feature, which is tracked under:
  53. // https://github.com/docker/docker/pull/20883
  54. if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
  55. user = r.TLS.PeerCertificates[0].Subject.CommonName
  56. userAuthNMethod = "TLS"
  57. }
  58. authCtx := NewCtx(plugins, user, userAuthNMethod, r.Method, r.RequestURI)
  59. if err := authCtx.AuthZRequest(w, r); err != nil {
  60. logrus.Errorf("AuthZRequest for %s %s returned error: %s", r.Method, r.RequestURI, err)
  61. return err
  62. }
  63. rw := NewResponseModifier(w)
  64. var errD error
  65. if errD = handler(ctx, rw, r, vars); errD != nil {
  66. logrus.Errorf("Handler for %s %s returned error: %s", r.Method, r.RequestURI, errD)
  67. }
  68. // There's a chance that the authCtx.plugins was updated. One of the reasons
  69. // this can happen is when an authzplugin is disabled.
  70. plugins = m.GetAuthzPlugins()
  71. if len(plugins) == 0 {
  72. logrus.Debug("There are no authz plugins in the chain")
  73. return nil
  74. }
  75. authCtx.plugins = plugins
  76. if err := authCtx.AuthZResponse(rw, r); errD == nil && err != nil {
  77. logrus.Errorf("AuthZResponse for %s %s returned error: %s", r.Method, r.RequestURI, err)
  78. return err
  79. }
  80. if errD != nil {
  81. return errD
  82. }
  83. return nil
  84. }
  85. }