-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuser.go
190 lines (167 loc) · 4.01 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
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
package etcdpasswd
import (
"context"
"encoding/json"
"errors"
"strconv"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/clientv3util"
)
// User represents a user managed by etcdpasswd.
type User struct {
Name string `json:"name"`
UID int `json:"uid"`
DisplayName string `json:"display-name"`
Group string `json:"group"`
Groups []string `json:"groups"`
Shell string `json:"shell"`
PubKeys []string `json:"public-keys"`
}
// Validate validates User attributes.
// If some attribute is not valid, a non-nil error is returned.
func (u *User) Validate() error {
if !IsValidUserName(u.Name) {
return errors.New("invalid user name: " + u.Name)
}
if !IsValidGroupName(u.Group) {
return errors.New("invalid group name: " + u.Group)
}
if len(u.Shell) == 0 {
return errors.New("shell must be specified")
}
return nil
}
// ListUsers lists all user names registered in the database.
// The result is sorted alphabetically.
func (c Client) ListUsers(ctx context.Context) ([]string, error) {
return c.list(ctx, KeyUsers)
}
// GetUser looks up named user from the database.
// If the user is not found, this returns ErrNotFound.
func (c Client) GetUser(ctx context.Context, name string) (*User, int64, error) {
key := KeyUsers + name
resp, err := c.Get(ctx, key)
if err != nil {
return nil, 0, err
}
if resp.Count == 0 {
return nil, 0, ErrNotFound
}
u := new(User)
err = json.Unmarshal(resp.Kvs[0].Value, u)
if err != nil {
return nil, 0, err
}
return u, resp.Kvs[0].ModRevision, nil
}
// AddUser adds a new managed user to the database.
// If a user having the same name already exists, ErrExists will be returned.
func (c Client) AddUser(ctx context.Context, user *User) error {
cfg, _, err := c.GetConfig(ctx)
if err != nil {
return err
}
if cfg.StartUID == 0 {
// not yet configured
return errors.New("start-uid has not been set")
}
if len(user.Group) == 0 {
user.Group = cfg.DefaultGroup
}
if len(user.Groups) == 0 {
user.Groups = cfg.DefaultGroups
}
if len(user.Shell) == 0 {
user.Shell = cfg.DefaultShell
}
err = user.Validate()
if err != nil {
return err
}
key := KeyUsers + user.Name
delKey := KeyDeletedUsers + user.Name
RETRY:
uid, rev, err := c.getLastID(ctx, KeyLastUID, cfg.StartUID)
if err != nil {
return err
}
user.UID = uid
j, err := json.Marshal(user)
if err != nil {
return err
}
uidData := strconv.Itoa(uid + 1)
resp, err := c.Txn(ctx).
If(
clientv3.Compare(clientv3.ModRevision(KeyLastUID), "=", rev),
).
Then(
clientv3.OpTxn(
[]clientv3.Cmp{clientv3util.KeyMissing(key)},
[]clientv3.Op{
clientv3.OpPut(KeyLastUID, uidData),
clientv3.OpPut(key, string(j)),
clientv3.OpDelete(delKey),
},
nil,
),
).
Commit()
if err != nil {
return err
}
if !resp.Succeeded {
goto RETRY
}
if !resp.Responses[0].GetResponseTxn().Succeeded {
return ErrExists
}
return nil
}
// UpdateUser updates an existing managed user in the database.
// This operation does compare-and-swap with rev. If CAS failed,
// ErrCASFailure will be returned.
func (c Client) UpdateUser(ctx context.Context, user *User, rev int64) error {
err := user.Validate()
if err != nil {
return err
}
key := KeyUsers + user.Name
j, err := json.Marshal(user)
if err != nil {
return err
}
resp, err := c.Txn(ctx).
If(
clientv3.Compare(clientv3.ModRevision(key), "=", rev),
).
Then(clientv3.OpPut(key, string(j))).
Commit()
if err != nil {
return err
}
if !resp.Succeeded {
return ErrCASFailure
}
return nil
}
// RemoveUser removes an existing managed user.
// If the user does not exist, ErrNotFound will be returned.
func (c Client) RemoveUser(ctx context.Context, name string) error {
key := KeyUsers + name
delKey := KeyDeletedUsers + name
resp, err := c.Txn(ctx).
If(clientv3util.KeyExists(key)).
Then(
clientv3.OpDelete(key),
clientv3.OpPut(delKey, ""),
).
Commit()
if err != nil {
return err
}
if !resp.Succeeded {
return ErrNotFound
}
return nil
}