Add NoSameOwner option when unpacking tars
When unpacking a TAR archive, containerd preserves file's owner: https://github.com/containerd/containerd/blob/main/archive/tar.go#L384 In some cases this behavior is not desired. In current implementation we avoid `Lchown` on Windows. Another case when this should be skipped is when using native snapshotter on darwin and running as non-root user. This PR extracts a generic option - `WithNoSameOwner` (same as `tar --no-same-owner`) to skip `Lchown` when its not required. Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
parent
f4a905109b
commit
0f51aa874d
@ -43,7 +43,7 @@ func TestPrefixHeaderReadable(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
_, err = Apply(context.Background(), tmpDir, r)
|
_, err = Apply(context.Background(), tmpDir, r, WithNoSameOwner())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
@ -290,7 +289,7 @@ func applyNaive(ctx context.Context, root string, r io.Reader, options ApplyOpti
|
|||||||
srcData := io.Reader(tr)
|
srcData := io.Reader(tr)
|
||||||
srcHdr := hdr
|
srcHdr := hdr
|
||||||
|
|
||||||
if err := createTarFile(ctx, path, root, srcHdr, srcData); err != nil {
|
if err := createTarFile(ctx, path, root, srcHdr, srcData, options.NoSameOwner); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -315,7 +314,7 @@ func applyNaive(ctx context.Context, root string, r io.Reader, options ApplyOpti
|
|||||||
return size, nil
|
return size, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTarFile(ctx context.Context, path, extractDir string, hdr *tar.Header, reader io.Reader) error {
|
func createTarFile(ctx context.Context, path, extractDir string, hdr *tar.Header, reader io.Reader, noSameOwner bool) error {
|
||||||
// hdr.Mode is in linux format, which we can use for syscalls,
|
// hdr.Mode is in linux format, which we can use for syscalls,
|
||||||
// but for os.Foo() calls we need the mode converted to os.FileMode,
|
// but for os.Foo() calls we need the mode converted to os.FileMode,
|
||||||
// so use hdrInfo.Mode() (they differ for e.g. setuid bits)
|
// so use hdrInfo.Mode() (they differ for e.g. setuid bits)
|
||||||
@ -380,8 +379,7 @@ func createTarFile(ctx context.Context, path, extractDir string, hdr *tar.Header
|
|||||||
return fmt.Errorf("unhandled tar header type %d", hdr.Typeflag)
|
return fmt.Errorf("unhandled tar header type %d", hdr.Typeflag)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lchown is not supported on Windows.
|
if !noSameOwner {
|
||||||
if runtime.GOOS != "windows" {
|
|
||||||
if err := os.Lchown(path, hdr.Uid, hdr.Gid); err != nil {
|
if err := os.Lchown(path, hdr.Uid, hdr.Gid); err != nil {
|
||||||
err = fmt.Errorf("failed to Lchown %q for UID %d, GID %d: %w", path, hdr.Uid, hdr.Gid, err)
|
err = fmt.Errorf("failed to Lchown %q for UID %d, GID %d: %w", path, hdr.Uid, hdr.Gid, err)
|
||||||
if errors.Is(err, syscall.EINVAL) && userns.RunningInUserNS() {
|
if errors.Is(err, syscall.EINVAL) && userns.RunningInUserNS() {
|
||||||
|
@ -27,6 +27,7 @@ type ApplyOptions struct {
|
|||||||
Filter Filter // Filter tar headers
|
Filter Filter // Filter tar headers
|
||||||
ConvertWhiteout ConvertWhiteout // Convert whiteout files
|
ConvertWhiteout ConvertWhiteout // Convert whiteout files
|
||||||
Parents []string // Parent directories to handle inherited attributes without CoW
|
Parents []string // Parent directories to handle inherited attributes without CoW
|
||||||
|
NoSameOwner bool // NoSameOwner will not attempt to preserve the owner specified in the tar archive.
|
||||||
|
|
||||||
applyFunc func(context.Context, string, io.Reader, ApplyOptions) (int64, error)
|
applyFunc func(context.Context, string, io.Reader, ApplyOptions) (int64, error)
|
||||||
}
|
}
|
||||||
@ -61,6 +62,15 @@ func WithConvertWhiteout(c ConvertWhiteout) ApplyOpt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithNoSameOwner is same as '--no-same-owner` in 'tar' command.
|
||||||
|
// It'll skip attempt to preserve the owner specified in the tar archive.
|
||||||
|
func WithNoSameOwner() ApplyOpt {
|
||||||
|
return func(options *ApplyOptions) error {
|
||||||
|
options.NoSameOwner = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithParents provides parent directories for resolving inherited attributes
|
// WithParents provides parent directories for resolving inherited attributes
|
||||||
// directory from the filesystem.
|
// directory from the filesystem.
|
||||||
// Inherited attributes are searched from first to last, making the first
|
// Inherited attributes are searched from first to last, making the first
|
||||||
|
@ -19,6 +19,7 @@ package apply
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/containerd/containerd/archive"
|
"github.com/containerd/containerd/archive"
|
||||||
"github.com/containerd/containerd/mount"
|
"github.com/containerd/containerd/mount"
|
||||||
@ -28,8 +29,14 @@ func apply(ctx context.Context, mounts []mount.Mount, r io.Reader) error {
|
|||||||
// We currently do not support mounts nor bind mounts on MacOS in the containerd daemon.
|
// We currently do not support mounts nor bind mounts on MacOS in the containerd daemon.
|
||||||
// Using this as an exception to enable native snapshotter and allow further research.
|
// Using this as an exception to enable native snapshotter and allow further research.
|
||||||
if len(mounts) == 1 && mounts[0].Type == "bind" {
|
if len(mounts) == 1 && mounts[0].Type == "bind" {
|
||||||
|
opts := []archive.ApplyOpt{}
|
||||||
|
|
||||||
|
if os.Getuid() != 0 {
|
||||||
|
opts = append(opts, archive.WithNoSameOwner())
|
||||||
|
}
|
||||||
|
|
||||||
path := mounts[0].Source
|
path := mounts[0].Source
|
||||||
_, err := archive.Apply(ctx, path, r)
|
_, err := archive.Apply(ctx, path, r, opts...)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
winio "github.com/Microsoft/go-winio"
|
"github.com/Microsoft/go-winio"
|
||||||
"github.com/containerd/containerd/archive"
|
"github.com/containerd/containerd/archive"
|
||||||
"github.com/containerd/containerd/archive/compression"
|
"github.com/containerd/containerd/archive/compression"
|
||||||
"github.com/containerd/containerd/content"
|
"github.com/containerd/containerd/content"
|
||||||
@ -38,7 +38,7 @@ import (
|
|||||||
"github.com/containerd/containerd/mount"
|
"github.com/containerd/containerd/mount"
|
||||||
"github.com/containerd/containerd/platforms"
|
"github.com/containerd/containerd/platforms"
|
||||||
"github.com/containerd/containerd/plugin"
|
"github.com/containerd/containerd/plugin"
|
||||||
digest "github.com/opencontainers/go-digest"
|
"github.com/opencontainers/go-digest"
|
||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@ -144,7 +144,13 @@ func (s windowsDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts
|
|||||||
return emptyDesc, err
|
return emptyDesc, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := archive.Apply(ctx, layer, rc, archive.WithParents(parentLayerPaths), archive.AsWindowsContainerLayer()); err != nil {
|
archiveOpts := []archive.ApplyOpt{
|
||||||
|
archive.WithParents(parentLayerPaths),
|
||||||
|
archive.AsWindowsContainerLayer(),
|
||||||
|
archive.WithNoSameOwner(), // Lchown is not supported on Windows
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := archive.Apply(ctx, layer, rc, archiveOpts...); err != nil {
|
||||||
return emptyDesc, err
|
return emptyDesc, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
install.go
13
install.go
@ -70,7 +70,8 @@ func (c *Client) Install(ctx context.Context, image Image, opts ...InstallOpts)
|
|||||||
ra.Close()
|
ra.Close()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := archive.Apply(ctx, path, r, archive.WithFilter(func(hdr *tar.Header) (bool, error) {
|
|
||||||
|
filter := archive.WithFilter(func(hdr *tar.Header) (bool, error) {
|
||||||
d := filepath.Dir(hdr.Name)
|
d := filepath.Dir(hdr.Name)
|
||||||
result := d == binDir
|
result := d == binDir
|
||||||
|
|
||||||
@ -87,7 +88,15 @@ func (c *Client) Install(ctx context.Context, image Image, opts ...InstallOpts)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
})); err != nil {
|
})
|
||||||
|
|
||||||
|
opts := []archive.ApplyOpt{filter}
|
||||||
|
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
opts = append(opts, archive.WithNoSameOwner())
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := archive.Apply(ctx, path, r, opts...); err != nil {
|
||||||
r.Close()
|
r.Close()
|
||||||
ra.Close()
|
ra.Close()
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user