]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
testing: Fix updown script in route-based/net2net-xfrmi-ike scenario
authorTobias Brunner <tobias@strongswan.org>
Tue, 21 Sep 2021 10:40:52 +0000 (12:40 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 21 Sep 2021 10:50:12 +0000 (12:50 +0200)
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.

testing/tests/route-based/net2net-xfrmi-ike/hosts/sun/etc/updown.py

index ee593deca7a789efd1ce1a00d29492cd6d4138b3..67985a40542633daf15460775acfe99ed80aef98 100755 (executable)
@@ -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: