-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdatabase.go
103 lines (92 loc) · 2.13 KB
/
database.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
package etcdpasswd
import (
"context"
"encoding/json"
"strconv"
clientv3 "go.etcd.io/etcd/client/v3"
)
// Database is a on-memory snapshot of users and groups in etcd database.
type Database struct {
Users []*User
Groups []Group
DeletedUsers []string
DeletedGroups []string
LockedUsers []string
}
// GetDatabase takes a snapshot of etcd database at revision rev.
// If rev is 0, the snapshot will be the latest one.
func GetDatabase(ctx context.Context, etcd *clientv3.Client, rev int64) (*Database, error) {
db := new(Database)
resp, err := etcd.Get(ctx, KeyUsers,
clientv3.WithPrefix(),
clientv3.WithRev(rev),
clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend))
if err != nil {
return nil, err
}
if resp.Count > 0 {
users := make([]*User, resp.Count)
for i, kv := range resp.Kvs {
u := new(User)
err := json.Unmarshal(kv.Value, u)
if err != nil {
return nil, err
}
users[i] = u
}
db.Users = users
}
resp, err = etcd.Get(ctx, KeyGroups,
clientv3.WithPrefix(),
clientv3.WithRev(rev),
clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend))
if err != nil {
return nil, err
}
if resp.Count > 0 {
groups := make([]Group, resp.Count)
for i, kv := range resp.Kvs {
name := string(kv.Key[len(KeyGroups):])
gid, err := strconv.Atoi(string(kv.Value))
if err != nil {
return nil, err
}
groups[i] = Group{name, gid}
}
db.Groups = groups
}
getKeys := func(prefix string) ([]string, error) {
resp, err := etcd.Get(ctx, prefix,
clientv3.WithPrefix(),
clientv3.WithRev(rev),
clientv3.WithKeysOnly(),
)
if err != nil {
return nil, err
}
if resp.Count == 0 {
return nil, nil
}
ret := make([]string, resp.Count)
for i, kv := range resp.Kvs {
ret[i] = string(kv.Key[len(prefix):])
}
return ret, nil
}
delus, err := getKeys(KeyDeletedUsers)
if err != nil {
return nil, err
}
db.DeletedUsers = delus
delgs, err := getKeys(KeyDeletedGroups)
if err != nil {
return nil, err
}
db.DeletedGroups = delgs
locked, err := getKeys(KeyLocked)
if err != nil {
return nil, err
}
db.LockedUsers = locked
return db, nil
}