forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
90 lines (82 loc) · 2.58 KB
/
user.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
package resource
import (
"fmt"
"github.com/aelsabbahy/goss/system"
"github.com/aelsabbahy/goss/util"
)
type User struct {
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Username string `json:"-" yaml:"-"`
Exists matcher `json:"exists" yaml:"exists"`
UID matcher `json:"uid,omitempty" yaml:"uid,omitempty"`
GID matcher `json:"gid,omitempty" yaml:"gid,omitempty"`
Groups matcher `json:"groups,omitempty" yaml:"groups,omitempty"`
Home matcher `json:"home,omitempty" yaml:"home,omitempty"`
Shell matcher `json:"shell,omitempty" yaml:"shell,omitempty"`
}
func (u *User) ID() string { return u.Username }
func (u *User) SetID(id string) { u.Username = id }
func (u *User) GetTitle() string { return u.Title }
func (u *User) GetMeta() meta { return u.Meta }
func (u *User) Validate(sys *system.System) []TestResult {
skip := false
sysuser := sys.NewUser(u.Username, sys, util.Config{})
var results []TestResult
results = append(results, ValidateValue(u, "exists", u.Exists, sysuser.Exists, skip))
if shouldSkip(results) {
skip = true
}
if u.UID != nil {
uUID := deprecateAtoI(u.UID, fmt.Sprintf("%s: user.uid", u.Username))
results = append(results, ValidateValue(u, "uid", uUID, sysuser.UID, skip))
}
if u.GID != nil {
uGID := deprecateAtoI(u.GID, fmt.Sprintf("%s: user.gid", u.Username))
results = append(results, ValidateValue(u, "gid", uGID, sysuser.GID, skip))
}
if u.Home != nil {
results = append(results, ValidateValue(u, "home", u.Home, sysuser.Home, skip))
}
if u.Groups != nil {
results = append(results, ValidateValue(u, "groups", u.Groups, sysuser.Groups, skip))
}
if u.Shell != nil {
results = append(results, ValidateValue(u, "shell", u.Shell, sysuser.Shell, skip))
}
return results
}
func NewUser(sysUser system.User, config util.Config) (*User, error) {
username := sysUser.Username()
exists, _ := sysUser.Exists()
u := &User{
Username: username,
Exists: exists,
}
if !contains(config.IgnoreList, "uid") {
if uid, err := sysUser.UID(); err == nil {
u.UID = uid
}
}
if !contains(config.IgnoreList, "gid") {
if gid, err := sysUser.GID(); err == nil {
u.GID = gid
}
}
if !contains(config.IgnoreList, "groups") {
if groups, err := sysUser.Groups(); err == nil {
u.Groups = groups
}
}
if !contains(config.IgnoreList, "home") {
if home, err := sysUser.Home(); err == nil {
u.Home = home
}
}
if !contains(config.IgnoreList, "shell") {
if shell, err := sysUser.Shell(); err == nil {
u.Shell = shell
}
}
return u, nil
}