vici: Refactor how commands are called in the Ruby bindings
Also expose a method to call arbitrary commands, which allows calling not yet wrapped commands. Exceptions are raised for all commands if the response includes a negative "success" key (similar to how it's done in the Python bindings).
This commit is contained in:
@@ -3,6 +3,9 @@
|
|||||||
# strongSwan VICI protocol. The Connection class provides a high-level
|
# strongSwan VICI protocol. The Connection class provides a high-level
|
||||||
# interface to issue requests or listen for events.
|
# interface to issue requests or listen for events.
|
||||||
#
|
#
|
||||||
|
# Copyright (C) 2019 Tobias Brunner
|
||||||
|
# HSR Hochschule fuer Technik Rapperswil
|
||||||
|
#
|
||||||
# Copyright (C) 2014 Martin Willi
|
# Copyright (C) 2014 Martin Willi
|
||||||
# Copyright (C) 2014 revosec AG
|
# Copyright (C) 2014 revosec AG
|
||||||
#
|
#
|
||||||
@@ -25,7 +28,6 @@
|
|||||||
# THE SOFTWARE.
|
# THE SOFTWARE.
|
||||||
|
|
||||||
module Vici
|
module Vici
|
||||||
|
|
||||||
##
|
##
|
||||||
# Vici specific exception all others inherit from
|
# Vici specific exception all others inherit from
|
||||||
class Error < StandardError
|
class Error < StandardError
|
||||||
@@ -433,117 +435,115 @@ module Vici
|
|||||||
##
|
##
|
||||||
# Load a connection into the daemon.
|
# Load a connection into the daemon.
|
||||||
def load_conn(conn)
|
def load_conn(conn)
|
||||||
check_success(@transp.request("load-conn", Message.new(conn)))
|
call("load-conn", Message.new(conn))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Unload a connection from the daemon.
|
# Unload a connection from the daemon.
|
||||||
def unload_conn(conn)
|
def unload_conn(conn)
|
||||||
check_success(@transp.request("unload-conn", Message.new(conn)))
|
call("unload-conn", Message.new(conn))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Get the names of connections managed by vici.
|
# Get the names of connections managed by vici.
|
||||||
def get_conns()
|
def get_conns()
|
||||||
@transp.request("get-conns").root
|
call("get-conns")
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Flush credential cache.
|
# Flush credential cache.
|
||||||
def flush_certs(match = nil)
|
def flush_certs(match = nil)
|
||||||
check_success(@transp.request("flush-certs", Message.new(match)))
|
call("flush-certs", Message.new(match))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Clear all loaded credentials.
|
# Clear all loaded credentials.
|
||||||
def clear_creds()
|
def clear_creds()
|
||||||
check_success(@transp.request("clear-creds"))
|
call("clear-creds")
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Load a certificate into the daemon.
|
# Load a certificate into the daemon.
|
||||||
def load_cert(cert)
|
def load_cert(cert)
|
||||||
check_success(@transp.request("load-cert", Message.new(cert)))
|
call("load-cert", Message.new(cert))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Load a private key into the daemon.
|
# Load a private key into the daemon.
|
||||||
def load_key(key)
|
def load_key(key)
|
||||||
check_success(@transp.request("load-key", Message.new(key)))
|
call("load-key", Message.new(key))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Load a shared key into the daemon.
|
# Load a shared key into the daemon.
|
||||||
def load_shared(shared)
|
def load_shared(shared)
|
||||||
check_success(@transp.request("load-shared", Message.new(shared)))
|
call("load-shared", Message.new(shared))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Load a virtual IP / attribute pool
|
# Load a virtual IP / attribute pool
|
||||||
def load_pool(pool)
|
def load_pool(pool)
|
||||||
check_success(@transp.request("load-pool", Message.new(pool)))
|
call("load-pool", Message.new(pool))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Unload a virtual IP / attribute pool
|
# Unload a virtual IP / attribute pool
|
||||||
def unload_pool(pool)
|
def unload_pool(pool)
|
||||||
check_success(@transp.request("unload-pool", Message.new(pool)))
|
call("unload-pool", Message.new(pool))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Get the currently loaded pools.
|
# Get the currently loaded pools.
|
||||||
def get_pools(options)
|
def get_pools(options)
|
||||||
@transp.request("get-pools", Message.new(options)).root
|
call("get-pools", Message.new(options))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Initiate a connection. The provided closure is invoked for each log line.
|
# Initiate a connection. The provided closure is invoked for each log line.
|
||||||
def initiate(options, &block)
|
def initiate(options, &block)
|
||||||
check_success(call_with_event("initiate", Message.new(options),
|
call_with_event("initiate", Message.new(options), "control-log", &block)
|
||||||
"control-log", &block))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Terminate a connection. The provided closure is invoked for each log line.
|
# Terminate a connection. The provided closure is invoked for each log line.
|
||||||
def terminate(options, &block)
|
def terminate(options, &block)
|
||||||
check_success(call_with_event("terminate", Message.new(options),
|
call_with_event("terminate", Message.new(options), "control-log", &block)
|
||||||
"control-log", &block))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Redirect an IKE_SA.
|
# Redirect an IKE_SA.
|
||||||
def redirect(options)
|
def redirect(options)
|
||||||
check_success(@transp.request("redirect", Message.new(options)))
|
call("redirect", Message.new(options))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Install a shunt/route policy.
|
# Install a shunt/route policy.
|
||||||
def install(policy)
|
def install(policy)
|
||||||
check_success(@transp.request("install", Message.new(policy)))
|
call("install", Message.new(policy))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Uninstall a shunt/route policy.
|
# Uninstall a shunt/route policy.
|
||||||
def uninstall(policy)
|
def uninstall(policy)
|
||||||
check_success(@transp.request("uninstall", Message.new(policy)))
|
call("uninstall", Message.new(policy))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Reload strongswan.conf settings.
|
# Reload strongswan.conf settings.
|
||||||
def reload_settings
|
def reload_settings
|
||||||
check_success(@transp.request("reload-settings", nil))
|
call("reload-settings")
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Get daemon statistics and information.
|
# Get daemon statistics and information.
|
||||||
def stats
|
def stats
|
||||||
@transp.request("stats", nil).root
|
call("stats")
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Get daemon version information
|
# Get daemon version information
|
||||||
def version
|
def version
|
||||||
@transp.request("version", nil).root
|
call("version")
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
@@ -573,6 +573,13 @@ module Vici
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Issue a command request. Checks if the reply of a command indicates
|
||||||
|
# "success", otherwise raises a CommandExecError exception.
|
||||||
|
def call(command, request = nil)
|
||||||
|
check_success(@transp.request(command, request))
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Issue a command request, but register for a specific event while the
|
# Issue a command request, but register for a specific event while the
|
||||||
# command is active. VICI uses this mechanism to stream potentially large
|
# command is active. VICI uses this mechanism to stream potentially large
|
||||||
@@ -590,7 +597,7 @@ module Vici
|
|||||||
ensure
|
ensure
|
||||||
@transp.unregister(event, method(:call_event))
|
@transp.unregister(event, method(:call_event))
|
||||||
end
|
end
|
||||||
reply
|
check_success(reply)
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
@@ -598,7 +605,7 @@ module Vici
|
|||||||
# CommandExecError exception
|
# CommandExecError exception
|
||||||
def check_success(reply)
|
def check_success(reply)
|
||||||
root = reply.root
|
root = reply.root
|
||||||
if root["success"] != "yes"
|
if root.key?("success") && root["success"] != "yes"
|
||||||
raise CommandExecError, root["errmsg"]
|
raise CommandExecError, root["errmsg"]
|
||||||
end
|
end
|
||||||
root
|
root
|
||||||
|
|||||||
Reference in New Issue
Block a user