This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
spec.go
152 lines (140 loc) · 3.88 KB
/
spec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package hcsv2
import (
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/opencontainers/runc/libcontainer/user"
oci "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
// getNetworkNamespaceID returns the `ToLower` of
// `spec.Windows.Network.NetworkNamespace` or `""`.
func getNetworkNamespaceID(spec *oci.Spec) string {
if spec.Windows != nil &&
spec.Windows.Network != nil {
return strings.ToLower(spec.Windows.Network.NetworkNamespace)
}
return ""
}
// isRootReadonly returns `true` if the spec specifies the rootfs is readonly.
func isRootReadonly(spec *oci.Spec) bool {
if spec.Root != nil {
return spec.Root.Readonly
}
return false
}
// isInMounts returns `true` if `target` matches a `Destination` in any of
// `mounts`.
func isInMounts(target string, mounts []oci.Mount) bool {
for _, m := range mounts {
if m.Destination == target {
return true
}
}
return false
}
func setProcess(spec *oci.Spec) {
if spec.Process == nil {
spec.Process = &oci.Process{}
}
}
// setUserStr sets `spec.Process` to the valid `userstr` based on the OCI Image Spec
// v1.0.0 `userstr`.
//
// Valid values are: user, uid, user:group, uid:gid, uid:group, user:gid.
// If uid is provided instead of the username then that value is not checked against the
// /etc/passwd file to verify if the user with given uid actually exists.
func setUserStr(spec *oci.Spec, userstr string) error {
setProcess(spec)
parts := strings.Split(userstr, ":")
switch len(parts) {
case 1:
v, err := strconv.Atoi(parts[0])
if err != nil {
// evaluate username to uid/gid
return setUsername(spec, userstr)
}
return setUserID(spec, int(v))
case 2:
var (
username, groupname string
uid, gid int
)
v, err := strconv.Atoi(parts[0])
if err != nil {
username = parts[0]
} else {
uid = int(v)
}
v, err = strconv.Atoi(parts[1])
if err != nil {
groupname = parts[1]
} else {
gid = int(v)
}
if username != "" {
u, err := getUser(spec, func(u user.User) bool {
return u.Name == username
})
if err != nil {
return errors.Wrapf(err, "failed to find user by username: %s", username)
}
uid = u.Uid
}
if groupname != "" {
g, err := getGroup(spec, func(g user.Group) bool {
return g.Name == groupname
})
if err != nil {
return errors.Wrapf(err, "failed to find group by groupname: %s", groupname)
}
gid = g.Gid
}
spec.Process.User.UID, spec.Process.User.GID = uint32(uid), uint32(gid)
return nil
default:
return fmt.Errorf("invalid userstr: '%s'", userstr)
}
}
func setUsername(spec *oci.Spec, username string) error {
u, err := getUser(spec, func(u user.User) bool {
return u.Name == username
})
if err != nil {
return errors.Wrapf(err, "failed to find user by username: %s", username)
}
spec.Process.User.UID, spec.Process.User.GID = uint32(u.Uid), uint32(u.Gid)
return nil
}
func setUserID(spec *oci.Spec, uid int) error {
u, err := getUser(spec, func(u user.User) bool {
return u.Uid == uid
})
if err != nil {
spec.Process.User.UID, spec.Process.User.GID = uint32(uid), 0
return nil
}
spec.Process.User.UID, spec.Process.User.GID = uint32(u.Uid), uint32(u.Gid)
return nil
}
func getUser(spec *oci.Spec, filter func(user.User) bool) (user.User, error) {
users, err := user.ParsePasswdFileFilter(filepath.Join(spec.Root.Path, "/etc/passwd"), filter)
if err != nil {
return user.User{}, err
}
if len(users) != 1 {
return user.User{}, errors.Errorf("expected exactly 1 user matched '%d'", len(users))
}
return users[0], nil
}
func getGroup(spec *oci.Spec, filter func(user.Group) bool) (user.Group, error) {
groups, err := user.ParseGroupFileFilter(filepath.Join(spec.Root.Path, "/etc/group"), filter)
if err != nil {
return user.Group{}, err
}
if len(groups) != 1 {
return user.Group{}, errors.Errorf("expected exactly 1 group matched '%d'", len(groups))
}
return groups[0], nil
}