Blame SOURCES/sos-bz2011536-iptables-based-on-ntf.patch

92cd8f
From 2ab8ba3ecbd52e452cc554d515e0782801dcb4b6 Mon Sep 17 00:00:00 2001
92cd8f
From: Pavel Moravec <pmoravec@redhat.com>
92cd8f
Date: Wed, 8 Sep 2021 15:31:48 +0200
92cd8f
Subject: [PATCH] [firewalld] collect nft rules in firewall_tables only
92cd8f
92cd8f
We collect 'nft list ruleset' in both plugins, while:
92cd8f
- nft is not shipped by firewalld package, so we should not collect
92cd8f
it in firewalld plugin
92cd8f
- running the command requires both nf_tables and nfnetlink kmods, so
92cd8f
we should use both kmods in the predicate
92cd8f
92cd8f
Resolves: #2679
92cd8f
92cd8f
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
92cd8f
---
92cd8f
 sos/report/plugins/firewall_tables.py | 9 +++++----
92cd8f
 sos/report/plugins/firewalld.py       | 8 +-------
92cd8f
 2 files changed, 6 insertions(+), 11 deletions(-)
92cd8f
92cd8f
diff --git a/sos/report/plugins/firewall_tables.py b/sos/report/plugins/firewall_tables.py
92cd8f
index 56058d3bf9..63a7dddeb5 100644
92cd8f
--- a/sos/report/plugins/firewall_tables.py
92cd8f
+++ b/sos/report/plugins/firewall_tables.py
92cd8f
@@ -40,10 +40,11 @@ def collect_nftables(self):
92cd8f
         """ Collects nftables rulesets with 'nft' commands if the modules
92cd8f
         are present """
92cd8f
 
92cd8f
-        self.add_cmd_output(
92cd8f
-            "nft list ruleset",
92cd8f
-            pred=SoSPredicate(self, kmods=['nf_tables'])
92cd8f
-        )
92cd8f
+        # collect nftables ruleset
92cd8f
+        nft_pred = SoSPredicate(self,
92cd8f
+                                kmods=['nf_tables', 'nfnetlink'],
92cd8f
+                                required={'kmods': 'all'})
92cd8f
+        self.add_cmd_output("nft list ruleset", pred=nft_pred, changes=True)
92cd8f
 
92cd8f
     def setup(self):
92cd8f
         # collect iptables -t for any existing table, if we can't read the
92cd8f
diff --git a/sos/report/plugins/firewalld.py b/sos/report/plugins/firewalld.py
92cd8f
index ec83527ed7..9401bfd239 100644
92cd8f
--- a/sos/report/plugins/firewalld.py
92cd8f
+++ b/sos/report/plugins/firewalld.py
92cd8f
@@ -9,7 +9,7 @@
92cd8f
 #
92cd8f
 # See the LICENSE file in the source distribution for further information.
92cd8f
 
92cd8f
-from sos.report.plugins import Plugin, RedHatPlugin, SoSPredicate
92cd8f
+from sos.report.plugins import Plugin, RedHatPlugin
92cd8f
 
92cd8f
 
92cd8f
 class FirewallD(Plugin, RedHatPlugin):
92cd8f
@@ -35,12 +35,6 @@ def setup(self):
92cd8f
             "/var/log/firewalld",
92cd8f
         ])
92cd8f
 
92cd8f
-        # collect nftables ruleset
92cd8f
-        nft_pred = SoSPredicate(self,
92cd8f
-                                kmods=['nf_tables', 'nfnetlink'],
92cd8f
-                                required={'kmods': 'all'})
92cd8f
-        self.add_cmd_output("nft list ruleset", pred=nft_pred, changes=True)
92cd8f
-
92cd8f
         # use a 10s timeout to workaround dbus problems in
92cd8f
         # docker containers.
92cd8f
         self.add_cmd_output([
92cd8f
-- 
92cd8f
2.31.1
92cd8f
92cd8f
92cd8f
From 2a7cf53b61943907dc823cf893530b620a87946c Mon Sep 17 00:00:00 2001
92cd8f
From: Pavel Moravec <pmoravec@redhat.com>
92cd8f
Date: Fri, 15 Oct 2021 22:31:36 +0200
92cd8f
Subject: [PATCH 1/3] [report] Use log_skipped_cmd method inside
92cd8f
 collect_cmd_output
92cd8f
92cd8f
Also, remove obsolete parameters of the log_skipped_cmd method.
92cd8f
92cd8f
Related: #2724
92cd8f
92cd8f
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
92cd8f
---
92cd8f
 sos/report/plugins/__init__.py | 26 ++++++++------------------
92cd8f
 1 file changed, 8 insertions(+), 18 deletions(-)
92cd8f
92cd8f
diff --git a/sos/report/plugins/__init__.py b/sos/report/plugins/__init__.py
92cd8f
index ec138f83..b60ab5f6 100644
92cd8f
--- a/sos/report/plugins/__init__.py
92cd8f
+++ b/sos/report/plugins/__init__.py
92cd8f
@@ -876,8 +876,7 @@ class Plugin():
92cd8f
             return bool(pred)
92cd8f
         return False
92cd8f
 
92cd8f
-    def log_skipped_cmd(self, pred, cmd, kmods=False, services=False,
92cd8f
-                        changes=False):
92cd8f
+    def log_skipped_cmd(self, cmd, pred, changes=False):
92cd8f
         """Log that a command was skipped due to predicate evaluation.
92cd8f
 
92cd8f
         Emit a warning message indicating that a command was skipped due
92cd8f
@@ -887,21 +886,17 @@ class Plugin():
92cd8f
         message indicating that the missing data can be collected by using
92cd8f
         the "--allow-system-changes" command line option will be included.
92cd8f
 
92cd8f
-        :param pred:    The predicate that caused the command to be skipped
92cd8f
-        :type pred:     ``SoSPredicate``
92cd8f
-
92cd8f
         :param cmd:     The command that was skipped
92cd8f
         :type cmd:      ``str``
92cd8f
 
92cd8f
-        :param kmods:   Did kernel modules cause the command to be skipped
92cd8f
-        :type kmods:    ``bool``
92cd8f
-
92cd8f
-        :param services: Did services cause the command to be skipped
92cd8f
-        :type services: ``bool``
92cd8f
+        :param pred:    The predicate that caused the command to be skipped
92cd8f
+        :type pred:     ``SoSPredicate``
92cd8f
 
92cd8f
         :param changes: Is the `--allow-system-changes` enabled
92cd8f
         :type changes:  ``bool``
92cd8f
         """
92cd8f
+        if pred is None:
92cd8f
+            pred = SoSPredicate(self)
92cd8f
         msg = "skipped command '%s': %s" % (cmd, pred.report_failure())
92cd8f
 
92cd8f
         if changes:
92cd8f
@@ -1700,9 +1693,7 @@ class Plugin():
92cd8f
             self.collect_cmds.append(soscmd)
92cd8f
             self._log_info("added cmd output '%s'" % soscmd.cmd)
92cd8f
         else:
92cd8f
-            self.log_skipped_cmd(pred, soscmd.cmd, kmods=bool(pred.kmods),
92cd8f
-                                 services=bool(pred.services),
92cd8f
-                                 changes=soscmd.changes)
92cd8f
+            self.log_skipped_cmd(soscmd.cmd, pred, changes=soscmd.changes)
92cd8f
 
92cd8f
     def add_cmd_output(self, cmds, suggest_filename=None,
92cd8f
                        root_symlink=None, timeout=None, stderr=True,
92cd8f
@@ -2112,7 +2103,7 @@ class Plugin():
92cd8f
                            root_symlink=False, timeout=None,
92cd8f
                            stderr=True, chroot=True, runat=None, env=None,
92cd8f
                            binary=False, sizelimit=None, pred=None,
92cd8f
-                           subdir=None, tags=[]):
92cd8f
+                           changes=False, subdir=None, tags=[]):
92cd8f
         """Execute a command and save the output to a file for inclusion in the
92cd8f
         report, then return the results for further use by the plugin
92cd8f
 
92cd8f
@@ -2163,8 +2154,7 @@ class Plugin():
92cd8f
         :rtype: ``dict``
92cd8f
         """
92cd8f
         if not self.test_predicate(cmd=True, pred=pred):
92cd8f
-            self._log_info("skipped cmd output '%s' due to predicate (%s)" %
92cd8f
-                           (cmd, self.get_predicate(cmd=True, pred=pred)))
92cd8f
+            self.log_skipped_cmd(cmd, pred, changes=changes)
92cd8f
             return {
92cd8f
                 'status': None,  # don't match on if result['status'] checks
92cd8f
                 'output': '',
92cd8f
-- 
92cd8f
2.31.1
92cd8f
92cd8f
92cd8f
From 6b1bea0ffb1df7f8e5001b06cf25f0741b007ddd Mon Sep 17 00:00:00 2001
92cd8f
From: Pavel Moravec <pmoravec@redhat.com>
92cd8f
Date: Fri, 15 Oct 2021 22:34:01 +0200
92cd8f
Subject: [PATCH 2/3] [firewall_tables] call iptables -t  based on nft
92cd8f
 list
92cd8f
92cd8f
If iptables are not realy in use, calling iptables -t 
92cd8f
would load corresponding nft table.
92cd8f
92cd8f
Therefore, call iptables -t only for the tables from "nft list ruleset"
92cd8f
output.
92cd8f
92cd8f
Example: nft list ruleset contains
92cd8f
92cd8f
table ip mangle {
92cd8f
..
92cd8f
}
92cd8f
92cd8f
so we can collect iptable -t mangle -nvL .
92cd8f
92cd8f
The same applies to ip6tables as well.
92cd8f
92cd8f
Resolves: #2724
92cd8f
92cd8f
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
92cd8f
---
92cd8f
 sos/report/plugins/firewall_tables.py | 29 ++++++++++++++++++++-------
92cd8f
 1 file changed, 22 insertions(+), 7 deletions(-)
92cd8f
92cd8f
diff --git a/sos/report/plugins/firewall_tables.py b/sos/report/plugins/firewall_tables.py
92cd8f
index 63a7ddde..ef04d939 100644
92cd8f
--- a/sos/report/plugins/firewall_tables.py
92cd8f
+++ b/sos/report/plugins/firewall_tables.py
92cd8f
@@ -44,26 +44,41 @@ class firewall_tables(Plugin, IndependentPlugin):
92cd8f
         nft_pred = SoSPredicate(self,
92cd8f
                                 kmods=['nf_tables', 'nfnetlink'],
92cd8f
                                 required={'kmods': 'all'})
92cd8f
-        self.add_cmd_output("nft list ruleset", pred=nft_pred, changes=True)
92cd8f
+        return self.collect_cmd_output("nft list ruleset", pred=nft_pred,
92cd8f
+                                       changes=True)
92cd8f
 
92cd8f
     def setup(self):
92cd8f
+        # first, collect "nft list ruleset" as collecting commands like
92cd8f
+        # ip6tables -t mangle -nvL
92cd8f
+        # depends on its output
92cd8f
+        # store in nft_ip_tables lists of ip[|6] tables from nft list
92cd8f
+        nft_list = self.collect_nftables()
92cd8f
+        nft_ip_tables = {'ip': [], 'ip6': []}
92cd8f
+        nft_lines = nft_list['output'] if nft_list['status'] == 0 else ''
92cd8f
+        for line in nft_lines.splitlines():
92cd8f
+            words = line.split()[0:3]
92cd8f
+            if len(words) == 3 and words[0] == 'table' and \
92cd8f
+                    words[1] in nft_ip_tables.keys():
92cd8f
+                nft_ip_tables[words[1]].append(words[2])
92cd8f
         # collect iptables -t for any existing table, if we can't read the
92cd8f
         # tables, collect 2 default ones (mangle, filter)
92cd8f
+        # do collect them only when relevant nft list ruleset exists
92cd8f
+        default_ip_tables = "mangle\nfilter\n"
92cd8f
         try:
92cd8f
             ip_tables_names = open("/proc/net/ip_tables_names").read()
92cd8f
         except IOError:
92cd8f
-            ip_tables_names = "mangle\nfilter\n"
92cd8f
+            ip_tables_names = default_ip_tables
92cd8f
         for table in ip_tables_names.splitlines():
92cd8f
-            self.collect_iptable(table)
92cd8f
+            if nft_list['status'] == 0 and table in nft_ip_tables['ip']:
92cd8f
+                self.collect_iptable(table)
92cd8f
         # collect the same for ip6tables
92cd8f
         try:
92cd8f
             ip_tables_names = open("/proc/net/ip6_tables_names").read()
92cd8f
         except IOError:
92cd8f
-            ip_tables_names = "mangle\nfilter\n"
92cd8f
+            ip_tables_names = default_ip_tables
92cd8f
         for table in ip_tables_names.splitlines():
92cd8f
-            self.collect_ip6table(table)
92cd8f
-
92cd8f
-        self.collect_nftables()
92cd8f
+            if nft_list['status'] == 0 and table in nft_ip_tables['ip6']:
92cd8f
+                self.collect_ip6table(table)
92cd8f
 
92cd8f
         # When iptables is called it will load the modules
92cd8f
         # iptables_filter (for kernel <= 3) or
92cd8f
-- 
92cd8f
2.31.1
92cd8f
92cd8f
92cd8f
From 464bd2d2e83f203e369f2ba7671bbb7da53e06f6 Mon Sep 17 00:00:00 2001
92cd8f
From: Pavel Moravec <pmoravec@redhat.com>
92cd8f
Date: Sun, 24 Oct 2021 16:00:31 +0200
92cd8f
Subject: [PATCH 3/3] [firewall_tables] Call iptables only when nft ip filter
92cd8f
 table exists
92cd8f
92cd8f
iptables -vnxL creates nft 'ip filter' table if it does not exist, hence
92cd8f
we must guard iptables execution by presence of the nft table.
92cd8f
92cd8f
An equivalent logic applies to ip6tables.
92cd8f
92cd8f
Resolves: #2724
92cd8f
92cd8f
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
92cd8f
---
92cd8f
 sos/report/plugins/firewall_tables.py | 26 ++++++++++++++------------
92cd8f
 1 file changed, 14 insertions(+), 12 deletions(-)
92cd8f
92cd8f
diff --git a/sos/report/plugins/firewall_tables.py b/sos/report/plugins/firewall_tables.py
92cd8f
index ef04d939..7eafd60f 100644
92cd8f
--- a/sos/report/plugins/firewall_tables.py
92cd8f
+++ b/sos/report/plugins/firewall_tables.py
92cd8f
@@ -80,19 +80,21 @@ class firewall_tables(Plugin, IndependentPlugin):
92cd8f
             if nft_list['status'] == 0 and table in nft_ip_tables['ip6']:
92cd8f
                 self.collect_ip6table(table)
92cd8f
 
92cd8f
-        # When iptables is called it will load the modules
92cd8f
-        # iptables_filter (for kernel <= 3) or
92cd8f
-        # nf_tables (for kernel >= 4) if they are not loaded.
92cd8f
+        # When iptables is called it will load:
92cd8f
+        # 1) the modules iptables_filter (for kernel <= 3) or
92cd8f
+        #    nf_tables (for kernel >= 4) if they are not loaded.
92cd8f
+        # 2) nft 'ip filter' table will be created
92cd8f
         # The same goes for ipv6.
92cd8f
-        self.add_cmd_output(
92cd8f
-            "iptables -vnxL",
92cd8f
-            pred=SoSPredicate(self, kmods=['iptable_filter', 'nf_tables'])
92cd8f
-        )
92cd8f
-
92cd8f
-        self.add_cmd_output(
92cd8f
-            "ip6tables -vnxL",
92cd8f
-            pred=SoSPredicate(self, kmods=['ip6table_filter', 'nf_tables'])
92cd8f
-        )
92cd8f
+        if nft_list['status'] != 0 or 'filter' in nft_ip_tables['ip']:
92cd8f
+            self.add_cmd_output(
92cd8f
+                "iptables -vnxL",
92cd8f
+                pred=SoSPredicate(self, kmods=['iptable_filter', 'nf_tables'])
92cd8f
+            )
92cd8f
+        if nft_list['status'] != 0 or 'filter' in nft_ip_tables['ip6']:
92cd8f
+            self.add_cmd_output(
92cd8f
+                "ip6tables -vnxL",
92cd8f
+                pred=SoSPredicate(self, kmods=['ip6table_filter', 'nf_tables'])
92cd8f
+            )
92cd8f
 
92cd8f
         self.add_copy_spec([
92cd8f
             "/etc/nftables",
92cd8f
-- 
92cd8f
2.31.1
92cd8f