Skip to content

Commit bfee07e

Browse files
authored
Merge pull request docker#11708 from ndeloof/entitlements
Introduce support for build.entitlements
2 parents 85567ae + 1d32592 commit bfee07e

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/Microsoft/go-winio v0.6.1
88
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
99
github.com/buger/goterm v1.0.4
10-
github.com/compose-spec/compose-go/v2 v2.0.2
10+
github.com/compose-spec/compose-go/v2 v2.0.3-0.20240407191136-f388192b8a39
1111
github.com/containerd/console v1.0.4
1212
github.com/containerd/containerd v1.7.13
1313
github.com/davecgh/go-spew v1.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+g
9090
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
9191
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
9292
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
93-
github.com/compose-spec/compose-go/v2 v2.0.2 h1:zhXMV7VWI00Su0LdKt8/sxeXxcjLWhmGmpEyw+ZYznI=
94-
github.com/compose-spec/compose-go/v2 v2.0.2/go.mod h1:bEPizBkIojlQ20pi2vNluBa58tevvj0Y18oUSHPyfdc=
93+
github.com/compose-spec/compose-go/v2 v2.0.3-0.20240407191136-f388192b8a39 h1:ZUpnv0xA75X9gy9Y7hjJm51nflGbr+2URaLXBtEic7A=
94+
github.com/compose-spec/compose-go/v2 v2.0.3-0.20240407191136-f388192b8a39/go.mod h1:bEPizBkIojlQ20pi2vNluBa58tevvj0Y18oUSHPyfdc=
9595
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
9696
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
9797
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=

pkg/compose/build.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,11 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
388388
if len(service.Build.Tags) > 0 {
389389
tags = append(tags, service.Build.Tags...)
390390
}
391-
var allow []entitlements.Entitlement
391+
392+
allow, err := buildflags.ParseEntitlements(service.Build.Entitlements)
393+
if err != nil {
394+
return build.Options{}, err
395+
}
392396
if service.Build.Privileged {
393397
allow = append(allow, entitlements.EntitlementSecurityInsecure)
394398
}

pkg/e2e/build_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,35 @@ func TestBuildBuilder(t *testing.T) {
471471
})
472472

473473
}
474+
475+
func TestBuildEntitlements(t *testing.T) {
476+
c := NewParallelCLI(t)
477+
478+
// declare builder
479+
result := c.RunDockerCmd(t, "buildx", "create", "--name", "build-insecure", "--use", "--bootstrap", "--buildkitd-flags",
480+
`'--allow-insecure-entitlement=security.insecure'`)
481+
assert.NilError(t, result.Error)
482+
483+
t.Cleanup(func() {
484+
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test/entitlements", "down")
485+
_ = c.RunDockerCmd(t, "buildx", "rm", "-f", "build-insecure")
486+
})
487+
488+
t.Run("use build privileged mode to run insecure build command", func(t *testing.T) {
489+
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test/entitlements", "build")
490+
capEffRe := regexp.MustCompile("CapEff:\t([0-9a-f]+)")
491+
matches := capEffRe.FindStringSubmatch(res.Stdout())
492+
assert.Equal(t, 2, len(matches), "Did not match CapEff in output, matches: %v", matches)
493+
494+
capEff, err := strconv.ParseUint(matches[1], 16, 64)
495+
assert.NilError(t, err, "Parsing CapEff: %s", matches[1])
496+
497+
// NOTE: can't use constant from x/sys/unix or tests won't compile on macOS/Windows
498+
// #define CAP_SYS_ADMIN 21
499+
// https://github.com/torvalds/linux/blob/v6.1/include/uapi/linux/capability.h#L278
500+
const capSysAdmin = 0x15
501+
if capEff&capSysAdmin != capSysAdmin {
502+
t.Fatalf("CapEff %s is missing CAP_SYS_ADMIN", matches[1])
503+
}
504+
})
505+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# syntax = docker/dockerfile:experimental
2+
3+
4+
# Copyright 2020 Docker Compose CLI authors
5+
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
FROM alpine
19+
RUN --security=insecure cat /proc/self/status | grep CapEff
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
privileged-service:
3+
build:
4+
context: .
5+
entitlements:
6+
- security.insecure
7+

0 commit comments

Comments
 (0)