Blame SOURCES/0044-fix-io-functions-check_config-against-on-disk-conf.patch

b8221b
From 0e28840f5c3362d032f2f805cbbe6fbbaa217437 Mon Sep 17 00:00:00 2001
b8221b
From: Eric Garver <eric@garver.life>
b8221b
Date: Wed, 27 Oct 2021 13:58:27 -0400
b8221b
Subject: [PATCH 44/50] fix(io/functions): check_config against on disk conf
b8221b
b8221b
Before this change the runtime FirewallConfig() instance was used. This
b8221b
caused some permanent configuration issues to not be caught due to
b8221b
comparing against the runtime instances of all objects.
b8221b
b8221b
For example, two zones in permanent configuration may use the same
b8221b
interface (which is not valid), but if the runtime configuration does
b8221b
not have have these interface assignments then check_config() won't
b8221b
catch the issue since it compares against the runtime configuration.
b8221b
b8221b
Fix is to build a temporary FirewallConfig() instance for all the
b8221b
on-disk/permanent configuration.
b8221b
b8221b
(cherry picked from commit 35d4facc8962cd1b66bc245fe03f658d491e1061)
b8221b
(cherry picked from commit 55a799e872dc88b1341a6bc38af33e77dfedb72f)
b8221b
---
b8221b
 src/firewall/core/io/functions.py | 47 ++++++++++++++++++++++---------
b8221b
 1 file changed, 34 insertions(+), 13 deletions(-)
b8221b
b8221b
diff --git a/src/firewall/core/io/functions.py b/src/firewall/core/io/functions.py
b8221b
index 0c7b1886426c..35a7eaf8dec8 100644
b8221b
--- a/src/firewall/core/io/functions.py
b8221b
+++ b/src/firewall/core/io/functions.py
b8221b
@@ -24,6 +24,7 @@ import os
b8221b
 from firewall import config
b8221b
 from firewall.errors import FirewallError
b8221b
 
b8221b
+from firewall.core.fw_config import FirewallConfig
b8221b
 from firewall.core.io.zone import zone_reader
b8221b
 from firewall.core.io.service import service_reader
b8221b
 from firewall.core.io.ipset import ipset_reader
b8221b
@@ -34,26 +35,46 @@ from firewall.core.io.direct import Direct
b8221b
 from firewall.core.io.lockdown_whitelist import LockdownWhitelist
b8221b
 from firewall.core.io.firewalld_conf import firewalld_conf
b8221b
 
b8221b
-def check_config(fw=None):
b8221b
+def check_config(fw):
b8221b
+    fw_config = FirewallConfig(fw)
b8221b
     readers = {
b8221b
-        "ipset" : (ipset_reader, [config.FIREWALLD_IPSETS, config.ETC_FIREWALLD_IPSETS]),
b8221b
-        "helper" : (helper_reader, [config.FIREWALLD_HELPERS, config.ETC_FIREWALLD_HELPERS]),
b8221b
-        "icmptype" : (icmptype_reader, [config.FIREWALLD_ICMPTYPES, config.ETC_FIREWALLD_ICMPTYPES]),
b8221b
-        "service" : (service_reader, [config.FIREWALLD_SERVICES, config.ETC_FIREWALLD_SERVICES]),
b8221b
-        "zone" : (zone_reader, [config.FIREWALLD_ZONES, config.ETC_FIREWALLD_ZONES]),
b8221b
-        "policy" : (policy_reader, [config.FIREWALLD_POLICIES, config.ETC_FIREWALLD_POLICIES]),
b8221b
+        "ipset":    {"reader": ipset_reader,
b8221b
+                     "add": fw_config.add_ipset,
b8221b
+                     "dirs": [config.FIREWALLD_IPSETS, config.ETC_FIREWALLD_IPSETS],
b8221b
+                    },
b8221b
+        "helper":   {"reader": helper_reader,
b8221b
+                     "add": fw_config.add_helper,
b8221b
+                     "dirs": [config.FIREWALLD_HELPERS, config.ETC_FIREWALLD_HELPERS],
b8221b
+                    },
b8221b
+        "icmptype": {"reader": icmptype_reader,
b8221b
+                     "add": fw_config.add_icmptype,
b8221b
+                     "dirs": [config.FIREWALLD_ICMPTYPES, config.ETC_FIREWALLD_ICMPTYPES],
b8221b
+                    },
b8221b
+        "service":  {"reader": service_reader,
b8221b
+                     "add": fw_config.add_service,
b8221b
+                     "dirs": [config.FIREWALLD_SERVICES, config.ETC_FIREWALLD_SERVICES],
b8221b
+                    },
b8221b
+        "zone":     {"reader": zone_reader,
b8221b
+                     "add": fw_config.add_zone,
b8221b
+                     "dirs": [config.FIREWALLD_ZONES, config.ETC_FIREWALLD_ZONES],
b8221b
+                    },
b8221b
+        "policy":   {"reader": policy_reader,
b8221b
+                     "add": fw_config.add_policy_object,
b8221b
+                     "dirs": [config.FIREWALLD_POLICIES, config.ETC_FIREWALLD_POLICIES],
b8221b
+                    },
b8221b
     }
b8221b
     for reader in readers.keys():
b8221b
-        for dir in readers[reader][1]:
b8221b
-            if not os.path.isdir(dir):
b8221b
+        for _dir in readers[reader]["dirs"]:
b8221b
+            if not os.path.isdir(_dir):
b8221b
                 continue
b8221b
-            for file in sorted(os.listdir(dir)):
b8221b
+            for file in sorted(os.listdir(_dir)):
b8221b
                 if file.endswith(".xml"):
b8221b
                     try:
b8221b
-                        obj = readers[reader][0](file, dir)
b8221b
-                        if fw and reader in ["zone", "policy"]:
b8221b
-                            obj.fw_config = fw.config
b8221b
+                        obj = readers[reader]["reader"](file, _dir)
b8221b
+                        if reader in ["zone", "policy"]:
b8221b
+                            obj.fw_config = fw_config
b8221b
                         obj.check_config(obj.export_config())
b8221b
+                        readers[reader]["add"](obj)
b8221b
                     except FirewallError as error:
b8221b
                         raise FirewallError(error.code, "'%s': %s" % (file, error.msg))
b8221b
                     except Exception as msg:
b8221b
-- 
b8221b
2.27.0
b8221b