1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/integration/build/build_userns_linux_test.go

132 lines
3.8 KiB
Go
Raw Normal View History

package build // import "github.com/docker/docker/integration/build"
import (
"bufio"
"bytes"
"context"
"io"
"os"
"strings"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/integration/internal/container"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/docker/testutil/daemon"
"github.com/docker/docker/testutil/fakecontext"
integration: fix TestBuildUserNamespaceValidateCapabilitiesAreV2 not using frozen image Commit f2f5106c92fb9399844dcb7315fe23f3612e7cea added this test to verify loading of images that were built with user-namespaces enabled. However, because this test spins up a new daemon, not the daemon that's set up by the test-suite's `TestMain()` (which loads the frozen images). As a result, the `debian:bullseye` image was pulled from Docker Hub when running the test; Calling POST /v1.41/images/load?quiet=1 Applying tar in /go/src/github.com/docker/docker/bundles/test-integration/TestBuildUserNamespaceValidateCapabilitiesAreV2/d4d366b15997b/root/165536.165536/overlay2/3f7f9375197667acaf7bc810b34689c21f8fed9c52c6765c032497092ca023d6/diff" storage-driver=overlay Applied tar sha256:845f0e5159140e9dbcad00c0326c2a506fbe375aa1c229c43f082867d283149c to 3f7f9375197667acaf7bc810b34689c21f8fed9c52c6765c032497092ca023d6, size: 5922359 Calling POST /v1.41/build?buildargs=null&cachefrom=null&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=&labels=null&memory=0&memswap=0&networkmode=&rm=0&shmsize=0&t=capabilities%3A1.0&target=&ulimits=null&version= Trying to pull debian from https://registry-1.docker.io v2 Fetching manifest from remote" digest="sha256:f169dbadc9021fc0b08e371d50a772809286a167f62a8b6ae86e4745878d283d" error="<nil>" remote="docker.io/library/debian:bullseye Pulling ref from V2 registry: debian:bullseye ... This patch updates `TestBuildUserNamespaceValidateCapabilitiesAreV2` to load the frozen image. `StartWithBusybox` is also changed to `Start`, because the test is not using the busybox image, so there's no need to load it. In a followup, we should probably add some utilities to make this easier to set up (and to allow passing the list frozen images that we want to load, without having to "hard-code" the image name to load). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-01-08 07:25:21 -05:00
"github.com/docker/docker/testutil/fixtures/load"
"gotest.tools/v3/assert"
"gotest.tools/v3/skip"
)
// Implements a test for https://github.com/moby/moby/issues/41723
// Images built in a user-namespaced daemon should have capabilities serialised in
// VFS_CAP_REVISION_2 (no user-namespace root uid) format rather than V3 (that includes
// the root uid).
func TestBuildUserNamespaceValidateCapabilitiesAreV2(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
skip.If(t, testEnv.IsRemoteDaemon())
skip.If(t, !testEnv.IsUserNamespaceInKernel())
skip.If(t, testEnv.IsRootless())
const imageTag = "capabilities:1.0"
tmp, err := os.MkdirTemp("", "integration-")
assert.NilError(t, err)
defer os.RemoveAll(tmp)
dUserRemap := daemon.New(t)
integration: fix TestBuildUserNamespaceValidateCapabilitiesAreV2 not using frozen image Commit f2f5106c92fb9399844dcb7315fe23f3612e7cea added this test to verify loading of images that were built with user-namespaces enabled. However, because this test spins up a new daemon, not the daemon that's set up by the test-suite's `TestMain()` (which loads the frozen images). As a result, the `debian:bullseye` image was pulled from Docker Hub when running the test; Calling POST /v1.41/images/load?quiet=1 Applying tar in /go/src/github.com/docker/docker/bundles/test-integration/TestBuildUserNamespaceValidateCapabilitiesAreV2/d4d366b15997b/root/165536.165536/overlay2/3f7f9375197667acaf7bc810b34689c21f8fed9c52c6765c032497092ca023d6/diff" storage-driver=overlay Applied tar sha256:845f0e5159140e9dbcad00c0326c2a506fbe375aa1c229c43f082867d283149c to 3f7f9375197667acaf7bc810b34689c21f8fed9c52c6765c032497092ca023d6, size: 5922359 Calling POST /v1.41/build?buildargs=null&cachefrom=null&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=&labels=null&memory=0&memswap=0&networkmode=&rm=0&shmsize=0&t=capabilities%3A1.0&target=&ulimits=null&version= Trying to pull debian from https://registry-1.docker.io v2 Fetching manifest from remote" digest="sha256:f169dbadc9021fc0b08e371d50a772809286a167f62a8b6ae86e4745878d283d" error="<nil>" remote="docker.io/library/debian:bullseye Pulling ref from V2 registry: debian:bullseye ... This patch updates `TestBuildUserNamespaceValidateCapabilitiesAreV2` to load the frozen image. `StartWithBusybox` is also changed to `Start`, because the test is not using the busybox image, so there's no need to load it. In a followup, we should probably add some utilities to make this easier to set up (and to allow passing the list frozen images that we want to load, without having to "hard-code" the image name to load). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-01-08 07:25:21 -05:00
dUserRemap.Start(t, "--userns-remap", "default")
ctx := context.Background()
clientUserRemap := dUserRemap.NewClientT(t)
defer clientUserRemap.Close()
integration: fix TestBuildUserNamespaceValidateCapabilitiesAreV2 not using frozen image Commit f2f5106c92fb9399844dcb7315fe23f3612e7cea added this test to verify loading of images that were built with user-namespaces enabled. However, because this test spins up a new daemon, not the daemon that's set up by the test-suite's `TestMain()` (which loads the frozen images). As a result, the `debian:bullseye` image was pulled from Docker Hub when running the test; Calling POST /v1.41/images/load?quiet=1 Applying tar in /go/src/github.com/docker/docker/bundles/test-integration/TestBuildUserNamespaceValidateCapabilitiesAreV2/d4d366b15997b/root/165536.165536/overlay2/3f7f9375197667acaf7bc810b34689c21f8fed9c52c6765c032497092ca023d6/diff" storage-driver=overlay Applied tar sha256:845f0e5159140e9dbcad00c0326c2a506fbe375aa1c229c43f082867d283149c to 3f7f9375197667acaf7bc810b34689c21f8fed9c52c6765c032497092ca023d6, size: 5922359 Calling POST /v1.41/build?buildargs=null&cachefrom=null&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=&labels=null&memory=0&memswap=0&networkmode=&rm=0&shmsize=0&t=capabilities%3A1.0&target=&ulimits=null&version= Trying to pull debian from https://registry-1.docker.io v2 Fetching manifest from remote" digest="sha256:f169dbadc9021fc0b08e371d50a772809286a167f62a8b6ae86e4745878d283d" error="<nil>" remote="docker.io/library/debian:bullseye Pulling ref from V2 registry: debian:bullseye ... This patch updates `TestBuildUserNamespaceValidateCapabilitiesAreV2` to load the frozen image. `StartWithBusybox` is also changed to `Start`, because the test is not using the busybox image, so there's no need to load it. In a followup, we should probably add some utilities to make this easier to set up (and to allow passing the list frozen images that we want to load, without having to "hard-code" the image name to load). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-01-08 07:25:21 -05:00
err = load.FrozenImagesLinux(clientUserRemap, "debian:bullseye-slim")
integration: fix TestBuildUserNamespaceValidateCapabilitiesAreV2 not using frozen image Commit f2f5106c92fb9399844dcb7315fe23f3612e7cea added this test to verify loading of images that were built with user-namespaces enabled. However, because this test spins up a new daemon, not the daemon that's set up by the test-suite's `TestMain()` (which loads the frozen images). As a result, the `debian:bullseye` image was pulled from Docker Hub when running the test; Calling POST /v1.41/images/load?quiet=1 Applying tar in /go/src/github.com/docker/docker/bundles/test-integration/TestBuildUserNamespaceValidateCapabilitiesAreV2/d4d366b15997b/root/165536.165536/overlay2/3f7f9375197667acaf7bc810b34689c21f8fed9c52c6765c032497092ca023d6/diff" storage-driver=overlay Applied tar sha256:845f0e5159140e9dbcad00c0326c2a506fbe375aa1c229c43f082867d283149c to 3f7f9375197667acaf7bc810b34689c21f8fed9c52c6765c032497092ca023d6, size: 5922359 Calling POST /v1.41/build?buildargs=null&cachefrom=null&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=&labels=null&memory=0&memswap=0&networkmode=&rm=0&shmsize=0&t=capabilities%3A1.0&target=&ulimits=null&version= Trying to pull debian from https://registry-1.docker.io v2 Fetching manifest from remote" digest="sha256:f169dbadc9021fc0b08e371d50a772809286a167f62a8b6ae86e4745878d283d" error="<nil>" remote="docker.io/library/debian:bullseye Pulling ref from V2 registry: debian:bullseye ... This patch updates `TestBuildUserNamespaceValidateCapabilitiesAreV2` to load the frozen image. `StartWithBusybox` is also changed to `Start`, because the test is not using the busybox image, so there's no need to load it. In a followup, we should probably add some utilities to make this easier to set up (and to allow passing the list frozen images that we want to load, without having to "hard-code" the image name to load). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-01-08 07:25:21 -05:00
assert.NilError(t, err)
dUserRemapRunning := true
defer func() {
if dUserRemapRunning {
dUserRemap.Stop(t)
dUserRemap.Cleanup(t)
}
}()
dockerfile := `
FROM debian:bullseye-slim
RUN apt-get update && apt-get install -y libcap2-bin --no-install-recommends
RUN setcap CAP_NET_BIND_SERVICE=+eip /bin/sleep
`
source := fakecontext.New(t, "", fakecontext.WithDockerfile(dockerfile))
defer source.Close()
resp, err := clientUserRemap.ImageBuild(ctx,
source.AsTarReader(t),
types.ImageBuildOptions{
Tags: []string{imageTag},
})
assert.NilError(t, err)
defer resp.Body.Close()
buf := bytes.NewBuffer(nil)
err = jsonmessage.DisplayJSONMessagesStream(resp.Body, buf, 0, false, nil)
assert.NilError(t, err)
reader, err := clientUserRemap.ImageSave(ctx, []string{imageTag})
assert.NilError(t, err, "failed to download capabilities image")
defer reader.Close()
tar, err := os.Create(tmp + "/image.tar")
assert.NilError(t, err, "failed to create image tar file")
defer tar.Close()
_, err = io.Copy(tar, reader)
assert.NilError(t, err, "failed to write image tar file")
dUserRemap.Stop(t)
dUserRemap.Cleanup(t)
dUserRemapRunning = false
dNoUserRemap := daemon.New(t)
integration: fix TestBuildUserNamespaceValidateCapabilitiesAreV2 not using frozen image Commit f2f5106c92fb9399844dcb7315fe23f3612e7cea added this test to verify loading of images that were built with user-namespaces enabled. However, because this test spins up a new daemon, not the daemon that's set up by the test-suite's `TestMain()` (which loads the frozen images). As a result, the `debian:bullseye` image was pulled from Docker Hub when running the test; Calling POST /v1.41/images/load?quiet=1 Applying tar in /go/src/github.com/docker/docker/bundles/test-integration/TestBuildUserNamespaceValidateCapabilitiesAreV2/d4d366b15997b/root/165536.165536/overlay2/3f7f9375197667acaf7bc810b34689c21f8fed9c52c6765c032497092ca023d6/diff" storage-driver=overlay Applied tar sha256:845f0e5159140e9dbcad00c0326c2a506fbe375aa1c229c43f082867d283149c to 3f7f9375197667acaf7bc810b34689c21f8fed9c52c6765c032497092ca023d6, size: 5922359 Calling POST /v1.41/build?buildargs=null&cachefrom=null&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=&labels=null&memory=0&memswap=0&networkmode=&rm=0&shmsize=0&t=capabilities%3A1.0&target=&ulimits=null&version= Trying to pull debian from https://registry-1.docker.io v2 Fetching manifest from remote" digest="sha256:f169dbadc9021fc0b08e371d50a772809286a167f62a8b6ae86e4745878d283d" error="<nil>" remote="docker.io/library/debian:bullseye Pulling ref from V2 registry: debian:bullseye ... This patch updates `TestBuildUserNamespaceValidateCapabilitiesAreV2` to load the frozen image. `StartWithBusybox` is also changed to `Start`, because the test is not using the busybox image, so there's no need to load it. In a followup, we should probably add some utilities to make this easier to set up (and to allow passing the list frozen images that we want to load, without having to "hard-code" the image name to load). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-01-08 07:25:21 -05:00
dNoUserRemap.Start(t)
defer func() {
dNoUserRemap.Stop(t)
dNoUserRemap.Cleanup(t)
}()
clientNoUserRemap := dNoUserRemap.NewClientT(t)
defer clientNoUserRemap.Close()
tarFile, err := os.Open(tmp + "/image.tar")
assert.NilError(t, err, "failed to open image tar file")
defer tarFile.Close()
tarReader := bufio.NewReader(tarFile)
loadResp, err := clientNoUserRemap.ImageLoad(ctx, tarReader, false)
assert.NilError(t, err, "failed to load image tar file")
defer loadResp.Body.Close()
buf = bytes.NewBuffer(nil)
err = jsonmessage.DisplayJSONMessagesStream(loadResp.Body, buf, 0, false, nil)
assert.NilError(t, err)
cid := container.Run(ctx, t, clientNoUserRemap,
container.WithImage(imageTag),
container.WithCmd("/sbin/getcap", "-n", "/bin/sleep"),
)
logReader, err := clientNoUserRemap.ContainerLogs(ctx, cid, types.ContainerLogsOptions{
ShowStdout: true,
})
assert.NilError(t, err)
defer logReader.Close()
actualStdout := new(bytes.Buffer)
actualStderr := io.Discard
_, err = stdcopy.StdCopy(actualStdout, actualStderr, logReader)
assert.NilError(t, err)
if strings.TrimSpace(actualStdout.String()) != "/bin/sleep cap_net_bind_service=eip" {
t.Fatalf("run produced invalid output: %q, expected %q", actualStdout.String(), "/bin/sleep cap_net_bind_service=eip")
}
}