Blame SOURCES/0052-python-sepolicy-Cache-conditional-rule-queries.patch

83e441
From 25373db5cac520b85350db91b8a7ed0737d3316c Mon Sep 17 00:00:00 2001
83e441
From: Vit Mojzis <vmojzis@redhat.com>
83e441
Date: Tue, 24 Jan 2023 21:05:05 +0100
83e441
Subject: [PATCH] python/sepolicy: Cache conditional rule queries
83e441
83e441
Commit 7506771e4b630fe0ab853f96574e039055cb72eb
83e441
"add missing booleans to man pages" dramatically slowed down
83e441
"sepolicy manpage -a" by removing caching of setools rule query.
83e441
Re-add said caching and update the query to only return conditional
83e441
rules.
83e441
83e441
Before commit 7506771e:
83e441
 #time sepolicy manpage -a
83e441
 real	1m43.153s
83e441
 # time sepolicy manpage -d httpd_t
83e441
 real	0m4.493s
83e441
83e441
After commit 7506771e:
83e441
 #time sepolicy manpage -a
83e441
 real   1h56m43.153s
83e441
 # time sepolicy manpage -d httpd_t
83e441
 real	0m8.352s
83e441
83e441
After this commit:
83e441
 #time sepolicy manpage -a
83e441
 real	1m41.074s
83e441
 # time sepolicy manpage -d httpd_t
83e441
 real	0m7.358s
83e441
83e441
Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
83e441
---
83e441
 python/sepolicy/sepolicy/__init__.py | 11 ++++++++++-
83e441
 1 file changed, 10 insertions(+), 1 deletion(-)
83e441
83e441
diff --git a/python/sepolicy/sepolicy/__init__.py b/python/sepolicy/sepolicy/__init__.py
83e441
index 0f51174d..f48231e9 100644
83e441
--- a/python/sepolicy/sepolicy/__init__.py
83e441
+++ b/python/sepolicy/sepolicy/__init__.py
83e441
@@ -114,6 +114,7 @@ all_attributes = None
83e441
 booleans = None
83e441
 booleans_dict = None
83e441
 all_allow_rules = None
83e441
+all_bool_rules = None
83e441
 all_transitions = None
83e441
 
83e441
 
83e441
@@ -1119,6 +1120,14 @@ def get_all_allow_rules():
83e441
         all_allow_rules = search([ALLOW])
83e441
     return all_allow_rules
83e441
 
83e441
+def get_all_bool_rules():
83e441
+    global all_bool_rules
83e441
+    if not all_bool_rules:
83e441
+        q = setools.TERuleQuery(_pol, boolean=".*", boolean_regex=True,
83e441
+                                ruletype=[ALLOW, DONTAUDIT])
83e441
+        all_bool_rules = [_setools_rule_to_dict(x) for x in q.results()]
83e441
+    return all_bool_rules
83e441
+
83e441
 def get_all_transitions():
83e441
     global all_transitions
83e441
     if not all_transitions:
83e441
@@ -1129,7 +1138,7 @@ def get_bools(setype):
83e441
     bools = []
83e441
     domainbools = []
83e441
     domainname, short_name = gen_short_name(setype)
83e441
-    for i in map(lambda x: x['booleans'], filter(lambda x: 'booleans' in x and x['source'] == setype, search([ALLOW, DONTAUDIT]))):
83e441
+    for i in map(lambda x: x['booleans'], filter(lambda x: 'booleans' in x and x['source'] == setype, get_all_bool_rules())):
83e441
         for b in i:
83e441
             if not isinstance(b, tuple):
83e441
                 continue
83e441
-- 
83e441
2.37.3
83e441