Class: Aerospike::AdminCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/aerospike/command/admin_command.rb

Overview

:nodoc:

Direct Known Subclasses

LoginCommand

Instance Method Summary collapse

Constructor Details

#initializeAdminCommand

Returns a new instance of AdminCommand.



67
68
69
70
# File 'lib/aerospike/command/admin_command.rb', line 67

def initialize
  @data_buffer = Buffer.get
  @data_offset =  8
end

Instance Method Details

#change_password(cluster, policy, user, password) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/aerospike/command/admin_command.rb', line 93

def change_password(cluster, policy, user, password)
  write_header(CHANGE_PASSWORD, 3)
  write_field_str(USER, user)
  write_field_bytes(OLD_PASSWORD, cluster.password)
  write_field_bytes(PASSWORD, password)
  execute_command(cluster, policy)
end

#create_role(cluster, policy, role_name, privileges = [], allowlist = [], read_quota = 0, write_quota = 0) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/aerospike/command/admin_command.rb', line 115

def create_role(cluster, policy, role_name, privileges = [], allowlist = [], read_quota = 0, write_quota = 0)
  field_count = 1
  field_count += 1 if privileges.size > 0
  field_count += 1 if allowlist.size > 0
  field_count += 1 if read_quota > 0
  field_count += 1 if write_quota > 0

  write_header(CREATE_ROLE, field_count)
  write_field_str(ROLE, role_name)

  write_privileges(privileges) if privileges.size > 0
  write_allowlist(allowlist) if allowlist.size > 0

  write_field_uint32(READ_QUOTA, read_quota) if read_quota > 0
  write_field_uint32(WRITE_QUOTA, write_quota) if write_quota > 0

  execute_command(cluster, policy)
end

#create_user(cluster, policy, user, password, roles) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/aerospike/command/admin_command.rb', line 72

def create_user(cluster, policy, user, password, roles)
  write_header(CREATE_USER, 3)
  write_field_str(USER, user)
  write_field_bytes(PASSWORD, password)
  write_roles(roles)
  execute_command(cluster, policy)
end

#drop_role(cluster, policy, role) ⇒ Object



134
135
136
137
138
# File 'lib/aerospike/command/admin_command.rb', line 134

def drop_role(cluster, policy, role)
  write_header(DROP_ROLE, 1)
  write_field_str(ROLE, role)
  execute_command(cluster, policy)
end

#drop_user(cluster, policy, user) ⇒ Object



80
81
82
83
84
# File 'lib/aerospike/command/admin_command.rb', line 80

def drop_user(cluster, policy, user)
  write_header(DROP_USER, 1)
  write_field_str(USER, user)
  execute_command(cluster, policy)
end

#execute_command(cluster, policy) ⇒ Object



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
# File 'lib/aerospike/command/admin_command.rb', line 338

def execute_command(cluster, policy)
  # TODO: Remove the workaround in the future
  sleep(0.010)

  write_size
  node = cluster.random_node

  timeout = 1
  timeout = policy.timeout if policy && policy.timeout > 0

  conn = node.get_connection(timeout)

  begin
    conn.write(@data_buffer, @data_offset)
    conn.read(@data_buffer, HEADER_SIZE)
    node.put_connection(conn)
  rescue => e
    conn.close if conn
    raise e
  end

  result = @data_buffer.read(RESULT_CODE)
  raise Exceptions::Aerospike.new(result) if result != 0

  Buffer.put(@data_buffer)
end

#grant_privileges(cluster, policy, role, privileges) ⇒ Object



140
141
142
143
144
145
# File 'lib/aerospike/command/admin_command.rb', line 140

def grant_privileges(cluster, policy, role, privileges)
  write_header(GRANT_PRIVILEGES, 2)
  write_field_str(ROLE, role)
  write_privileges(privileges)
  execute_command(cluster, policy)
end

#grant_roles(cluster, policy, user, roles) ⇒ Object



101
102
103
104
105
106
# File 'lib/aerospike/command/admin_command.rb', line 101

def grant_roles(cluster, policy, user, roles)
  write_header(GRANT_ROLES, 2)
  write_field_str(USER, user)
  write_roles(roles)
  execute_command(cluster, policy)
end

#parse_allowlist(len) ⇒ Object



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/aerospike/command/admin_command.rb', line 631

def parse_allowlist(len)
  list = []
  begn = @data_offset
  max = begn + len

  while @data_offset < max
    if @data_buffer.read(@data_offset) == ','
      l = @data_offset - begn
      if l > 0
        s = @data_buffer.read(begn, l)
        list << s
      end
      @data_offset += 1
      begn = @data_offset
    else
      @data_offset += 1
    end
  end

  l = @data_offset - begn
  if l > 0
    s = @data_buffer.read(begn, l)
    list << s
  end

  list
end

#parse_infoObject



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/aerospike/command/admin_command.rb', line 483

def parse_info
  size = @data_buffer.read(@data_offset)
  @data_offset += 1
  list = []

  i = 0
  while i < size
    val = @data_buffer.read_int32(@data_offset)
    @data_offset += 4
    list << val

    i = i.succ
  end

  list
end

#parse_privileges(role) ⇒ Object



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/aerospike/command/admin_command.rb', line 602

def parse_privileges(role)
  size = @data_buffer.read(@data_offset)
  @data_offset += 1
  role.privileges = []

  i = 0
  while i < size
    priv = Privilege.new
    priv.code = Privilege.from(@data_buffer.read(@data_offset))
    @data_offset += 1

    if priv.can_scope?
      len = @data_buffer.read(@data_offset)
      @data_offset += 1
      priv.namespace = @data_buffer.read(@data_offset, len)
      @data_offset += len

      len = @data_buffer.read(@data_offset)
      @data_offset += 1
      priv.set_name = @data_buffer.read(@data_offset, len)
      @data_offset += len
    end

    role.privileges << priv

    i = i.succ
  end
end

#parse_roles(user_roles) ⇒ Object



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/aerospike/command/admin_command.rb', line 466

def parse_roles(user_roles)
  size = @data_buffer.read(@data_offset)
  @data_offset += 1
  user_roles.roles = []

  i = 0
  while i < size
    len = @data_buffer.read(@data_offset)
    @data_offset += 1
    role = @data_buffer.read(@data_offset, len)
    @data_offset += len
    user_roles.roles << role

    i = i.succ
  end
end

#parse_roles_full(receive_size) ⇒ Object



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/aerospike/command/admin_command.rb', line 549

def parse_roles_full(receive_size)
  @data_offset = 0
  list = []

  while @data_offset < receive_size
    result_code = @data_buffer.read(@data_offset+1)

    if result_code != 0
      return (result_code == QUERY_END ? -1 : result_code)
    end

    role = Role.new
    field_count = @data_buffer.read(@data_offset+3)
    @data_offset += HEADER_REMAINING

    i = 0
    while i  < field_count
      len = @data_buffer.read_int32(@data_offset)
      @data_offset += 4
      id = @data_buffer.read(@data_offset)
      @data_offset += 1
      len -= 1

      case id
      when ROLE
        role.name = @data_buffer.read(@data_offset, len).to_s
        @data_offset += len
      when PRIVILEGES
        parse_privileges(role)
      when ALLOWLIST
        role.allowlist = parse_allowlist(len)
      when READ_QUOTA
        role.read_quota = @data_buffer.read_uint32(@data_offset)
        @data_offset += len
      when WRITE_QUOTA
        role.write_quota = @data_buffer.read_uint32(@data_offset)
        @data_offset += len
      else
        @data_offset += len
      end

      i = i.succ
    end

    next if role.name == "" && role.privileges == nil

    role.privileges ||= []
    list << role
  end

  return 0, list
end

#parse_users(receive_size) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/aerospike/command/admin_command.rb', line 414

def parse_users(receive_size)
  @data_offset = 0
  list = []

  while @data_offset < receive_size
    result_code = @data_buffer.read(@data_offset+1)

    if result_code != 0
      return (result_code == QUERY_END ? -1 : result_code)
    end

    user_roles = UserRoles.new
    field_count = @data_buffer.read(@data_offset+3)
    @data_offset += HEADER_REMAINING

    i = 0
    while i  < field_count
      len = @data_buffer.read_int32(@data_offset)
      @data_offset += 4
      id = @data_buffer.read(@data_offset)
      @data_offset += 1
      len -= 1

      case id
      when USER
        user_roles.user = @data_buffer.read(@data_offset, len)
        @data_offset += len
      when ROLES
        parse_roles(user_roles)
      when READ_INFO
        user_roles.read_info = parse_info
      when WRITE_INFO
        user_roles.write_info = parse_info
      when CONNECTIONS
        user_roles.conns_in_use = @data_buffer.read_int32(@data_offset)
        @data_offset += len
      else
        @data_offset += len
      end

      i = i.succ
    end

    next if user_roles.user == "" && user_roles.roles == nil

    user_roles.roles = [] if user_roles.roles == nil
    list << user_roles
  end

  return 0, list
end

#query_role(cluster, policy, role) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/aerospike/command/admin_command.rb', line 196

def query_role(cluster, policy, role)
  # TODO: Remove the workaround in the future
  sleep(0.010)

  list = []
  begin
    write_header(QUERY_ROLES, 1)
    write_field_str(ROLE, role)
    list = read_roles(cluster, policy)
    return (list.is_a?(Array) && list.length > 0 ? list.first : nil)
  ensure
    Buffer.put(@data_buffer)
  end
end

#query_roles(cluster, policy) ⇒ Object



211
212
213
214
215
216
217
218
219
220
# File 'lib/aerospike/command/admin_command.rb', line 211

def query_roles(cluster, policy)
  # TODO: Remove the workaround in the future
  sleep(0.010)
  begin
    write_header(QUERY_ROLES, 0)
    return read_roles(cluster, policy)
  ensure
    Buffer.put(@data_buffer)
  end
end

#query_user(cluster, policy, user) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/aerospike/command/admin_command.rb', line 170

def query_user(cluster, policy, user)
  # TODO: Remove the workaround in the future
  sleep(0.010)

  list = []
  begin
    write_header(QUERY_USERS, 1)
    write_field_str(USER, user)
    list = read_users(cluster, policy)
    return (list.is_a?(Array) && list.length > 0 ? list.first : nil)
  ensure
    Buffer.put(@data_buffer)
  end
end

#query_users(cluster, policy) ⇒ Object



185
186
187
188
189
190
191
192
193
194
# File 'lib/aerospike/command/admin_command.rb', line 185

def query_users(cluster, policy)
  # TODO: Remove the workaround in the future
  sleep(0.010)
  begin
    write_header(QUERY_USERS, 0)
    return read_users(cluster, policy)
  ensure
    Buffer.put(@data_buffer)
  end
end

#read_role_blocks(conn) ⇒ Object



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/aerospike/command/admin_command.rb', line 524

def read_role_blocks(conn)
  rlist = []
  status = 0
  begin
    while status == 0
      conn.read(@data_buffer, 8)
      size = @data_buffer.read_int64(0)
      receive_size = (size & 0xFFFFFFFFFFFF)

      if receive_size > 0
        @data_buffer.resize(receive_size) if receive_size > @data_buffer.size

        conn.read(@data_buffer, receive_size)
        status, list = parse_roles_full(receive_size)
        rlist.concat(list.to_a)
      else
        break
      end
    end
    return status, rlist
  rescue => e
    return -1, []
  end
end

#read_roles(cluster, policy) ⇒ Object



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/aerospike/command/admin_command.rb', line 500

def read_roles(cluster, policy)
  write_size
  node = cluster.random_node

  timeout = 1
  timeout = policy.timeout if policy != nil && policy.timeout > 0

  status = -1
  list = []
  begin
    conn = node.get_connection(timeout)
    conn.write(@data_buffer, @data_offset)
    status, list = read_role_blocks(conn)
    node.put_connection(conn)
  rescue => e
    conn.close if conn
    raise e
  end

  raise Exceptions::Aerospike.new(status) if status > 0

  return list
end

#read_user_blocks(conn) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/aerospike/command/admin_command.rb', line 389

def read_user_blocks(conn)
  rlist = []
  status = 0
  begin
    while status == 0
      conn.read(@data_buffer, 8)
      size = @data_buffer.read_int64(0)
      receive_size = (size & 0xFFFFFFFFFFFF)

      if receive_size > 0
        @data_buffer.resize(receive_size) if receive_size > @data_buffer.size

        conn.read(@data_buffer, receive_size)
        status, list = parse_users(receive_size)
        rlist.concat(list.to_a)
      else
        break
      end
    end
    return status, rlist
  rescue
    return -1, []
  end
end

#read_users(cluster, policy) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/aerospike/command/admin_command.rb', line 365

def read_users(cluster, policy)
  write_size
  node = cluster.random_node

  timeout = 1
  timeout = policy.timeout if policy != nil && policy.timeout > 0

  status = -1
  list = []
  begin
    conn = node.get_connection(timeout)
    conn.write(@data_buffer, @data_offset)
    status, list = read_user_blocks(conn)
    node.put_connection(conn)
  rescue => e
    conn.close if conn
    raise e
  end

  raise Exceptions::Aerospike.new(status) if status > 0

  return list
end

#revoke_privileges(cluster, policy, role, privileges) ⇒ Object



147
148
149
150
151
152
# File 'lib/aerospike/command/admin_command.rb', line 147

def revoke_privileges(cluster, policy, role, privileges)
  write_header(REVOKE_PRIVILEGES, 2)
  write_field_str(ROLE, role)
  write_privileges(privileges)
  execute_command(cluster, policy)
end

#revoke_roles(cluster, policy, user, roles) ⇒ Object



108
109
110
111
112
113
# File 'lib/aerospike/command/admin_command.rb', line 108

def revoke_roles(cluster, policy, user, roles)
  write_header(REVOKE_ROLES, 2)
  write_field_str(USER, user)
  write_roles(roles)
  execute_command(cluster, policy)
end

#set_allowlist(cluster, policy, role, allowlist = []) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/aerospike/command/admin_command.rb', line 154

def set_allowlist(cluster, policy, role, allowlist = [])
  field_count = 1
  field_count += 1 if allowlist.size > 0
  write_header(SET_WHITELIST, field_count)
  write_allowlist(allowlist) if allowlist.size > 0
  execute_command(cluster, policy)
end

#set_password(cluster, policy, user, password) ⇒ Object



86
87
88
89
90
91
# File 'lib/aerospike/command/admin_command.rb', line 86

def set_password(cluster, policy, user, password)
  write_header(SET_PASSWORD, 2)
  write_field_str(USER, user)
  write_field_bytes(PASSWORD, password)
  execute_command(cluster, policy)
end

#set_quotas(cluster, policy, role, read_quota, write_quota) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/aerospike/command/admin_command.rb', line 162

def set_quotas(cluster, policy, role, read_quota, write_quota)
  write_header(SET_QUOTAS, 3)
  write_field_str(ROLE, role)
  write_field_uint32(READ_QUOTA, read_quota)
  write_field_uint32(WRITE_QUOTA, write_quota)
  execute_command(cluster, policy)
end

#write_allowlist(allowlist) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/aerospike/command/admin_command.rb', line 271

def write_allowlist(allowlist)
  offset = @data_offset
  @data_offset += FIELD_HEADER_SIZE

  comma = false
  for addr in allowlist
    if comma
      write_byte(",")
    else
      comma = true
    end

    @data_offset += @data_buffer.write_binary(addr, @data_offset)
  end

  size = @data_offset - offset - FIELD_HEADER_SIZE
  @data_offset = offset
  write_field_header(ALLOWLIST, size)
  @data_offset += size
end

#write_byte(b) ⇒ Object



304
305
306
# File 'lib/aerospike/command/admin_command.rb', line 304

def write_byte(b)
  @data_offset += @data_buffer.write_byte(b, @data_offset)
end

#write_field_bytes(id, bytes) ⇒ Object



325
326
327
328
329
# File 'lib/aerospike/command/admin_command.rb', line 325

def write_field_bytes(id, bytes)
  @data_buffer.write_binary(bytes, @data_offset+FIELD_HEADER_SIZE)
  write_field_header(id, bytes.bytesize)
  @data_offset += bytes.bytesize
end

#write_field_header(id, size) ⇒ Object



331
332
333
334
335
336
# File 'lib/aerospike/command/admin_command.rb', line 331

def write_field_header(id, size)
  @data_buffer.write_int32(size+1, @data_offset)
  @data_offset += 4
  @data_buffer.write_byte(id, @data_offset)
  @data_offset += 1
end

#write_field_str(id, str) ⇒ Object



313
314
315
316
317
# File 'lib/aerospike/command/admin_command.rb', line 313

def write_field_str(id, str)
  len = @data_buffer.write_binary(str, @data_offset+FIELD_HEADER_SIZE)
  write_field_header(id, len)
  @data_offset += len
end

#write_field_uint32(id, val) ⇒ Object



319
320
321
322
323
# File 'lib/aerospike/command/admin_command.rb', line 319

def write_field_uint32(id, val)
  len = @data_buffer.write_uint32(val, @data_offset+FIELD_HEADER_SIZE)
  write_field_header(id, len)
  @data_offset += len
end

#write_header(command, field_count) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
# File 'lib/aerospike/command/admin_command.rb', line 292

def write_header(command, field_count)
  # Authenticate header is almost all zeros
  i = @data_offset
  while i < @data_offset+16
    @data_buffer.write_byte(0, i)
    i = i.succ
  end
  @data_buffer.write_byte(command, @data_offset+2)
  @data_buffer.write_byte(field_count, @data_offset+3)
  @data_offset += 16
end

#write_privileges(privileges) ⇒ Object



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
# File 'lib/aerospike/command/admin_command.rb', line 244

def write_privileges(privileges)
  offset = @data_offset
  @data_offset += FIELD_HEADER_SIZE
  write_byte(privileges.size)

  for privilege in privileges
    write_byte(privilege.to_code)
    if privilege.can_scope?
      if privilege.set_name.to_s.size > 0 && privilege.namespace.to_s.size == 0
        raise Aerospike::Exceptions::Aerospike.new(Aerospike::ResultCode::INVALID_PRIVILEGE, "Admin privilege #{privilege.namespace} has a set scope with an empty namespace")
      end

      write_str(privilege.namespace.to_s)
      write_str(privilege.set_name.to_s)
    else
      if privilege.set_name.to_s.bytesize > 0 || privilege.namespace.to_s.bytesize > 0
        raise Aerospike::Exceptions::Aerospike.new(Aerospike::ResultCode::INVALID_PRIVILEGE, "Admin global privilege #{privilege} can't have a namespace or set")
      end
    end
  end

  size = @data_offset - offset - FIELD_HEADER_SIZE
  @data_offset = offset
  write_field_header(PRIVILEGES, size)
  @data_offset += size
end

#write_roles(roles) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/aerospike/command/admin_command.rb', line 222

def write_roles(roles)
  offset = @data_offset + FIELD_HEADER_SIZE
  @data_buffer.write_byte(roles.length.ord, offset)
  offset += 1

  roles.each do |role|
    len = @data_buffer.write_binary(role, offset+1)
    @data_buffer.write_byte(len, offset)
    offset += len + 1
  end

  size = offset - @data_offset - FIELD_HEADER_SIZE
  write_field_header(ROLES, size)
  @data_offset = offset
end

#write_sizeObject



238
239
240
241
242
# File 'lib/aerospike/command/admin_command.rb', line 238

def write_size
  # Write total size of message which is the current offset.
  size = Integer(@data_offset-8) | Integer(MSG_VERSION << 56) | Integer(MSG_TYPE << 48)
  @data_buffer.write_int64(size, 0)
end

#write_str(str) ⇒ Object



308
309
310
311
# File 'lib/aerospike/command/admin_command.rb', line 308

def write_str(str)
  @data_offset += @data_buffer.write_byte(str.bytesize, @data_offset)
  @data_offset += @data_buffer.write_binary(str, @data_offset)
end