vici: Some code style fixes in the Ruby bindings

As reported by rubocop (some issues were not fixed, in particular
related to class/method length metrics).
This commit is contained in:
Tobias Brunner
2019-04-26 10:15:43 +02:00
parent 1fef01af58
commit cc2ef8f8a7
2 changed files with 105 additions and 131 deletions
@@ -0,0 +1,5 @@
Naming/AccessorMethodName:
Enabled: false
Style/StringLiterals:
EnforcedStyle: double_quotes
+100 -131
View File
@@ -78,12 +78,10 @@ module Vici
class StopEventListening < Exception class StopEventListening < Exception
end end
## ##
# The Message class provides the low level encoding and decoding of vici # The Message class provides the low level encoding and decoding of vici
# protocol messages. Directly using this class is usually not required. # protocol messages. Directly using this class is usually not required.
class Message class Message
SECTION_START = 1 SECTION_START = 1
SECTION_END = 2 SECTION_END = 2
KEY_VALUE = 3 KEY_VALUE = 3
@@ -92,8 +90,8 @@ module Vici
LIST_END = 6 LIST_END = 6
def initialize(data = "") def initialize(data = "")
if data == nil if data.nil?
@root = Hash.new() @root = {}
elsif data.is_a?(Hash) elsif data.is_a?(Hash)
@root = data @root = data
else else
@@ -104,18 +102,14 @@ module Vici
## ##
# Get the raw byte encoding of an on-the-wire message # Get the raw byte encoding of an on-the-wire message
def encoding def encoding
if @encoded == nil @encoded = encode(@root) if @encoded.nil?
@encoded = encode(@root)
end
@encoded @encoded
end end
## ##
# Get the root element of the parsed ruby data structures # Get the root element of the parsed ruby data structures
def root def root
if @root == nil @root = parse(@encoded) if @root.nil?
@root = parse(@encoded)
end
@root @root
end end
@@ -126,9 +120,7 @@ module Vici
end end
def encode_value(value) def encode_value(value)
if value.class != String value = value.to_s if value.class != String
value = value.to_s
end
[value.length].pack("n") << value [value.length].pack("n") << value
end end
@@ -152,18 +144,13 @@ module Vici
def encode(node) def encode(node)
encoding = "" encoding = ""
node.each do |key, value| node.each do |key, value|
case value.class encoding = if value.is_a?(Hash)
when String, Fixnum, true, false encode_section(encoding, key, value)
encoding = encode_kv(encoding, key, value) elsif value.is_a?(Array)
else encode_list(encoding, key, value)
if value.is_a?(Hash) else
encoding = encode_section(encoding, key, value) encode_kv(encoding, key, value)
elsif value.is_a?(Array) end
encoding = encode_list(encoding, key, value)
else
encoding = encode_kv(encoding, key, value)
end
end
end end
encoding encoding
end end
@@ -171,63 +158,57 @@ module Vici
def parse_name(encoding) def parse_name(encoding)
len = encoding.unpack("c")[0] len = encoding.unpack("c")[0]
name = encoding[1, len] name = encoding[1, len]
return encoding[(1 + len)..-1], name [encoding[(1 + len)..-1], name]
end end
def parse_value(encoding) def parse_value(encoding)
len = encoding.unpack("n")[0] len = encoding.unpack("n")[0]
value = encoding[2, len] value = encoding[2, len]
return encoding[(2 + len)..-1], value [encoding[(2 + len)..-1], value]
end end
def parse(encoding) def parse(encoding)
stack = [Hash.new] stack = [{}]
list = nil list = nil
while encoding.length != 0 do until encoding.empty?
type = encoding.unpack("c")[0] type = encoding.unpack("c")[0]
encoding = encoding[1..-1] encoding = encoding[1..-1]
case type case type
when SECTION_START when SECTION_START
encoding, name = parse_name(encoding) encoding, name = parse_name(encoding)
stack.push(stack[-1][name] = Hash.new) stack.push(stack[-1][name] = {})
when SECTION_END when SECTION_END
if stack.length() == 1 raise ParseError, "unexpected section end" if stack.length == 1
raise ParseError, "unexpected section end" stack.pop
end when KEY_VALUE
stack.pop() encoding, name = parse_name(encoding)
when KEY_VALUE encoding, value = parse_value(encoding)
encoding, name = parse_name(encoding) stack[-1][name] = value
encoding, value = parse_value(encoding) when LIST_START
stack[-1][name] = value encoding, name = parse_name(encoding)
when LIST_START stack[-1][name] = []
encoding, name = parse_name(encoding) list = name
stack[-1][name] = [] when LIST_ITEM
list = name raise ParseError, "unexpected list item" if list.nil?
when LIST_ITEM encoding, value = parse_value(encoding)
raise ParseError, "unexpected list item" if list == nil stack[-1][list].push(value)
encoding, value = parse_value(encoding) when LIST_END
stack[-1][list].push(value) raise ParseError, "unexpected list end" if list.nil?
when LIST_END list = nil
raise ParseError, "unexpected list end" if list == nil else
list = nil raise ParseError, "invalid type: #{type}"
else
raise ParseError, "invalid type: #{type}"
end end
end end
if stack.length() > 1 raise ParseError, "unexpected message end" if stack.length > 1
raise ParseError, "unexpected message end"
end
stack[0] stack[0]
end end
end end
## ##
# The Transport class implements to low level segmentation of packets # The Transport class implements to low level segmentation of packets
# to the underlying transport stream. Directly using this class is usually # to the underlying transport stream. Directly using this class is usually
# not required. # not required.
class Transport class Transport
CMD_REQUEST = 0 CMD_REQUEST = 0
CMD_RESPONSE = 1 CMD_RESPONSE = 1
CMD_UNKNOWN = 2 CMD_UNKNOWN = 2
@@ -241,18 +222,16 @@ module Vici
# Create a transport layer using a provided socket for communication. # Create a transport layer using a provided socket for communication.
def initialize(socket) def initialize(socket)
@socket = socket @socket = socket
@events = Hash.new @events = {}
end end
## ##
# Receive data from socket, until len bytes read # Receive data from socket, until len bytes read
def recv_all(len) def recv_all(len)
encoding = "" encoding = ""
while encoding.length < len do while encoding.length < len
data = @socket.recv(len - encoding.length) data = @socket.recv(len - encoding.length)
if data.empty? raise TransportError, "connection closed" if data.empty?
raise TransportError, "connection closed"
end
encoding << data encoding << data
end end
encoding encoding
@@ -262,9 +241,7 @@ module Vici
# Send data to socket, until all bytes sent # Send data to socket, until all bytes sent
def send_all(encoding) def send_all(encoding)
len = 0 len = 0
while len < encoding.length do len += @socket.send(encoding[len..-1], 0) while len < encoding.length
len += @socket.send(encoding[len..-1], 0)
end
end end
## ##
@@ -272,12 +249,8 @@ module Vici
# specifies the message, the optional label and message get appended. # specifies the message, the optional label and message get appended.
def write(type, label, message) def write(type, label, message)
encoding = "" encoding = ""
if label encoding << label.length << label if label
encoding << label.length << label encoding << message.encoding if message
end
if message
encoding << message.encoding
end
send_all([encoding.length + 1, type].pack("Nc") + encoding) send_all([encoding.length + 1, type].pack("Nc") + encoding)
end end
@@ -290,18 +263,20 @@ module Vici
type = encoding.unpack("c")[0] type = encoding.unpack("c")[0]
len = 1 len = 1
case type case type
when CMD_REQUEST, EVENT_REGISTER, EVENT_UNREGISTER, EVENT when CMD_REQUEST, EVENT_REGISTER, EVENT_UNREGISTER, EVENT
label = encoding[2, encoding[1].unpack("c")[0]] label = encoding[2, encoding[1].unpack("c")[0]]
len += label.length + 1 len += label.length + 1
when CMD_RESPONSE, CMD_UNKNOWN, EVENT_CONFIRM, EVENT_UNKNOWN when CMD_RESPONSE, CMD_UNKNOWN, EVENT_CONFIRM, EVENT_UNKNOWN
label = nil label = nil
else else
raise TransportError, "invalid message: #{type}" raise TransportError, "invalid message: #{type}"
end end
if encoding.length == len message = if encoding.length == len
return type, label, Message.new Message.new
end else
return type, label, Message.new(encoding[len..-1]) Message.new(encoding[len..-1])
end
[type, label, message]
end end
def dispatch_event(name, message) def dispatch_event(name, message)
@@ -312,22 +287,17 @@ module Vici
def read_and_dispatch_event def read_and_dispatch_event
type, label, message = read type, label, message = read
p raise TransportError, "unexpected message: #{type}" if type != EVENT
if type == EVENT
dispatch_event(label, message) dispatch_event(label, message)
else
raise TransportError, "unexpected message: #{type}"
end
end end
def read_and_dispatch_events def read_and_dispatch_events
loop do loop do
type, label, message = read type, label, message = read
if type == EVENT return type, label, message if type != EVENT
dispatch_event(label, message)
else dispatch_event(label, message)
return type, label, message
end
end end
end end
@@ -336,14 +306,14 @@ module Vici
# the reply message on success. # the reply message on success.
def request(name, message = nil) def request(name, message = nil)
write(CMD_REQUEST, name, message) write(CMD_REQUEST, name, message)
type, label, message = read_and_dispatch_events type, _label, message = read_and_dispatch_events
case type case type
when CMD_RESPONSE when CMD_RESPONSE
return message return message
when CMD_UNKNOWN when CMD_UNKNOWN
raise CommandUnknownError, name raise CommandUnknownError, name
else else
raise CommandError, "invalid response for #{name}" raise CommandError, "invalid response for #{name}"
end end
end end
@@ -351,18 +321,18 @@ module Vici
# Register a handler method for the given event name # Register a handler method for the given event name
def register(name, handler) def register(name, handler)
write(EVENT_REGISTER, name, nil) write(EVENT_REGISTER, name, nil)
type, label, message = read_and_dispatch_events type, _label, _message = read_and_dispatch_events
case type case type
when EVENT_CONFIRM when EVENT_CONFIRM
if @events.has_key?(name) if @events.key?(name)
@events[name] += [handler] @events[name] += [handler]
else
@events[name] = [handler];
end
when EVENT_UNKNOWN
raise EventUnknownError, name
else else
raise EventError, "invalid response for #{name} register" @events[name] = [handler]
end
when EVENT_UNKNOWN
raise EventUnknownError, name
else
raise EventError, "invalid response for #{name} register"
end end
end end
@@ -370,19 +340,18 @@ module Vici
# Unregister a handler method for the given event name # Unregister a handler method for the given event name
def unregister(name, handler) def unregister(name, handler)
write(EVENT_UNREGISTER, name, nil) write(EVENT_UNREGISTER, name, nil)
type, label, message = read_and_dispatch_events type, _label, _message = read_and_dispatch_events
case type case type
when EVENT_CONFIRM when EVENT_CONFIRM
@events[name] -= [handler] @events[name] -= [handler]
when EVENT_UNKNOWN when EVENT_UNKNOWN
raise EventUnknownError, name raise EventUnknownError, name
else else
raise EventError, "invalid response for #{name} unregister" raise EventError, "invalid response for #{name} unregister"
end end
end end
end end
## ##
# The Connection class provides the high-level interface to monitor, configure # The Connection class provides the high-level interface to monitor, configure
# and control the IKE daemon. It takes a connected stream-oriented Socket for # and control the IKE daemon. It takes a connected stream-oriented Socket for
@@ -395,11 +364,10 @@ module Vici
# Non-String values that are not a Hash nor an Array get converted with .to_s # Non-String values that are not a Hash nor an Array get converted with .to_s
# during encoding. # during encoding.
class Connection class Connection
##
# Create a connection, optionally using the given socket
def initialize(socket = nil) def initialize(socket = nil)
if socket == nil socket = UNIXSocket.new("/var/run/charon.vici") if socket.nil?
socket = UNIXSocket.new("/var/run/charon.vici")
end
@transp = Transport.new(socket) @transp = Transport.new(socket)
end end
@@ -481,7 +449,7 @@ module Vici
## ##
# Get the names of connections managed by vici. # Get the names of connections managed by vici.
def get_conns() def get_conns
call("get-conns") call("get-conns")
end end
@@ -502,7 +470,7 @@ module Vici
## ##
# Get the names of certification authorities managed by vici. # Get the names of certification authorities managed by vici.
def get_authorities() def get_authorities
call("get-authorities") call("get-authorities")
end end
@@ -538,7 +506,7 @@ module Vici
## ##
# Get the identifiers of private keys loaded via vici. # Get the identifiers of private keys loaded via vici.
def get_keys() def get_keys
call("get-keys") call("get-keys")
end end
@@ -562,7 +530,7 @@ module Vici
## ##
# Get the unique identifiers of shared keys loaded via vici. # Get the unique identifiers of shared keys loaded via vici.
def get_shared() def get_shared
call("get-shared") call("get-shared")
end end
@@ -574,7 +542,7 @@ module Vici
## ##
# Clear all loaded credentials. # Clear all loaded credentials.
def clear_creds() def clear_creds
call("clear-creds") call("clear-creds")
end end
@@ -610,7 +578,7 @@ module Vici
## ##
# Get currently loaded algorithms and their implementation. # Get currently loaded algorithms and their implementation.
def get_algorithms() def get_algorithms
call("get-algorithms") call("get-algorithms")
end end
@@ -667,7 +635,7 @@ module Vici
# event messages. # event messages.
def call_with_event(command, request, event, &block) def call_with_event(command, request, event, &block)
self.class.instance_eval do self.class.instance_eval do
define_method(:call_event) do |label, message| define_method(:call_event) do |_label, message|
block.call(message.root) block.call(message.root)
end end
end end
@@ -688,6 +656,7 @@ module Vici
if root.key?("success") && root["success"] != "yes" if root.key?("success") && root["success"] != "yes"
raise CommandExecError, root["errmsg"] raise CommandExecError, root["errmsg"]
end end
root root
end end
end end