-
Notifications
You must be signed in to change notification settings - Fork 43
/
users_stage.go
81 lines (69 loc) · 2.07 KB
/
users_stage.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
package osbuild
import (
"github.com/osbuild/images/pkg/crypt"
"github.com/osbuild/images/pkg/customizations/users"
)
type UsersStageOptions struct {
Users map[string]UsersStageOptionsUser `json:"users"`
}
func (UsersStageOptions) isStageOptions() {}
type UsersStageOptionsUser struct {
UID *int `json:"uid,omitempty"`
GID *int `json:"gid,omitempty"`
Groups []string `json:"groups,omitempty"`
Description *string `json:"description,omitempty"`
Home *string `json:"home,omitempty"`
Shell *string `json:"shell,omitempty"`
Password *string `json:"password,omitempty"`
Key *string `json:"key,omitempty"`
ExpireDate *int `json:"expiredate,omitempty"`
}
func NewUsersStage(options *UsersStageOptions) *Stage {
return &Stage{
Type: "org.osbuild.users",
Options: options,
}
}
func NewUsersStageOptions(userCustomizations []users.User, omitKey bool) (*UsersStageOptions, error) {
if len(userCustomizations) == 0 {
return nil, nil
}
users := make(map[string]UsersStageOptionsUser, len(userCustomizations))
for _, uc := range userCustomizations {
// Don't hash empty passwords, set to nil to lock account
if uc.Password != nil && len(*uc.Password) == 0 {
uc.Password = nil
}
// Hash non-empty un-hashed passwords
if uc.Password != nil && !crypt.PasswordIsCrypted(*uc.Password) {
cryptedPassword, err := crypt.CryptSHA512(*uc.Password)
if err != nil {
return nil, err
}
uc.Password = &cryptedPassword
}
user := UsersStageOptionsUser{
UID: uc.UID,
GID: uc.GID,
Groups: uc.Groups,
Description: uc.Description,
Home: uc.Home,
Shell: uc.Shell,
Password: uc.Password,
Key: nil,
ExpireDate: uc.ExpireDate,
}
if !omitKey {
user.Key = uc.Key
}
users[uc.Name] = user
}
return &UsersStageOptions{Users: users}, nil
}
func GenUsersStage(users []users.User, omitKey bool) (*Stage, error) {
options, err := NewUsersStageOptions(users, omitKey)
if err != nil {
return nil, err
}
return NewUsersStage(options), nil
}