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.
-
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.
Public Class Methods
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
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
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 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 all loaded credentials.
# File lib/vici.rb, line 545 def clear_creds call("clear-creds") end
Flush credential cache.
# File lib/vici.rb, line 539 def flush_certs(match = nil) call("flush-certs", Message.new(match)) end
Get currently loaded algorithms and their implementation.
# File lib/vici.rb, line 581 def get_algorithms call("get-algorithms") end
Get the names of connections managed by vici.
# File lib/vici.rb, line 452 def get_conns call("get-conns") end
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 the identifiers of private keys loaded via vici.
# File lib/vici.rb, line 509 def get_keys call("get-keys") end
Get the currently loaded pools.
# File lib/vici.rb, line 575 def get_pools(options) call("get-pools", Message.new(options)) end
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 a shunt/route policy.
# File lib/vici.rb, line 418 def install(policy) call("install", Message.new(policy)) end
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 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 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 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 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 a certificate into the daemon.
# File lib/vici.rb, line 491 def load_cert(cert) call("load-cert", Message.new(cert)) end
Load a connection into the daemon.
# File lib/vici.rb, line 479 def load_conn(conn) call("load-conn", Message.new(conn)) end
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 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 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 an IKE_SA.
# File lib/vici.rb, line 412 def redirect(options) call("redirect", Message.new(options)) end
Initiate the rekeying of an SA.
# File lib/vici.rb, line 406 def rekey(options) call("rekey", Message.new(options)) end
Reload strongswan.conf settings.
# File lib/vici.rb, line 388 def reload_settings call("reload-settings") end
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
Get daemon statistics and information.
# File lib/vici.rb, line 382 def stats call("stats") end
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 a shunt/route policy.
# File lib/vici.rb, line 424 def uninstall(policy) call("uninstall", Message.new(policy)) end
Unload a connection from the daemon.
# File lib/vici.rb, line 485 def unload_conn(conn) call("unload-conn", Message.new(conn)) end
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 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
Get daemon version information
# File lib/vici.rb, line 376 def version call("version") end