class Vici::Connection

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.

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

Public Class Methods

new(socket = nil) click to toggle source

Create a connection, optionally using the given socket

# File lib/vici.rb, line 369
def initialize(socket = nil)
  socket = UNIXSocket.new("/var/run/charon.vici") if socket.nil?
  @transp = Transport.new(socket)
end

Public Instance Methods

call(command, request = nil) click to toggle source

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

# File lib/vici.rb, line 627
def call(command, request = nil)
  check_success(@transp.request(command, request))
end
call_with_event(command, request, event, &block) click to toggle source

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.

# File lib/vici.rb, line 636
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) click to toggle source

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

# File lib/vici.rb, line 654
def check_success(reply)
  root = reply.root
  if root.key?("success") && root["success"] != "yes"
    raise CommandExecError, root["errmsg"]
  end

  root
end
clear_creds() click to toggle source

Clear all loaded credentials.

# File lib/vici.rb, line 545
def clear_creds
  call("clear-creds")
end
flush_certs(match = nil) click to toggle source

Flush credential cache.

# File lib/vici.rb, line 539
def flush_certs(match = nil)
  call("flush-certs", Message.new(match))
end
get_algorithms() click to toggle source

Get currently loaded algorithms and their implementation.

# File lib/vici.rb, line 581
def get_algorithms
  call("get-algorithms")
end
get_authorities() click to toggle source

Get the names of certification authorities managed by vici.

# File lib/vici.rb, line 473
def get_authorities
  call("get-authorities")
end
get_conns() click to toggle source

Get the names of connections managed by vici.

# File lib/vici.rb, line 452
def get_conns
  call("get-conns")
end
get_counters(options = nil) click to toggle source

Get global or connection-specific counters for IKE events.

# File lib/vici.rb, line 587
def get_counters(options = nil)
  call("get-counters", Message.new(options))
end
get_keys() click to toggle source

Get the identifiers of private keys loaded via vici.

# File lib/vici.rb, line 509
def get_keys
  call("get-keys")
end
get_pools(options) click to toggle source

Get the currently loaded pools.

# File lib/vici.rb, line 575
def get_pools(options)
  call("get-pools", Message.new(options))
end
get_shared() click to toggle source

Get the unique identifiers of shared keys loaded via vici.

# File lib/vici.rb, line 533
def get_shared
  call("get-shared")
end
initiate(options, &block) click to toggle source

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

# File lib/vici.rb, line 394
def initiate(options, &block)
  call_with_event("initiate", Message.new(options), "control-log", &block)
end
install(policy) click to toggle source

Install a shunt/route policy.

# File lib/vici.rb, line 418
def install(policy)
  call("install", Message.new(policy))
end
list_authorities(match = nil, &block) click to toggle source

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

# File lib/vici.rb, line 466
def list_authorities(match = nil, &block)
  call_with_event("list-authorities", Message.new(match), "list-authority",
                  &block)
end
list_certs(match = nil, &block) click to toggle source

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

# File lib/vici.rb, line 459
def list_certs(match = nil, &block)
  call_with_event("list-certs", Message.new(match), "list-cert", &block)
end
list_conns(match = nil, &block) click to toggle source

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

# File lib/vici.rb, line 446
def list_conns(match = nil, &block)
  call_with_event("list-conns", Message.new(match), "list-conn", &block)
end
list_policies(match, &block) click to toggle source

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

# File lib/vici.rb, line 438
def list_policies(match, &block)
  call_with_event("list-policies", Message.new(match), "list-policy",
                  &block)
end
list_sas(match = nil, &block) click to toggle source

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

# File lib/vici.rb, line 431
def list_sas(match = nil, &block)
  call_with_event("list-sas", Message.new(match), "list-sa", &block)
end
listen_events(events, &block) click to toggle source

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.

# File lib/vici.rb, line 603
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) click to toggle source

Load a certification authority into the daemon.

# File lib/vici.rb, line 551
def load_authority(authority)
  call("load-authority", Message.new(authority))
end
load_cert(cert) click to toggle source

Load a certificate into the daemon.

# File lib/vici.rb, line 491
def load_cert(cert)
  call("load-cert", Message.new(cert))
end
load_conn(conn) click to toggle source

Load a connection into the daemon.

# File lib/vici.rb, line 479
def load_conn(conn)
  call("load-conn", Message.new(conn))
end
load_key(key) click to toggle source

Load a private key into the daemon.

# File lib/vici.rb, line 497
def load_key(key)
  call("load-key", Message.new(key))
end
load_pool(pool) click to toggle source

Load a virtual IP / attribute pool into the daemon.

# File lib/vici.rb, line 563
def load_pool(pool)
  call("load-pool", Message.new(pool))
end
load_shared(shared) click to toggle source

Load a shared key into the daemon.

# File lib/vici.rb, line 521
def load_shared(shared)
  call("load-shared", Message.new(shared))
end
load_token(token) click to toggle source

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

# File lib/vici.rb, line 515
def load_token(token)
  call("load-token", Message.new(token))
end
redirect(options) click to toggle source

Redirect an IKE_SA.

# File lib/vici.rb, line 412
def redirect(options)
  call("redirect", Message.new(options))
end
rekey(options) click to toggle source

Initiate the rekeying of an SA.

# File lib/vici.rb, line 406
def rekey(options)
  call("rekey", Message.new(options))
end
reload_settings() click to toggle source

Reload strongswan.conf settings.

# File lib/vici.rb, line 388
def reload_settings
  call("reload-settings")
end
reset_counters(options = nil) click to toggle source

Reset global or connection-specific IKE event counters.

# File lib/vici.rb, line 593
def reset_counters(options = nil)
  call("reset-counters", Message.new(options))
end
stats() click to toggle source

Get daemon statistics and information.

# File lib/vici.rb, line 382
def stats
  call("stats")
end
terminate(options, &block) click to toggle source

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

# File lib/vici.rb, line 400
def terminate(options, &block)
  call_with_event("terminate", Message.new(options), "control-log", &block)
end
uninstall(policy) click to toggle source

Uninstall a shunt/route policy.

# File lib/vici.rb, line 424
def uninstall(policy)
  call("uninstall", Message.new(policy))
end
unload_authority(authority) click to toggle source

Unload a certification authority from the daemon.

# File lib/vici.rb, line 557
def unload_authority(authority)
  call("unload-authority", Message.new(authority))
end
unload_conn(conn) click to toggle source

Unload a connection from the daemon.

# File lib/vici.rb, line 485
def unload_conn(conn)
  call("unload-conn", Message.new(conn))
end
unload_key(key) click to toggle source

Unload a private key from the daemon.

# File lib/vici.rb, line 503
def unload_key(key)
  call("unload-key", Message.new(key))
end
unload_pool(pool) click to toggle source

Unload a virtual IP / attribute pool from the daemon.

# File lib/vici.rb, line 569
def unload_pool(pool)
  call("unload-pool", Message.new(pool))
end
unload_shared(shared) click to toggle source

Unload a shared key from the daemon.

# File lib/vici.rb, line 527
def unload_shared(shared)
  call("unload-shared", Message.new(shared))
end
version() click to toggle source

Get daemon version information

# File lib/vici.rb, line 376
def version
  call("version")
end