Skip to content

Commit 1d9b158

Browse files
authored
Merge pull request #4017 from thaJeztah/migrate_libcontainer_user
Deprecate libcontainer/user, and migrate to github.com/moby/sys/user
2 parents 1614cab + d9ea71b commit 1d9b158

File tree

12 files changed

+437
-532
lines changed

12 files changed

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

0 commit comments

Comments
 (0)