2015-11-12 06:06:47 -05:00
|
|
|
package authorization
|
|
|
|
|
2015-12-18 06:34:19 -05:00
|
|
|
import "github.com/docker/docker/pkg/plugins"
|
2015-11-12 06:06:47 -05:00
|
|
|
|
|
|
|
// Plugin allows third party plugins to authorize requests and responses
|
|
|
|
// in the context of docker API
|
|
|
|
type Plugin interface {
|
2015-12-18 06:34:19 -05:00
|
|
|
// Name returns the registered plugin name
|
|
|
|
Name() string
|
|
|
|
|
2015-11-12 06:06:47 -05:00
|
|
|
// AuthZRequest authorize the request from the client to the daemon
|
2015-12-16 06:01:04 -05:00
|
|
|
AuthZRequest(*Request) (*Response, error)
|
2015-11-12 06:06:47 -05:00
|
|
|
|
|
|
|
// AuthZResponse authorize the response from the daemon to the client
|
2015-12-16 06:01:04 -05:00
|
|
|
AuthZResponse(*Request) (*Response, error)
|
2015-11-12 06:06:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPlugins constructs and initialize the authorization plugins based on plugin names
|
|
|
|
func NewPlugins(names []string) []Plugin {
|
2015-12-20 13:44:01 -05: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 06:06:47 -05: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 06:34:19 -05:00
|
|
|
func (a *authorizationPlugin) Name() string {
|
|
|
|
return a.name
|
|
|
|
}
|
2015-11-12 06:06:47 -05:00
|
|
|
|
2015-12-18 06:34:19 -05:00
|
|
|
func (a *authorizationPlugin) AuthZRequest(authReq *Request) (*Response, error) {
|
2015-12-16 06:01:04 -05:00
|
|
|
if err := a.initPlugin(); err != nil {
|
2015-11-12 06:06:47 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-16 06:01:04 -05:00
|
|
|
authRes := &Response{}
|
|
|
|
if err := a.plugin.Client.Call(AuthZApiRequest, authReq, authRes); err != nil {
|
2015-11-12 06:06:47 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return authRes, nil
|
|
|
|
}
|
|
|
|
|
2015-12-16 06:01:04 -05:00
|
|
|
func (a *authorizationPlugin) AuthZResponse(authReq *Request) (*Response, error) {
|
|
|
|
if err := a.initPlugin(); err != nil {
|
2015-11-12 06:06:47 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-16 06:01:04 -05:00
|
|
|
authRes := &Response{}
|
|
|
|
if err := a.plugin.Client.Call(AuthZApiResponse, authReq, authRes); err != nil {
|
2015-11-12 06:06:47 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return authRes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// initPlugin initialize the authorization plugin if needed
|
2015-12-16 06:01:04 -05:00
|
|
|
func (a *authorizationPlugin) initPlugin() error {
|
2015-11-12 06:06:47 -05:00
|
|
|
// Lazy loading of plugins
|
|
|
|
if a.plugin == nil {
|
2015-12-16 06:01:04 -05:00
|
|
|
var err error
|
2015-11-12 06:06:47 -05:00
|
|
|
a.plugin, err = plugins.Get(a.name, AuthZApiImplements)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|