Repository: RedHatInsights/insights-core

Issue:
Whole content of "fitlerable=True" specs will be collected when no filters are added.
E.g.

In filters.yaml, uncomment the filters of `cloud_init_log`:
```
# insights.specs.Specs.cloud_init_log:
# - '401'
```

Do a collection with the modified filter.yaml, in the archive check the `cloud-init.log` file:
```
❯ wc ./insights-insights-release.hosts.dev.psi.pek2.redhat.com-20231116114801/data/var/log/cloud-init.log
  5060  40120 740425 ./insights-insights-release.hosts.dev.psi.pek2.redhat.com-20231116114801/data/var/log/cloud-init.log
```

The whole file was collected.

This is an old issue.


Discussion:

Resolving patch:
diff --git a/insights/core/spec_factory.py b/insights/core/spec_factory.py
--- a/insights/core/spec_factory.py
+++ b/insights/core/spec_factory.py
@@ -150,6 +150,9 @@ def validate(self):
             log.warning("WARNING: Skipping file %s", "/" + self.relative_path)
             raise BlacklistedSpec()
 
+        if self.ds and self.ds.filterable and not get_filters(self.ds):
+            raise ContentException("Skipping %s due to no filters." % dr.get_name(self.ds))
+
         if not os.path.exists(self.path):
             raise ContentException("%s does not exist." % self.path)
 
@@ -347,6 +350,9 @@ def validate(self):
             log.warning("WARNING: Skipping command %s", self.cmd)
             raise BlacklistedSpec()
 
+        if self.ds and self.ds.filterable and not get_filters(self.ds):
+            raise ContentException("Skipping %s due to no filters." % dr.get_name(self.ds))
+
         cmd = shlex.split(self.cmd)[0]
         if not which(cmd, env=self._env):
             raise ContentException("Command not found: %s" % cmd)


Test patch:
diff --git a/insights/tests/test_specs.py b/insights/tests/test_specs.py
--- a/insights/tests/test_specs.py
+++ b/insights/tests/test_specs.py
@@ -4,6 +4,7 @@
 import tempfile
 
 from insights.core import Parser, dr
+from insights.core import filters
 from insights.core.context import HostContext
 from insights.core.exceptions import ContentException
 from insights.core.filters import add_filter
@@ -43,7 +44,8 @@ class Specs(SpecSet):
     no_such_file = RegistryPoint()
     smpl_cmd = RegistryPoint()
     smpl_cmd_list_of_lists = RegistryPoint(filterable=True)
-    smpl_file = RegistryPoint(filterable=True)
+    smpl_file = RegistryPoint()
+    smpl_file_w_filter = RegistryPoint(filterable=True)
 
 
 class Stuff(Specs):
@@ -53,6 +55,7 @@ class Stuff(Specs):
     smpl_cmd = simple_command("/usr/bin/uptime")
     smpl_cmd_list_of_lists = simple_command("echo -n ' hello '")
     smpl_file = simple_file(this_file)
+    smpl_file_w_filter = simple_file(this_file)
 
 
 class stage(dr.ComponentType):
@@ -64,11 +67,19 @@ def invoke(self, broker):
 #
 
 
-@stage(Stuff.smpl_file, Stuff.many, Stuff.smpl_cmd,
+def teardown_function(func):
+    filters._CACHE = {}
+
+    if func in [test_spec_factory, test_line_terminators]:
+        del filters.FILTERS[Stuff.smpl_file_w_filter]
+
+
+@stage(Stuff.smpl_file, Stuff.smpl_file_w_filter, Stuff.many, Stuff.smpl_cmd,
        Stuff.smpl_cmd_list_of_lists,
        optional=[Stuff.no_such_cmd, Stuff.no_such_file])
 def dostuff(broker):
     assert Stuff.smpl_file in broker
+    assert Stuff.smpl_file_w_filter in broker
     assert Stuff.many in broker
     assert Stuff.smpl_cmd in broker
     assert Stuff.smpl_cmd_list_of_lists in broker
@@ -78,6 +89,7 @@ def dostuff(broker):
 
 def test_spec_factory():
     add_filter(Stuff.smpl_cmd_list_of_lists, " hello ")
+    add_filter(Stuff.smpl_file_w_filter, "def test")
     hn = HostContext()
     broker = dr.Broker()
     broker[HostContext] = hn
@@ -97,13 +109,13 @@ def test_spec_factory():
 
 
 def test_line_terminators():
-    add_filter(Stuff.smpl_file, "def test")
+    add_filter(Stuff.smpl_file_w_filter, "def test")
     hn = HostContext()
     broker = dr.Broker()
     broker[HostContext] = hn
     broker = dr.run(dr.get_dependency_graph(dostuff), broker)
 
-    content = broker[Stuff.smpl_file].content
+    content = broker[Stuff.smpl_file_w_filter].content
     assert all("def test" in l for l in content), content
     assert not any(l.endswith("\n") for l in content)
 
@@ -128,3 +140,14 @@ def parse_content(self, content):
     p = MyParser(ds)
     assert p.content == data.splitlines()
     assert list(ds.stream()) == data.splitlines()
+
+
+def test_exp_no_filters():
+    hn = HostContext()
+    broker = dr.Broker()
+    broker[HostContext] = hn
+    broker = dr.run(dr.get_dependency_graph(dostuff), broker)
+    assert dostuff not in broker
+    for exp in broker.exceptions:
+        if "due to" in str(exp):
+            assert "smpl_file_w_filter" in str(exp)