2021-08-23 09:14:53 -04:00
|
|
|
//go:build !windows
|
2017-09-22 09:05:56 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package authz // import "github.com/docker/docker/integration/plugin/authz"
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-10-02 09:17:51 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-09-22 09:05:56 -04:00
|
|
|
eventtypes "github.com/docker/docker/api/types/events"
|
2017-10-02 09:17:51 -04:00
|
|
|
"github.com/docker/docker/client"
|
2018-02-12 18:08:25 -05:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2018-04-11 15:19:15 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2017-09-22 09:05:56 -04:00
|
|
|
"github.com/docker/docker/pkg/authorization"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/environment"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
"gotest.tools/v3/skip"
|
2017-09-22 09:05:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testAuthZPlugin = "authzplugin"
|
|
|
|
unauthorizedMessage = "User unauthorized authz plugin"
|
|
|
|
errorMessage = "something went wrong..."
|
|
|
|
serverVersionAPI = "/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
alwaysAllowed = []string{"/_ping", "/info"}
|
|
|
|
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
|
|
|
|
versionReqCount int // versionReqCount counts the number of requests to the server version API endpoint
|
|
|
|
versionResCount int // versionResCount counts the number of responses from the server version API endpoint
|
|
|
|
requestsURIs []string // requestsURIs stores all request URIs that are sent to the authorization controller
|
|
|
|
reqUser string
|
|
|
|
resUser string
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupTestV1(t *testing.T) func() {
|
|
|
|
ctrl = &authorizationController{}
|
|
|
|
teardown := setupTest(t)
|
|
|
|
|
|
|
|
err := os.MkdirAll("/etc/docker/plugins", 0755)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
fileName := fmt.Sprintf("/etc/docker/plugins/%s.spec", testAuthZPlugin)
|
2021-08-24 06:10:50 -04:00
|
|
|
err = os.WriteFile(fileName, []byte(server.URL), 0644)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
return func() {
|
|
|
|
err := os.RemoveAll("/etc/docker/plugins")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
teardown()
|
|
|
|
ctrl = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthZPluginAllowRequest(t *testing.T) {
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
ctrl.reqRes.Allow = true
|
|
|
|
ctrl.resRes.Allow = true
|
|
|
|
d.StartWithBusybox(t, "--authorization-plugin="+testAuthZPlugin)
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
c := d.NewClientT(t)
|
2018-02-12 18:08:25 -05:00
|
|
|
ctx := context.Background()
|
2017-10-02 09:17:51 -04:00
|
|
|
|
2018-02-12 18:08:25 -05:00
|
|
|
// Ensure command successful
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, c)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
assertURIRecorded(t, ctrl.requestsURIs, "/containers/create")
|
2018-02-12 18:08:25 -05:00
|
|
|
assertURIRecorded(t, ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", cID))
|
2017-09-22 09:05:56 -04:00
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
_, err := c.ServerVersion(ctx)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, 1, ctrl.versionReqCount)
|
|
|
|
assert.Equal(t, 1, ctrl.versionResCount)
|
2017-09-22 09:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthZPluginTLS(t *testing.T) {
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
const (
|
|
|
|
testDaemonHTTPSAddr = "tcp://localhost:4271"
|
|
|
|
cacertPath = "../../testdata/https/ca.pem"
|
|
|
|
serverCertPath = "../../testdata/https/server-cert.pem"
|
|
|
|
serverKeyPath = "../../testdata/https/server-key.pem"
|
|
|
|
clientCertPath = "../../testdata/https/client-cert.pem"
|
|
|
|
clientKeyPath = "../../testdata/https/client-key.pem"
|
|
|
|
)
|
|
|
|
|
|
|
|
d.Start(t,
|
|
|
|
"--authorization-plugin="+testAuthZPlugin,
|
|
|
|
"--tlsverify",
|
|
|
|
"--tlscacert", cacertPath,
|
|
|
|
"--tlscert", serverCertPath,
|
|
|
|
"--tlskey", serverKeyPath,
|
|
|
|
"-H", testDaemonHTTPSAddr)
|
|
|
|
|
|
|
|
ctrl.reqRes.Allow = true
|
|
|
|
ctrl.resRes.Allow = true
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
c, err := newTLSAPIClient(testDaemonHTTPSAddr, cacertPath, clientCertPath, clientKeyPath)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
_, err = c.ServerVersion(context.Background())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, "client", ctrl.reqUser)
|
|
|
|
assert.Equal(t, "client", ctrl.resUser)
|
2017-09-22 09:05:56 -04:00
|
|
|
}
|
|
|
|
|
2018-02-16 13:24:57 -05:00
|
|
|
func newTLSAPIClient(host, cacertPath, certPath, keyPath string) (client.APIClient, error) {
|
|
|
|
dialer := &net.Dialer{
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
}
|
|
|
|
return client.NewClientWithOpts(
|
|
|
|
client.WithTLSClientConfig(cacertPath, certPath, keyPath),
|
2018-12-28 10:10:32 -05:00
|
|
|
client.WithDialContext(dialer.DialContext),
|
2018-02-16 13:24:57 -05:00
|
|
|
client.WithHost(host))
|
|
|
|
}
|
|
|
|
|
2017-09-22 09:05:56 -04:00
|
|
|
func TestAuthZPluginDenyRequest(t *testing.T) {
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
d.Start(t, "--authorization-plugin="+testAuthZPlugin)
|
|
|
|
ctrl.reqRes.Allow = false
|
|
|
|
ctrl.reqRes.Msg = unauthorizedMessage
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
c := d.NewClientT(t)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
// Ensure command is blocked
|
2018-12-22 09:53:02 -05:00
|
|
|
_, err := c.ServerVersion(context.Background())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
assert.Equal(t, 1, ctrl.versionReqCount)
|
|
|
|
assert.Equal(t, 0, ctrl.versionResCount)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
// Ensure unauthorized message appears in response
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, fmt.Sprintf("Error response from daemon: authorization denied by plugin %s: %s", testAuthZPlugin, unauthorizedMessage), err.Error())
|
2017-09-22 09:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestAuthZPluginAPIDenyResponse validates that when authorization
|
|
|
|
// plugin deny the request, the status code is forbidden
|
|
|
|
func TestAuthZPluginAPIDenyResponse(t *testing.T) {
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
d.Start(t, "--authorization-plugin="+testAuthZPlugin)
|
|
|
|
ctrl.reqRes.Allow = false
|
|
|
|
ctrl.resRes.Msg = unauthorizedMessage
|
|
|
|
|
|
|
|
daemonURL, err := url.Parse(d.Sock())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
conn, err := net.DialTimeout(daemonURL.Scheme, daemonURL.Path, time.Second*10)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-12-22 09:53:02 -05:00
|
|
|
c := httputil.NewClientConn(conn, nil)
|
2019-10-12 14:42:02 -04:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "/version", nil)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-12-22 09:53:02 -05:00
|
|
|
resp, err := c.Do(req)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, http.StatusForbidden, resp.StatusCode)
|
2017-09-22 09:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthZPluginDenyResponse(t *testing.T) {
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
d.Start(t, "--authorization-plugin="+testAuthZPlugin)
|
|
|
|
ctrl.reqRes.Allow = true
|
|
|
|
ctrl.resRes.Allow = false
|
|
|
|
ctrl.resRes.Msg = unauthorizedMessage
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
c := d.NewClientT(t)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
// Ensure command is blocked
|
2018-12-22 09:53:02 -05:00
|
|
|
_, err := c.ServerVersion(context.Background())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
assert.Equal(t, 1, ctrl.versionReqCount)
|
|
|
|
assert.Equal(t, 1, ctrl.versionResCount)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
// Ensure unauthorized message appears in response
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, fmt.Sprintf("Error response from daemon: authorization denied by plugin %s: %s", testAuthZPlugin, unauthorizedMessage), err.Error())
|
2017-09-22 09:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestAuthZPluginAllowEventStream verifies event stream propagates
|
|
|
|
// correctly after request pass through by the authorization plugin
|
|
|
|
func TestAuthZPluginAllowEventStream(t *testing.T) {
|
2018-06-11 09:32:11 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
ctrl.reqRes.Allow = true
|
|
|
|
ctrl.resRes.Allow = true
|
|
|
|
d.StartWithBusybox(t, "--authorization-plugin="+testAuthZPlugin)
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
c := d.NewClientT(t)
|
2018-02-12 18:08:25 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
startTime := strconv.FormatInt(systemTime(t, c, testEnv).Unix(), 10)
|
|
|
|
events, errs, cancel := systemEventsSince(c, startTime)
|
2017-09-22 09:05:56 -04:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Create a container and wait for the creation events
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, c)
|
2019-01-08 22:08:47 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, c, cID, "running"))
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
created := false
|
|
|
|
started := false
|
|
|
|
for !created && !started {
|
|
|
|
select {
|
|
|
|
case event := <-events:
|
2018-02-12 18:08:25 -05:00
|
|
|
if event.Type == eventtypes.ContainerEventType && event.Actor.ID == cID {
|
2017-09-22 09:05:56 -04:00
|
|
|
if event.Action == "create" {
|
|
|
|
created = true
|
|
|
|
}
|
|
|
|
if event.Action == "start" {
|
|
|
|
started = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case err := <-errs:
|
|
|
|
if err == io.EOF {
|
|
|
|
t.Fatal("premature end of event stream")
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
case <-time.After(30 * time.Second):
|
|
|
|
// Fail the test
|
|
|
|
t.Fatal("event stream timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure both events and container endpoints are passed to the
|
|
|
|
// authorization plugin
|
|
|
|
assertURIRecorded(t, ctrl.requestsURIs, "/events")
|
|
|
|
assertURIRecorded(t, ctrl.requestsURIs, "/containers/create")
|
2018-02-12 18:08:25 -05:00
|
|
|
assertURIRecorded(t, ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", cID))
|
2017-10-02 09:17:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func systemTime(t *testing.T, client client.APIClient, testEnv *environment.Execution) time.Time {
|
|
|
|
if testEnv.IsLocalDaemon() {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
info, err := client.Info(ctx)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-02 09:17:51 -04:00
|
|
|
|
|
|
|
dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err, "invalid time format in GET /info response")
|
2017-10-02 09:17:51 -04:00
|
|
|
return dt
|
|
|
|
}
|
|
|
|
|
|
|
|
func systemEventsSince(client client.APIClient, since string) (<-chan eventtypes.Message, <-chan error, func()) {
|
|
|
|
eventOptions := types.EventsOptions{
|
|
|
|
Since: since,
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
events, errs := client.Events(ctx, eventOptions)
|
|
|
|
|
|
|
|
return events, errs, cancel
|
2017-09-22 09:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthZPluginErrorResponse(t *testing.T) {
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
d.Start(t, "--authorization-plugin="+testAuthZPlugin)
|
|
|
|
ctrl.reqRes.Allow = true
|
|
|
|
ctrl.resRes.Err = errorMessage
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
c := d.NewClientT(t)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
// Ensure command is blocked
|
2018-12-22 09:53:02 -05:00
|
|
|
_, err := c.ServerVersion(context.Background())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
assert.Equal(t, fmt.Sprintf("Error response from daemon: plugin %s failed with error: %s: %s", testAuthZPlugin, authorization.AuthZApiResponse, errorMessage), err.Error())
|
2017-09-22 09:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthZPluginErrorRequest(t *testing.T) {
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
d.Start(t, "--authorization-plugin="+testAuthZPlugin)
|
|
|
|
ctrl.reqRes.Err = errorMessage
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
c := d.NewClientT(t)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
// Ensure command is blocked
|
2018-12-22 09:53:02 -05:00
|
|
|
_, err := c.ServerVersion(context.Background())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
assert.Equal(t, fmt.Sprintf("Error response from daemon: plugin %s failed with error: %s: %s", testAuthZPlugin, authorization.AuthZApiRequest, errorMessage), err.Error())
|
2017-09-22 09:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthZPluginEnsureNoDuplicatePluginRegistration(t *testing.T) {
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
d.Start(t, "--authorization-plugin="+testAuthZPlugin, "--authorization-plugin="+testAuthZPlugin)
|
|
|
|
|
|
|
|
ctrl.reqRes.Allow = true
|
|
|
|
ctrl.resRes.Allow = true
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
c := d.NewClientT(t)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
_, err := c.ServerVersion(context.Background())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
// assert plugin is only called once..
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, 1, ctrl.versionReqCount)
|
|
|
|
assert.Equal(t, 1, ctrl.versionResCount)
|
2017-09-22 09:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthZPluginEnsureLoadImportWorking(t *testing.T) {
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
ctrl.reqRes.Allow = true
|
|
|
|
ctrl.resRes.Allow = true
|
|
|
|
d.StartWithBusybox(t, "--authorization-plugin="+testAuthZPlugin, "--authorization-plugin="+testAuthZPlugin)
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
c := d.NewClientT(t)
|
2018-02-12 18:08:25 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
tmp, err := os.MkdirTemp("", "test-authz-load-import")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
savedImagePath := filepath.Join(tmp, "save.tar")
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
err = imageSave(c, savedImagePath, "busybox")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-12-22 09:53:02 -05:00
|
|
|
err = imageLoad(c, savedImagePath)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
exportedImagePath := filepath.Join(tmp, "export.tar")
|
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, c)
|
2017-10-02 09:17:51 -04:00
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
responseReader, err := c.ContainerExport(context.Background(), cID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-02 09:17:51 -04:00
|
|
|
defer responseReader.Close()
|
|
|
|
file, err := os.Create(exportedImagePath)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-02 09:17:51 -04:00
|
|
|
defer file.Close()
|
|
|
|
_, err = io.Copy(file, responseReader)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-02 09:17:51 -04:00
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
err = imageImport(c, exportedImagePath)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-02 09:17:51 -04:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:19:15 -04:00
|
|
|
func TestAuthzPluginEnsureContainerCopyToFrom(t *testing.T) {
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
ctrl.reqRes.Allow = true
|
|
|
|
ctrl.resRes.Allow = true
|
|
|
|
d.StartWithBusybox(t, "--authorization-plugin="+testAuthZPlugin, "--authorization-plugin="+testAuthZPlugin)
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
dir, err := os.MkdirTemp("", t.Name())
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2018-04-11 15:19:15 -04:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
f, err := os.CreateTemp(dir, "send")
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2018-04-11 15:19:15 -04:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
fileSize := len(buf) * 1024 * 10
|
|
|
|
for written := 0; written < fileSize; {
|
|
|
|
n, err := f.Write(buf)
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2018-04-11 15:19:15 -04:00
|
|
|
written += n
|
|
|
|
}
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
c := d.NewClientT(t)
|
2018-04-11 15:19:15 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, c)
|
2018-12-22 09:53:02 -05:00
|
|
|
defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
|
2018-04-11 15:19:15 -04:00
|
|
|
|
|
|
|
_, err = f.Seek(0, io.SeekStart)
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2018-04-11 15:19:15 -04:00
|
|
|
|
|
|
|
srcInfo, err := archive.CopyInfoSourcePath(f.Name(), false)
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2018-04-11 15:19:15 -04:00
|
|
|
srcArchive, err := archive.TarResource(srcInfo)
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2018-04-11 15:19:15 -04:00
|
|
|
defer srcArchive.Close()
|
|
|
|
|
|
|
|
dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, archive.CopyInfo{Path: "/test"})
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2018-04-11 15:19:15 -04:00
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
err = c.CopyToContainer(ctx, cID, dstDir, preparedArchive, types.CopyToContainerOptions{})
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2018-04-11 15:19:15 -04:00
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
rdr, _, err := c.CopyFromContainer(ctx, cID, "/test")
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2021-08-24 06:10:50 -04:00
|
|
|
_, err = io.Copy(io.Discard, rdr)
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2018-04-11 15:19:15 -04:00
|
|
|
}
|
|
|
|
|
2017-10-02 09:17:51 -04:00
|
|
|
func imageSave(client client.APIClient, path, image string) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
responseReader, err := client.ImageSave(ctx, []string{image})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseReader.Close()
|
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
_, err = io.Copy(file, responseReader)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func imageLoad(client client.APIClient, path string) error {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
quiet := true
|
|
|
|
ctx := context.Background()
|
|
|
|
response, err := client.ImageLoad(ctx, file, quiet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func imageImport(client client.APIClient, path string) error {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
options := types.ImageImportOptions{}
|
|
|
|
ref := ""
|
|
|
|
source := types.ImageImportSource{
|
|
|
|
Source: file,
|
|
|
|
SourceName: "-",
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
responseReader, err := client.ImageImport(ctx, source, ref, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseReader.Close()
|
|
|
|
return nil
|
2017-09-22 09:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthZPluginHeader(t *testing.T) {
|
|
|
|
defer setupTestV1(t)()
|
|
|
|
ctrl.reqRes.Allow = true
|
|
|
|
ctrl.resRes.Allow = true
|
|
|
|
d.StartWithBusybox(t, "--debug", "--authorization-plugin="+testAuthZPlugin)
|
|
|
|
|
|
|
|
daemonURL, err := url.Parse(d.Sock())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
|
|
|
|
conn, err := net.DialTimeout(daemonURL.Scheme, daemonURL.Path, time.Second*10)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
client := httputil.NewClientConn(conn, nil)
|
2019-10-12 14:42:02 -04:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "/version", nil)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-22 09:05:56 -04:00
|
|
|
resp, err := client.Do(req)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, "application/json", resp.Header["Content-Type"][0])
|
2017-09-22 09:05:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// assertURIRecorded verifies that the given URI was sent and recorded
|
|
|
|
// in the authz plugin
|
|
|
|
func assertURIRecorded(t *testing.T, uris []string, uri string) {
|
|
|
|
var found bool
|
|
|
|
for _, u := range uris {
|
|
|
|
if strings.Contains(u, uri) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("Expected to find URI '%s', recorded uris '%s'", uri, strings.Join(uris, ","))
|
|
|
|
}
|
|
|
|
}
|