2020-12-02 04:12:41 -05:00
|
|
|
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"
|
2021-01-26 09:22:13 -05:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2020-12-02 04:12:41 -05:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
|
|
|
"github.com/docker/docker/testutil/daemon"
|
|
|
|
"github.com/docker/docker/testutil/fakecontext"
|
2021-01-08 07:25:21 -05:00
|
|
|
"github.com/docker/docker/testutil/fixtures/load"
|
2020-12-02 04:12:41 -05:00
|
|
|
"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"
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
tmp, err := os.MkdirTemp("", "integration-")
|
2020-12-02 04:12:41 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
dUserRemap := daemon.New(t)
|
2021-01-08 07:25:21 -05:00
|
|
|
dUserRemap.Start(t, "--userns-remap", "default")
|
|
|
|
ctx := context.Background()
|
|
|
|
clientUserRemap := dUserRemap.NewClientT(t)
|
2021-11-05 12:29:44 -04:00
|
|
|
defer clientUserRemap.Close()
|
2021-01-08 07:25:21 -05:00
|
|
|
|
2021-08-19 17:40:38 -04:00
|
|
|
err = load.FrozenImagesLinux(clientUserRemap, "debian:bullseye-slim")
|
2021-01-08 07:25:21 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2020-12-02 04:12:41 -05:00
|
|
|
dUserRemapRunning := true
|
|
|
|
defer func() {
|
|
|
|
if dUserRemapRunning {
|
|
|
|
dUserRemap.Stop(t)
|
2021-11-05 12:29:44 -04:00
|
|
|
dUserRemap.Cleanup(t)
|
2020-12-02 04:12:41 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
dockerfile := `
|
2021-08-19 17:40:38 -04:00
|
|
|
FROM debian:bullseye-slim
|
|
|
|
RUN apt-get update && apt-get install -y libcap2-bin --no-install-recommends
|
2020-12-02 04:12:41 -05:00
|
|
|
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()
|
2021-01-26 09:22:13 -05:00
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
err = jsonmessage.DisplayJSONMessagesStream(resp.Body, buf, 0, false, nil)
|
|
|
|
assert.NilError(t, err)
|
2020-12-02 04:12:41 -05:00
|
|
|
|
|
|
|
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)
|
2021-01-08 07:25:21 -05:00
|
|
|
dNoUserRemap.Start(t)
|
2021-11-05 12:29:44 -04:00
|
|
|
defer func() {
|
|
|
|
dNoUserRemap.Stop(t)
|
|
|
|
dNoUserRemap.Cleanup(t)
|
|
|
|
}()
|
2020-12-02 04:12:41 -05:00
|
|
|
|
|
|
|
clientNoUserRemap := dNoUserRemap.NewClientT(t)
|
2021-11-05 12:29:44 -04:00
|
|
|
defer clientNoUserRemap.Close()
|
2020-12-02 04:12:41 -05:00
|
|
|
|
|
|
|
tarFile, err := os.Open(tmp + "/image.tar")
|
|
|
|
assert.NilError(t, err, "failed to open image tar file")
|
2021-11-05 12:29:44 -04:00
|
|
|
defer tarFile.Close()
|
2020-12-02 04:12:41 -05:00
|
|
|
|
|
|
|
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()
|
2021-01-26 09:22:13 -05:00
|
|
|
buf = bytes.NewBuffer(nil)
|
|
|
|
err = jsonmessage.DisplayJSONMessagesStream(loadResp.Body, buf, 0, false, nil)
|
|
|
|
assert.NilError(t, err)
|
2020-12-02 04:12:41 -05:00
|
|
|
|
|
|
|
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)
|
2021-11-05 12:29:44 -04:00
|
|
|
defer logReader.Close()
|
2020-12-02 04:12:41 -05:00
|
|
|
|
|
|
|
actualStdout := new(bytes.Buffer)
|
2021-08-24 06:10:50 -04:00
|
|
|
actualStderr := io.Discard
|
2020-12-02 04:12:41 -05:00
|
|
|
_, err = stdcopy.StdCopy(actualStdout, actualStderr, logReader)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
if strings.TrimSpace(actualStdout.String()) != "/bin/sleep cap_net_bind_service=eip" {
|
2020-11-27 09:38:26 -05:00
|
|
|
t.Fatalf("run produced invalid output: %q, expected %q", actualStdout.String(), "/bin/sleep cap_net_bind_service=eip")
|
2020-12-02 04:12:41 -05:00
|
|
|
}
|
|
|
|
}
|