forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_command.go
401 lines (333 loc) · 10.4 KB
/
admin_command.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
// Copyright 2013-2015 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use acmd file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aerospike
import (
"time"
"github.com/aerospike/aerospike-client-go/pkg/bcrypt"
. "github.com/aerospike/aerospike-client-go/types"
Buffer "github.com/aerospike/aerospike-client-go/utils/buffer"
)
const (
// Commands
_AUTHENTICATE byte = 0
_CREATE_USER byte = 1
_DROP_USER byte = 2
_SET_PASSWORD byte = 3
_CHANGE_PASSWORD byte = 4
_GRANT_ROLES byte = 5
_REVOKE_ROLES byte = 6
_REPLACE_ROLES byte = 7
//_CREATE_ROLE byte = 8;
_QUERY_USERS byte = 9
//_QUERY_ROLES byte = 10;
// Field IDs
_USER byte = 0
_PASSWORD byte = 1
_OLD_PASSWORD byte = 2
_CREDENTIAL byte = 3
_ROLES byte = 10
//_PRIVILEGES byte = 11;
// Misc
_MSG_VERSION int64 = 0
_MSG_TYPE int64 = 2
_HEADER_SIZE int = 24
_HEADER_REMAINING int = 16
_RESULT_CODE int = 9
_QUERY_END int = 50
)
type AdminCommand struct {
dataBuffer []byte
dataOffset int
}
func newAdminCommand() *AdminCommand {
return &AdminCommand{
dataBuffer: bufPool.Get(),
dataOffset: 8,
}
}
func (acmd *AdminCommand) authenticate(conn *Connection, user string, password []byte) error {
acmd.setAuthenticate(user, password)
if _, err := conn.Write(acmd.dataBuffer[:acmd.dataOffset]); err != nil {
return err
}
if _, err := conn.Read(acmd.dataBuffer, _HEADER_SIZE); err != nil {
return err
}
result := acmd.dataBuffer[_RESULT_CODE]
if result != 0 {
return NewAerospikeError(ResultCode(result), "Authentication failed")
}
bufPool.Put(acmd.dataBuffer)
return nil
}
func (acmd *AdminCommand) setAuthenticate(user string, password []byte) int {
acmd.writeHeader(_AUTHENTICATE, 2)
acmd.writeFieldStr(_USER, user)
acmd.writeFieldBytes(_CREDENTIAL, password)
acmd.writeSize()
return acmd.dataOffset
}
func (acmd *AdminCommand) createUser(cluster *Cluster, policy *AdminPolicy, user string, password []byte, roles []string) error {
acmd.writeHeader(_CREATE_USER, 3)
acmd.writeFieldStr(_USER, user)
acmd.writeFieldBytes(_PASSWORD, password)
acmd.writeRoles(roles)
return acmd.executeCommand(cluster, policy)
}
func (acmd *AdminCommand) dropUser(cluster *Cluster, policy *AdminPolicy, user string) error {
acmd.writeHeader(_DROP_USER, 1)
acmd.writeFieldStr(_USER, user)
return acmd.executeCommand(cluster, policy)
}
func (acmd *AdminCommand) setPassword(cluster *Cluster, policy *AdminPolicy, user string, password []byte) error {
acmd.writeHeader(_SET_PASSWORD, 2)
acmd.writeFieldStr(_USER, user)
acmd.writeFieldBytes(_PASSWORD, password)
return acmd.executeCommand(cluster, policy)
}
func (acmd *AdminCommand) changePassword(cluster *Cluster, policy *AdminPolicy, user string, password []byte) error {
acmd.writeHeader(_CHANGE_PASSWORD, 3)
acmd.writeFieldStr(_USER, user)
acmd.writeFieldBytes(_OLD_PASSWORD, cluster.Password())
acmd.writeFieldBytes(_PASSWORD, password)
return acmd.executeCommand(cluster, policy)
}
func (acmd *AdminCommand) grantRoles(cluster *Cluster, policy *AdminPolicy, user string, roles []string) error {
acmd.writeHeader(_GRANT_ROLES, 2)
acmd.writeFieldStr(_USER, user)
acmd.writeRoles(roles)
return acmd.executeCommand(cluster, policy)
}
func (acmd *AdminCommand) revokeRoles(cluster *Cluster, policy *AdminPolicy, user string, roles []string) error {
acmd.writeHeader(_REVOKE_ROLES, 2)
acmd.writeFieldStr(_USER, user)
acmd.writeRoles(roles)
return acmd.executeCommand(cluster, policy)
}
func (acmd *AdminCommand) queryUser(cluster *Cluster, policy *AdminPolicy, user string) (*UserRoles, error) {
// TODO: Remove the workaround in the future
time.Sleep(time.Millisecond * 10)
defer bufPool.Put(acmd.dataBuffer)
acmd.writeHeader(_QUERY_USERS, 1)
acmd.writeFieldStr(_USER, user)
list, err := acmd.readUsers(cluster, policy)
if err != nil {
return nil, err
}
if len(list) > 0 {
return list[0], nil
}
return nil, nil
}
func (acmd *AdminCommand) queryUsers(cluster *Cluster, policy *AdminPolicy) ([]*UserRoles, error) {
// TODO: Remove the workaround in the future
time.Sleep(time.Millisecond * 10)
defer bufPool.Put(acmd.dataBuffer)
acmd.writeHeader(_QUERY_USERS, 0)
list, err := acmd.readUsers(cluster, policy)
if err != nil {
return nil, err
}
return list, nil
}
func (acmd *AdminCommand) writeRoles(roles []string) {
offset := acmd.dataOffset + int(_FIELD_HEADER_SIZE)
acmd.dataBuffer[offset] = byte(len(roles))
offset++
for _, role := range roles {
len := copy(acmd.dataBuffer[offset+1:], role)
acmd.dataBuffer[offset] = byte(len)
offset += len + 1
}
size := offset - acmd.dataOffset - int(_FIELD_HEADER_SIZE)
acmd.writeFieldHeader(_ROLES, size)
acmd.dataOffset = offset
}
func (acmd *AdminCommand) writeSize() {
// Write total size of message which is the current offset.
var size = int64(acmd.dataOffset-8) | (_MSG_VERSION << 56) | (_MSG_TYPE << 48)
Buffer.Int64ToBytes(size, acmd.dataBuffer, 0)
}
func (acmd *AdminCommand) writeHeader(command byte, fieldCount int) {
// Authenticate header is almost all zeros
for i := acmd.dataOffset; i < acmd.dataOffset+16; i++ {
acmd.dataBuffer[i] = 0
}
acmd.dataBuffer[acmd.dataOffset+2] = command
acmd.dataBuffer[acmd.dataOffset+3] = byte(fieldCount)
acmd.dataOffset += 16
}
func (acmd *AdminCommand) writeFieldStr(id byte, str string) {
len := copy(acmd.dataBuffer[acmd.dataOffset+int(_FIELD_HEADER_SIZE):], str)
acmd.writeFieldHeader(id, len)
acmd.dataOffset += len
}
func (acmd *AdminCommand) writeFieldBytes(id byte, bytes []byte) {
copy(acmd.dataBuffer[acmd.dataOffset+int(_FIELD_HEADER_SIZE):], bytes)
acmd.writeFieldHeader(id, len(bytes))
acmd.dataOffset += len(bytes)
}
func (acmd *AdminCommand) writeFieldHeader(id byte, size int) {
Buffer.Int32ToBytes(int32(size+1), acmd.dataBuffer, acmd.dataOffset)
acmd.dataOffset += 4
acmd.dataBuffer[acmd.dataOffset] = id
acmd.dataOffset++
}
func (acmd *AdminCommand) executeCommand(cluster *Cluster, policy *AdminPolicy) error {
// TODO: Remove the workaround in the future
defer time.Sleep(time.Millisecond * 10)
defer bufPool.Put(acmd.dataBuffer)
acmd.writeSize()
node, err := cluster.GetRandomNode()
if err != nil {
return nil
}
timeout := 1 * time.Second
if policy != nil && policy.Timeout > 0 {
timeout = policy.Timeout
}
conn, err := node.GetConnection(timeout)
if err != nil {
return err
}
if _, err := conn.Write(acmd.dataBuffer[:acmd.dataOffset]); err != nil {
node.InvalidateConnection(conn)
return err
}
if _, err := conn.Read(acmd.dataBuffer, _HEADER_SIZE); err != nil {
node.InvalidateConnection(conn)
return err
}
node.PutConnection(conn)
result := acmd.dataBuffer[_RESULT_CODE]
if result != 0 {
return NewAerospikeError(ResultCode(result))
}
return nil
}
func (acmd *AdminCommand) readUsers(cluster *Cluster, policy *AdminPolicy) ([]*UserRoles, error) {
acmd.writeSize()
node, err := cluster.GetRandomNode()
if err != nil {
return nil, err
}
timeout := 1 * time.Second
if policy != nil && policy.Timeout > 0 {
timeout = policy.Timeout
}
conn, err := node.GetConnection(timeout)
if err != nil {
return nil, err
}
if _, err := conn.Write(acmd.dataBuffer[:acmd.dataOffset]); err != nil {
node.InvalidateConnection(conn)
return nil, err
}
status, list, err := acmd.readUserBlocks(conn)
if err != nil {
node.InvalidateConnection(conn)
return nil, err
}
node.PutConnection(conn)
if status > 0 {
return nil, NewAerospikeError(ResultCode(status))
}
return list, nil
}
func (acmd *AdminCommand) readUserBlocks(conn *Connection) (status int, rlist []*UserRoles, err error) {
var list []*UserRoles
for status == 0 {
if _, err = conn.Read(acmd.dataBuffer, 8); err != nil {
return -1, nil, err
}
size := Buffer.BytesToInt64(acmd.dataBuffer, 0)
receiveSize := (size & 0xFFFFFFFFFFFF)
if receiveSize > 0 {
if receiveSize > int64(len(acmd.dataBuffer)) {
acmd.dataBuffer = make([]byte, receiveSize)
}
if _, err = conn.Read(acmd.dataBuffer, int(receiveSize)); err != nil {
return -1, nil, err
}
status, list, err = acmd.parseUsers(int(receiveSize))
if err != nil {
return -1, nil, err
}
rlist = append(rlist, list...)
} else {
break
}
}
return status, rlist, nil
}
func (acmd *AdminCommand) parseUsers(receiveSize int) (int, []*UserRoles, error) {
acmd.dataOffset = 0
list := make([]*UserRoles, 0, 100)
for acmd.dataOffset < receiveSize {
resultCode := int(acmd.dataBuffer[acmd.dataOffset+1])
if resultCode != 0 {
if resultCode == _QUERY_END {
return -1, nil, nil
}
return resultCode, nil, nil
}
userRoles := &UserRoles{}
fieldCount := int(acmd.dataBuffer[acmd.dataOffset+3])
acmd.dataOffset += _HEADER_REMAINING
for i := 0; i < fieldCount; i++ {
len := int(Buffer.BytesToInt32(acmd.dataBuffer, acmd.dataOffset))
acmd.dataOffset += 4
id := acmd.dataBuffer[acmd.dataOffset]
acmd.dataOffset++
len--
if id == _USER {
userRoles.User = string(acmd.dataBuffer[acmd.dataOffset : acmd.dataOffset+len])
acmd.dataOffset += len
} else if id == _ROLES {
acmd.parseRoles(userRoles)
} else {
acmd.dataOffset += len
}
}
if userRoles.User == "" && userRoles.Roles == nil {
continue
}
if userRoles.Roles == nil {
userRoles.Roles = make([]string, 0)
}
list = append(list, userRoles)
}
return 0, list, nil
}
func (acmd *AdminCommand) parseRoles(userRoles *UserRoles) {
size := int(acmd.dataBuffer[acmd.dataOffset])
acmd.dataOffset++
userRoles.Roles = make([]string, 0, size)
for i := 0; i < size; i++ {
len := int(acmd.dataBuffer[acmd.dataOffset])
acmd.dataOffset++
role := string(acmd.dataBuffer[acmd.dataOffset : acmd.dataOffset+len])
acmd.dataOffset += len
userRoles.Roles = append(userRoles.Roles, role)
}
}
func hashPassword(password string) ([]byte, error) {
// Hashing the password with the cost of 10, with a static salt
const salt = "$2a$10$7EqJtq98hPqEX7fNZaFWoO"
hashedPassword, err := bcrypt.Hash(password, salt)
if err != nil {
return nil, err
}
return []byte(hashedPassword), nil
}