Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Add get-user-info utility #3516

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/buildkitd/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"

_ "github.com/moby/buildkit/solver/llbsolver/ops"
_ "github.com/moby/buildkit/util/system/getuserinfo"
"github.com/pkg/errors"
)

Expand Down
47 changes: 47 additions & 0 deletions util/system/getuserinfo/userinfo_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package getuserinfo

import (
"encoding/json"
"fmt"
"os"
"syscall"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use "golang.org/x/sys/windows" instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x/sys is where new stuff gets added. The syscall package only changes when those changes need to support the standard library.

The LookupSID() function from the syscall package is identical to the one in x/sys/windows, and calls into a stable Windows API which, judging by Microsofts track record for backwards compatibility, will probably remain unchanged for many many years.

Unless we want to use an old version of go that does not have this function in the standard library, I think we can use syscall.

If you prefer, I can change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are already using this pkg

that's why I suggested to use it instead but I don't have a strict pref.

Copy link
Collaborator

@TBBle TBBle Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My recollection of a discussion in the go-winio repo was that we were leaning towards preferring x/sys/windows over syscall where possible. I have a PR there for example which replaces all uses of syscall with x/sys/windows versions.

Which I would like to come back to at some point; it was delayed because there was a syscall.Handle in a public API being changed to windows.Handle and they wanted to batch that up some other API breaks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it's settled. Will replace with x/sys/windows ASAP.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, the PR and discussion I mentioned is microsoft/go-winio#197.


"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/reexec"
)

const (
getUserInfoCmd = "get-user-info"
)

func init() {
reexec.Register(getUserInfoCmd, userInfoMain)
}

func userInfoMain() {
if len(os.Args) != 2 {
fmt.Println("Usage: get-user-info usernameOrGroup")
os.Exit(1)
}
username := os.Args[1]
sid, _, _, err := syscall.LookupSID("", username)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sid, _, _, err := syscall.LookupSID("", username)
sid, _, _, err := windows.LookupSID("", username)

if err != nil {
fmt.Println(err)
os.Exit(3)
}

sidAsString, err := sid.String()
if err != nil {
fmt.Println(err)
os.Exit(4)
}
ident := idtools.Identity{
SID: sidAsString,
}
asJson, err := json.Marshal(ident)
if err != nil {
fmt.Println(err)
os.Exit(5)
}
fmt.Fprintf(os.Stdout, "%s", string(asJson))
}