Class: EnfCli::Cmd::Xiam

Inherits:
EnfThor
  • Object
show all
Defined in:
lib/enfcli/commands/xiam.rb

Instance Method Summary collapse

Methods inherited from EnfThor

capture_stdout, command_help, handle_argument_error, help

Instance Method Details

#add_group_to_networkObject



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
# File 'lib/enfcli/commands/xiam.rb', line 268

def add_group_to_network
  try_with_rescue_in_session do
    network = EnfCli::IPV6Cidr.new(options[:network]).to_s

    # scan if gid not in options
    if options[:gid]
      gid = options[:gid]
    else
      gid = scan_gid
    end

    if options[:set_as_default]
      modified_group = {
        :gid => gid,
        :default_network => network,
      }

      EnfApi::Iam.instance.update_group gid, modified_group
      say "Associated group #{gid} with #{network} as default network", :green
    else
      EnfApi::Iam.instance.add_group_to_network gid, network
      say "Associated group #{gid} with #{network}", :green
    end
  end
end

#create_endpoint_certObject



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/enfcli/commands/xiam.rb', line 364

def create_endpoint_cert
  try_with_rescue_in_session do
    # get options
    ipv6 = EnfCli::IPV6.new(options["identity"]).to_s
    keyfile = EnfCli::expand_path(options["key-in-file"])
    certfile = EnfCli::expand_path(options["cert-out-file"])

    # read the private key
    key = read_ec_key_file keyfile

    # generate cert
    cert = EnfCli::generate_ec_cert(key, ipv6)

    # write to file
    write_pem_file certfile, cert.to_pem
  end
end

#create_endpoint_from_certObject



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/enfcli/commands/xiam.rb', line 442

def create_endpoint_from_cert
  try_with_rescue_in_session do
    # read options
    certfile = EnfCli::expand_path(options["cert-in-file"])
    raise EnfCli::ERROR, "#{certfile} does not exist!" unless File.exists?(certfile)

    # read cert file
    rawcert = File.read certfile
    cert = OpenSSL::X509::Certificate.new rawcert

    # get subject
    subject = cert.subject.to_a

    # get ipv6 CN record
    cn = subject.select { |d| "CN".eql?(d[0].upcase) }
    ipv6_address = EnfCli::IPV6.new cn[0][1]

    # get the public key
    pub = cert.public_key.public_key

    # create_endpoint
    provision_endpoint ipv6_address, pub
  end
end

#create_endpoint_in_networkObject



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/enfcli/commands/xiam.rb', line 386

def create_endpoint_in_network
  try_with_rescue_in_session do
    # Read options
    network = EnfCli::IPV6Cidr.new(options[:network]).to_s
    keyfile = EnfCli::expand_path(options["public-key-in-file"])

    # read keyfile
    key = read_ec_key_file keyfile

    # encode public key
    pub = key.public_key
    pubkey = pub.to_bn.to_s(16)

    # create NEW_CREDENTIAL_ECDSA
    new_credential = {
      :type => "ecdsa_p256",
      :key => pubkey,
    }

    # create NEW_ENDPOINT_AUTH
    new_endpoint = {
      :ecdsa_credential => new_credential,
      :address_request => {
        :network => network,
      },
    }

    data = EnfApi::Iam.instance.provision_endpoint new_endpoint
    provisioned_ip = data[:address]

    say "Created new ipv6 endpoint #{provisioned_ip}", :green
  end
end

#create_endpoint_keyObject



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/enfcli/commands/xiam.rb', line 340

def create_endpoint_key
  try_with_rescue_in_session do
    # get options
    keyfile = options["key-out-file"]
    pubfile = options["public-key-out-file"]

    # Generate a key pair
    key = OpenSSL::PKey::EC.new("prime256v1")
    key.generate_key

    # write key pair to file
    write_pem_file keyfile, key.to_pem

    # write public key to file
    key.private_key = nil
    write_pem_file pubfile, key.to_pem
  end
end

#create_endpoint_with_addressObject



424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/enfcli/commands/xiam.rb', line 424

def create_endpoint_with_address
  try_with_rescue_in_session do
    # Read address
    ipv6_address = EnfCli::IPV6.new options[:address]

    # read key
    keyfile = EnfCli::expand_path(options["public-key-in-file"])
    key = read_ec_key_file keyfile
    pub = key.public_key

    # create_endpoint
    provision_endpoint ipv6_address, pub
  end
end

#get_groupObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/enfcli/commands/xiam.rb', line 162

def get_group
  try_with_rescue_in_session do
    # scan if gid not in options
    if options[:gid]
      gid = options[:gid]
    else
      gid = scan_gid
    end

    data = EnfApi::Iam.instance.get_group gid
    groups = [data]

    # display data
    display_groups groups
  end
end

#list_groupsObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/enfcli/commands/xiam.rb', line 183

def list_groups
  try_with_rescue_in_session do
    # The xiam API doesn't properly support the 'network' query
    # parameter for listing groups. Until that is fixed, use the
    # /network/N/groups resource instead.
    #
    # See https://github.com/xaptum/xiam/issues/54
    #
    if options[:network]
      # validate network
      network = EnfCli::IPV6Cidr.new options[:network]

      # call the api
      data = EnfApi::Iam.instance.list_network_groups network.to_s
    else
      # call the api
      data = EnfApi::Iam.instance.list_groups
    end

    groups = data[:body]
    display_groups groups
  end
end

#provision_groupObject



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
# File 'lib/enfcli/commands/xiam.rb', line 298

def provision_group
  try_with_rescue_in_session do
    qr_code_info = scan_group_qr_code()

    # Extract gpk and basename
    gpk_info = qr_code_info.split(",")
    basename = gpk_info[0]
    gpk = gpk_info[1]

    # Calculate gid
    gid = calculate_gid basename, gpk

    # Extract options
    operator = options.operator.join(" ").gsub(/\A"+(.*?)"+\Z/m, '\1')
    quantity = options[:'device-count']

    # create NEW_PROVISIONING
    new_provisioning = {
      :group_id => gid,
      :quantity => quantity,
      :operator => operator,
    }

    # create NEW_GROUP
    new_group = {
      :group_public_key => gpk,
      :basename => basename,
      :provisionings => [new_provisioning],
    }

    # Post to server
    EnfApi::Iam.instance.provision_group new_group

    # display success
    say "Provisioned a new DAA group #{gid}", :green
  end
end

#set_group_default_networkObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/enfcli/commands/xiam.rb', line 211

def set_group_default_network
  try_with_rescue_in_session do
    network = EnfCli::IPV6Cidr.new(options[:network]).to_s

    # scan if gid not in options
    if options[:gid]
      gid = options[:gid]
    else
      gid = scan_gid
    end

    # create MODIFIED_GROUP
    modified_group = {
      :gid => gid,
      :default_network => network,
    }

    # Post to server
    EnfApi::Iam.instance.update_group gid, modified_group

    say "Set #{network} as default network for group #{gid}", :green
  end
end

#set_group_domainObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/enfcli/commands/xiam.rb', line 239

def set_group_domain
  try_with_rescue_in_session do
    domain = EnfCli::IPV6Cidr.new(options[:domain]).to_s

    # scan if gid not in options
    if options[:gid]
      gid = options[:gid]
    else
      gid = scan_gid
    end

    # create MODIFIED_GROUP
    modified_group = {
      :gid => gid,
      :domain => domain,
    }

    # Post to server
    EnfApi::Iam.instance.update_group gid, modified_group

    say "Associated group #{gid} with #{domain}", :green
  end
end

#update_endpoint_certObject



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/enfcli/commands/xiam.rb', line 489

def update_endpoint_cert
  try_with_rescue_in_session do
    # read options
    certfile = EnfCli::expand_path(options["cert-in-file"])
    raise EnfCli::ERROR, "#{certfile} does not exist!" unless File.exists?(certfile)

    # read cert file
    rawcert = File.read certfile
    cert = OpenSSL::X509::Certificate.new rawcert

    # get subject
    subject = cert.subject.to_a

    # get ipv6 CN record
    cn = subject.select { |d| "CN".eql?(d[0].upcase) }
    ipv6_address = EnfCli::IPV6.new cn[0][1]

    # get the public key
    pub = cert.public_key.public_key

    # update endpoint
    update_endpoint_credentials ipv6_address, pub
  end
end

#update_endpoint_keyObject



471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/enfcli/commands/xiam.rb', line 471

def update_endpoint_key
  try_with_rescue_in_session do
    # Read address
    ipv6_address = EnfCli::IPV6.new options[:address]

    # read key
    keyfile = EnfCli::expand_path(options["public-key-in-file"])
    key = read_ec_key_file keyfile
    pub = key.public_key

    # update endpoint
    update_endpoint_credentials ipv6_address, pub
  end
end