plugin.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 authorizes the request from the client to the daemon
  9. AuthZRequest(*Request) (*Response, error)
  10. // AuthZResponse authorizes the response from the daemon to the client
  11. AuthZResponse(*Request) (*Response, error)
  12. }
  13. // NewPlugins constructs and initializes the authorization plugins based on plugin names
  14. func NewPlugins(names []string) []Plugin {
  15. plugins := []Plugin{}
  16. pluginsMap := make(map[string]struct{})
  17. for _, name := range names {
  18. if _, ok := pluginsMap[name]; ok {
  19. continue
  20. }
  21. pluginsMap[name] = struct{}{}
  22. plugins = append(plugins, newAuthorizationPlugin(name))
  23. }
  24. return plugins
  25. }
  26. // authorizationPlugin is an internal adapter to docker plugin system
  27. type authorizationPlugin struct {
  28. plugin *plugins.Plugin
  29. name string
  30. }
  31. func newAuthorizationPlugin(name string) Plugin {
  32. return &authorizationPlugin{name: name}
  33. }
  34. func (a *authorizationPlugin) Name() string {
  35. return a.name
  36. }
  37. func (a *authorizationPlugin) AuthZRequest(authReq *Request) (*Response, error) {
  38. if err := a.initPlugin(); err != nil {
  39. return nil, err
  40. }
  41. authRes := &Response{}
  42. if err := a.plugin.Client.Call(AuthZApiRequest, authReq, authRes); err != nil {
  43. return nil, err
  44. }
  45. return authRes, nil
  46. }
  47. func (a *authorizationPlugin) AuthZResponse(authReq *Request) (*Response, error) {
  48. if err := a.initPlugin(); err != nil {
  49. return nil, err
  50. }
  51. authRes := &Response{}
  52. if err := a.plugin.Client.Call(AuthZApiResponse, authReq, authRes); err != nil {
  53. return nil, err
  54. }
  55. return authRes, nil
  56. }
  57. // initPlugin initializes the authorization plugin if needed
  58. func (a *authorizationPlugin) initPlugin() error {
  59. // Lazy loading of plugins
  60. if a.plugin == nil {
  61. var err error
  62. a.plugin, err = plugins.Get(a.name, AuthZApiImplements)
  63. if err != nil {
  64. return err
  65. }
  66. }
  67. return nil
  68. }