mirror of
https://github.com/docker/compose.git
synced 2026-11-17 21:57:19 +00:00
fix(bake): grant access to ssh, local caches and OCI contexts
Grant access to SSH keys and sockets from build configuration and CLI options, and authorize default agent forwarding, including implicit SSH Git contexts. Serialize pathless SSH entries without an empty source so Bake applies the agent entitlement check. Grant read access to local cache imports and OCI layout contexts, and write access to local cache exports. Match Bake's path and cache syntax while excluding service, target and image references from filesystem grants. Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
parent
43a5d5a253
commit
1e1c577eab
3 changed files with 364 additions and 12 deletions
2
go.mod
2
go.mod
|
|
@ -42,6 +42,7 @@ require (
|
|||
github.com/spf13/cobra v1.10.2
|
||||
github.com/spf13/pflag v1.0.10
|
||||
github.com/tilt-dev/fsnotify v1.4.8-0.20220602155310-fff9c274a375
|
||||
github.com/tonistiigi/go-csvvalue v0.0.0-20240814133006-030d3b2625d0
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0
|
||||
go.opentelemetry.io/otel v1.46.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.46.0
|
||||
|
|
@ -108,7 +109,6 @@ require (
|
|||
github.com/secure-systems-lab/go-securesystemslib v0.11.0 // indirect
|
||||
github.com/shibumi/go-pathspec v1.3.0 // indirect
|
||||
github.com/tonistiigi/fsutil v0.0.0-20260819142231-83cac42c1c52 // indirect
|
||||
github.com/tonistiigi/go-csvvalue v0.0.0-20240814133006-030d3b2625d0 // indirect
|
||||
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea // indirect
|
||||
github.com/tonistiigi/vt100 v0.0.0-20240514184818-90bafcd6abab // indirect
|
||||
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ import (
|
|||
"github.com/compose-spec/compose-go/v2/types"
|
||||
"github.com/containerd/console"
|
||||
"github.com/containerd/errdefs"
|
||||
"github.com/distribution/reference"
|
||||
"github.com/docker/cli/cli-plugins/manager"
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/docker/cli/cli/command/image/build"
|
||||
|
|
@ -43,10 +44,12 @@ import (
|
|||
"github.com/google/uuid"
|
||||
"github.com/moby/buildkit/client"
|
||||
gitutil "github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
|
||||
buildkitgit "github.com/moby/buildkit/util/gitutil"
|
||||
"github.com/moby/buildkit/util/progress/progressui"
|
||||
"github.com/moby/moby/client/pkg/versions"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/tonistiigi/go-csvvalue"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/docker/compose/v5/pkg/api"
|
||||
|
|
@ -121,12 +124,13 @@ type buildStatus struct {
|
|||
// drive `buildx bake`: the bake file definition, plus the settings that travel
|
||||
// as command arguments or environment variables rather than in the file.
|
||||
type bakeBuild struct {
|
||||
cfg bakeConfig
|
||||
targetNames map[string]string // service name -> bake target name
|
||||
expectedImages map[string]string // service name -> expected image
|
||||
localPaths []string // local build inputs bake needs `--allow fs.read` for
|
||||
entitlements []string
|
||||
secretsEnv []string
|
||||
cfg bakeConfig
|
||||
targetNames map[string]string // service name -> bake target name
|
||||
expectedImages map[string]string // service name -> expected image
|
||||
localPaths []string // local build inputs bake needs `--allow fs.read` for
|
||||
localWritePaths []string // local cache destinations bake needs `--allow fs.write` for
|
||||
entitlements []string
|
||||
secretsEnv []string
|
||||
}
|
||||
|
||||
func (s *composeService) doBuildBake(ctx context.Context, project *types.Project, serviceToBeBuild types.Services, options api.BuildOptions) (map[string]string, error) {
|
||||
|
|
@ -253,6 +257,23 @@ func (s *composeService) prepareBakeBuild(project *types.Project, serviceToBeBui
|
|||
entitlements := bakeEntitlements(buildConfig)
|
||||
bake.entitlements = append(bake.entitlements, entitlements...)
|
||||
bake.localPaths = append(bake.localPaths, localBuildPaths(project, buildConfig)...)
|
||||
bake.localPaths = append(bake.localPaths, localCachePaths(buildConfig.CacheFrom, "src")...)
|
||||
bake.localWritePaths = append(bake.localWritePaths, localCachePaths(buildConfig.CacheTo, "dest")...)
|
||||
|
||||
ssh := slices.Concat(buildConfig.SSH, options.SSHs)
|
||||
for _, key := range ssh {
|
||||
if key.Path == "" {
|
||||
bake.entitlements = append(bake.entitlements, "ssh")
|
||||
} else {
|
||||
bake.localPaths = append(bake.localPaths, strings.Split(key.Path, ",")...)
|
||||
}
|
||||
}
|
||||
// Bake implicitly forwards the default agent for SSH Git contexts.
|
||||
if len(ssh) == 0 {
|
||||
if u, err := buildkitgit.ParseURL(buildConfig.Context); err == nil && u.Scheme == buildkitgit.SSHProtocol {
|
||||
bake.entitlements = append(bake.entitlements, "ssh")
|
||||
}
|
||||
}
|
||||
|
||||
image := api.GetImageNameOrDefault(service, project.Name)
|
||||
if _, ok := serviceToBeBuild[serviceName]; ok {
|
||||
|
|
@ -280,7 +301,7 @@ func (s *composeService) prepareBakeBuild(project *types.Project, serviceToBeBui
|
|||
Platforms: buildConfig.Platforms,
|
||||
Target: buildConfig.Target,
|
||||
Secrets: secrets,
|
||||
SSH: toBakeSSH(append(buildConfig.SSH, options.SSHs...)),
|
||||
SSH: toBakeSSH(ssh),
|
||||
Pull: buildConfig.Pull || options.Pull,
|
||||
NoCache: buildConfig.NoCache || options.NoCache,
|
||||
ShmSize: buildConfig.ShmSize,
|
||||
|
|
@ -361,13 +382,20 @@ func localBuildPaths(project *types.Project, buildConfig types.BuildConfig) []st
|
|||
}
|
||||
var local []string
|
||||
for _, path := range paths {
|
||||
if _, _, err := gitutil.ParseGitRef(path); !strings.Contains(path, "://") && err != nil {
|
||||
if cwdPath, ok := strings.CutPrefix(path, "cwd://"); ok {
|
||||
local = append(local, cwdPath)
|
||||
} else if layout, ok := strings.CutPrefix(path, "oci-layout://"); ok {
|
||||
local = append(local, ociLayoutPath(layout))
|
||||
} else if strings.HasPrefix(path, types.ServicePrefix) || strings.HasPrefix(path, "target:") || strings.HasPrefix(path, "docker-image:") {
|
||||
continue
|
||||
} else if _, _, err := gitutil.ParseGitRef(path); !strings.Contains(path, "://") && err != nil {
|
||||
local = append(local, path)
|
||||
}
|
||||
}
|
||||
contextPath := strings.TrimPrefix(buildConfig.Context, "cwd://")
|
||||
if buildConfig.Dockerfile != "" && buildConfig.DockerfileInline == "" &&
|
||||
(!isRemoteContext(buildConfig.Context) || filepath.IsAbs(buildConfig.Dockerfile)) {
|
||||
local = append(local, dockerFilePath(buildConfig.Context, buildConfig.Dockerfile))
|
||||
(!isRemoteContext(contextPath) || filepath.IsAbs(buildConfig.Dockerfile)) {
|
||||
local = append(local, dockerFilePath(contextPath, buildConfig.Dockerfile))
|
||||
}
|
||||
for _, ref := range buildConfig.Secrets {
|
||||
secret := project.Secrets[ref.Source]
|
||||
|
|
@ -378,6 +406,59 @@ func localBuildPaths(project *types.Project, buildConfig types.BuildConfig) []st
|
|||
return local
|
||||
}
|
||||
|
||||
// ociLayoutPath extracts the filesystem path using Buildx's OCI layout syntax.
|
||||
// The caller removes the scheme; Bake validates the original reference.
|
||||
// Path extraction adapted from https://github.com/docker/buildx/blob/c7748d0a0bdbdad73fb939cb62fcd4e634e3d7e6/util/ocilayout/parse.go
|
||||
func ociLayoutPath(path string) string {
|
||||
if i := strings.LastIndex(path, "@"); i >= 0 && reference.DigestRegexp.MatchString(path[i+1:]) {
|
||||
path = path[:i]
|
||||
}
|
||||
if i := strings.LastIndex(path, ":"); i >= 0 && !isWindowsDrivePath(path, i) {
|
||||
if reference.TagRegexp.MatchString(path[i+1:]) {
|
||||
path = path[:i]
|
||||
}
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// isWindowsDrivePath reports whether colon marks an absolute Windows drive prefix.
|
||||
// Adapted from https://github.com/docker/buildx/blob/c7748d0a0bdbdad73fb939cb62fcd4e634e3d7e6/util/ocilayout/parse.go
|
||||
func isWindowsDrivePath(path string, colon int) bool {
|
||||
if colon != 1 || len(path) < 3 {
|
||||
return false
|
||||
}
|
||||
if path[2] != '/' && path[2] != '\\' {
|
||||
return false
|
||||
}
|
||||
c := path[0]
|
||||
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')
|
||||
}
|
||||
|
||||
// localCachePaths extracts local cache sources or destinations. Bake reports
|
||||
// invalid cache definitions; they must not produce grants here.
|
||||
func localCachePaths(entries []string, key string) []string {
|
||||
var paths []string
|
||||
for _, entry := range entries {
|
||||
fields, err := csvvalue.Fields(entry, nil)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
attrs := make(map[string]string, len(fields))
|
||||
for _, field := range fields {
|
||||
name, value, ok := strings.Cut(field, "=")
|
||||
if !ok {
|
||||
attrs = nil
|
||||
break
|
||||
}
|
||||
attrs[strings.ToLower(name)] = value
|
||||
}
|
||||
if attrs["type"] == "local" && attrs[key] != "" {
|
||||
paths = append(paths, attrs[key])
|
||||
}
|
||||
}
|
||||
return paths
|
||||
}
|
||||
|
||||
// bakeMetadataPath picks a fresh temporary path for bake's --metadata-file.
|
||||
// We don't use os.CreateTemp here as we need a temporary file name, but don't
|
||||
// want it actually created, as bake relies on atomicwriter and this creates
|
||||
|
|
@ -404,6 +485,9 @@ func bakeArgs(bake *bakeBuild, metadataFile string, options api.BuildOptions) []
|
|||
for _, path := range slices.Compact(slices.Sorted(slices.Values(bake.localPaths))) {
|
||||
args = append(args, "--allow", "fs.read="+path)
|
||||
}
|
||||
for _, path := range slices.Compact(slices.Sorted(slices.Values(bake.localWritePaths))) {
|
||||
args = append(args, "--allow", "fs.write="+path)
|
||||
}
|
||||
for _, entitlement := range slices.Compact(slices.Sorted(slices.Values(bake.entitlements))) {
|
||||
args = append(args, "--allow", entitlement)
|
||||
}
|
||||
|
|
@ -582,7 +666,11 @@ func toBakeUlimits(ulimits map[string]*types.UlimitsConfig) []string {
|
|||
func toBakeSSH(ssh types.SSHConfig) []string {
|
||||
var s []string
|
||||
for _, key := range ssh {
|
||||
s = append(s, fmt.Sprintf("%s=%s", key.ID, key.Path))
|
||||
if key.Path == "" {
|
||||
s = append(s, key.ID)
|
||||
} else {
|
||||
s = append(s, fmt.Sprintf("%s=%s", key.ID, key.Path))
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/compose-spec/compose-go/v2/types"
|
||||
|
|
@ -169,6 +170,269 @@ func TestPrepareBakeBuildGrants(t *testing.T) {
|
|||
assert.Assert(t, !slices.Contains(args, "fs.read="+filepath.Dir(dockerfile)), "must not grant the Dockerfile's parent directory")
|
||||
}
|
||||
|
||||
func TestPrepareBakeCWDGrants(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
t.Chdir(root)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
context string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "relative context",
|
||||
context: "cwd://app",
|
||||
want: filepath.Join("docker", "Dockerfile"),
|
||||
},
|
||||
{
|
||||
name: "absolute context",
|
||||
context: "cwd://" + filepath.Join(root, "app"),
|
||||
want: filepath.Join(root, "docker", "Dockerfile"),
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dockerfile := filepath.Join("..", "docker", "Dockerfile")
|
||||
project := &types.Project{
|
||||
Name: "cwd-grants",
|
||||
Services: types.Services{"app": {Name: "app", WorkloadSpec: types.WorkloadSpec{Build: &types.BuildConfig{
|
||||
Context: tc.context, Dockerfile: dockerfile,
|
||||
}}}},
|
||||
}
|
||||
s := &composeService{proxyConfig: map[string]string{}, events: &ignore{}}
|
||||
bake := s.prepareBakeBuild(project, project.Services, api.BuildOptions{})
|
||||
assert.Equal(t, bake.cfg.Targets["app"].Dockerfile, dockerfile)
|
||||
assert.DeepEqual(t, bake.localPaths, []string{strings.TrimPrefix(tc.context, "cwd://"), tc.want})
|
||||
assert.Assert(t, slices.Contains(bakeArgs(bake, "metadata.json", api.BuildOptions{}), "fs.read="+tc.want))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareBakeSSHGrants(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
context string
|
||||
ssh types.SSHConfig
|
||||
cliSSH []types.SSHKey
|
||||
wantSSH []string
|
||||
wantGrants []string
|
||||
}{
|
||||
{
|
||||
name: "none",
|
||||
},
|
||||
{
|
||||
name: "configured key",
|
||||
ssh: types.SSHConfig{{ID: "key", Path: "/keys/id"}},
|
||||
wantSSH: []string{"key=/keys/id"},
|
||||
wantGrants: []string{"fs.read=/keys/id"},
|
||||
},
|
||||
{
|
||||
name: "configured agent",
|
||||
ssh: types.SSHConfig{{ID: "default"}},
|
||||
wantSSH: []string{"default"},
|
||||
wantGrants: []string{"ssh"},
|
||||
},
|
||||
{
|
||||
name: "CLI socket",
|
||||
cliSSH: []types.SSHKey{{ID: "default", Path: "/agent/socket"}},
|
||||
wantSSH: []string{"default=/agent/socket"},
|
||||
wantGrants: []string{"fs.read=/agent/socket"},
|
||||
},
|
||||
{
|
||||
name: "CLI agent",
|
||||
cliSSH: []types.SSHKey{{ID: "default"}},
|
||||
wantSSH: []string{"default"},
|
||||
wantGrants: []string{"ssh"},
|
||||
},
|
||||
{
|
||||
name: "multiple key paths",
|
||||
cliSSH: []types.SSHKey{{ID: "keys", Path: "/keys/one,/keys/two"}},
|
||||
wantSSH: []string{"keys=/keys/one,/keys/two"},
|
||||
wantGrants: []string{"fs.read=/keys/one", "fs.read=/keys/two"},
|
||||
},
|
||||
{
|
||||
name: "both sources",
|
||||
ssh: types.SSHConfig{{ID: "key", Path: "/keys/id"}},
|
||||
cliSSH: []types.SSHKey{{ID: "default"}},
|
||||
wantSSH: []string{"key=/keys/id", "default"},
|
||||
wantGrants: []string{"fs.read=/keys/id", "ssh"},
|
||||
},
|
||||
{
|
||||
name: "implicit Git agent",
|
||||
context: "git@github.com:docker/compose.git",
|
||||
wantGrants: []string{"ssh"},
|
||||
},
|
||||
{
|
||||
name: "implicit SSH URL agent",
|
||||
context: "ssh://git@example.com/project.git",
|
||||
wantGrants: []string{"ssh"},
|
||||
},
|
||||
{
|
||||
name: "HTTPS Git context",
|
||||
context: "https://example.com/project.git",
|
||||
},
|
||||
{
|
||||
name: "explicit Git key",
|
||||
context: "git@github.com:docker/compose.git",
|
||||
ssh: types.SSHConfig{{ID: "default", Path: "/keys/id"}},
|
||||
wantSSH: []string{"default=/keys/id"},
|
||||
wantGrants: []string{"fs.read=/keys/id"},
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
contextPath := tc.context
|
||||
if contextPath == "" {
|
||||
contextPath = "https://example.com/project.git"
|
||||
}
|
||||
project := &types.Project{Name: "ssh", Services: types.Services{"app": {
|
||||
Name: "app", WorkloadSpec: types.WorkloadSpec{Build: &types.BuildConfig{Context: contextPath, SSH: tc.ssh}},
|
||||
}}}
|
||||
s := &composeService{proxyConfig: map[string]string{}, events: &ignore{}}
|
||||
bake := s.prepareBakeBuild(project, project.Services, api.BuildOptions{SSHs: tc.cliSSH})
|
||||
assert.DeepEqual(t, bake.cfg.Targets["app"].SSH, tc.wantSSH)
|
||||
assert.DeepEqual(t, bake.cfg.Targets["app"].Entitlements, []string(nil))
|
||||
want := []string{"bake", "--file", "-", "--progress", "rawjson", "--metadata-file", "metadata.json"}
|
||||
for _, grant := range tc.wantGrants {
|
||||
want = append(want, "--allow", grant)
|
||||
}
|
||||
assert.DeepEqual(t, bakeArgs(bake, "metadata.json", api.BuildOptions{}), want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalCachePaths(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
entry string
|
||||
key string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
entry: "type=local,src=/cache",
|
||||
key: "src",
|
||||
want: []string{"/cache"},
|
||||
},
|
||||
{
|
||||
entry: "type=local,dest=/cache",
|
||||
key: "dest",
|
||||
want: []string{"/cache"},
|
||||
},
|
||||
{
|
||||
entry: `type=local,"src=/cache,one"`,
|
||||
key: "src",
|
||||
want: []string{"/cache,one"},
|
||||
},
|
||||
{
|
||||
entry: "TYPE=local,SRC=/cache",
|
||||
key: "src",
|
||||
want: []string{"/cache"},
|
||||
},
|
||||
{
|
||||
entry: "type=local,src=/old,src=/new",
|
||||
key: "src",
|
||||
want: []string{"/new"},
|
||||
},
|
||||
{
|
||||
entry: "type=local,src=/cache,type=registry",
|
||||
key: "src",
|
||||
},
|
||||
{
|
||||
entry: "type=local,src=/cache",
|
||||
key: "dest",
|
||||
},
|
||||
{
|
||||
entry: "image:cache",
|
||||
key: "src",
|
||||
},
|
||||
{
|
||||
entry: "type=registry,ref=image:cache",
|
||||
key: "src",
|
||||
},
|
||||
{
|
||||
entry: "type=local,src=/cache,invalid",
|
||||
key: "src",
|
||||
},
|
||||
{
|
||||
entry: `type=local,"src=/cache`,
|
||||
key: "src",
|
||||
},
|
||||
} {
|
||||
t.Run(tc.entry+"/"+tc.key, func(t *testing.T) {
|
||||
assert.DeepEqual(t, localCachePaths([]string{tc.entry}, tc.key), tc.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareBakeCacheGrants(t *testing.T) {
|
||||
project := &types.Project{Name: "cache", Services: types.Services{"app": {
|
||||
Name: "app", WorkloadSpec: types.WorkloadSpec{Build: &types.BuildConfig{
|
||||
Context: "https://example.com/project.git",
|
||||
CacheFrom: []string{"type=local,src=/cache/in"}, CacheTo: []string{"type=local,dest=/cache/out"},
|
||||
}},
|
||||
}}}
|
||||
s := &composeService{proxyConfig: map[string]string{}, events: &ignore{}}
|
||||
bake := s.prepareBakeBuild(project, project.Services, api.BuildOptions{})
|
||||
assert.DeepEqual(t, bakeArgs(bake, "metadata.json", api.BuildOptions{}), []string{
|
||||
"bake", "--file", "-", "--progress", "rawjson", "--metadata-file", "metadata.json",
|
||||
"--allow", "fs.read=/cache/in", "--allow", "fs.write=/cache/out",
|
||||
})
|
||||
}
|
||||
|
||||
func TestLocalBuildContextPaths(t *testing.T) {
|
||||
digest := "sha256:" + strings.Repeat("a", 64)
|
||||
for _, tc := range []struct {
|
||||
context string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
context: "oci-layout:///layouts/image",
|
||||
want: []string{"/layouts/image"},
|
||||
},
|
||||
{
|
||||
context: "oci-layout:///layouts/image:latest",
|
||||
want: []string{"/layouts/image"},
|
||||
},
|
||||
{
|
||||
context: "oci-layout:///layouts/image@" + digest,
|
||||
want: []string{"/layouts/image"},
|
||||
},
|
||||
{
|
||||
context: "oci-layout:///layouts/image:v1@" + digest,
|
||||
want: []string{"/layouts/image"},
|
||||
},
|
||||
{
|
||||
context: "oci-layout:///layouts/@/image",
|
||||
want: []string{"/layouts/@/image"},
|
||||
},
|
||||
{
|
||||
context: `oci-layout://C:\layouts\image`,
|
||||
want: []string{`C:\layouts\image`},
|
||||
},
|
||||
{
|
||||
context: "oci-layout://C:/layouts/image:v1@" + digest,
|
||||
want: []string{"C:/layouts/image"},
|
||||
},
|
||||
{
|
||||
context: "cwd://../local",
|
||||
want: []string{"../local"},
|
||||
},
|
||||
{
|
||||
context: "service:base",
|
||||
},
|
||||
{
|
||||
context: "target:base",
|
||||
},
|
||||
{
|
||||
context: "docker-image:alpine:3.23",
|
||||
},
|
||||
{
|
||||
context: "https://example.com/project.git",
|
||||
},
|
||||
} {
|
||||
t.Run(tc.context, func(t *testing.T) {
|
||||
config := types.BuildConfig{Context: "https://example.com/main.git", AdditionalContexts: map[string]string{"base": tc.context}}
|
||||
assert.DeepEqual(t, localBuildPaths(&types.Project{}, config), tc.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBakeTargetNames(t *testing.T) {
|
||||
project := &types.Project{
|
||||
Services: types.Services{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue