/
security_context.go
64 lines (58 loc) · 1.65 KB
/
security_context.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
package provider
import (
"fmt"
"os/user"
"strconv"
corev1 "k8s.io/api/core/v1"
)
// uidGidFromSecurityContext returns the uid and gid (as a string) from the SecurityContext.
// If windowsOptions are set, the uid and gid *names* found there are returned. If these users
// can not be found on the system an error is returned. Every user must have a primary group, so
// either uid and gid or both empty or both set.
//
// If the uid is found, but gid is not, the primary group for uid is searched and returned.
// If no securityContext is found this returns the empty strings for uid and gid.
func uidGidFromSecurityContext(pod *corev1.Pod, maproot int) (uid, gid string, err error) {
if pod.Spec.SecurityContext == nil {
return "", "", nil
}
u := &user.User{}
s := pod.Spec.SecurityContext
if s.RunAsUser != nil {
uid = strconv.FormatInt(*s.RunAsUser, 10)
u, err = user.LookupId(uid)
if err != nil {
return "", "", err
}
}
if s.RunAsGroup != nil {
gid = strconv.FormatInt(*s.RunAsGroup, 10)
}
if s.WindowsOptions != nil {
if s.WindowsOptions.RunAsUserName != nil {
uid = *s.WindowsOptions.RunAsUserName
u, err = user.Lookup(uid)
if err != nil {
return "", "", err
}
uid = u.Uid
}
}
// if uid is set, but gid isn't, return the primary group for uid.
if uid != "" && gid == "" {
if u != nil {
gid = u.Gid
}
}
// Check if maproot is set and convert.
if uid == "0" && maproot > 0 {
mapuid := strconv.FormatInt(int64(maproot), 10)
u, err = user.LookupId(mapuid)
if err != nil {
return "", "", fmt.Errorf("root override UID %q, not found: %s", mapuid, err)
}
uid = u.Uid
gid = u.Gid
}
return uid, gid, nil
}