-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
storage_urm.go
227 lines (184 loc) · 5.43 KB
/
storage_urm.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
package tenant
import (
"context"
"encoding/json"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kv"
)
var urmBucket = []byte("userresourcemappingsv1")
// NOTE(affo): On URM creation, we check that the user exists.
// We do not check that the resource it is pointing to exists.
// This decision takes into account that different resources could not be in the same store.
// To perform that kind of check, we must rely on the service layer.
// However, we do not want having the storage layer depend on the service layer above.
func (s *Store) CreateURM(ctx context.Context, tx kv.Tx, urm *influxdb.UserResourceMapping) error {
if _, err := s.GetUser(ctx, tx, urm.UserID); err != nil {
return err
}
if err := s.uniqueUserResourceMapping(ctx, tx, urm); err != nil {
return err
}
v, err := json.Marshal(urm)
if err != nil {
return ErrUnprocessableMapping(err)
}
key, err := userResourceKey(urm.ResourceID, urm.UserID)
if err != nil {
return err
}
b, err := tx.Bucket(urmBucket)
if err != nil {
return UnavailableURMServiceError(err)
}
if err := b.Put(key, v); err != nil {
return UnavailableURMServiceError(err)
}
// insert urm into by user index
userID, err := urm.UserID.Encode()
if err != nil {
return err
}
if err := s.urmByUserIndex.Insert(tx, userID, key); err != nil {
return err
}
return nil
}
func (s *Store) ListURMs(ctx context.Context, tx kv.Tx, filter influxdb.UserResourceMappingFilter, opt ...influxdb.FindOptions) ([]*influxdb.UserResourceMapping, error) {
ms := []*influxdb.UserResourceMapping{}
b, err := tx.Bucket(urmBucket)
if err != nil {
return nil, UnavailableURMServiceError(err)
}
filterFn := func(m *influxdb.UserResourceMapping) bool {
return (!filter.UserID.Valid() || (filter.UserID == m.UserID)) &&
(!filter.ResourceID.Valid() || (filter.ResourceID == m.ResourceID)) &&
(filter.UserType == "" || (filter.UserType == m.UserType)) &&
(filter.ResourceType == "" || (filter.ResourceType == m.ResourceType))
}
if filter.UserID.Valid() {
var (
// urm by user index lookup
userID, _ = filter.UserID.Encode()
seen int
)
err := s.urmByUserIndex.Walk(ctx, tx, userID, func(k, v []byte) (bool, error) {
m := &influxdb.UserResourceMapping{}
if err := json.Unmarshal(v, m); err != nil {
return false, CorruptURMError(err)
}
// respect offset parameter
reachedOffset := (len(opt) == 0 || seen >= opt[0].Offset)
if reachedOffset && filterFn(m) {
ms = append(ms, m)
}
seen++
return (len(opt) == 0 || opt[0].Limit <= 0 || len(ms) < opt[0].Limit), nil
})
return ms, err
}
// for now the best we can do is use the resourceID if we have that as a forward cursor option
var prefix []byte
var cursorOptions []kv.CursorOption
if filter.ResourceID.Valid() {
p, err := userResourcePrefixKey(filter.ResourceID)
if err != nil {
return nil, err
}
prefix = p
cursorOptions = append(cursorOptions, kv.WithCursorPrefix(p))
}
cur, err := b.ForwardCursor(prefix, cursorOptions...)
if err != nil {
return nil, err
}
defer cur.Close()
for k, v := cur.Next(); k != nil; k, v = cur.Next() {
m := &influxdb.UserResourceMapping{}
if err := json.Unmarshal(v, m); err != nil {
return nil, CorruptURMError(err)
}
// check to see if it matches the filter
if filterFn(m) {
ms = append(ms, m)
}
if len(opt) > 0 && opt[0].Limit > 0 && len(ms) >= opt[0].Limit {
break
}
}
return ms, cur.Err()
}
func (s *Store) GetURM(ctx context.Context, tx kv.Tx, resourceID, userID platform.ID) (*influxdb.UserResourceMapping, error) {
key, err := userResourceKey(resourceID, userID)
if err != nil {
return nil, err
}
b, err := tx.Bucket(urmBucket)
if err != nil {
return nil, UnavailableURMServiceError(err)
}
val, err := b.Get(key)
if err != nil {
return nil, err
}
m := &influxdb.UserResourceMapping{}
if err := json.Unmarshal(val, m); err != nil {
return nil, CorruptURMError(err)
}
return m, nil
}
func (s *Store) DeleteURM(ctx context.Context, tx kv.Tx, resourceID, userID platform.ID) error {
key, err := userResourceKey(resourceID, userID)
if err != nil {
return err
}
b, err := tx.Bucket(urmBucket)
if err != nil {
return err
}
// remove user resource mapping from by user index
uid, err := userID.Encode()
if err != nil {
return err
}
if err := s.urmByUserIndex.Delete(tx, uid, key); err != nil {
return err
}
return b.Delete(key)
}
func userResourcePrefixKey(resourceID platform.ID) ([]byte, error) {
encodedResourceID, err := resourceID.Encode()
if err != nil {
return nil, ErrInvalidURMID
}
return encodedResourceID, nil
}
func userResourceKey(resourceID, userID platform.ID) ([]byte, error) {
encodedResourceID, err := resourceID.Encode()
if err != nil {
return nil, ErrInvalidURMID
}
encodedUserID, err := userID.Encode()
if err != nil {
return nil, ErrInvalidURMID
}
key := make([]byte, len(encodedResourceID)+len(encodedUserID))
copy(key, encodedResourceID)
copy(key[len(encodedResourceID):], encodedUserID)
return key, nil
}
func (s *Store) uniqueUserResourceMapping(ctx context.Context, tx kv.Tx, m *influxdb.UserResourceMapping) error {
key, err := userResourceKey(m.ResourceID, m.UserID)
if err != nil {
return err
}
b, err := tx.Bucket(urmBucket)
if err != nil {
return UnavailableURMServiceError(err)
}
_, err = b.Get(key)
if !kv.IsNotFound(err) {
return NonUniqueMappingError(m.UserID)
}
return nil
}