authorization.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package middleware
  2. import (
  3. "net/http"
  4. "github.com/Sirupsen/logrus"
  5. "github.com/docker/docker/api/server/httputils"
  6. "github.com/docker/docker/pkg/authorization"
  7. "golang.org/x/net/context"
  8. )
  9. // NewAuthorizationMiddleware creates a new Authorization middleware.
  10. func NewAuthorizationMiddleware(plugins []authorization.Plugin) Middleware {
  11. return func(handler httputils.APIFunc) httputils.APIFunc {
  12. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  13. user := ""
  14. userAuthNMethod := ""
  15. // Default authorization using existing TLS connection credentials
  16. // FIXME: Non trivial authorization mechanisms (such as advanced certificate validations, kerberos support
  17. // and ldap) will be extracted using AuthN feature, which is tracked under:
  18. // https://github.com/docker/docker/pull/20883
  19. if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
  20. user = r.TLS.PeerCertificates[0].Subject.CommonName
  21. userAuthNMethod = "TLS"
  22. }
  23. authCtx := authorization.NewCtx(plugins, user, userAuthNMethod, r.Method, r.RequestURI)
  24. if err := authCtx.AuthZRequest(w, r); err != nil {
  25. logrus.Errorf("AuthZRequest for %s %s returned error: %s", r.Method, r.RequestURI, err)
  26. return err
  27. }
  28. rw := authorization.NewResponseModifier(w)
  29. if err := handler(ctx, rw, r, vars); err != nil {
  30. logrus.Errorf("Handler for %s %s returned error: %s", r.Method, r.RequestURI, err)
  31. return err
  32. }
  33. if err := authCtx.AuthZResponse(rw, r); err != nil {
  34. logrus.Errorf("AuthZResponse for %s %s returned error: %s", r.Method, r.RequestURI, err)
  35. return err
  36. }
  37. return nil
  38. }
  39. }
  40. }