Skip to content

Commit

Permalink
Implement readUser on Windows
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
  • Loading branch information
gabriel-samfira committed Aug 30, 2023
1 parent 05eb728 commit aca323e
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 63 deletions.
48 changes: 5 additions & 43 deletions solver/llbsolver/file/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/containerd/continuity/fs"
"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/solver/llbsolver/ops/fileoptypes"
"github.com/moby/buildkit/solver/pb"
Expand All @@ -27,46 +28,6 @@ func timestampToTime(ts int64) *time.Time {
return &tm
}

func mapUserToChowner(user *copy.User, idmap *idtools.IdentityMapping) (copy.Chowner, error) {
if user == nil {
return func(old *copy.User) (*copy.User, error) {
if old == nil {
if idmap == nil {
return nil, nil
}
old = &copy.User{} // root
// non-nil old is already mapped
if idmap != nil {
identity, err := idmap.ToHost(idtools.Identity{
UID: old.UID,
GID: old.GID,
})
if err != nil {
return nil, err
}
return &copy.User{UID: identity.UID, GID: identity.GID}, nil
}
}
return old, nil
}, nil
}
u := *user
if idmap != nil {
identity, err := idmap.ToHost(idtools.Identity{
UID: user.UID,
GID: user.GID,
})
if err != nil {
return nil, err
}
u.UID = identity.UID
u.GID = identity.GID
}
return func(*copy.User) (*copy.User, error) {
return &u, nil
}, nil
}

func mkdir(ctx context.Context, d string, action pb.FileActionMkDir, user *copy.User, idmap *idtools.IdentityMapping) error {
p, err := fs.RootPath(d, action.Path)
if err != nil {
Expand Down Expand Up @@ -251,6 +212,7 @@ func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *
}

type Backend struct {
Executor executor.Executor
}

func (fb *Backend) Mkdir(ctx context.Context, m, user, group fileoptypes.Mount, action pb.FileActionMkDir) error {
Expand All @@ -266,7 +228,7 @@ func (fb *Backend) Mkdir(ctx context.Context, m, user, group fileoptypes.Mount,
}
defer lm.Unmount()

u, err := readUser(action.Owner, user, group)
u, err := readUser(action.Owner, user, group, fb.Executor)
if err != nil {
return err
}
Expand All @@ -287,7 +249,7 @@ func (fb *Backend) Mkfile(ctx context.Context, m, user, group fileoptypes.Mount,
}
defer lm.Unmount()

u, err := readUser(action.Owner, user, group)
u, err := readUser(action.Owner, user, group, fb.Executor)
if err != nil {
return err
}
Expand Down Expand Up @@ -335,7 +297,7 @@ func (fb *Backend) Copy(ctx context.Context, m1, m2, user, group fileoptypes.Mou
}
defer lm2.Unmount()

u, err := readUser(action.Owner, user, group)
u, err := readUser(action.Owner, user, group, fb.Executor)
if err != nil {
return err
}
Expand Down
49 changes: 49 additions & 0 deletions solver/llbsolver/file/backend_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//go:build !windows
// +build !windows

package file

import (
"github.com/docker/docker/pkg/idtools"
copy "github.com/tonistiigi/fsutil/copy"
)

func mapUserToChowner(user *copy.User, idmap *idtools.IdentityMapping) (copy.Chowner, error) {
if user == nil {
return func(old *copy.User) (*copy.User, error) {
if old == nil {
if idmap == nil {
return nil, nil
}
old = &copy.User{} // root
// non-nil old is already mapped
if idmap != nil {
identity, err := idmap.ToHost(idtools.Identity{
UID: old.UID,
GID: old.GID,
})
if err != nil {
return nil, err
}
return &copy.User{UID: identity.UID, GID: identity.GID}, nil
}
}
return old, nil
}, nil
}
u := *user
if idmap != nil {
identity, err := idmap.ToHost(idtools.Identity{
UID: user.UID,
GID: user.GID,
})
if err != nil {
return nil, err
}
u.UID = identity.UID
u.GID = identity.GID
}
return func(*copy.User) (*copy.User, error) {
return &u, nil
}, nil
}
22 changes: 22 additions & 0 deletions solver/llbsolver/file/backend_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package file

import (
"github.com/docker/docker/pkg/idtools"
copy "github.com/tonistiigi/fsutil/copy"
)

func mapUserToChowner(user *copy.User, idmap *idtools.IdentityMapping) (copy.Chowner, error) {
if user == nil || user.SID == "" {
return func(old *copy.User) (*copy.User, error) {
if old == nil || old.SID == "" {
old = &copy.User{
SID: idtools.ContainerAdministratorSidString,
}
}
return old, nil
}, nil
}
return func(*copy.User) (*copy.User, error) {
return user, nil
}, nil
}
3 changes: 2 additions & 1 deletion solver/llbsolver/file/user_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"syscall"

"github.com/containerd/continuity/fs"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/solver/llbsolver/ops/fileoptypes"
"github.com/moby/buildkit/solver/pb"
Expand All @@ -13,7 +14,7 @@ import (
copy "github.com/tonistiigi/fsutil/copy"
)

func readUser(chopt *pb.ChownOpt, mu, mg fileoptypes.Mount) (*copy.User, error) {
func readUser(chopt *pb.ChownOpt, mu, mg fileoptypes.Mount, exec executor.Executor) (*copy.User, error) {
if chopt == nil {
return nil, nil
}
Expand Down
18 changes: 0 additions & 18 deletions solver/llbsolver/file/user_nolinux.go

This file was deleted.

19 changes: 19 additions & 0 deletions solver/llbsolver/file/user_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build !linux && !windows
// +build !linux,!windows

package file

import (
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/solver/llbsolver/ops/fileoptypes"
"github.com/moby/buildkit/solver/pb"
"github.com/pkg/errors"
copy "github.com/tonistiigi/fsutil/copy"
)

func readUser(chopt *pb.ChownOpt, mu, mg fileoptypes.Mount, exec executor.Executor) (*copy.User, error) {
if chopt == nil {
return nil, nil
}
return nil, errors.New("only implemented in linux and windows")
}
45 changes: 45 additions & 0 deletions solver/llbsolver/file/user_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package file

import (
"context"

"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/solver/llbsolver/ops/fileoptypes"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/windows"
"github.com/pkg/errors"
copy "github.com/tonistiigi/fsutil/copy"
)

func readUser(chopt *pb.ChownOpt, mu, mg fileoptypes.Mount, exec executor.Executor) (*copy.User, error) {
if chopt == nil {
return nil, nil
}

if chopt.User != nil {
switch u := chopt.User.User.(type) {
case *pb.UserOpt_ByName:
if mu == nil {
return nil, errors.Errorf("invalid missing user mount")
}
mmu, ok := mu.(*Mount)
if !ok {
return nil, errors.Errorf("invalid mount type %T", mu)
}
rootMounts, release, err := mmu.m.Mount()
if err != nil {
return nil, err
}
defer release()
ident, err := windows.ResolveUsernameToSID(context.Background(), exec, rootMounts, u.ByName.Name)
if err != nil {
return nil, err
}
return &copy.User{SID: ident.SID}, nil
default:
return &copy.User{SID: idtools.ContainerAdministratorSidString}, nil
}
}
return &copy.User{SID: idtools.ContainerAdministratorSidString}, nil
}
2 changes: 1 addition & 1 deletion solver/llbsolver/ops/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (f *fileOp) Exec(ctx context.Context, g session.Group, inputs []solver.Resu
inpRefs = append(inpRefs, workerRef.ImmutableRef)
}

fs := NewFileOpSolver(f.w, &file.Backend{}, f.refManager)
fs := NewFileOpSolver(f.w, &file.Backend{Executor: f.w.Executor()}, f.refManager)
outs, err := fs.Solve(ctx, inpRefs, f.op.Actions, g)
if err != nil {
return nil, err
Expand Down

0 comments on commit aca323e

Please sign in to comment.