2015-11-12 11:06:47 +00:00
|
|
|
package authorization
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/pkg/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Plugin allows third party plugins to authorize requests and responses
|
|
|
|
// in the context of docker API
|
|
|
|
type Plugin interface {
|
|
|
|
// 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-16 11:01:04 +00:00
|
|
|
func (a *authorizationPlugin) AuthZRequest(authReq *Request) (*Response, error) {
|
2015-11-12 11:06:47 +00:00
|
|
|
logrus.Debugf("AuthZ requset using plugins %s", a.name)
|
|
|
|
|
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) {
|
2015-11-12 11:06:47 +00:00
|
|
|
logrus.Debugf("AuthZ response using plugins %s", a.name)
|
|
|
|
|
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(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
|
|
|
|
}
|