2015-11-12 06:06:47 -05:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
2016-05-04 12:42:36 -04:00
|
|
|
"path/filepath"
|
2015-11-12 06:06:47 -05:00
|
|
|
"strings"
|
2015-12-16 10:35:35 -05:00
|
|
|
|
2016-02-04 10:41:41 -05:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2016-02-10 01:28:50 -05:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2016-05-04 12:42:36 -04:00
|
|
|
"net"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2016-12-09 04:17:53 -05:00
|
|
|
"github.com/docker/docker/integration-cli/daemon"
|
2015-12-16 10:35:35 -05:00
|
|
|
"github.com/docker/docker/pkg/authorization"
|
|
|
|
"github.com/docker/docker/pkg/plugins"
|
|
|
|
"github.com/go-check/check"
|
2015-11-12 06:06:47 -05:00
|
|
|
)
|
|
|
|
|
2015-12-15 03:49:18 -05:00
|
|
|
const (
|
|
|
|
testAuthZPlugin = "authzplugin"
|
|
|
|
unauthorizedMessage = "User unauthorized authz plugin"
|
|
|
|
errorMessage = "something went wrong..."
|
|
|
|
containerListAPI = "/containers/json"
|
|
|
|
)
|
2015-11-12 06:06:47 -05:00
|
|
|
|
2016-02-19 13:12:39 -05:00
|
|
|
var (
|
|
|
|
alwaysAllowed = []string{"/_ping", "/info"}
|
|
|
|
)
|
|
|
|
|
2015-11-12 06:06:47 -05:00
|
|
|
func init() {
|
|
|
|
check.Suite(&DockerAuthzSuite{
|
|
|
|
ds: &DockerSuite{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type DockerAuthzSuite struct {
|
|
|
|
server *httptest.Server
|
|
|
|
ds *DockerSuite
|
2016-12-09 04:17:53 -05:00
|
|
|
d *daemon.Daemon
|
2015-11-12 06:06:47 -05:00
|
|
|
ctrl *authorizationController
|
|
|
|
}
|
|
|
|
|
|
|
|
type authorizationController struct {
|
|
|
|
reqRes authorization.Response // reqRes holds the plugin response to the initial client request
|
|
|
|
resRes authorization.Response // resRes holds the plugin response to the daemon response
|
|
|
|
psRequestCnt int // psRequestCnt counts the number of calls to list container request api
|
|
|
|
psResponseCnt int // psResponseCnt counts the number of calls to list containers response API
|
|
|
|
requestsURIs []string // requestsURIs stores all request URIs that are sent to the authorization controller
|
2016-03-27 08:53:37 -04:00
|
|
|
reqUser string
|
|
|
|
resUser string
|
2015-11-12 06:06:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerAuthzSuite) SetUpTest(c *check.C) {
|
2016-12-09 04:17:53 -05:00
|
|
|
s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
|
2017-01-13 11:23:28 -05:00
|
|
|
Experimental: testEnv.ExperimentalDaemon(),
|
2016-12-09 04:17:53 -05:00
|
|
|
})
|
2015-11-12 06:06:47 -05:00
|
|
|
s.ctrl = &authorizationController{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerAuthzSuite) TearDownTest(c *check.C) {
|
2016-11-21 12:37:13 -05:00
|
|
|
if s.d != nil {
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Stop(c)
|
2016-11-21 12:37:13 -05:00
|
|
|
s.ds.TearDownTest(c)
|
|
|
|
s.ctrl = nil
|
|
|
|
}
|
2015-11-12 06:06:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerAuthzSuite) SetUpSuite(c *check.C) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
s.server = httptest.NewServer(mux)
|
|
|
|
|
|
|
|
mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
b, err := json.Marshal(plugins.Manifest{Implements: []string{authorization.AuthZApiImplements}})
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
w.Write(b)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/AuthZPlugin.AuthZReq", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
authReq := authorization.Request{}
|
|
|
|
err = json.Unmarshal(body, &authReq)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
assertBody(c, authReq.RequestURI, authReq.RequestHeaders, authReq.RequestBody)
|
|
|
|
assertAuthHeaders(c, authReq.RequestHeaders)
|
|
|
|
|
|
|
|
// Count only container list api
|
|
|
|
if strings.HasSuffix(authReq.RequestURI, containerListAPI) {
|
|
|
|
s.ctrl.psRequestCnt++
|
|
|
|
}
|
|
|
|
|
|
|
|
s.ctrl.requestsURIs = append(s.ctrl.requestsURIs, authReq.RequestURI)
|
|
|
|
|
2016-02-19 13:12:39 -05:00
|
|
|
reqRes := s.ctrl.reqRes
|
|
|
|
if isAllowed(authReq.RequestURI) {
|
|
|
|
reqRes = authorization.Response{Allow: true}
|
|
|
|
}
|
|
|
|
if reqRes.Err != "" {
|
2015-12-15 03:49:18 -05:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
2016-02-19 13:12:39 -05:00
|
|
|
b, err := json.Marshal(reqRes)
|
2015-11-12 06:06:47 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
2016-03-27 08:53:37 -04:00
|
|
|
s.ctrl.reqUser = authReq.User
|
2015-11-12 06:06:47 -05:00
|
|
|
w.Write(b)
|
2016-02-19 13:12:39 -05:00
|
|
|
})
|
2015-11-12 06:06:47 -05:00
|
|
|
|
2016-02-19 13:12:39 -05:00
|
|
|
mux.HandleFunc("/AuthZPlugin.AuthZRes", func(w http.ResponseWriter, r *http.Request) {
|
2015-11-12 06:06:47 -05:00
|
|
|
defer r.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
authReq := authorization.Request{}
|
|
|
|
err = json.Unmarshal(body, &authReq)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
assertBody(c, authReq.RequestURI, authReq.ResponseHeaders, authReq.ResponseBody)
|
|
|
|
assertAuthHeaders(c, authReq.ResponseHeaders)
|
|
|
|
|
|
|
|
// Count only container list api
|
|
|
|
if strings.HasSuffix(authReq.RequestURI, containerListAPI) {
|
|
|
|
s.ctrl.psResponseCnt++
|
|
|
|
}
|
2016-02-19 13:12:39 -05:00
|
|
|
resRes := s.ctrl.resRes
|
|
|
|
if isAllowed(authReq.RequestURI) {
|
|
|
|
resRes = authorization.Response{Allow: true}
|
|
|
|
}
|
|
|
|
if resRes.Err != "" {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(resRes)
|
|
|
|
c.Assert(err, check.IsNil)
|
2016-03-27 08:53:37 -04:00
|
|
|
s.ctrl.resUser = authReq.User
|
2016-02-19 13:12:39 -05:00
|
|
|
w.Write(b)
|
2015-11-12 06:06:47 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
err := os.MkdirAll("/etc/docker/plugins", 0755)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
fileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", testAuthZPlugin)
|
|
|
|
err = ioutil.WriteFile(fileName, []byte(s.server.URL), 0644)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
2016-02-19 13:12:39 -05:00
|
|
|
// check for always allowed endpoints to not inhibit test framework functions
|
|
|
|
func isAllowed(reqURI string) bool {
|
|
|
|
for _, endpoint := range alwaysAllowed {
|
|
|
|
if strings.HasSuffix(reqURI, endpoint) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-12 06:06:47 -05:00
|
|
|
// assertAuthHeaders validates authentication headers are removed
|
|
|
|
func assertAuthHeaders(c *check.C, headers map[string]string) error {
|
|
|
|
for k := range headers {
|
|
|
|
if strings.Contains(strings.ToLower(k), "auth") || strings.Contains(strings.ToLower(k), "x-registry") {
|
|
|
|
c.Errorf("Found authentication headers in request '%v'", headers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertBody asserts that body is removed for non text/json requests
|
|
|
|
func assertBody(c *check.C, requestURI string, headers map[string]string, body []byte) {
|
|
|
|
if strings.Contains(strings.ToLower(requestURI), "auth") && len(body) > 0 {
|
|
|
|
//return fmt.Errorf("Body included for authentication endpoint %s", string(body))
|
|
|
|
c.Errorf("Body included for authentication endpoint %s", string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range headers {
|
|
|
|
if strings.EqualFold(k, "Content-Type") && strings.HasPrefix(v, "text/") || v == "application/json" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(body) > 0 {
|
|
|
|
c.Errorf("Body included while it should not (Headers: '%v')", headers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerAuthzSuite) TearDownSuite(c *check.C) {
|
|
|
|
if s.server == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.server.Close()
|
|
|
|
|
|
|
|
err := os.RemoveAll("/etc/docker/plugins")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerAuthzSuite) TestAuthZPluginAllowRequest(c *check.C) {
|
2015-12-24 10:18:23 -05:00
|
|
|
// start the daemon and load busybox, --net=none build fails otherwise
|
|
|
|
// cause it needs to pull busybox
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c, "--authorization-plugin="+testAuthZPlugin)
|
2015-11-12 06:06:47 -05:00
|
|
|
s.ctrl.reqRes.Allow = true
|
|
|
|
s.ctrl.resRes.Allow = true
|
2016-02-19 01:00:45 -05:00
|
|
|
c.Assert(s.d.LoadBusybox(), check.IsNil)
|
2015-11-12 06:06:47 -05:00
|
|
|
|
|
|
|
// Ensure command successful
|
2015-12-24 10:18:23 -05:00
|
|
|
out, err := s.d.Cmd("run", "-d", "busybox", "top")
|
2015-11-12 06:06:47 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2015-12-24 10:18:23 -05:00
|
|
|
id := strings.TrimSpace(out)
|
2015-11-12 06:06:47 -05:00
|
|
|
assertURIRecorded(c, s.ctrl.requestsURIs, "/containers/create")
|
|
|
|
assertURIRecorded(c, s.ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", id))
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("ps")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(assertContainerList(out, []string{id}), check.Equals, true)
|
|
|
|
c.Assert(s.ctrl.psRequestCnt, check.Equals, 1)
|
|
|
|
c.Assert(s.ctrl.psResponseCnt, check.Equals, 1)
|
|
|
|
}
|
|
|
|
|
2016-03-27 08:53:37 -04:00
|
|
|
func (s *DockerAuthzSuite) TestAuthZPluginTls(c *check.C) {
|
|
|
|
|
|
|
|
const testDaemonHTTPSAddr = "tcp://localhost:4271"
|
|
|
|
// start the daemon and load busybox, --net=none build fails otherwise
|
|
|
|
// cause it needs to pull busybox
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c,
|
2016-03-27 08:53:37 -04:00
|
|
|
"--authorization-plugin="+testAuthZPlugin,
|
|
|
|
"--tlsverify",
|
|
|
|
"--tlscacert",
|
|
|
|
"fixtures/https/ca.pem",
|
|
|
|
"--tlscert",
|
|
|
|
"fixtures/https/server-cert.pem",
|
|
|
|
"--tlskey",
|
|
|
|
"fixtures/https/server-key.pem",
|
2016-12-09 17:20:14 -05:00
|
|
|
"-H", testDaemonHTTPSAddr)
|
2016-03-27 08:53:37 -04:00
|
|
|
|
|
|
|
s.ctrl.reqRes.Allow = true
|
|
|
|
s.ctrl.resRes.Allow = true
|
|
|
|
|
|
|
|
out, _ := dockerCmd(
|
|
|
|
c,
|
|
|
|
"--tlsverify",
|
|
|
|
"--tlscacert", "fixtures/https/ca.pem",
|
|
|
|
"--tlscert", "fixtures/https/client-cert.pem",
|
|
|
|
"--tlskey", "fixtures/https/client-key.pem",
|
|
|
|
"-H",
|
|
|
|
testDaemonHTTPSAddr,
|
|
|
|
"version",
|
|
|
|
)
|
|
|
|
if !strings.Contains(out, "Server") {
|
|
|
|
c.Fatalf("docker version should return information of server side")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Assert(s.ctrl.reqUser, check.Equals, "client")
|
|
|
|
c.Assert(s.ctrl.resUser, check.Equals, "client")
|
|
|
|
}
|
|
|
|
|
2015-11-12 06:06:47 -05:00
|
|
|
func (s *DockerAuthzSuite) TestAuthZPluginDenyRequest(c *check.C) {
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c, "--authorization-plugin="+testAuthZPlugin)
|
2015-11-12 06:06:47 -05:00
|
|
|
s.ctrl.reqRes.Allow = false
|
|
|
|
s.ctrl.reqRes.Msg = unauthorizedMessage
|
|
|
|
|
|
|
|
// Ensure command is blocked
|
|
|
|
res, err := s.d.Cmd("ps")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(s.ctrl.psRequestCnt, check.Equals, 1)
|
|
|
|
c.Assert(s.ctrl.psResponseCnt, check.Equals, 0)
|
|
|
|
|
|
|
|
// Ensure unauthorized message appears in response
|
2015-12-18 06:34:19 -05:00
|
|
|
c.Assert(res, check.Equals, fmt.Sprintf("Error response from daemon: authorization denied by plugin %s: %s\n", testAuthZPlugin, unauthorizedMessage))
|
2015-11-12 06:06:47 -05:00
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04:00
|
|
|
// TestAuthZPluginAPIDenyResponse validates that when authorization plugin deny the request, the status code is forbidden
|
|
|
|
func (s *DockerAuthzSuite) TestAuthZPluginAPIDenyResponse(c *check.C) {
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c, "--authorization-plugin="+testAuthZPlugin)
|
2016-05-02 04:54:09 -04:00
|
|
|
s.ctrl.reqRes.Allow = false
|
|
|
|
s.ctrl.resRes.Msg = unauthorizedMessage
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
daemonURL, err := url.Parse(s.d.Sock())
|
2016-05-02 04:54:09 -04:00
|
|
|
|
|
|
|
conn, err := net.DialTimeout(daemonURL.Scheme, daemonURL.Path, time.Second*10)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
client := httputil.NewClientConn(conn, nil)
|
|
|
|
req, err := http.NewRequest("GET", "/version", nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusForbidden)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
2015-11-12 06:06:47 -05:00
|
|
|
func (s *DockerAuthzSuite) TestAuthZPluginDenyResponse(c *check.C) {
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c, "--authorization-plugin="+testAuthZPlugin)
|
2015-11-12 06:06:47 -05:00
|
|
|
s.ctrl.reqRes.Allow = true
|
|
|
|
s.ctrl.resRes.Allow = false
|
|
|
|
s.ctrl.resRes.Msg = unauthorizedMessage
|
|
|
|
|
|
|
|
// Ensure command is blocked
|
|
|
|
res, err := s.d.Cmd("ps")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(s.ctrl.psRequestCnt, check.Equals, 1)
|
|
|
|
c.Assert(s.ctrl.psResponseCnt, check.Equals, 1)
|
|
|
|
|
|
|
|
// Ensure unauthorized message appears in response
|
2015-12-18 06:34:19 -05:00
|
|
|
c.Assert(res, check.Equals, fmt.Sprintf("Error response from daemon: authorization denied by plugin %s: %s\n", testAuthZPlugin, unauthorizedMessage))
|
2015-11-12 06:06:47 -05:00
|
|
|
}
|
|
|
|
|
2016-02-11 18:21:52 -05:00
|
|
|
// TestAuthZPluginAllowEventStream verifies event stream propagates correctly after request pass through by the authorization plugin
|
2016-02-04 10:41:41 -05:00
|
|
|
func (s *DockerAuthzSuite) TestAuthZPluginAllowEventStream(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
2016-02-10 01:28:50 -05:00
|
|
|
// start the daemon and load busybox to avoid pulling busybox from Docker Hub
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c, "--authorization-plugin="+testAuthZPlugin)
|
2016-02-04 10:41:41 -05:00
|
|
|
s.ctrl.reqRes.Allow = true
|
|
|
|
s.ctrl.resRes.Allow = true
|
2016-02-19 01:00:45 -05:00
|
|
|
c.Assert(s.d.LoadBusybox(), check.IsNil)
|
2016-02-04 10:41:41 -05:00
|
|
|
|
|
|
|
startTime := strconv.FormatInt(daemonTime(c).Unix(), 10)
|
|
|
|
// Add another command to to enable event pipelining
|
2016-12-09 04:17:53 -05:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "--host", s.d.Sock(), "events", "--since", startTime)
|
2016-02-04 10:41:41 -05:00
|
|
|
stdout, err := eventsCmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
observer := eventObserver{
|
|
|
|
buffer: new(bytes.Buffer),
|
|
|
|
command: eventsCmd,
|
|
|
|
scanner: bufio.NewScanner(stdout),
|
|
|
|
startTime: startTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = observer.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer observer.Stop()
|
|
|
|
|
|
|
|
// Create a container and wait for the creation events
|
|
|
|
out, err := s.d.Cmd("run", "-d", "busybox", "top")
|
2016-02-10 01:28:50 -05:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
2016-02-04 10:41:41 -05:00
|
|
|
containerID := strings.TrimSpace(out)
|
2016-12-09 04:17:53 -05:00
|
|
|
c.Assert(s.d.WaitRun(containerID), checker.IsNil)
|
2016-02-04 10:41:41 -05:00
|
|
|
|
|
|
|
events := map[string]chan bool{
|
2016-02-18 16:10:29 -05:00
|
|
|
"create": make(chan bool, 1),
|
|
|
|
"start": make(chan bool, 1),
|
2016-02-04 10:41:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
matcher := matchEventLine(containerID, "container", events)
|
|
|
|
processor := processEventMatch(events)
|
|
|
|
go observer.Match(matcher, processor)
|
|
|
|
|
|
|
|
// Ensure all events are received
|
|
|
|
for event, eventChannel := range events {
|
|
|
|
|
|
|
|
select {
|
2016-02-18 16:10:29 -05:00
|
|
|
case <-time.After(30 * time.Second):
|
2016-02-04 10:41:41 -05:00
|
|
|
// Fail the test
|
|
|
|
observer.CheckEventError(c, containerID, event, matcher)
|
|
|
|
c.FailNow()
|
|
|
|
case <-eventChannel:
|
|
|
|
// Ignore, event received
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure both events and container endpoints are passed to the authorization plugin
|
|
|
|
assertURIRecorded(c, s.ctrl.requestsURIs, "/events")
|
|
|
|
assertURIRecorded(c, s.ctrl.requestsURIs, "/containers/create")
|
|
|
|
assertURIRecorded(c, s.ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", containerID))
|
|
|
|
}
|
|
|
|
|
2015-12-15 03:49:18 -05:00
|
|
|
func (s *DockerAuthzSuite) TestAuthZPluginErrorResponse(c *check.C) {
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c, "--authorization-plugin="+testAuthZPlugin)
|
2015-12-15 03:49:18 -05:00
|
|
|
s.ctrl.reqRes.Allow = true
|
|
|
|
s.ctrl.resRes.Err = errorMessage
|
|
|
|
|
|
|
|
// Ensure command is blocked
|
|
|
|
res, err := s.d.Cmd("ps")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2015-12-18 06:34:19 -05:00
|
|
|
c.Assert(res, check.Equals, fmt.Sprintf("Error response from daemon: plugin %s failed with error: %s: %s\n", testAuthZPlugin, authorization.AuthZApiResponse, errorMessage))
|
2015-12-15 03:49:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerAuthzSuite) TestAuthZPluginErrorRequest(c *check.C) {
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c, "--authorization-plugin="+testAuthZPlugin)
|
2015-12-15 03:49:18 -05:00
|
|
|
s.ctrl.reqRes.Err = errorMessage
|
|
|
|
|
|
|
|
// Ensure command is blocked
|
|
|
|
res, err := s.d.Cmd("ps")
|
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2015-12-18 06:34:19 -05:00
|
|
|
c.Assert(res, check.Equals, fmt.Sprintf("Error response from daemon: plugin %s failed with error: %s: %s\n", testAuthZPlugin, authorization.AuthZApiRequest, errorMessage))
|
2015-12-15 03:49:18 -05:00
|
|
|
}
|
|
|
|
|
2015-12-20 13:44:01 -05:00
|
|
|
func (s *DockerAuthzSuite) TestAuthZPluginEnsureNoDuplicatePluginRegistration(c *check.C) {
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c, "--authorization-plugin="+testAuthZPlugin, "--authorization-plugin="+testAuthZPlugin)
|
2015-12-20 13:44:01 -05:00
|
|
|
|
|
|
|
s.ctrl.reqRes.Allow = true
|
|
|
|
s.ctrl.resRes.Allow = true
|
|
|
|
|
|
|
|
out, err := s.d.Cmd("ps")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
|
|
|
|
// assert plugin is only called once..
|
|
|
|
c.Assert(s.ctrl.psRequestCnt, check.Equals, 1)
|
|
|
|
c.Assert(s.ctrl.psResponseCnt, check.Equals, 1)
|
|
|
|
}
|
|
|
|
|
2016-05-04 12:42:36 -04:00
|
|
|
func (s *DockerAuthzSuite) TestAuthZPluginEnsureLoadImportWorking(c *check.C) {
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c, "--authorization-plugin="+testAuthZPlugin, "--authorization-plugin="+testAuthZPlugin)
|
2016-05-04 12:42:36 -04:00
|
|
|
s.ctrl.reqRes.Allow = true
|
|
|
|
s.ctrl.resRes.Allow = true
|
|
|
|
c.Assert(s.d.LoadBusybox(), check.IsNil)
|
|
|
|
|
|
|
|
tmp, err := ioutil.TempDir("", "test-authz-load-import")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
savedImagePath := filepath.Join(tmp, "save.tar")
|
|
|
|
|
|
|
|
out, err := s.d.Cmd("save", "-o", savedImagePath, "busybox")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
out, err = s.d.Cmd("load", "--input", savedImagePath)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
|
|
|
|
exportedImagePath := filepath.Join(tmp, "export.tar")
|
|
|
|
|
|
|
|
out, err = s.d.Cmd("run", "-d", "--name", "testexport", "busybox")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
out, err = s.d.Cmd("export", "-o", exportedImagePath, "testexport")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
out, err = s.d.Cmd("import", exportedImagePath)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(out))
|
|
|
|
}
|
|
|
|
|
2016-08-24 00:08:23 -04:00
|
|
|
func (s *DockerAuthzSuite) TestAuthZPluginHeader(c *check.C) {
|
2016-12-09 17:20:14 -05:00
|
|
|
s.d.Start(c, "--debug", "--authorization-plugin="+testAuthZPlugin)
|
2016-08-24 00:08:23 -04:00
|
|
|
s.ctrl.reqRes.Allow = true
|
|
|
|
s.ctrl.resRes.Allow = true
|
|
|
|
c.Assert(s.d.LoadBusybox(), check.IsNil)
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
daemonURL, err := url.Parse(s.d.Sock())
|
2016-08-24 00:08:23 -04:00
|
|
|
|
|
|
|
conn, err := net.DialTimeout(daemonURL.Scheme, daemonURL.Path, time.Second*10)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
client := httputil.NewClientConn(conn, nil)
|
|
|
|
req, err := http.NewRequest("GET", "/version", nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(resp.Header["Content-Type"][0], checker.Equals, "application/json")
|
|
|
|
}
|
|
|
|
|
2015-11-12 06:06:47 -05:00
|
|
|
// assertURIRecorded verifies that the given URI was sent and recorded in the authz plugin
|
|
|
|
func assertURIRecorded(c *check.C, uris []string, uri string) {
|
2015-12-20 13:44:01 -05:00
|
|
|
var found bool
|
2015-11-12 06:06:47 -05:00
|
|
|
for _, u := range uris {
|
|
|
|
if strings.Contains(u, uri) {
|
|
|
|
found = true
|
2015-12-20 13:44:01 -05:00
|
|
|
break
|
2015-11-12 06:06:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
c.Fatalf("Expected to find URI '%s', recorded uris '%s'", uri, strings.Join(uris, ","))
|
|
|
|
}
|
|
|
|
}
|