Class: Vici::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/vici.rb

Overview

The Connection class provides the high-level interface to monitor, configure and control the IKE daemon. It takes a connected stream-oriented Socket for the communication with the IKE daemon.

This class takes and returns ruby objects for the exchanged message data.

  • Sections get encoded as Hash, containing other sections as Hash, or

  • Key/Values, where the values are Strings as Hash values

  • Lists get encoded as Arrays with String values

Non-String values that are not a Hash nor an Array get converted with .to_s during encoding.

Instance Method Summary collapse

Constructor Details

#initialize(socket = nil) ⇒ Connection

Create a connection, optionally using the given socket



366
367
368
369
# File 'lib/vici.rb', line 366

def initialize(socket = nil)
  socket = UNIXSocket.new("/var/run/charon.vici") if socket.nil?
  @transp = Transport.new(socket)
end

Instance Method Details

#call(command, request = nil) ⇒ Object

Issue a command request. Checks if the reply of a command indicates “success”, otherwise raises a CommandExecError exception.



624
625
626
# File 'lib/vici.rb', line 624

def call(command, request = nil)
  check_success(@transp.request(command, request))
end

#call_with_event(command, request, event, &block) ⇒ Object

Issue a command request, but register for a specific event while the command is active. VICI uses this mechanism to stream potentially large data objects continuously. The provided closure is invoked for all event messages.



633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/vici.rb', line 633

def call_with_event(command, request, event, &block)
  self.class.instance_eval do
    define_method(:call_event) do |_label, message|
      block.call(message.root)
    end
  end
  @transp.register(event, method(:call_event))
  begin
    reply = @transp.request(command, request)
  ensure
    @transp.unregister(event, method(:call_event))
  end
  check_success(reply)
end

#check_success(reply) ⇒ Object

Check if the reply of a command indicates “success”, otherwise raise a CommandExecError exception



651
652
653
654
655
656
657
658
# File 'lib/vici.rb', line 651

def check_success(reply)
  root = reply.root
  if root.key?("success") && root["success"] != "yes"
    raise CommandExecError, root["errmsg"]
  end

  root
end

#clear_credsObject

Clear all loaded credentials.



542
543
544
# File 'lib/vici.rb', line 542

def clear_creds
  call("clear-creds")
end

#flush_certs(match = nil) ⇒ Object

Flush credential cache.



536
537
538
# File 'lib/vici.rb', line 536

def flush_certs(match = nil)
  call("flush-certs", Message.new(match))
end

#get_algorithmsObject

Get currently loaded algorithms and their implementation.



578
579
580
# File 'lib/vici.rb', line 578

def get_algorithms
  call("get-algorithms")
end

#get_authoritiesObject

Get the names of certification authorities managed by vici.



470
471
472
# File 'lib/vici.rb', line 470

def get_authorities
  call("get-authorities")
end

#get_connsObject

Get the names of connections managed by vici.



449
450
451
# File 'lib/vici.rb', line 449

def get_conns
  call("get-conns")
end

#get_counters(options = nil) ⇒ Object

Get global or connection-specific counters for IKE events.



584
585
586
# File 'lib/vici.rb', line 584

def get_counters(options = nil)
  call("get-counters", Message.new(options))
end

#get_keysObject

Get the identifiers of private keys loaded via vici.



506
507
508
# File 'lib/vici.rb', line 506

def get_keys
  call("get-keys")
end

#get_pools(options) ⇒ Object

Get the currently loaded pools.



572
573
574
# File 'lib/vici.rb', line 572

def get_pools(options)
  call("get-pools", Message.new(options))
end

#get_sharedObject

Get the unique identifiers of shared keys loaded via vici.



530
531
532
# File 'lib/vici.rb', line 530

def get_shared
  call("get-shared")
end

#initiate(options, &block) ⇒ Object

Initiate a connection. The provided closure is invoked for each log line.



391
392
393
# File 'lib/vici.rb', line 391

def initiate(options, &block)
  call_with_event("initiate", Message.new(options), "control-log", &block)
end

#install(policy) ⇒ Object

Install a shunt/route policy.



415
416
417
# File 'lib/vici.rb', line 415

def install(policy)
  call("install", Message.new(policy))
end

#list_authorities(match = nil, &block) ⇒ Object

List matching loaded certification authorities. The provided closure is invoked for each matching certification authority definition.



463
464
465
466
# File 'lib/vici.rb', line 463

def list_authorities(match = nil, &block)
  call_with_event("list-authorities", Message.new(match), "list-authority",
                  &block)
end

#list_certs(match = nil, &block) ⇒ Object

List matching loaded certificates. The provided closure is invoked for each matching certificate definition.



456
457
458
# File 'lib/vici.rb', line 456

def list_certs(match = nil, &block)
  call_with_event("list-certs", Message.new(match), "list-cert", &block)
end

#list_conns(match = nil, &block) ⇒ Object

List matching loaded connections. The provided closure is invoked for each matching connection.



443
444
445
# File 'lib/vici.rb', line 443

def list_conns(match = nil, &block)
  call_with_event("list-conns", Message.new(match), "list-conn", &block)
end

#list_policies(match, &block) ⇒ Object

List matching installed policies. The provided closure is invoked for each matching policy.



435
436
437
438
# File 'lib/vici.rb', line 435

def list_policies(match, &block)
  call_with_event("list-policies", Message.new(match), "list-policy",
                  &block)
end

#list_sas(match = nil, &block) ⇒ Object

List matching active SAs. The provided closure is invoked for each matching SA.



428
429
430
# File 'lib/vici.rb', line 428

def list_sas(match = nil, &block)
  call_with_event("list-sas", Message.new(match), "list-sa", &block)
end

#listen_events(events, &block) ⇒ Object

Listen for a set of event messages. This call is blocking, and invokes the passed closure for each event received. The closure receives the event name and the event message as argument. To stop listening, the closure may raise a StopEventListening exception, the only caught exception.



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/vici.rb', line 600

def listen_events(events, &block)
  self.class.instance_eval do
    define_method(:listen_event) do |label, message|
      block.call(label, message.root)
    end
  end
  events.each do |event|
    @transp.register(event, method(:listen_event))
  end
  begin
    loop do
      @transp.read_and_dispatch_event
    end
  rescue StopEventListening
  ensure
    events.each do |event|
      @transp.unregister(event, method(:listen_event))
    end
  end
end

#load_authority(authority) ⇒ Object

Load a certification authority into the daemon.



548
549
550
# File 'lib/vici.rb', line 548

def load_authority(authority)
  call("load-authority", Message.new(authority))
end

#load_cert(cert) ⇒ Object

Load a certificate into the daemon.



488
489
490
# File 'lib/vici.rb', line 488

def load_cert(cert)
  call("load-cert", Message.new(cert))
end

#load_conn(conn) ⇒ Object

Load a connection into the daemon.



476
477
478
# File 'lib/vici.rb', line 476

def load_conn(conn)
  call("load-conn", Message.new(conn))
end

#load_key(key) ⇒ Object

Load a private key into the daemon.



494
495
496
# File 'lib/vici.rb', line 494

def load_key(key)
  call("load-key", Message.new(key))
end

#load_pool(pool) ⇒ Object

Load a virtual IP / attribute pool into the daemon.



560
561
562
# File 'lib/vici.rb', line 560

def load_pool(pool)
  call("load-pool", Message.new(pool))
end

#load_shared(shared) ⇒ Object

Load a shared key into the daemon.



518
519
520
# File 'lib/vici.rb', line 518

def load_shared(shared)
  call("load-shared", Message.new(shared))
end

#load_token(token) ⇒ Object

Load a private key located on a token into the daemon.



512
513
514
# File 'lib/vici.rb', line 512

def load_token(token)
  call("load-token", Message.new(token))
end

#redirect(options) ⇒ Object

Redirect an IKE_SA.



409
410
411
# File 'lib/vici.rb', line 409

def redirect(options)
  call("redirect", Message.new(options))
end

#rekey(options) ⇒ Object

Initiate the rekeying of an SA.



403
404
405
# File 'lib/vici.rb', line 403

def rekey(options)
  call("rekey", Message.new(options))
end

#reload_settingsObject

Reload strongswan.conf settings.



385
386
387
# File 'lib/vici.rb', line 385

def reload_settings
  call("reload-settings")
end

#reset_counters(options = nil) ⇒ Object

Reset global or connection-specific IKE event counters.



590
591
592
# File 'lib/vici.rb', line 590

def reset_counters(options = nil)
  call("reset-counters", Message.new(options))
end

#statsObject

Get daemon statistics and information.



379
380
381
# File 'lib/vici.rb', line 379

def stats
  call("stats")
end

#terminate(options, &block) ⇒ Object

Terminate a connection. The provided closure is invoked for each log line.



397
398
399
# File 'lib/vici.rb', line 397

def terminate(options, &block)
  call_with_event("terminate", Message.new(options), "control-log", &block)
end

#uninstall(policy) ⇒ Object

Uninstall a shunt/route policy.



421
422
423
# File 'lib/vici.rb', line 421

def uninstall(policy)
  call("uninstall", Message.new(policy))
end

#unload_authority(authority) ⇒ Object

Unload a certification authority from the daemon.



554
555
556
# File 'lib/vici.rb', line 554

def unload_authority(authority)
  call("unload-authority", Message.new(authority))
end

#unload_conn(conn) ⇒ Object

Unload a connection from the daemon.



482
483
484
# File 'lib/vici.rb', line 482

def unload_conn(conn)
  call("unload-conn", Message.new(conn))
end

#unload_key(key) ⇒ Object

Unload a private key from the daemon.



500
501
502
# File 'lib/vici.rb', line 500

def unload_key(key)
  call("unload-key", Message.new(key))
end

#unload_pool(pool) ⇒ Object

Unload a virtual IP / attribute pool from the daemon.



566
567
568
# File 'lib/vici.rb', line 566

def unload_pool(pool)
  call("unload-pool", Message.new(pool))
end

#unload_shared(shared) ⇒ Object

Unload a shared key from the daemon.



524
525
526
# File 'lib/vici.rb', line 524

def unload_shared(shared)
  call("unload-shared", Message.new(shared))
end

#versionObject

Get daemon version information



373
374
375
# File 'lib/vici.rb', line 373

def version
  call("version")
end