testing: Fix updown script in route-based/net2net-xfrmi-ike scenario

With the update to Python 3 the encoding of the values in vici messages
changed to bytestrings (the keys are properly decoded).  And getting the
first CHILD_SA also needs a change.

The logger is now also initialized after daemonizing to avoid that opened
sockets are closed etc.
This commit is contained in:
Tobias Brunner
2021-09-21 12:50:12 +02:00
parent 3e2841572b
commit 6467f0416f
@@ -10,10 +10,13 @@ import resource
logger = logging.getLogger('updownLogger')
handler = SysLogHandler(address='/dev/log', facility=SysLogHandler.LOG_DAEMON)
handler.setFormatter(logging.Formatter('charon-updown: %(message)s'))
logger.addHandler(handler)
logger.setLevel(logging.INFO)
def setup_logger():
handler = SysLogHandler(address='/dev/log', facility=SysLogHandler.LOG_DAEMON)
handler.setFormatter(logging.Formatter('charon-updown: %(message)s'))
logger.addHandler(handler)
logger.setLevel(logging.INFO)
def handle_interfaces(ike_sa, up):
@@ -48,9 +51,10 @@ def handle_interfaces(ike_sa, up):
def install_routes(ike_sa):
if_id_out = int(ike_sa['if-id-out'], 16)
ifname_out = "xfrm-{}-out".format(if_id_out)
child_sa = next(ike_sa["child-sas"].itervalues())
child_sa = next(iter(ike_sa['child-sas'].values()))
for ts in child_sa['remote-ts']:
ts = ts.decode('UTF-8')
logger.info("add route to %s via %s", ts, ifname_out)
subprocess.call(["ip", "route", "add", ts, "dev", ifname_out])
@@ -64,10 +68,11 @@ resource.setrlimit(resource.RLIMIT_NOFILE, (256, 256))
# daemonize and run parallel to the IKE daemon
with daemon.DaemonContext():
setup_logger()
logger.debug("starting Python updown listener")
try:
session = vici.Session()
ver = session.version()
ver = {k: v.decode("UTF-8") for k, v in session.version().items()}
logger.info("connected to {daemon} {version} ({sysname}, {release}, "
"{machine})".format(**ver))
except:
@@ -79,13 +84,13 @@ with daemon.DaemonContext():
logger.debug("received event: %s %s", label, repr(event))
name = next((x for x in iter(event) if x != "up"))
up = event.get("up", "") == "yes"
up = event.get("up", "") == b"yes"
ike_sa = event[name]
if label == "ike-updown":
if label == b"ike-updown":
handle_interfaces(ike_sa, up)
elif label == "child-updown" and up:
elif label == b"child-updown" and up:
install_routes(ike_sa)
except IOError: