github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/builder/dockerfile/internals_windows.go (about) 1 package dockerfile // import "github.com/Prakhar-Agarwal-byte/moby/builder/dockerfile" 2 3 import ( 4 "bytes" 5 "context" 6 "os" 7 "path/filepath" 8 "strings" 9 10 "github.com/containerd/containerd/platforms" 11 "github.com/Prakhar-Agarwal-byte/moby/api/types/container" 12 "github.com/Prakhar-Agarwal-byte/moby/api/types/mount" 13 "github.com/Prakhar-Agarwal-byte/moby/pkg/idtools" 14 "github.com/Prakhar-Agarwal-byte/moby/pkg/jsonmessage" 15 "golang.org/x/sys/windows" 16 ) 17 18 func parseChownFlag(ctx context.Context, builder *Builder, state *dispatchState, chown, ctrRootPath string, identityMapping idtools.IdentityMapping) (idtools.Identity, error) { 19 if builder.options.Platform == "windows" { 20 return getAccountIdentity(ctx, builder, chown, ctrRootPath, state) 21 } 22 23 return identityMapping.RootPair(), nil 24 } 25 26 func getAccountIdentity(ctx context.Context, builder *Builder, accountName string, ctrRootPath string, state *dispatchState) (idtools.Identity, error) { 27 // If this is potentially a string SID then attempt to convert it to verify 28 // this, otherwise continue looking for the account. 29 if strings.HasPrefix(accountName, "S-") || strings.HasPrefix(accountName, "s-") { 30 sid, err := windows.StringToSid(accountName) 31 32 if err == nil { 33 return idtools.Identity{SID: sid.String()}, nil 34 } 35 } 36 37 // Attempt to obtain the SID using the name. 38 sid, _, accType, err := windows.LookupSID("", accountName) 39 40 // If this is a SID that is built-in and hence the same across all systems then use that. 41 if err == nil && (accType == windows.SidTypeAlias || accType == windows.SidTypeWellKnownGroup) { 42 return idtools.Identity{SID: sid.String()}, nil 43 } 44 45 // Check if the account name is one unique to containers. 46 if strings.EqualFold(accountName, "ContainerAdministrator") { 47 return idtools.Identity{SID: idtools.ContainerAdministratorSidString}, nil 48 } else if strings.EqualFold(accountName, "ContainerUser") { 49 return idtools.Identity{SID: idtools.ContainerUserSidString}, nil 50 } 51 52 // All other lookups failed, so therefore determine if the account in 53 // question exists in the container and if so, obtain its SID. 54 return lookupNTAccount(ctx, builder, accountName, state) 55 } 56 57 func lookupNTAccount(ctx context.Context, builder *Builder, accountName string, state *dispatchState) (idtools.Identity, error) { 58 source, _ := filepath.Split(os.Args[0]) 59 60 target := "C:\\Docker" 61 targetExecutable := target + "\\containerutility.exe" 62 63 optionsPlatform, err := platforms.Parse(builder.options.Platform) 64 if err != nil { 65 return idtools.Identity{}, err 66 } 67 68 runConfig := copyRunConfig(state.runConfig, 69 withCmdCommentString("internal run to obtain NT account information.", optionsPlatform.OS)) 70 71 runConfig.Cmd = []string{targetExecutable, "getaccountsid", accountName} 72 73 hostConfig := &container.HostConfig{ 74 Mounts: []mount.Mount{ 75 { 76 Type: mount.TypeBind, 77 Source: source, 78 Target: target, 79 ReadOnly: true, 80 }, 81 }, 82 } 83 84 container, err := builder.containerManager.Create(ctx, runConfig, hostConfig) 85 if err != nil { 86 return idtools.Identity{}, err 87 } 88 89 stdout := new(bytes.Buffer) 90 stderr := new(bytes.Buffer) 91 92 if err := builder.containerManager.Run(ctx, container.ID, stdout, stderr); err != nil { 93 if err, ok := err.(*statusCodeError); ok { 94 return idtools.Identity{}, &jsonmessage.JSONError{ 95 Message: stderr.String(), 96 Code: err.StatusCode(), 97 } 98 } 99 return idtools.Identity{}, err 100 } 101 102 accountSid := stdout.String() 103 104 return idtools.Identity{SID: accountSid}, nil 105 }