-
Notifications
You must be signed in to change notification settings - Fork 227
/
config_user.go
460 lines (418 loc) · 12.7 KB
/
config_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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package proxmox
import (
"encoding/json"
"errors"
"fmt"
"strings"
"unicode/utf8"
)
const Error_NewUserID string = "no username or realm specified, syntax is \"username@realm\""
// User options for the Proxmox API
type ConfigUser struct {
User UserID `json:"user"`
Comment string `json:"comment,omitempty"`
Email string `json:"email,omitempty"`
Enable bool `json:"enable"`
Expire uint `json:"expire"`
FirstName string `json:"firstname,omitempty"`
Groups *[]GroupName `json:"groups,omitempty"`
Keys string `json:"keys,omitempty"`
LastName string `json:"lastname,omitempty"`
// Password is always empty when getting information from Proxmox
Password UserPassword `json:"-"`
}
func (config ConfigUser) CreateUser(client *Client) (err error) {
err = config.Validate()
if err != nil {
return
}
params := config.mapToApiValues(true)
err = client.Post(params, "/access/users")
if err != nil {
params, _ := json.Marshal(¶ms)
return fmt.Errorf("error creating User: %v, (params: %v)", err, string(params))
}
return
}
func (config ConfigUser) DeleteUser(client *Client) (err error) {
existence, err := CheckUserExistence(config.User, client)
if err != nil {
return
}
if !existence {
return fmt.Errorf("user (%s) could not be deleted, the user does not exist", config.User.String())
}
// Proxmox silently fails a user delete if the users does not exist
return client.Delete("/access/users/" + config.User.String())
}
// Maps the struct to the API values proxmox understands
func (config ConfigUser) mapToApiValues(create bool) (params map[string]interface{}) {
params = map[string]interface{}{
"comment": config.Comment,
"email": config.Email,
"enable": config.Enable,
"expire": config.Expire,
"firstname": config.FirstName,
"groups": GroupName("").arrayToCsv(config.Groups),
"keys": config.Keys,
"lastname": config.LastName,
}
if create {
params["password"] = config.Password
params["userid"] = config.User.String()
}
return
}
func (ConfigUser) mapToArray(params []interface{}) *[]ConfigUser {
users := make([]ConfigUser, len(params))
for i, e := range params {
users[i] = *ConfigUser{}.mapToStruct(e.(map[string]interface{}))
}
return &users
}
// Maps the API values from proxmox to a struct
func (config ConfigUser) mapToStruct(params map[string]interface{}) *ConfigUser {
if _, isSet := params["userid"]; isSet {
config.User = UserID{}.mapToStruct(params["userid"].(string))
}
if _, isSet := params["comment"]; isSet {
config.Comment = params["comment"].(string)
}
if _, isSet := params["email"]; isSet {
config.Email = params["email"].(string)
}
if _, isSet := params["enable"]; isSet {
config.Enable = Itob(int(params["enable"].(float64)))
}
if _, isSet := params["expire"]; isSet {
config.Expire = uint(params["expire"].(float64))
}
if _, isSet := params["firstname"]; isSet {
config.FirstName = params["firstname"].(string)
}
if _, isSet := params["keys"]; isSet {
config.Keys = params["keys"].(string)
}
if _, isSet := params["lastname"]; isSet {
config.LastName = params["lastname"].(string)
}
if _, isSet := params["groups"]; isSet {
config.Groups = GroupName("").mapToArray(params["groups"])
}
return &config
}
// Create or update the user depending on if the user already exists or not.
// "userId" and "password" overwrite what is specified in "*ConfigUser".
func (config *ConfigUser) SetUser(userId UserID, password UserPassword, client *Client) (err error) {
if config != nil {
config.User = userId
config.Password = password
}
userExists, err := CheckUserExistence(userId, client)
if err != nil {
return err
}
if config != nil {
if userExists {
err = config.UpdateUser(client)
if err != nil {
return err
}
} else {
err = config.CreateUser(client)
}
} else {
config = &ConfigUser{
Password: password,
User: userId,
}
if userExists {
if config.Password != "" {
err = config.UpdateUserPassword(client)
}
} else {
err = config.CreateUser(client)
}
}
return
}
func (config *ConfigUser) UpdateUser(client *Client) (err error) {
params := config.mapToApiValues(false)
err = client.Put(params, "/access/users/"+config.User.String())
if err != nil {
params, _ := json.Marshal(¶ms)
return fmt.Errorf("error updating User: %v, (params: %v)", err, string(params))
}
if config.Password != "" {
err = config.UpdateUserPassword(client)
}
return
}
func (config ConfigUser) UpdateUserPassword(client *Client) (err error) {
err = config.Password.Validate()
if err != nil {
return err
}
return client.Put(map[string]interface{}{
"userid": config.User.String(),
"password": config.Password,
}, "/access/password")
}
type ApiToken struct {
TokenId string `json:"tokenid"`
Comment string `json:"comment,omitempty"`
Expire int64 `json:"expire"`
Privsep bool `json:"privsep"`
}
type ApiTokenCreateResult struct {
Info map[string]interface{} `json:"info"`
Value string `json:"value"`
}
type ApiTokenCreateResultWrapper struct {
Data ApiTokenCreateResult `json:"data"`
}
// Maps the API values from proxmox to a struct
func (tokens ApiToken) mapToStruct(params map[string]interface{}) *ApiToken {
if _, isSet := params["tokenid"]; isSet {
tokens.TokenId = params["tokenid"].(string)
}
if _, isSet := params["comment"]; isSet {
tokens.Comment = params["comment"].(string)
}
if _, isSet := params["expire"]; isSet {
tokens.Expire = int64(params["expire"].(float64))
}
if _, isSet := params["privsep"]; isSet {
tokens.Privsep = false
if params["privsep"] == 1 {
tokens.Privsep = true
}
}
return &tokens
}
func (ApiToken) mapToArray(params []interface{}) *[]ApiToken {
tokens := make([]ApiToken, len(params))
for i, e := range params {
tokens[i] = *ApiToken{}.mapToStruct(e.(map[string]interface{}))
}
return &tokens
}
func (config ConfigUser) CreateApiToken(client *Client, token ApiToken) (value string, err error) {
status, err := client.CreateItemReturnStatus(map[string]interface{}{
"comment": token.Comment,
"expire": token.Expire,
"privsep": token.Privsep,
}, "/access/users/"+config.User.String()+"/token/"+token.TokenId)
if err != nil {
return
}
var resultWrapper *ApiTokenCreateResultWrapper
err = json.Unmarshal([]byte(status), &resultWrapper)
value = resultWrapper.Data.Value
return
}
func (config ConfigUser) UpdateApiToken(client *Client, token ApiToken) (err error) {
err = client.Put(map[string]interface{}{
"comment": token.Comment,
"expire": token.Expire,
"privsep": token.Privsep,
}, "/access/users/"+config.User.String()+"/token/"+token.TokenId)
return
}
func (config ConfigUser) ListApiTokens(client *Client) (tokens *[]ApiToken, err error) {
status, err := client.GetItemListInterfaceArray("/access/users/" + config.User.String() + "/token")
if err != nil {
return
}
tokens = ApiToken{}.mapToArray(status)
return
}
func (config ConfigUser) DeleteApiToken(client *Client, token ApiToken) (err error) {
err = client.Delete("/access/users/" + config.User.String() + "/token/" + token.TokenId)
return
}
// Validates all items and sub items in the ConfigUser struct
func (config ConfigUser) Validate() (err error) {
err = config.User.Validate()
if err != nil {
return
}
if config.Groups != nil {
if len(*config.Groups) != 0 {
for _, e := range *config.Groups {
err = e.Validate()
if err != nil {
return
}
}
}
}
return config.Password.Validate()
}
// user config used when only the group group membership needs updating.
type configUserShort struct {
User UserID
Groups *[]GroupName
}
func (config configUserShort) mapToApiValues() map[string]interface{} {
return map[string]interface{}{
"groups": GroupName("").arrayToCsv(config.Groups),
}
}
func (config configUserShort) updateUserMembership(client *Client) (err error) {
err = updateUser(config.User, config.mapToApiValues(), client)
if err != nil {
return fmt.Errorf("error updating User: %v", err)
}
return
}
func (configUserShort) updateUsersMembership(users *[]configUserShort, client *Client) (err error) {
if users == nil {
return
}
for _, e := range *users {
err = e.updateUserMembership(client)
if err != nil {
return
}
}
return
}
type UserID struct {
// TODO create custom type for Name.
// the name only seems to allows some characters, and using the string type would imply that all characters are allowed.
// https://bugzilla.proxmox.com/show_bug.cgi?id=4461
Name string `json:"name"`
// TODO create custom type for Realm.
// the realm only allows some characters, and using the string type would imply that all characters are allowed.
// https://bugzilla.proxmox.com/show_bug.cgi?id=4462
Realm string `json:"realm"`
}
// Map the params to an array of UserID objects
func (UserID) mapToArray(params []interface{}) *[]UserID {
members := ArrayToStringType(params)
UserList := make([]UserID, len(members))
for i := range members {
UserList[i] = UserID{}.mapToStruct(members[i])
}
return &UserList
}
// transforms the username@realm to a UserID object
func (UserID) mapToStruct(userId string) UserID {
user, _ := NewUserID(userId)
return user
}
// Converts the userID to "username@realm"
// Returns an empty string when either the Name or Realm is empty
func (id UserID) String() string {
if id.Name == "" || id.Realm == "" {
return ""
}
return id.Name + "@" + id.Realm
}
// deprecated use String() instead
func (id UserID) ToString() string {
return id.String()
}
// TODO improve when Name and Realm have their own types
func (id UserID) Validate() error {
if id.Name == "" {
return errors.New("no username is specified")
}
if id.Realm == "" {
return errors.New("no realm is specified")
}
return nil
}
// May be empty or should be at least be 5 characters long.
type UserPassword string
func (password UserPassword) Validate() error {
if utf8.RuneCountInString(string(password)) >= 5 || password == "" {
return nil
}
return errors.New("the minimum password length is 5")
}
// Check if the user already exists in proxmox.
func CheckUserExistence(userId UserID, client *Client) (existence bool, err error) {
list, err := listUsersFull(client)
if err != nil {
return
}
// This should be the case where you have an API Token with privilege separation but no permissions attached
if len(list) == 0 {
return false, fmt.Errorf("user %s has valid credentials but cannot retrieve user list, check privilege separation of api token", userId.String())
}
existence = ItemInKeyOfArray(list, "userid", userId.String())
return
}
// List all users that exist in proxmox
// Setting full to TRUE the output wil include group information.
// Depending on the number of existing groups it take substantially longer to parse
func ListUsers(client *Client, full bool) (*[]ConfigUser, error) {
var err error
var userList []interface{}
if full {
userList, err = listUsersFull(client)
} else {
userList, err = listUsersPartial(client)
}
if err != nil {
return nil, err
}
return ConfigUser{}.mapToArray(userList), nil
}
// Returns users without group information
func listUsersPartial(client *Client) ([]interface{}, error) {
return client.GetItemListInterfaceArray("/access/users")
}
// Returns users with group information
func listUsersFull(client *Client) ([]interface{}, error) {
return client.GetItemListInterfaceArray("/access/users?full=1")
}
func NewConfigUserFromApi(userId UserID, client *Client) (*ConfigUser, error) {
userConfig, err := client.GetItemConfigMapStringInterface("/access/users/"+userId.String(), "user", "CONFIG")
if err != nil {
return nil, err
}
return ConfigUser{User: userId}.mapToStruct(userConfig), nil
}
func NewConfigUserFromJson(input []byte) (config *ConfigUser, err error) {
if len(input) != 0 {
config = &ConfigUser{}
err = json.Unmarshal([]byte(input), config)
}
return
}
// Converts "username@realm" to a UserID object
func NewUserID(userId string) (id UserID, err error) {
tmpList := strings.Split(userId, "@")
if len(tmpList) == 2 {
if tmpList[0] != "" && tmpList[1] != "" {
return UserID{
Name: tmpList[0],
Realm: tmpList[1],
}, nil
}
}
return UserID{}, errors.New(Error_NewUserID)
}
// Converts an comma separated list of "username@realm" to a array of UserID objects
func NewUserIDs(userIds string) (*[]UserID, error) {
if userIds == "" {
return &[]UserID{}, nil
}
tmpUserIds := strings.Split(userIds, ",")
users := make([]UserID, len(tmpUserIds))
for i, e := range tmpUserIds {
var err error
users[i], err = NewUserID(e)
if err != nil {
return nil, err
}
}
return &users, nil
}
// URL for updating users
func updateUser(user UserID, params map[string]interface{}, client *Client) error {
return client.Put(params, "/access/users/"+user.String())
}