plugin.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package authorization
  2. import "github.com/docker/docker/pkg/plugins"
  3. // Plugin allows third party plugins to authorize requests and responses
  4. // in the context of docker API
  5. type Plugin interface {
  6. // Name returns the registered plugin name
  7. Name() string
  8. // AuthZRequest authorize the request from the client to the daemon
  9. AuthZRequest(*Request) (*Response, error)
  10. // AuthZResponse authorize the response from the daemon to the client
  11. AuthZResponse(*Request) (*Response, error)
  12. }
  13. // NewPlugins constructs and initialize the authorization plugins based on plugin names
  14. func NewPlugins(names []string) []Plugin {
  15. plugins := make([]Plugin, len(names))
  16. for i, name := range names {
  17. plugins[i] = newAuthorizationPlugin(name)
  18. }
  19. return plugins
  20. }
  21. // authorizationPlugin is an internal adapter to docker plugin system
  22. type authorizationPlugin struct {
  23. plugin *plugins.Plugin
  24. name string
  25. }
  26. func newAuthorizationPlugin(name string) Plugin {
  27. return &authorizationPlugin{name: name}
  28. }
  29. func (a *authorizationPlugin) Name() string {
  30. return a.name
  31. }
  32. func (a *authorizationPlugin) AuthZRequest(authReq *Request) (*Response, error) {
  33. if err := a.initPlugin(); err != nil {
  34. return nil, err
  35. }
  36. authRes := &Response{}
  37. if err := a.plugin.Client.Call(AuthZApiRequest, authReq, authRes); err != nil {
  38. return nil, err
  39. }
  40. return authRes, nil
  41. }
  42. func (a *authorizationPlugin) AuthZResponse(authReq *Request) (*Response, error) {
  43. if err := a.initPlugin(); err != nil {
  44. return nil, err
  45. }
  46. authRes := &Response{}
  47. if err := a.plugin.Client.Call(AuthZApiResponse, authReq, authRes); err != nil {
  48. return nil, err
  49. }
  50. return authRes, nil
  51. }
  52. // initPlugin initialize the authorization plugin if needed
  53. func (a *authorizationPlugin) initPlugin() error {
  54. // Lazy loading of plugins
  55. if a.plugin == nil {
  56. var err error
  57. a.plugin, err = plugins.Get(a.name, AuthZApiImplements)
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. return nil
  63. }