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
|
|
|
|
|
2015-11-12 11:06:47 +00:00
|
|
|
// AuthZRequest authorize 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
|
|
|
|
|
|
|
// AuthZResponse authorize 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
|
|
|
}
|
|
|
|
|
|
|
|
// NewPlugins constructs and initialize the authorization plugins based on plugin names
|
|
|
|
func NewPlugins(names []string) []Plugin {
|
|
|
|
plugins := make([]Plugin, len(names))
|
|
|
|
for i, name := range names {
|
|
|
|
plugins[i] = newAuthorizationPlugin(name)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// initPlugin initialize 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
|
|
|
|
}
|