2015-11-12 11:06:47 +00:00
|
|
|
package authorization
|
|
|
|
|
2015-12-18 11:34:19 +00:00
|
|
|
import "github.com/docker/docker/pkg/plugins"
|
2015-11-12 11:06:47 +00:00
|
|
|
|
|
|
|
// Plugin allows third party plugins to authorize requests and responses
|
|
|
|
// in the context of docker API
|
|
|
|
type Plugin interface {
|
2015-12-18 11:34:19 +00:00
|
|
|
// Name returns the registered plugin name
|
|
|
|
Name() string
|
|
|
|
|
2016-05-03 15:12:38 +00:00
|
|
|
// AuthZRequest authorizes the request from the client to the daemon
|
2015-12-16 11:01:04 +00:00
|
|
|
AuthZRequest(*Request) (*Response, error)
|
2015-11-12 11:06:47 +00:00
|
|
|
|
2016-05-03 15:12:38 +00:00
|
|
|
// AuthZResponse authorizes the response from the daemon to the client
|
2015-12-16 11:01:04 +00:00
|
|
|
AuthZResponse(*Request) (*Response, error)
|
2015-11-12 11:06:47 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 15:12:38 +00:00
|
|
|
// NewPlugins constructs and initializes the authorization plugins based on plugin names
|
2015-11-12 11:06:47 +00:00
|
|
|
func NewPlugins(names []string) []Plugin {
|
2015-12-20 18:44:01 +00:00
|
|
|
plugins := []Plugin{}
|
|
|
|
pluginsMap := make(map[string]struct{})
|
|
|
|
for _, name := range names {
|
|
|
|
if _, ok := pluginsMap[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pluginsMap[name] = struct{}{}
|
|
|
|
plugins = append(plugins, newAuthorizationPlugin(name))
|
2015-11-12 11:06:47 +00:00
|
|
|
}
|
|
|
|
return plugins
|
|
|
|
}
|
|
|
|
|
|
|
|
// authorizationPlugin is an internal adapter to docker plugin system
|
|
|
|
type authorizationPlugin struct {
|
|
|
|
plugin *plugins.Plugin
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAuthorizationPlugin(name string) Plugin {
|
|
|
|
return &authorizationPlugin{name: name}
|
|
|
|
}
|
|
|
|
|
2015-12-18 11:34:19 +00:00
|
|
|
func (a *authorizationPlugin) Name() string {
|
|
|
|
return a.name
|
|
|
|
}
|
2015-11-12 11:06:47 +00:00
|
|
|
|
2015-12-18 11:34:19 +00:00
|
|
|
func (a *authorizationPlugin) AuthZRequest(authReq *Request) (*Response, error) {
|
2015-12-16 11:01:04 +00:00
|
|
|
if err := a.initPlugin(); err != nil {
|
2015-11-12 11:06:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:01:04 +00:00
|
|
|
authRes := &Response{}
|
|
|
|
if err := a.plugin.Client.Call(AuthZApiRequest, authReq, authRes); err != nil {
|
2015-11-12 11:06:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return authRes, nil
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:01:04 +00:00
|
|
|
func (a *authorizationPlugin) AuthZResponse(authReq *Request) (*Response, error) {
|
|
|
|
if err := a.initPlugin(); err != nil {
|
2015-11-12 11:06:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:01:04 +00:00
|
|
|
authRes := &Response{}
|
|
|
|
if err := a.plugin.Client.Call(AuthZApiResponse, authReq, authRes); err != nil {
|
2015-11-12 11:06:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return authRes, nil
|
|
|
|
}
|
|
|
|
|
2016-05-03 15:12:38 +00:00
|
|
|
// initPlugin initializes the authorization plugin if needed
|
2015-12-16 11:01:04 +00:00
|
|
|
func (a *authorizationPlugin) initPlugin() error {
|
2015-11-12 11:06:47 +00:00
|
|
|
// Lazy loading of plugins
|
|
|
|
if a.plugin == nil {
|
2015-12-16 11:01:04 +00:00
|
|
|
var err error
|
2015-11-12 11:06:47 +00:00
|
|
|
a.plugin, err = plugins.Get(a.name, AuthZApiImplements)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|