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

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