aerospike 1.0.12 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -212,7 +212,7 @@ module Aerospike
212
212
 
213
213
  def launch_tend_thread
214
214
  @tend_thread = Thread.new do
215
- abort_on_exception = false
215
+ Thread.current.abort_on_exception = false
216
216
  while true
217
217
  begin
218
218
  tend
@@ -325,9 +325,9 @@ module Aerospike
325
325
 
326
326
  def update_cluster_features
327
327
  # Cluster supports features that are supported by all nodes
328
- @features.update do |cluster_features|
329
- node_featues = self.nodes.map(&:features)
330
- cluster_features.replace(node_featues.reduce(&:intersection))
328
+ @features.update do
329
+ node_features = self.nodes.map(&:features)
330
+ node_features.reduce(&:intersection) || Set.new
331
331
  end
332
332
  end
333
333
 
@@ -17,330 +17,330 @@
17
17
  module Aerospike
18
18
 
19
19
  private
20
- # Commands
21
- AUTHENTICATE = 0
22
- CREATE_USER = 1
23
- DROP_USER = 2
24
- SET_PASSWORD = 3
25
- CHANGE_PASSWORD = 4
26
- GRANT_ROLES = 5
27
- REVOKE_ROLES = 6
28
- REPLACE_ROLES = 7
29
- #CREATE_ROLE = 8
30
- QUERY_USERS = 9
31
- #QUERY_ROLES = 10
32
-
33
- # Field IDs
34
- USER = 0
35
- PASSWORD = 1
36
- OLD_PASSWORD = 2
37
- CREDENTIAL = 3
38
- ROLES = 10
39
- #PRIVILEGES = 11
40
-
41
- # Misc
42
- MSG_VERSION = 0
43
- MSG_TYPE = 2
44
-
45
- HEADER_SIZE = 24
46
- HEADER_REMAINING = 16
47
- RESULT_CODE = 9
48
- QUERY_END = 50
49
-
50
- class AdminCommand #:nodoc:
51
-
52
- def initialize
53
- @data_buffer = Buffer.get
54
- @data_offset = 8
55
- end
56
-
57
- def authenticate(conn, user, password)
58
- begin
59
- set_authenticate(user, password)
60
- conn.write(@data_buffer, @data_offset)
61
- conn.read(@data_buffer, HEADER_SIZE)
62
-
63
- result = @data_buffer.read(RESULT_CODE)
64
- raise Exceptions::Aerospike.new(result, "Authentication failed") if result != 0
65
- ensure
66
- Buffer.put(@data_buffer)
67
- end
68
- end
69
-
70
- def set_authenticate(user, password)
71
- write_header(AUTHENTICATE, 2)
72
- write_field_str(USER, user)
73
- write_field_bytes(CREDENTIAL, password)
74
- write_size
75
-
76
- return @data_offset
77
- end
78
-
79
- def create_user(cluster, policy, user, password, roles)
80
- write_header(CREATE_USER, 3)
81
- write_field_str(USER, user)
82
- write_field_bytes(PASSWORD, password)
83
- write_roles(roles)
84
- execute_command(cluster, policy)
85
- end
86
-
87
- def drop_user(cluster, policy, user)
88
- write_header(DROP_USER, 1)
89
- write_field_str(USER, user)
90
- execute_command(cluster, policy)
91
- end
92
-
93
- def set_password(cluster, policy, user, password)
94
- write_header(SET_PASSWORD, 2)
95
- write_field_str(USER, user)
96
- write_field_bytes(PASSWORD, password)
97
- execute_command(cluster, policy)
98
- end
99
-
100
- def change_password(cluster, policy, user, password)
101
- write_header(CHANGE_PASSWORD, 3)
102
- write_field_str(USER, user)
103
- write_field_bytes(OLD_PASSWORD, cluster.password)
104
- write_field_bytes(PASSWORD, password)
105
- execute_command(cluster, policy)
106
- end
107
-
108
- def grant_roles(cluster, policy, user, roles)
109
- write_header(GRANT_ROLES, 2)
110
- write_field_str(USER, user)
111
- write_roles(roles)
112
- execute_command(cluster, policy)
113
- end
114
-
115
- def revoke_roles(cluster, policy, user, roles)
116
- write_header(REVOKE_ROLES, 2)
117
- write_field_str(USER, user)
118
- write_roles(roles)
119
- execute_command(cluster, policy)
120
- end
121
-
122
- def query_user(cluster, policy, user)
123
- # TODO: Remove the workaround in the future
124
- sleep(0.010)
125
-
126
- list = []
127
- begin
128
- write_header(QUERY_USERS, 1)
129
- write_field_str(USER, user)
130
- list = read_users(cluster, policy)
131
- return (list.is_a?(Array) && list.length > 0 ? list.first : nil)
132
- ensure
133
- Buffer.put(@data_buffer)
134
- end
135
- end
136
-
137
- def query_users(cluster, policy)
138
- # TODO: Remove the workaround in the future
139
- sleep(0.010)
140
- begin
141
- write_header(QUERY_USERS, 0)
142
- return read_users(cluster, policy)
143
- ensure
144
- Buffer.put(@data_buffer)
145
- end
146
- end
147
-
148
- def write_roles(roles)
149
- offset = @data_offset + FIELD_HEADER_SIZE
150
- @data_buffer.write_byte(roles.length.ord, offset)
151
- offset += 1
152
-
153
- roles.each do |role|
154
- len = @data_buffer.write_binary(role, offset+1)
155
- @data_buffer.write_byte(len, offset)
156
- offset += len + 1
157
- end
158
-
159
- size = offset - @data_offset - FIELD_HEADER_SIZE
160
- write_field_header(ROLES, size)
161
- @data_offset = offset
162
- end
163
-
164
- def write_size
165
- # Write total size of message which is the current offset.
166
- size = Integer(@data_offset-8) | Integer(MSG_VERSION << 56) | Integer(MSG_TYPE << 48)
167
- @data_buffer.write_int64(size, 0)
168
- end
169
-
170
- def write_header(command, field_count)
171
- # Authenticate header is almost all zeros
172
- i = @data_offset
173
- while i < @data_offset+16
174
- @data_buffer.write_byte(0, i)
175
- i = i.succ
176
- end
177
- @data_buffer.write_byte(command, @data_offset+2)
178
- @data_buffer.write_byte(field_count, @data_offset+3)
179
- @data_offset += 16
180
- end
181
-
182
- def write_field_str(id, str)
183
- len = @data_buffer.write_binary(str, @data_offset+FIELD_HEADER_SIZE)
184
- write_field_header(id, len)
185
- @data_offset += len
186
- end
187
-
188
- def write_field_bytes(id, bytes)
189
- @data_buffer.write_binary(bytes, @data_offset+FIELD_HEADER_SIZE)
190
- write_field_header(id, bytes.bytesize)
191
- @data_offset += bytes.bytesize
192
- end
193
-
194
- def write_field_header(id, size)
195
- @data_buffer.write_int32(size+1, @data_offset)
196
- @data_offset += 4
197
- @data_buffer.write_byte(id, @data_offset)
198
- @data_offset += 1
199
- end
200
-
201
- def execute_command(cluster, policy)
202
- # TODO: Remove the workaround in the future
203
- sleep(0.010)
204
-
205
- write_size
206
- node = cluster.random_node
207
-
208
- timeout = 1
209
- timeout = policy.timeout if policy && policy.timeout > 0
210
-
211
- conn = node.get_connection(timeout)
212
-
213
- begin
214
- conn.write(@data_buffer, @data_offset)
215
- conn.read(@data_buffer, HEADER_SIZE)
216
- node.put_connection(conn)
217
- rescue => e
218
- conn.close if conn
219
- raise e
220
- end
221
-
222
- result = @data_buffer.read(RESULT_CODE)
223
- raise Exceptions::Aerospike.new(result) if result != 0
224
-
225
- Buffer.put(@data_buffer)
226
- end
227
-
228
- def read_users(cluster, policy)
229
- write_size
230
- node = cluster.random_node
231
-
232
- timeout = 1
233
- timeout = policy.timeout if policy != nil && policy.timeout > 0
234
-
235
- status = -1
236
- list = []
237
- begin
238
- conn = node.get_connection(timeout)
239
- conn.write(@data_buffer, @data_offset)
240
- status, list = read_user_blocks(conn)
241
- node.put_connection(conn)
242
- rescue => e
243
- conn.close if conn
244
- raise e
245
- end
246
-
247
- raise Exceptions::Aerospike.new(result) if status > 0
248
-
249
- return list
250
- end
251
-
252
- def read_user_blocks(conn)
253
- rlist = []
254
- status = 0
255
- begin
256
- while status == 0
257
- conn.read(@data_buffer, 8)
258
- size = @data_buffer.read_int64(0)
259
- receive_size = (size & 0xFFFFFFFFFFFF)
260
-
261
- if receive_size > 0
262
- @data_buffer.resize(receive_size) if receive_size > @data_buffer.size
263
-
264
- conn.read(@data_buffer, receive_size)
265
- status, list = parse_users(receive_size)
266
- rlist.concat(list.to_a)
267
- else
268
- break
269
- end
270
- end
271
- return status, rlist
272
- rescue => e
273
- return -1, []
274
- end
275
- end
276
-
277
- def parse_users(receive_size)
278
- @data_offset = 0
279
- list = []
280
-
281
- while @data_offset < receive_size
282
- result_code = @data_buffer.read(@data_offset+1)
283
-
284
- if result_code != 0
285
- return (result_code == QUERY_END ? -1 : result_code)
286
- end
287
-
288
- userRoles = UserRoles.new
289
- field_count = @data_buffer.read(@data_offset+3)
290
- @data_offset += HEADER_REMAINING
291
-
292
- i = 0
293
- while i < field_count
294
- len = @data_buffer.read_int32(@data_offset)
295
- @data_offset += 4
296
- id = @data_buffer.read(@data_offset)
297
- @data_offset += 1
298
- len -= 1
299
-
300
- case id
301
- when USER
302
- userRoles.user = @data_buffer.read(@data_offset, len)
303
- @data_offset += len
304
- when ROLES
305
- parse_roles(userRoles)
306
- else
307
- @data_offset += len
308
- end
309
-
310
- i = i.succ
311
- end
312
-
313
- next if userRoles.user == "" && userRoles.roles == nil
314
-
315
- userRoles.roles = [] if userRoles.roles == nil
316
- list << userRoles
317
- end
318
-
319
- return 0, list
320
- end
321
-
322
- def parse_roles(userRoles)
323
- size = @data_buffer.read(@data_offset)
324
- @data_offset += 1
325
- userRoles.roles = []
326
-
327
- i = 0
328
- while i < size
329
- len = @data_buffer.read(@data_offset)
330
- @data_offset += 1
331
- role = @data_buffer.read(@data_offset, len)
332
- @data_offset += len
333
- userRoles.roles << role
334
-
335
- i = i.succ
336
- end
337
- end
338
-
339
- SALT = '$2a$10$7EqJtq98hPqEX7fNZaFWoO'
340
- def self.hash_password(password)
341
- # Hashing the password with the cost of 10, with a static salt
342
- return BCrypt::Engine.hash_secret(password, SALT, :cost => 10)
343
- end
344
- end
20
+ # Commands
21
+ AUTHENTICATE = 0
22
+ CREATE_USER = 1
23
+ DROP_USER = 2
24
+ SET_PASSWORD = 3
25
+ CHANGE_PASSWORD = 4
26
+ GRANT_ROLES = 5
27
+ REVOKE_ROLES = 6
28
+ REPLACE_ROLES = 7
29
+ #CREATE_ROLE = 8
30
+ QUERY_USERS = 9
31
+ #QUERY_ROLES = 10
32
+
33
+ # Field IDs
34
+ USER = 0
35
+ PASSWORD = 1
36
+ OLD_PASSWORD = 2
37
+ CREDENTIAL = 3
38
+ ROLES = 10
39
+ #PRIVILEGES = 11
40
+
41
+ # Misc
42
+ MSG_VERSION = 0
43
+ MSG_TYPE = 2
44
+
45
+ HEADER_SIZE = 24
46
+ HEADER_REMAINING = 16
47
+ RESULT_CODE = 9
48
+ QUERY_END = 50
49
+
50
+ class AdminCommand #:nodoc:
51
+
52
+ def initialize
53
+ @data_buffer = Buffer.get
54
+ @data_offset = 8
55
+ end
56
+
57
+ def authenticate(conn, user, password)
58
+ begin
59
+ set_authenticate(user, password)
60
+ conn.write(@data_buffer, @data_offset)
61
+ conn.read(@data_buffer, HEADER_SIZE)
62
+
63
+ result = @data_buffer.read(RESULT_CODE)
64
+ raise Exceptions::Aerospike.new(result, "Authentication failed") if result != 0
65
+ ensure
66
+ Buffer.put(@data_buffer)
67
+ end
68
+ end
69
+
70
+ def set_authenticate(user, password)
71
+ write_header(AUTHENTICATE, 2)
72
+ write_field_str(USER, user)
73
+ write_field_bytes(CREDENTIAL, password)
74
+ write_size
75
+
76
+ return @data_offset
77
+ end
78
+
79
+ def create_user(cluster, policy, user, password, roles)
80
+ write_header(CREATE_USER, 3)
81
+ write_field_str(USER, user)
82
+ write_field_bytes(PASSWORD, password)
83
+ write_roles(roles)
84
+ execute_command(cluster, policy)
85
+ end
86
+
87
+ def drop_user(cluster, policy, user)
88
+ write_header(DROP_USER, 1)
89
+ write_field_str(USER, user)
90
+ execute_command(cluster, policy)
91
+ end
92
+
93
+ def set_password(cluster, policy, user, password)
94
+ write_header(SET_PASSWORD, 2)
95
+ write_field_str(USER, user)
96
+ write_field_bytes(PASSWORD, password)
97
+ execute_command(cluster, policy)
98
+ end
99
+
100
+ def change_password(cluster, policy, user, password)
101
+ write_header(CHANGE_PASSWORD, 3)
102
+ write_field_str(USER, user)
103
+ write_field_bytes(OLD_PASSWORD, cluster.password)
104
+ write_field_bytes(PASSWORD, password)
105
+ execute_command(cluster, policy)
106
+ end
107
+
108
+ def grant_roles(cluster, policy, user, roles)
109
+ write_header(GRANT_ROLES, 2)
110
+ write_field_str(USER, user)
111
+ write_roles(roles)
112
+ execute_command(cluster, policy)
113
+ end
114
+
115
+ def revoke_roles(cluster, policy, user, roles)
116
+ write_header(REVOKE_ROLES, 2)
117
+ write_field_str(USER, user)
118
+ write_roles(roles)
119
+ execute_command(cluster, policy)
120
+ end
121
+
122
+ def query_user(cluster, policy, user)
123
+ # TODO: Remove the workaround in the future
124
+ sleep(0.010)
125
+
126
+ list = []
127
+ begin
128
+ write_header(QUERY_USERS, 1)
129
+ write_field_str(USER, user)
130
+ list = read_users(cluster, policy)
131
+ return (list.is_a?(Array) && list.length > 0 ? list.first : nil)
132
+ ensure
133
+ Buffer.put(@data_buffer)
134
+ end
135
+ end
136
+
137
+ def query_users(cluster, policy)
138
+ # TODO: Remove the workaround in the future
139
+ sleep(0.010)
140
+ begin
141
+ write_header(QUERY_USERS, 0)
142
+ return read_users(cluster, policy)
143
+ ensure
144
+ Buffer.put(@data_buffer)
145
+ end
146
+ end
147
+
148
+ def write_roles(roles)
149
+ offset = @data_offset + FIELD_HEADER_SIZE
150
+ @data_buffer.write_byte(roles.length.ord, offset)
151
+ offset += 1
152
+
153
+ roles.each do |role|
154
+ len = @data_buffer.write_binary(role, offset+1)
155
+ @data_buffer.write_byte(len, offset)
156
+ offset += len + 1
157
+ end
158
+
159
+ size = offset - @data_offset - FIELD_HEADER_SIZE
160
+ write_field_header(ROLES, size)
161
+ @data_offset = offset
162
+ end
163
+
164
+ def write_size
165
+ # Write total size of message which is the current offset.
166
+ size = Integer(@data_offset-8) | Integer(MSG_VERSION << 56) | Integer(MSG_TYPE << 48)
167
+ @data_buffer.write_int64(size, 0)
168
+ end
169
+
170
+ def write_header(command, field_count)
171
+ # Authenticate header is almost all zeros
172
+ i = @data_offset
173
+ while i < @data_offset+16
174
+ @data_buffer.write_byte(0, i)
175
+ i = i.succ
176
+ end
177
+ @data_buffer.write_byte(command, @data_offset+2)
178
+ @data_buffer.write_byte(field_count, @data_offset+3)
179
+ @data_offset += 16
180
+ end
181
+
182
+ def write_field_str(id, str)
183
+ len = @data_buffer.write_binary(str, @data_offset+FIELD_HEADER_SIZE)
184
+ write_field_header(id, len)
185
+ @data_offset += len
186
+ end
187
+
188
+ def write_field_bytes(id, bytes)
189
+ @data_buffer.write_binary(bytes, @data_offset+FIELD_HEADER_SIZE)
190
+ write_field_header(id, bytes.bytesize)
191
+ @data_offset += bytes.bytesize
192
+ end
193
+
194
+ def write_field_header(id, size)
195
+ @data_buffer.write_int32(size+1, @data_offset)
196
+ @data_offset += 4
197
+ @data_buffer.write_byte(id, @data_offset)
198
+ @data_offset += 1
199
+ end
200
+
201
+ def execute_command(cluster, policy)
202
+ # TODO: Remove the workaround in the future
203
+ sleep(0.010)
204
+
205
+ write_size
206
+ node = cluster.random_node
207
+
208
+ timeout = 1
209
+ timeout = policy.timeout if policy && policy.timeout > 0
210
+
211
+ conn = node.get_connection(timeout)
212
+
213
+ begin
214
+ conn.write(@data_buffer, @data_offset)
215
+ conn.read(@data_buffer, HEADER_SIZE)
216
+ node.put_connection(conn)
217
+ rescue => e
218
+ conn.close if conn
219
+ raise e
220
+ end
221
+
222
+ result = @data_buffer.read(RESULT_CODE)
223
+ raise Exceptions::Aerospike.new(result) if result != 0
224
+
225
+ Buffer.put(@data_buffer)
226
+ end
227
+
228
+ def read_users(cluster, policy)
229
+ write_size
230
+ node = cluster.random_node
231
+
232
+ timeout = 1
233
+ timeout = policy.timeout if policy != nil && policy.timeout > 0
234
+
235
+ status = -1
236
+ list = []
237
+ begin
238
+ conn = node.get_connection(timeout)
239
+ conn.write(@data_buffer, @data_offset)
240
+ status, list = read_user_blocks(conn)
241
+ node.put_connection(conn)
242
+ rescue => e
243
+ conn.close if conn
244
+ raise e
245
+ end
246
+
247
+ raise Exceptions::Aerospike.new(result) if status > 0
248
+
249
+ return list
250
+ end
251
+
252
+ def read_user_blocks(conn)
253
+ rlist = []
254
+ status = 0
255
+ begin
256
+ while status == 0
257
+ conn.read(@data_buffer, 8)
258
+ size = @data_buffer.read_int64(0)
259
+ receive_size = (size & 0xFFFFFFFFFFFF)
260
+
261
+ if receive_size > 0
262
+ @data_buffer.resize(receive_size) if receive_size > @data_buffer.size
263
+
264
+ conn.read(@data_buffer, receive_size)
265
+ status, list = parse_users(receive_size)
266
+ rlist.concat(list.to_a)
267
+ else
268
+ break
269
+ end
270
+ end
271
+ return status, rlist
272
+ rescue
273
+ return -1, []
274
+ end
275
+ end
276
+
277
+ def parse_users(receive_size)
278
+ @data_offset = 0
279
+ list = []
280
+
281
+ while @data_offset < receive_size
282
+ result_code = @data_buffer.read(@data_offset+1)
283
+
284
+ if result_code != 0
285
+ return (result_code == QUERY_END ? -1 : result_code)
286
+ end
287
+
288
+ userRoles = UserRoles.new
289
+ field_count = @data_buffer.read(@data_offset+3)
290
+ @data_offset += HEADER_REMAINING
291
+
292
+ i = 0
293
+ while i < field_count
294
+ len = @data_buffer.read_int32(@data_offset)
295
+ @data_offset += 4
296
+ id = @data_buffer.read(@data_offset)
297
+ @data_offset += 1
298
+ len -= 1
299
+
300
+ case id
301
+ when USER
302
+ userRoles.user = @data_buffer.read(@data_offset, len)
303
+ @data_offset += len
304
+ when ROLES
305
+ parse_roles(userRoles)
306
+ else
307
+ @data_offset += len
308
+ end
309
+
310
+ i = i.succ
311
+ end
312
+
313
+ next if userRoles.user == "" && userRoles.roles == nil
314
+
315
+ userRoles.roles = [] if userRoles.roles == nil
316
+ list << userRoles
317
+ end
318
+
319
+ return 0, list
320
+ end
321
+
322
+ def parse_roles(userRoles)
323
+ size = @data_buffer.read(@data_offset)
324
+ @data_offset += 1
325
+ userRoles.roles = []
326
+
327
+ i = 0
328
+ while i < size
329
+ len = @data_buffer.read(@data_offset)
330
+ @data_offset += 1
331
+ role = @data_buffer.read(@data_offset, len)
332
+ @data_offset += len
333
+ userRoles.roles << role
334
+
335
+ i = i.succ
336
+ end
337
+ end
338
+
339
+ SALT = '$2a$10$7EqJtq98hPqEX7fNZaFWoO'
340
+ def self.hash_password(password)
341
+ # Hashing the password with the cost of 10, with a static salt
342
+ return BCrypt::Engine.hash_secret(password, SALT, :cost => 10)
343
+ end
344
+ end
345
345
  end
346
346