diff --git a/go.mod b/go.mod index 32c77020978..ff6404473eb 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/Microsoft/go-winio v0.6.3 github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d github.com/buger/goterm v1.0.4 - github.com/compose-spec/compose-go/v2 v2.16.2-0.20261015044941-5515644a24af + github.com/compose-spec/compose-go/v2 v2.16.2 github.com/containerd/console v1.0.5 github.com/containerd/containerd/v2 v2.4.2 github.com/containerd/errdefs v1.0.0 diff --git a/go.sum b/go.sum index 9acda1765bb..3cd52e59558 100644 --- a/go.sum +++ b/go.sum @@ -30,8 +30,8 @@ github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4= github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE= github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4= -github.com/compose-spec/compose-go/v2 v2.16.2-0.20261015044941-5515644a24af h1:TOZLcS+qr4t2Xv8Wh0i9ebHLF7nlRt2hZ59pI6s49z8= -github.com/compose-spec/compose-go/v2 v2.16.2-0.20261015044941-5515644a24af/go.mod h1:Q1+qtN4vhzEjGrnqRtzx1xa8raDZQlMUe3WJxndYNiQ= +github.com/compose-spec/compose-go/v2 v2.16.2 h1:pTp1DqQwJzFd85GzlWsSszKA1ABrV/ySqsClujIcWA8= +github.com/compose-spec/compose-go/v2 v2.16.2/go.mod h1:Q1+qtN4vhzEjGrnqRtzx1xa8raDZQlMUe3WJxndYNiQ= github.com/containerd/cgroups/v3 v3.1.3 h1:eUNflyMddm18+yrDmZPn3jI7C5hJ9ahABE5q6dyLYXQ= github.com/containerd/cgroups/v3 v3.1.3/go.mod h1:PKZ2AcWmSBsY/tJUVhtS/rluX0b1uq1GmPO1ElCmbOw= github.com/containerd/console v1.0.5 h1:R0ymNeydRqH2DmakFNdmjR2k0t7UPuiOV/N/27/qqsc= diff --git a/pkg/compose/api_versions.go b/pkg/compose/api_versions.go index d39aef8aacd..fe9145fa3fe 100644 --- a/pkg/compose/api_versions.go +++ b/pkg/compose/api_versions.go @@ -48,6 +48,16 @@ const ( // - interface_name was not configurable // - ImageList didn't support platform filtering apiVersion149 = "1.49" + + // apiVersion156 represents Docker Engine API version 1.56 (Engine v29.8). + // + // New features in this version: + // - HostConfig.Umask in ContainerCreate + // + // Before this version: + // - The daemon silently ignores HostConfig.Umask, so the container would + // run with the runtime's default umask instead of the requested one + apiVersion156 = "1.56" ) // Docker Engine version strings for user-facing error messages. @@ -58,6 +68,9 @@ const ( // DockerEngineV28_1 is the specific version string for Docker Engine 28.1 DockerEngineV28_1 = "v28.1" + + // dockerEngineV29_8 is the specific version string for Docker Engine 29.8 + dockerEngineV29_8 = "v29.8" ) // Build tool version constants diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 71fcca2b8d1..193eca49606 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -403,6 +403,10 @@ func (s *composeService) getCreateConfigs(ctx context.Context, if err != nil { return createConfigs{}, err } + umask, err := umaskOption(service, apiVersion) + if err != nil { + return createConfigs{}, err + } portBindings, err := buildContainerPortBindingOptions(service) if err != nil { return createConfigs{}, err @@ -467,6 +471,7 @@ func (s *composeService) getCreateConfigs(ctx context.Context, GroupAdd: service.GroupAdd, Links: links, OomScoreAdj: int(service.OomScoreAdj), + Umask: umask, } if unconfined { @@ -732,6 +737,20 @@ func defaultNetworkSettings(project *types.Project, return container.NetworkMode(primaryNetworkMobyNetworkName), networkConfig, nil } +// umaskOption returns the umask to set on the container, if any. An engine +// older than API 1.56 ignores HostConfig.Umask without any error: running the +// container with the runtime's default mask instead of the requested one would +// go unnoticed, so the unsupported case is reported. +func umaskOption(service types.ServiceConfig, version string) (*uint32, error) { + if service.Umask == nil { + return nil, nil + } + if versions.LessThan(version, apiVersion156) { + return nil, fmt.Errorf("umask requires Docker Engine %s or later", dockerEngineV29_8) + } + return service.Umask, nil +} + func getRestartPolicy(service types.ServiceConfig) container.RestartPolicy { var restart container.RestartPolicy if service.Restart != "" { diff --git a/pkg/compose/hash.go b/pkg/compose/hash.go index 48977531c8c..5da2c39750f 100644 --- a/pkg/compose/hash.go +++ b/pkg/compose/hash.go @@ -145,6 +145,10 @@ var serviceHashKeyOrder = []string{ // merge order made the fallback place it here already: listing it is a // no-op for every hash, it only freezes the position) "pull_refresh_after", + // added with compose-go v2.16.2 (the umask service attribute): the + // sorted fallback would place it here already, listing it only freezes + // the position + "umask", } // trimServiceHashFields removes the attributes deliberately excluded from the diff --git a/pkg/compose/umask_test.go b/pkg/compose/umask_test.go new file mode 100644 index 00000000000..c917f305b0b --- /dev/null +++ b/pkg/compose/umask_test.go @@ -0,0 +1,96 @@ +/* + Copyright 2020 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package compose + +import ( + "testing" + + "github.com/compose-spec/compose-go/v2/types" + "gotest.tools/v3/assert" +) + +func TestUmaskOption(t *testing.T) { + zero, mask := uint32(0), uint32(0o77) + + t.Run("not set", func(t *testing.T) { + got, err := umaskOption(types.ServiceConfig{}, "1.44") + assert.NilError(t, err) + assert.Assert(t, got == nil) + }) + t.Run("set on a supporting engine", func(t *testing.T) { + got, err := umaskOption(types.ServiceConfig{ContainerSpec: types.ContainerSpec{Umask: &mask}}, apiVersion156) + assert.NilError(t, err) + assert.DeepEqual(t, got, &mask) + }) + t.Run("zero is a value, not an absence", func(t *testing.T) { + got, err := umaskOption(types.ServiceConfig{ContainerSpec: types.ContainerSpec{Umask: &zero}}, "1.57") + assert.NilError(t, err) + assert.Assert(t, got != nil) + assert.Equal(t, *got, uint32(0)) + }) + t.Run("set on an engine that would ignore it", func(t *testing.T) { + _, err := umaskOption(types.ServiceConfig{ContainerSpec: types.ContainerSpec{Umask: &mask}}, "1.55") + assert.ErrorContains(t, err, "umask requires Docker Engine v29.8 or later") + }) + t.Run("unset is accepted by any engine", func(t *testing.T) { + _, err := umaskOption(types.ServiceConfig{}, "1.24") + assert.NilError(t, err) + }) +} + +// The umask reaches the engine in the container's HostConfig, for services as +// well as for the pre_start containers, which are created the same way. +func TestGetCreateConfigsUmask(t *testing.T) { + mask := uint32(0o27) + project := &types.Project{Name: "demo"} + service := types.ServiceConfig{ + Name: "app", + ContainerSpec: types.ContainerSpec{Image: "alpine", Umask: &mask}, + } + + t.Run("supported", func(t *testing.T) { + tested, _ := newPreStartTestServiceWithVersion(t, apiVersion156) + cfgs, err := tested.getCreateConfigs(t.Context(), project, service, 1, nil, createOptions{Labels: map[string]string{}}) + assert.NilError(t, err) + assert.Assert(t, cfgs.Host.Umask != nil) + assert.Equal(t, *cfgs.Host.Umask, uint32(0o27)) + }) + t.Run("not set", func(t *testing.T) { + tested, _ := newPreStartTestServiceWithVersion(t, apiVersion156) + service := service + service.Umask = nil + cfgs, err := tested.getCreateConfigs(t.Context(), project, service, 1, nil, createOptions{Labels: map[string]string{}}) + assert.NilError(t, err) + assert.Assert(t, cfgs.Host.Umask == nil) + }) + t.Run("unsupported engine", func(t *testing.T) { + tested, _ := newPreStartTestServiceWithVersion(t, "1.55") + _, err := tested.getCreateConfigs(t.Context(), project, service, 1, nil, createOptions{Labels: map[string]string{}}) + assert.ErrorContains(t, err, "umask requires Docker Engine v29.8 or later") + }) +} + +func TestServiceHashTracksUmask(t *testing.T) { + zero, mask := uint32(0), uint32(0o77) + hash := func(umask *uint32) string { + h, err := ServiceHash(types.ServiceConfig{Name: "app", ContainerSpec: types.ContainerSpec{Image: "alpine", Umask: umask}}) + assert.NilError(t, err) + return h + } + assert.Assert(t, hash(nil) != hash(&zero), "an explicit 0 differs from no umask") + assert.Assert(t, hash(&zero) != hash(&mask)) +} diff --git a/pkg/e2e/scenario.go b/pkg/e2e/scenario.go index cd2c3a145df..1b6ea7b5e2c 100644 --- a/pkg/e2e/scenario.go +++ b/pkg/e2e/scenario.go @@ -28,6 +28,7 @@ import ( "time" "unicode" + "github.com/moby/moby/client/pkg/versions" "gotest.tools/v3/icmd" ) @@ -648,6 +649,19 @@ func ContainerdImageStore(t testing.TB, c *CLI) string { return "" } +// EngineAPIVersionAtLeast requires a minimum Engine API version (for example +// "1.56"), the unit in which the daemon's features are versioned. +func EngineAPIVersionAtLeast(minimum string) Requirement { + return func(t testing.TB, c *CLI) string { + t.Helper() + version := strings.TrimSpace(c.RunDockerCmd(t, "version", "-f", "{{.Server.APIVersion}}").Stdout()) + if versions.LessThan(version, minimum) { + return fmt.Sprintf("engine API version %s < %s", version, minimum) + } + return "" + } +} + // EngineVersionAtLeast requires a minimum daemon major version. func EngineVersionAtLeast(major int) Requirement { return func(t testing.TB, c *CLI) string { diff --git a/pkg/e2e/testdata/TestUmask/compose.yaml b/pkg/e2e/testdata/TestUmask/compose.yaml new file mode 100644 index 00000000000..97bd42476eb --- /dev/null +++ b/pkg/e2e/testdata/TestUmask/compose.yaml @@ -0,0 +1,6 @@ +services: + app: + image: alpine + umask: 027 + # the entrypoint records the mask it started with + command: sh -c "umask > /tmp/entrypoint-umask && sleep infinity" diff --git a/pkg/e2e/umask_test.go b/pkg/e2e/umask_test.go new file mode 100644 index 00000000000..07f53314b13 --- /dev/null +++ b/pkg/e2e/umask_test.go @@ -0,0 +1,34 @@ +//go:build e2e + +/* + Copyright 2020 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package e2e + +import ( + "testing" + "time" +) + +func TestUmask(t *testing.T) { + NewScenario(t, "umask must set the file mode creation mask of the entrypoint and of exec processes"). + Requires(EngineAPIVersionAtLeast("1.56")). + Step("up starts the service with its umask, which the entrypoint and an exec process both see", + ComposeCmd("up", "-d"), + Eventually(ServiceState("app", "running"), 10*time.Second), + Eventually(ExecOutputContains("app", "cat /tmp/entrypoint-umask", "0027"), 10*time.Second), + ExecOutputContains("app", "umask", "0027")) +}