2015-11-12 11:06:47 +00:00
|
|
|
package authorization
|
|
|
|
|
|
|
|
import (
|
2016-01-13 22:01:48 +00:00
|
|
|
"bufio"
|
2015-11-12 11:06:47 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2015-12-18 11:34:19 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-01-13 22:01:48 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-11-12 11:06:47 +00:00
|
|
|
)
|
|
|
|
|
2016-01-13 22:01:48 +00:00
|
|
|
const maxBodySize = 1048576 // 1MB
|
|
|
|
|
2015-11-12 11:06:47 +00:00
|
|
|
// NewCtx creates new authZ context, it is used to store authorization information related to a specific docker
|
|
|
|
// REST http session
|
|
|
|
// A context provides two method:
|
|
|
|
// Authenticate Request:
|
|
|
|
// Call authZ plugins with current REST request and AuthN response
|
|
|
|
// Request contains full HTTP packet sent to the docker daemon
|
|
|
|
// https://docs.docker.com/reference/api/docker_remote_api/
|
|
|
|
//
|
|
|
|
// Authenticate Response:
|
|
|
|
// Call authZ plugins with full info about current REST request, REST response and AuthN response
|
|
|
|
// The response from this method may contains content that overrides the daemon response
|
|
|
|
// This allows authZ plugins to filter privileged content
|
|
|
|
//
|
|
|
|
// If multiple authZ plugins are specified, the block/allow decision is based on ANDing all plugin results
|
|
|
|
// For response manipulation, the response from each plugin is piped between plugins. Plugin execution order
|
|
|
|
// is determined according to daemon parameters
|
|
|
|
func NewCtx(authZPlugins []Plugin, user, userAuthNMethod, requestMethod, requestURI string) *Ctx {
|
2015-12-16 11:01:04 +00:00
|
|
|
return &Ctx{
|
|
|
|
plugins: authZPlugins,
|
|
|
|
user: user,
|
|
|
|
userAuthNMethod: userAuthNMethod,
|
|
|
|
requestMethod: requestMethod,
|
|
|
|
requestURI: requestURI,
|
|
|
|
}
|
2015-11-12 11:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ctx stores a a single request-response interaction context
|
|
|
|
type Ctx struct {
|
|
|
|
user string
|
|
|
|
userAuthNMethod string
|
|
|
|
requestMethod string
|
|
|
|
requestURI string
|
|
|
|
plugins []Plugin
|
|
|
|
// authReq stores the cached request object for the current transaction
|
|
|
|
authReq *Request
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthZRequest authorized the request to the docker daemon using authZ plugins
|
2015-12-18 11:34:19 +00:00
|
|
|
func (ctx *Ctx) AuthZRequest(w http.ResponseWriter, r *http.Request) error {
|
2015-11-12 11:06:47 +00:00
|
|
|
var body []byte
|
2016-02-23 10:04:16 +00:00
|
|
|
if sendBody(ctx.requestURI, r.Header) && r.ContentLength > 0 && r.ContentLength < maxBodySize {
|
|
|
|
var err error
|
|
|
|
body, r.Body, err = drainBody(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-11-12 11:06:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var h bytes.Buffer
|
2015-12-16 11:01:04 +00:00
|
|
|
if err := r.Header.Write(&h); err != nil {
|
2015-11-12 11:06:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-18 11:34:19 +00:00
|
|
|
ctx.authReq = &Request{
|
|
|
|
User: ctx.user,
|
|
|
|
UserAuthNMethod: ctx.userAuthNMethod,
|
|
|
|
RequestMethod: ctx.requestMethod,
|
|
|
|
RequestURI: ctx.requestURI,
|
2015-11-12 11:06:47 +00:00
|
|
|
RequestBody: body,
|
2015-12-18 11:34:19 +00:00
|
|
|
RequestHeaders: headers(r.Header),
|
|
|
|
}
|
2015-11-12 11:06:47 +00:00
|
|
|
|
2015-12-18 11:34:19 +00:00
|
|
|
for _, plugin := range ctx.plugins {
|
|
|
|
logrus.Debugf("AuthZ request using plugin %s", plugin.Name())
|
2015-11-12 11:06:47 +00:00
|
|
|
|
2015-12-18 11:34:19 +00:00
|
|
|
authRes, err := plugin.AuthZRequest(ctx.authReq)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("plugin %s failed with error: %s", plugin.Name(), err)
|
2015-12-15 08:49:18 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 11:06:47 +00:00
|
|
|
if !authRes.Allow {
|
2016-05-02 08:54:09 +00:00
|
|
|
return newAuthorizationError(plugin.Name(), authRes.Msg)
|
2015-11-12 11:06:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthZResponse authorized and manipulates the response from docker daemon using authZ plugins
|
2015-12-18 11:34:19 +00:00
|
|
|
func (ctx *Ctx) AuthZResponse(rm ResponseModifier, r *http.Request) error {
|
|
|
|
ctx.authReq.ResponseStatusCode = rm.StatusCode()
|
|
|
|
ctx.authReq.ResponseHeaders = headers(rm.Header())
|
2015-11-12 11:06:47 +00:00
|
|
|
|
2015-12-18 11:34:19 +00:00
|
|
|
if sendBody(ctx.requestURI, rm.Header()) {
|
|
|
|
ctx.authReq.ResponseBody = rm.RawBody()
|
2015-11-12 11:06:47 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 11:34:19 +00:00
|
|
|
for _, plugin := range ctx.plugins {
|
|
|
|
logrus.Debugf("AuthZ response using plugin %s", plugin.Name())
|
2015-11-12 11:06:47 +00:00
|
|
|
|
2015-12-18 11:34:19 +00:00
|
|
|
authRes, err := plugin.AuthZResponse(ctx.authReq)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("plugin %s failed with error: %s", plugin.Name(), err)
|
2015-12-15 08:49:18 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 11:06:47 +00:00
|
|
|
if !authRes.Allow {
|
2016-05-02 08:54:09 +00:00
|
|
|
return newAuthorizationError(plugin.Name(), authRes.Msg)
|
2015-11-12 11:06:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:41:41 +00:00
|
|
|
rm.FlushAll()
|
2015-11-12 11:06:47 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-23 10:04:16 +00:00
|
|
|
// drainBody dump the body (if it's length is less than 1MB) without modifying the request state
|
2016-01-13 22:01:48 +00:00
|
|
|
func drainBody(body io.ReadCloser) ([]byte, io.ReadCloser, error) {
|
|
|
|
bufReader := bufio.NewReaderSize(body, maxBodySize)
|
|
|
|
newBody := ioutils.NewReadCloserWrapper(bufReader, func() error { return body.Close() })
|
|
|
|
|
|
|
|
data, err := bufReader.Peek(maxBodySize)
|
2016-02-23 10:04:16 +00:00
|
|
|
// Body size exceeds max body size
|
|
|
|
if err == nil {
|
|
|
|
logrus.Warnf("Request body is larger than: '%d' skipping body", maxBodySize)
|
|
|
|
return nil, newBody, nil
|
2015-11-12 11:06:47 +00:00
|
|
|
}
|
2016-02-23 10:04:16 +00:00
|
|
|
// Body size is less than maximum size
|
|
|
|
if err == io.EOF {
|
|
|
|
return data, newBody, nil
|
|
|
|
}
|
|
|
|
// Unknown error
|
|
|
|
return nil, newBody, err
|
2015-11-12 11:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendBody returns true when request/response body should be sent to AuthZPlugin
|
|
|
|
func sendBody(url string, header http.Header) bool {
|
|
|
|
// Skip body for auth endpoint
|
|
|
|
if strings.HasSuffix(url, "/auth") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// body is sent only for text or json messages
|
2016-02-23 10:04:16 +00:00
|
|
|
return header.Get("Content-Type") == "application/json"
|
2015-11-12 11:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// headers returns flatten version of the http headers excluding authorization
|
|
|
|
func headers(header http.Header) map[string]string {
|
|
|
|
v := make(map[string]string, 0)
|
|
|
|
for k, values := range header {
|
|
|
|
// Skip authorization headers
|
|
|
|
if strings.EqualFold(k, "Authorization") || strings.EqualFold(k, "X-Registry-Config") || strings.EqualFold(k, "X-Registry-Auth") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, val := range values {
|
|
|
|
v[k] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
2016-05-02 08:54:09 +00:00
|
|
|
|
|
|
|
// authorizationError represents an authorization deny error
|
|
|
|
type authorizationError struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPErrorStatusCode returns the authorization error status code (forbidden)
|
|
|
|
func (e authorizationError) HTTPErrorStatusCode() int {
|
|
|
|
return http.StatusForbidden
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAuthorizationError(plugin, msg string) authorizationError {
|
|
|
|
return authorizationError{error: fmt.Errorf("authorization denied by plugin %s: %s", plugin, msg)}
|
|
|
|
}
|