Skip to content

Commit ca32014

Browse files
committed
migrate libcontainer/user to github.com/moby/sys/user
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 1614cab commit ca32014

File tree

12 files changed

+434
-532
lines changed

12 files changed

+434
-532
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/docker/go-units v0.5.0
1212
github.com/godbus/dbus/v5 v5.1.0
1313
github.com/moby/sys/mountinfo v0.6.2
14+
github.com/moby/sys/user v0.1.0
1415
github.com/mrunalp/fileutils v0.5.0
1516
github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4
1617
github.com/opencontainers/selinux v1.11.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
2828
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2929
github.com/moby/sys/mountinfo v0.6.2 h1:BzJjoreD5BMFNmD9Rus6gdd1pLuecOFPt8wC+Vygl78=
3030
github.com/moby/sys/mountinfo v0.6.2/go.mod h1:IJb6JQeOklcdMU9F5xQ8ZALD+CUr5VlGpwtX+VE0rpI=
31+
github.com/moby/sys/user v0.1.0 h1:WmZ93f5Ux6het5iituh9x2zAG7NFY9Aqi49jjE1PaQg=
32+
github.com/moby/sys/user v0.1.0/go.mod h1:fKJhFOnsCN6xZ5gSfbM6zaHGgDJMrqt9/reuj4T7MmU=
3133
github.com/mrunalp/fileutils v0.5.0 h1:NKzVxiH7eSk+OQ4M+ZYW1K6h27RUV3MI6NUTsHhU6Z4=
3234
github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
3335
github.com/opencontainers/runtime-spec v1.1.1-0.20230823135140-4fec88fd00a4 h1:EctkgBjZ1y4q+sibyuuIgiKpa0QSd2elFtSSdNvBVow=

libcontainer/init_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414

1515
"github.com/containerd/console"
16+
"github.com/moby/sys/user"
1617
"github.com/opencontainers/runtime-spec/specs-go"
1718
"github.com/sirupsen/logrus"
1819
"github.com/vishvananda/netlink"
@@ -22,7 +23,6 @@ import (
2223
"github.com/opencontainers/runc/libcontainer/cgroups"
2324
"github.com/opencontainers/runc/libcontainer/configs"
2425
"github.com/opencontainers/runc/libcontainer/system"
25-
"github.com/opencontainers/runc/libcontainer/user"
2626
"github.com/opencontainers/runc/libcontainer/utils"
2727
)
2828

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package user
2+
3+
import (
4+
"io"
5+
6+
"github.com/moby/sys/user"
7+
)
8+
9+
// LookupUser looks up a user by their username in /etc/passwd. If the user
10+
// cannot be found (or there is no /etc/passwd file on the filesystem), then
11+
// LookupUser returns an error.
12+
func LookupUser(username string) (user.User, error) {
13+
return user.LookupUser(username)
14+
}
15+
16+
// LookupUid looks up a user by their user id in /etc/passwd. If the user cannot
17+
// be found (or there is no /etc/passwd file on the filesystem), then LookupId
18+
// returns an error.
19+
func LookupUid(uid int) (user.User, error) { //nolint:revive // ignore var-naming: func LookupUid should be LookupUID
20+
return user.LookupUid(uid)
21+
}
22+
23+
// LookupGroup looks up a group by its name in /etc/group. If the group cannot
24+
// be found (or there is no /etc/group file on the filesystem), then LookupGroup
25+
// returns an error.
26+
func LookupGroup(groupname string) (user.Group, error) {
27+
return user.LookupGroup(groupname)
28+
}
29+
30+
// LookupGid looks up a group by its group id in /etc/group. If the group cannot
31+
// be found (or there is no /etc/group file on the filesystem), then LookupGid
32+
// returns an error.
33+
func LookupGid(gid int) (user.Group, error) {
34+
return user.LookupGid(gid)
35+
}
36+
37+
func GetPasswdPath() (string, error) {
38+
return user.GetPasswdPath()
39+
}
40+
41+
func GetPasswd() (io.ReadCloser, error) {
42+
return user.GetPasswd()
43+
}
44+
45+
func GetGroupPath() (string, error) {
46+
return user.GetGroupPath()
47+
}
48+
49+
func GetGroup() (io.ReadCloser, error) {
50+
return user.GetGroup()
51+
}
52+
53+
// CurrentUser looks up the current user by their user id in /etc/passwd. If the
54+
// user cannot be found (or there is no /etc/passwd file on the filesystem),
55+
// then CurrentUser returns an error.
56+
func CurrentUser() (user.User, error) {
57+
return user.CurrentUser()
58+
}
59+
60+
// CurrentGroup looks up the current user's group by their primary group id's
61+
// entry in /etc/passwd. If the group cannot be found (or there is no
62+
// /etc/group file on the filesystem), then CurrentGroup returns an error.
63+
func CurrentGroup() (user.Group, error) {
64+
return user.CurrentGroup()
65+
}
66+
67+
func CurrentUserSubUIDs() ([]user.SubID, error) {
68+
return user.CurrentUserSubUIDs()
69+
}
70+
71+
func CurrentUserSubGIDs() ([]user.SubID, error) {
72+
return user.CurrentUserSubGIDs()
73+
}
74+
75+
func CurrentProcessUIDMap() ([]user.IDMap, error) {
76+
return user.CurrentProcessUIDMap()
77+
}
78+
79+
func CurrentProcessGIDMap() ([]user.IDMap, error) {
80+
return user.CurrentProcessGIDMap()
81+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package user
2+
3+
import (
4+
"io"
5+
6+
"github.com/moby/sys/user"
7+
)
8+
9+
var (
10+
// ErrNoPasswdEntries is returned if no matching entries were found in /etc/group.
11+
ErrNoPasswdEntries = user.ErrNoPasswdEntries
12+
// ErrNoGroupEntries is returned if no matching entries were found in /etc/passwd.
13+
ErrNoGroupEntries = user.ErrNoGroupEntries
14+
// ErrRange is returned if a UID or GID is outside of the valid range.
15+
ErrRange = user.ErrRange
16+
)
17+
18+
type (
19+
User = user.User
20+
21+
Group = user.Group
22+
23+
// SubID represents an entry in /etc/sub{u,g}id.
24+
SubID = user.SubID
25+
26+
// IDMap represents an entry in /proc/PID/{u,g}id_map.
27+
IDMap = user.IDMap
28+
29+
ExecUser = user.ExecUser
30+
)
31+
32+
func ParsePasswdFile(path string) ([]user.User, error) {
33+
return user.ParsePasswdFile(path)
34+
}
35+
36+
func ParsePasswd(passwd io.Reader) ([]user.User, error) {
37+
return user.ParsePasswd(passwd)
38+
}
39+
40+
func ParsePasswdFileFilter(path string, filter func(user.User) bool) ([]user.User, error) {
41+
return user.ParsePasswdFileFilter(path, filter)
42+
}
43+
44+
func ParsePasswdFilter(r io.Reader, filter func(user.User) bool) ([]user.User, error) {
45+
return user.ParsePasswdFilter(r, filter)
46+
}
47+
48+
func ParseGroupFile(path string) ([]user.Group, error) {
49+
return user.ParseGroupFile(path)
50+
}
51+
52+
func ParseGroup(group io.Reader) ([]user.Group, error) {
53+
return user.ParseGroup(group)
54+
}
55+
56+
func ParseGroupFileFilter(path string, filter func(user.Group) bool) ([]user.Group, error) {
57+
return user.ParseGroupFileFilter(path, filter)
58+
}
59+
60+
func ParseGroupFilter(r io.Reader, filter func(user.Group) bool) ([]user.Group, error) {
61+
return user.ParseGroupFilter(r, filter)
62+
}
63+
64+
// GetExecUserPath is a wrapper for GetExecUser. It reads data from each of the
65+
// given file paths and uses that data as the arguments to GetExecUser. If the
66+
// files cannot be opened for any reason, the error is ignored and a nil
67+
// io.Reader is passed instead.
68+
func GetExecUserPath(userSpec string, defaults *user.ExecUser, passwdPath, groupPath string) (*user.ExecUser, error) {
69+
return user.GetExecUserPath(userSpec, defaults, passwdPath, groupPath)
70+
}
71+
72+
// GetExecUser parses a user specification string (using the passwd and group
73+
// readers as sources for /etc/passwd and /etc/group data, respectively). In
74+
// the case of blank fields or missing data from the sources, the values in
75+
// defaults is used.
76+
//
77+
// GetExecUser will return an error if a user or group literal could not be
78+
// found in any entry in passwd and group respectively.
79+
//
80+
// Examples of valid user specifications are:
81+
// - ""
82+
// - "user"
83+
// - "uid"
84+
// - "user:group"
85+
// - "uid:gid
86+
// - "user:gid"
87+
// - "uid:group"
88+
//
89+
// It should be noted that if you specify a numeric user or group id, they will
90+
// not be evaluated as usernames (only the metadata will be filled). So attempting
91+
// to parse a user with user.Name = "1337" will produce the user with a UID of
92+
// 1337.
93+
func GetExecUser(userSpec string, defaults *user.ExecUser, passwd, group io.Reader) (*user.ExecUser, error) {
94+
return user.GetExecUser(userSpec, defaults, passwd, group)
95+
}
96+
97+
// GetAdditionalGroups looks up a list of groups by name or group id
98+
// against the given /etc/group formatted data. If a group name cannot
99+
// be found, an error will be returned. If a group id cannot be found,
100+
// or the given group data is nil, the id will be returned as-is
101+
// provided it is in the legal range.
102+
func GetAdditionalGroups(additionalGroups []string, group io.Reader) ([]int, error) {
103+
return user.GetAdditionalGroups(additionalGroups, group)
104+
}
105+
106+
// GetAdditionalGroupsPath is a wrapper around GetAdditionalGroups
107+
// that opens the groupPath given and gives it as an argument to
108+
// GetAdditionalGroups.
109+
func GetAdditionalGroupsPath(additionalGroups []string, groupPath string) ([]int, error) {
110+
return user.GetAdditionalGroupsPath(additionalGroups, groupPath)
111+
}
112+
113+
func ParseSubIDFile(path string) ([]user.SubID, error) {
114+
return user.ParseSubIDFile(path)
115+
}
116+
117+
func ParseSubID(subid io.Reader) ([]user.SubID, error) {
118+
return user.ParseSubID(subid)
119+
}
120+
121+
func ParseSubIDFileFilter(path string, filter func(user.SubID) bool) ([]user.SubID, error) {
122+
return user.ParseSubIDFileFilter(path, filter)
123+
}
124+
125+
func ParseSubIDFilter(r io.Reader, filter func(user.SubID) bool) ([]user.SubID, error) {
126+
return user.ParseSubIDFilter(r, filter)
127+
}
128+
129+
func ParseIDMapFile(path string) ([]user.IDMap, error) {
130+
return user.ParseIDMapFile(path)
131+
}
132+
133+
func ParseIDMap(r io.Reader) ([]user.IDMap, error) {
134+
return user.ParseIDMap(r)
135+
}
136+
137+
func ParseIDMapFileFilter(path string, filter func(user.IDMap) bool) ([]user.IDMap, error) {
138+
return user.ParseIDMapFileFilter(path, filter)
139+
}
140+
141+
func ParseIDMapFilter(r io.Reader, filter func(user.IDMap) bool) ([]user.IDMap, error) {
142+
return user.ParseIDMapFilter(r, filter)
143+
}

0 commit comments

Comments
 (0)